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