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.Context;
20 import android.content.Intent;
21 import android.net.Uri;
22 import android.os.PersistableBundle;
23 import android.provider.Settings;
24 import android.telephony.CarrierConfigManager;
25 import android.telephony.SubscriptionManager;
26 import android.telephony.TelephonyManager;
27 import android.text.TextUtils;
28 
29 import androidx.preference.Preference;
30 
31 import com.android.settings.network.CarrierConfigCache;
32 
33 /**
34  * Preference controller for "Data service setup"
35  */
36 public class DataServiceSetupPreferenceController extends TelephonyBasePreferenceController {
37 
38     private CarrierConfigCache mCarrierConfigCache;
39     private TelephonyManager mTelephonyManager;
40     private String mSetupUrl;
41 
DataServiceSetupPreferenceController(Context context, String key)42     public DataServiceSetupPreferenceController(Context context, String key) {
43         super(context, key);
44         mCarrierConfigCache = CarrierConfigCache.getInstance(context);
45         mTelephonyManager = context.getSystemService(TelephonyManager.class);
46         mSetupUrl = Settings.Global.getString(mContext.getContentResolver(),
47                 Settings.Global.SETUP_PREPAID_DATA_SERVICE_URL);
48     }
49 
50     @Override
getAvailabilityStatus(int subId)51     public int getAvailabilityStatus(int subId) {
52         final PersistableBundle carrierConfig = mCarrierConfigCache.getConfigForSubId(subId);
53         return subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID
54                 && carrierConfig != null
55                 && !carrierConfig.getBoolean(
56                 CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL)
57                 && mTelephonyManager.isLteCdmaEvdoGsmWcdmaEnabled() && !TextUtils.isEmpty(mSetupUrl)
58                 ? AVAILABLE
59                 : CONDITIONALLY_UNAVAILABLE;
60     }
61 
init(int subId)62     public void init(int subId) {
63         mSubId = subId;
64         mTelephonyManager = mContext.getSystemService(TelephonyManager.class)
65                 .createForSubscriptionId(mSubId);
66     }
67 
68     @Override
handlePreferenceTreeClick(Preference preference)69     public boolean handlePreferenceTreeClick(Preference preference) {
70         if (getPreferenceKey().equals(preference.getKey())) {
71             if (!TextUtils.isEmpty(mSetupUrl)) {
72                 String imsi = mTelephonyManager.getSubscriberId();
73                 if (imsi == null) {
74                     imsi = "";
75                 }
76                 final String url = TextUtils.expandTemplate(mSetupUrl, imsi).toString();
77                 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
78                 mContext.startActivity(intent);
79             }
80             return true;
81         }
82 
83         return false;
84     }
85 }
86