1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settings.system
18 
19 import android.content.Context
20 import android.content.Intent
21 import android.telephony.CarrierConfigManager
22 import android.util.Log
23 import com.android.settings.network.telephony.safeGetConfig
24 
25 class ClientInitiatedActionRepository(private val context: Context) {
26     private val configManager = context.getSystemService(CarrierConfigManager::class.java)!!
27 
28     /**
29      * Trigger client initiated action (send intent) on system update
30      */
onSystemUpdatenull31     fun onSystemUpdate() {
32         val bundle =
33             configManager.safeGetConfig(
34                 keys = listOf(
35                     CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_BOOL,
36                     CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_INTENT_STRING,
37                     CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_EXTRA_STRING,
38                     CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_EXTRA_VAL_STRING,
39                 ),
40             )
41 
42         if (!bundle.getBoolean(CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_BOOL)) return
43 
44         val action =
45             bundle.getString(CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_INTENT_STRING)
46         if (action.isNullOrEmpty()) return
47         val extra = bundle.getString(CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_EXTRA_STRING)
48         val extraValue =
49             bundle.getString(CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_EXTRA_VAL_STRING)
50         Log.d(TAG, "onSystemUpdate: broadcasting intent $action with extra $extra, $extraValue")
51         val intent = Intent(action).apply {
52             if (!extra.isNullOrEmpty()) putExtra(extra, extraValue)
53             addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND)
54         }
55         context.applicationContext.sendBroadcast(intent)
56     }
57 
58     companion object {
59         private const val TAG = "ClientInitiatedAction"
60     }
61 }
62