Merge "Extract ClientInitiatedActionRepository" into main
This commit is contained in:
@@ -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"
|
||||
}
|
||||
}
|
@@ -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"
|
||||
}
|
||||
|
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* 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 androidx.core.os.persistableBundleOf
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.kotlin.any
|
||||
import org.mockito.kotlin.anyVararg
|
||||
import org.mockito.kotlin.argumentCaptor
|
||||
import org.mockito.kotlin.doReturn
|
||||
import org.mockito.kotlin.mock
|
||||
import org.mockito.kotlin.never
|
||||
import org.mockito.kotlin.stub
|
||||
import org.mockito.kotlin.verify
|
||||
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class ClientInitiatedActionRepositoryTest {
|
||||
private val mockCarrierConfigManager = mock<CarrierConfigManager>()
|
||||
|
||||
private val context = mock<Context> {
|
||||
on { applicationContext } doReturn mock
|
||||
on { getSystemService(CarrierConfigManager::class.java) } doReturn mockCarrierConfigManager
|
||||
}
|
||||
|
||||
private val repository = ClientInitiatedActionRepository(context)
|
||||
|
||||
@Test
|
||||
fun onSystemUpdate_notEnabled() {
|
||||
mockCarrierConfigManager.stub {
|
||||
on { getConfig(anyVararg()) } doReturn persistableBundleOf()
|
||||
}
|
||||
|
||||
repository.onSystemUpdate()
|
||||
|
||||
verify(context, never()).sendBroadcast(any())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun onSystemUpdate_enabled() {
|
||||
mockCarrierConfigManager.stub {
|
||||
on { getConfig(anyVararg()) } doReturn persistableBundleOf(
|
||||
CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_BOOL to true,
|
||||
CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_INTENT_STRING to ACTION,
|
||||
)
|
||||
}
|
||||
|
||||
repository.onSystemUpdate()
|
||||
|
||||
val intent = argumentCaptor<Intent> {
|
||||
verify(context).sendBroadcast(capture())
|
||||
}.firstValue
|
||||
assertThat(intent.action).isEqualTo(ACTION)
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val ACTION = "ACTION"
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user