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 static android.provider.Telephony.Carriers.ENFORCE_MANAGED_URI;
20 
21 import android.content.Context;
22 import android.content.Intent;
23 import android.database.ContentObserver;
24 import android.os.Handler;
25 import android.os.Looper;
26 import android.os.PersistableBundle;
27 import android.provider.Settings;
28 import android.telephony.CarrierConfigManager;
29 
30 import androidx.annotation.VisibleForTesting;
31 import androidx.preference.Preference;
32 import androidx.preference.PreferenceScreen;
33 
34 import com.android.settings.SettingsActivity;
35 import com.android.settings.network.CarrierConfigCache;
36 import com.android.settings.network.apn.ApnSettings;
37 import com.android.settingslib.RestrictedLockUtilsInternal;
38 import com.android.settingslib.RestrictedPreference;
39 import com.android.settingslib.core.lifecycle.LifecycleObserver;
40 import com.android.settingslib.core.lifecycle.events.OnStart;
41 import com.android.settingslib.core.lifecycle.events.OnStop;
42 
43 /**
44  * Preference controller for "Apn settings"
45  */
46 public class ApnPreferenceController extends TelephonyBasePreferenceController implements
47         LifecycleObserver, OnStart, OnStop {
48 
49     @VisibleForTesting
50     CarrierConfigCache mCarrierConfigCache;
51     private Preference mPreference;
52     private DpcApnEnforcedObserver mDpcApnEnforcedObserver;
53 
ApnPreferenceController(Context context, String key)54     public ApnPreferenceController(Context context, String key) {
55         super(context, key);
56         mCarrierConfigCache = CarrierConfigCache.getInstance(context);
57         mDpcApnEnforcedObserver = new DpcApnEnforcedObserver(new Handler(Looper.getMainLooper()));
58     }
59 
60     @Override
getAvailabilityStatus(int subId)61     public int getAvailabilityStatus(int subId) {
62         final PersistableBundle carrierConfig = mCarrierConfigCache.getConfigForSubId(subId);
63         final boolean isCdmaApn = MobileNetworkUtils.isCdmaOptions(mContext, subId)
64                 && carrierConfig != null
65                 && carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_APN_SETTING_CDMA_BOOL);
66         final boolean isGsmApn = MobileNetworkUtils.isGsmOptions(mContext, subId)
67                 && carrierConfig != null
68                 && carrierConfig.getBoolean(CarrierConfigManager.KEY_APN_EXPAND_BOOL);
69         final boolean hideCarrierNetwork = carrierConfig == null
70                 || carrierConfig.getBoolean(
71                 CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL);
72 
73         return !hideCarrierNetwork && (isCdmaApn || isGsmApn)
74                 ? AVAILABLE
75                 : CONDITIONALLY_UNAVAILABLE;
76     }
77 
78     @Override
onStart()79     public void onStart() {
80         mDpcApnEnforcedObserver.register(mContext);
81     }
82 
83     @Override
onStop()84     public void onStop() {
85         mDpcApnEnforcedObserver.unRegister(mContext);
86     }
87 
88     @Override
displayPreference(PreferenceScreen screen)89     public void displayPreference(PreferenceScreen screen) {
90         super.displayPreference(screen);
91         mPreference = screen.findPreference(getPreferenceKey());
92     }
93 
94     @Override
updateState(Preference preference)95     public void updateState(Preference preference) {
96         super.updateState(preference);
97         if (mPreference == null) {
98             return;
99         }
100         ((RestrictedPreference) mPreference).setDisabledByAdmin(
101                 MobileNetworkUtils.isDpcApnEnforced(mContext)
102                         ? RestrictedLockUtilsInternal.getDeviceOwner(mContext)
103                         : null);
104     }
105 
106     @Override
handlePreferenceTreeClick(Preference preference)107     public boolean handlePreferenceTreeClick(Preference preference) {
108         if (getPreferenceKey().equals(preference.getKey())) {
109             // This activity runs in phone process, we must use intent to start
110             final Intent intent = new Intent(Settings.ACTION_APN_SETTINGS);
111             intent.setPackage(mContext.getPackageName());
112             // This will setup the Home and Search affordance
113             intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, true);
114             intent.putExtra(ApnSettings.SUB_ID, mSubId);
115             mContext.startActivity(intent);
116             return true;
117         }
118 
119         return false;
120     }
121 
init(int subId)122     public void init(int subId) {
123         mSubId = subId;
124     }
125 
126     @VisibleForTesting
setPreference(Preference preference)127     void setPreference(Preference preference) {
128         mPreference = preference;
129     }
130 
131     private class DpcApnEnforcedObserver extends ContentObserver {
DpcApnEnforcedObserver(Handler handler)132         DpcApnEnforcedObserver(Handler handler) {
133             super(handler);
134         }
135 
register(Context context)136         public void register(Context context) {
137             context.getContentResolver().registerContentObserver(ENFORCE_MANAGED_URI, false, this);
138 
139         }
140 
unRegister(Context context)141         public void unRegister(Context context) {
142             context.getContentResolver().unregisterContentObserver(this);
143         }
144 
145         @Override
onChange(boolean selfChange)146         public void onChange(boolean selfChange) {
147             updateState(mPreference);
148         }
149     }
150 }
151