1 /*
2  * Copyright (C) 2018 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.car.settings.system;
18 
19 import static android.content.Context.CARRIER_CONFIG_SERVICE;
20 
21 import android.car.drivingstate.CarUxRestrictions;
22 import android.car.userlib.CarUserManagerHelper;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.pm.ApplicationInfo;
26 import android.content.pm.PackageManager;
27 import android.content.pm.ResolveInfo;
28 import android.os.PersistableBundle;
29 import android.telephony.CarrierConfigManager;
30 import android.text.TextUtils;
31 
32 import androidx.preference.Preference;
33 
34 import com.android.car.settings.R;
35 import com.android.car.settings.common.FragmentController;
36 import com.android.car.settings.common.Logger;
37 import com.android.car.settings.common.PreferenceController;
38 
39 import java.util.List;
40 
41 /**
42  * Controller which determines if the system update preference should be displayed based on
43  * device and user status. When the preference is clicked, this controller broadcasts a client
44  * initiated action if an intent is available in carrier-specific telephony configuration.
45  *
46  * @see CarrierConfigManager#KEY_CI_ACTION_ON_SYS_UPDATE_BOOL
47  */
48 public class SystemUpdatePreferenceController extends PreferenceController<Preference> {
49 
50     private static final Logger LOG = new Logger(SystemUpdatePreferenceController.class);
51 
52     private final CarUserManagerHelper mCarUserManagerHelper;
53     private boolean mActivityFound;
54 
SystemUpdatePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)55     public SystemUpdatePreferenceController(Context context, String preferenceKey,
56             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
57         super(context, preferenceKey, fragmentController, uxRestrictions);
58         mCarUserManagerHelper = new CarUserManagerHelper(context);
59     }
60 
61     @Override
getPreferenceType()62     protected Class<Preference> getPreferenceType() {
63         return Preference.class;
64     }
65 
66     @Override
getAvailabilityStatus()67     protected int getAvailabilityStatus() {
68         if (!getContext().getResources().getBoolean(R.bool.config_show_system_update_settings)) {
69             return UNSUPPORTED_ON_DEVICE;
70         }
71         return mCarUserManagerHelper.isCurrentProcessAdminUser() ? AVAILABLE : DISABLED_FOR_USER;
72     }
73 
74     @Override
onCreateInternal()75     protected void onCreateInternal() {
76         Preference preference = getPreference();
77         Intent intent = preference.getIntent();
78         if (intent != null) {
79             // Find the activity that is in the system image.
80             PackageManager pm = getContext().getPackageManager();
81             List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);
82             int listSize = list.size();
83             for (int i = 0; i < listSize; i++) {
84                 ResolveInfo resolveInfo = list.get(i);
85                 if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)
86                         != 0) {
87                     // Replace the intent with this specific activity.
88                     preference.setIntent(
89                             new Intent().setClassName(resolveInfo.activityInfo.packageName,
90                                     resolveInfo.activityInfo.name));
91                     // Set the preference title to the activity's label.
92                     preference.setTitle(resolveInfo.loadLabel(pm));
93                     mActivityFound = true;
94                 }
95             }
96         }
97     }
98 
99     @Override
updateState(Preference preference)100     protected void updateState(Preference preference) {
101         preference.setVisible(mActivityFound);
102     }
103 
104     @Override
handlePreferenceClicked(Preference preference)105     protected boolean handlePreferenceClicked(Preference preference) {
106         CarrierConfigManager configManager = (CarrierConfigManager) getContext().getSystemService(
107                 CARRIER_CONFIG_SERVICE);
108         PersistableBundle b = configManager.getConfig();
109         if (b != null && b.getBoolean(CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_BOOL)) {
110             ciActionOnSysUpdate(b);
111         }
112         // Don't handle so that preference framework will launch the preference intent.
113         return false;
114     }
115 
116     /** Trigger client initiated action (send intent) on system update. */
ciActionOnSysUpdate(PersistableBundle b)117     private void ciActionOnSysUpdate(PersistableBundle b) {
118         String intentStr = b.getString(
119                 CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_INTENT_STRING);
120         if (!TextUtils.isEmpty(intentStr)) {
121             String extra = b.getString(
122                     CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_EXTRA_STRING);
123             String extraVal = b.getString(
124                     CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_EXTRA_VAL_STRING);
125 
126             Intent intent = new Intent(intentStr);
127             if (!TextUtils.isEmpty(extra)) {
128                 intent.putExtra(extra, extraVal);
129             }
130             LOG.d("ciActionOnSysUpdate: broadcasting intent " + intentStr + " with extra " + extra
131                     + ", " + extraVal);
132             getContext().getApplicationContext().sendBroadcast(intent);
133         }
134     }
135 }
136