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