1 /*
2  * Copyright (C) 2016 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 package com.android.settings.deviceinfo;
17 
18 import android.content.Context;
19 import android.content.Intent;
20 import android.os.Build;
21 import android.os.PersistableBundle;
22 import android.os.UserManager;
23 import android.support.v7.preference.Preference;
24 import android.support.v7.preference.PreferenceScreen;
25 import android.telephony.CarrierConfigManager;
26 import android.text.TextUtils;
27 import android.util.Log;
28 
29 import com.android.settings.R;
30 import com.android.settings.Utils;
31 import com.android.settings.core.PreferenceController;
32 
33 import static android.content.Context.CARRIER_CONFIG_SERVICE;
34 
35 public class SystemUpdatePreferenceController extends PreferenceController {
36 
37     private static final String TAG = "SysUpdatePrefContr";
38 
39     private static final String KEY_SYSTEM_UPDATE_SETTINGS = "system_update_settings";
40 
41     private final UserManager mUm;
42 
SystemUpdatePreferenceController(Context context, UserManager um)43     public SystemUpdatePreferenceController(Context context, UserManager um) {
44         super(context);
45         mUm = um;
46     }
47 
48     @Override
isAvailable()49     public boolean isAvailable() {
50         return mUm.isAdminUser();
51     }
52 
53     @Override
getPreferenceKey()54     public String getPreferenceKey() {
55         return KEY_SYSTEM_UPDATE_SETTINGS;
56     }
57 
58     @Override
displayPreference(PreferenceScreen screen)59     public void displayPreference(PreferenceScreen screen) {
60         if (isAvailable()) {
61             Utils.updatePreferenceToSpecificActivityOrRemove(mContext, screen,
62                     KEY_SYSTEM_UPDATE_SETTINGS,
63                     Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
64         } else {
65             removePreference(screen, KEY_SYSTEM_UPDATE_SETTINGS);
66         }
67     }
68 
69     @Override
handlePreferenceTreeClick(Preference preference)70     public boolean handlePreferenceTreeClick(Preference preference) {
71         if (KEY_SYSTEM_UPDATE_SETTINGS.equals(preference.getKey())) {
72             CarrierConfigManager configManager =
73                     (CarrierConfigManager) mContext.getSystemService(CARRIER_CONFIG_SERVICE);
74             PersistableBundle b = configManager.getConfig();
75             if (b != null && b.getBoolean(CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_BOOL)) {
76                 ciActionOnSysUpdate(b);
77             }
78         }
79         // always return false here because this handler does not want to block other handlers.
80         return false;
81     }
82 
83     @Override
updateState(Preference preference)84     public void updateState(Preference preference) {
85         preference.setSummary(mContext.getString(R.string.about_summary,
86                 Build.VERSION.RELEASE));
87     }
88 
89     /**
90      * Trigger client initiated action (send intent) on system update
91      */
ciActionOnSysUpdate(PersistableBundle b)92     private void ciActionOnSysUpdate(PersistableBundle b) {
93         String intentStr = b.getString(CarrierConfigManager.
94                 KEY_CI_ACTION_ON_SYS_UPDATE_INTENT_STRING);
95         if (!TextUtils.isEmpty(intentStr)) {
96             String extra = b.getString(CarrierConfigManager.
97                     KEY_CI_ACTION_ON_SYS_UPDATE_EXTRA_STRING);
98             String extraVal = b.getString(CarrierConfigManager.
99                     KEY_CI_ACTION_ON_SYS_UPDATE_EXTRA_VAL_STRING);
100 
101             Intent intent = new Intent(intentStr);
102             if (!TextUtils.isEmpty(extra)) {
103                 intent.putExtra(extra, extraVal);
104             }
105             Log.d(TAG, "ciActionOnSysUpdate: broadcasting intent " + intentStr +
106                     " with extra " + extra + ", " + extraVal);
107             mContext.getApplicationContext().sendBroadcast(intent);
108         }
109     }
110 }
111