Extract ClientInitiatedActionRepository

Also add unit test.

Bug: 300851543
Test: manual - on system page
Test: unit test
Change-Id: I362afb4aa0683ebcc6695ff0b5bc35ef8afb5697
This commit is contained in:
Chaohui Wang
2023-11-10 14:28:33 +08:00
parent c292adaf6b
commit de8876e9b5
3 changed files with 138 additions and 29 deletions

View File

@@ -0,0 +1,58 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.system
import android.content.Context
import android.content.Intent
import android.telephony.CarrierConfigManager
import android.util.Log
class ClientInitiatedActionRepository(private val context: Context) {
private val configManager = context.getSystemService(CarrierConfigManager::class.java)!!
/**
* Trigger client initiated action (send intent) on system update
*/
fun onSystemUpdate() {
val bundle =
configManager.getConfig(
CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_BOOL,
CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_INTENT_STRING,
CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_EXTRA_STRING,
CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_EXTRA_VAL_STRING,
)
if (!bundle.getBoolean(CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_BOOL)) return
val action =
bundle.getString(CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_INTENT_STRING)
if (action.isNullOrEmpty()) return
val extra = bundle.getString(CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_EXTRA_STRING)
val extraValue =
bundle.getString(CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_EXTRA_VAL_STRING)
Log.d(TAG, "onSystemUpdate: broadcasting intent $action with extra $extra, $extraValue")
val intent = Intent(action).apply {
if (!extra.isNullOrEmpty()) putExtra(extra, extraValue)
addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND)
}
context.applicationContext.sendBroadcast(intent)
}
companion object {
private const val TAG = "ClientInitiatedAction"
}
}

View File

@@ -17,12 +17,9 @@
package com.android.settings.system
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.PersistableBundle
import android.os.SystemUpdateManager
import android.os.UserManager
import android.telephony.CarrierConfigManager
import android.util.Log
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
@@ -39,6 +36,7 @@ import kotlinx.coroutines.launch
open class SystemUpdatePreferenceController(context: Context, preferenceKey: String) :
BasePreferenceController(context, preferenceKey) {
private val userManager: UserManager = context.userManager
private val clientInitiatedActionRepository = ClientInitiatedActionRepository(context)
private lateinit var preference: Preference
override fun getAvailabilityStatus() =
@@ -61,12 +59,7 @@ open class SystemUpdatePreferenceController(context: Context, preferenceKey: Str
override fun handlePreferenceTreeClick(preference: Preference): Boolean {
if (preferenceKey == preference.key) {
val configManager = mContext.getSystemService(CarrierConfigManager::class.java)!!
configManager.getConfig(CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_BOOL)?.let {
if (it.getBoolean(CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_BOOL)) {
ciActionOnSysUpdate(it)
}
}
clientInitiatedActionRepository.onSystemUpdate()
}
// always return false here because this handler does not want to block other handlers.
return false
@@ -111,26 +104,6 @@ open class SystemUpdatePreferenceController(context: Context, preferenceKey: Str
Build.VERSION.RELEASE_OR_PREVIEW_DISPLAY,
)
/**
* Trigger client initiated action (send intent) on system update
*/
private fun ciActionOnSysUpdate(b: PersistableBundle) {
val intentStr = b.getString(CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_INTENT_STRING)
if (intentStr.isNullOrEmpty()) return
val extra = b.getString(CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_EXTRA_STRING)
val extraVal =
b.getString(CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_EXTRA_VAL_STRING)
Log.d(
TAG,
"ciActionOnSysUpdate: broadcasting intent $intentStr with extra $extra, $extraVal"
)
val intent = Intent(intentStr).apply {
if (!extra.isNullOrEmpty()) putExtra(extra, extraVal)
addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND)
}
mContext.applicationContext.sendBroadcast(intent)
}
companion object {
private const val TAG = "SysUpdatePrefContr"
}