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