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.settings.network.telephony;
18 
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.pm.PackageManager;
23 import android.content.pm.ResolveInfo;
24 import android.os.PersistableBundle;
25 import android.telephony.CarrierConfigManager;
26 import android.telephony.SubscriptionManager;
27 
28 import androidx.annotation.VisibleForTesting;
29 import androidx.preference.Preference;
30 
31 /**
32  * Preference controller for "Carrier Settings"
33  */
34 public class CarrierPreferenceController extends TelephonyBasePreferenceController {
35 
36     @VisibleForTesting
37     CarrierConfigManager mCarrierConfigManager;
38 
CarrierPreferenceController(Context context, String key)39     public CarrierPreferenceController(Context context, String key) {
40         super(context, key);
41         mCarrierConfigManager = context.getSystemService(CarrierConfigManager.class);
42     }
43 
init(int subId)44     public void init(int subId) {
45         mSubId = subId;
46     }
47 
48     @Override
getAvailabilityStatus(int subId)49     public int getAvailabilityStatus(int subId) {
50         final PersistableBundle carrierConfig = mCarrierConfigManager.getConfigForSubId(subId);
51 
52         // Return available if it is in CDMA or GSM mode, and the flag is on
53         return carrierConfig != null
54                 && carrierConfig.getBoolean(CarrierConfigManager.KEY_CARRIER_SETTINGS_ENABLE_BOOL)
55                 && (MobileNetworkUtils.isCdmaOptions(mContext, subId)
56                 || MobileNetworkUtils.isGsmOptions(mContext, subId))
57                 ? AVAILABLE
58                 : CONDITIONALLY_UNAVAILABLE;
59     }
60 
61     @Override
handlePreferenceTreeClick(Preference preference)62     public boolean handlePreferenceTreeClick(Preference preference) {
63         if (getPreferenceKey().equals(preference.getKey())) {
64             final Intent carrierSettingsIntent = getCarrierSettingsActivityIntent(mSubId);
65             if (carrierSettingsIntent != null) {
66                 mContext.startActivity(carrierSettingsIntent);
67             }
68             return true;
69         }
70 
71         return false;
72     }
73 
getCarrierSettingsActivityIntent(int subId)74     private Intent getCarrierSettingsActivityIntent(int subId) {
75         final PersistableBundle config = mCarrierConfigManager.getConfigForSubId(subId);
76         final ComponentName cn = ComponentName.unflattenFromString(
77                 config == null ? "" : config.getString(
78                         CarrierConfigManager.KEY_CARRIER_SETTINGS_ACTIVITY_COMPONENT_NAME_STRING,
79                         "" /* default value */));
80 
81         if (cn == null) return null;
82 
83         final Intent intent = new Intent(Intent.ACTION_MAIN);
84         intent.setComponent(cn);
85         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
86         intent.putExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, subId);
87 
88         final PackageManager pm = mContext.getPackageManager();
89         final ResolveInfo resolveInfo = pm.resolveActivity(intent, 0 /* flags */);
90         return resolveInfo != null ? intent : null;
91     }
92 }
93