• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.car.developeroptions.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.car.developeroptions.SettingsActivity;
35 import com.android.car.developeroptions.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 
69         return isCdmaApn || isGsmApn
70                 ? AVAILABLE
71                 : CONDITIONALLY_UNAVAILABLE;
72     }
73 
74     @Override
onStart()75     public void onStart() {
76         mDpcApnEnforcedObserver.register(mContext);
77     }
78 
79     @Override
onStop()80     public void onStop() {
81         mDpcApnEnforcedObserver.unRegister(mContext);
82     }
83 
84     @Override
displayPreference(PreferenceScreen screen)85     public void displayPreference(PreferenceScreen screen) {
86         super.displayPreference(screen);
87         mPreference = screen.findPreference(getPreferenceKey());
88     }
89 
90     @Override
updateState(Preference preference)91     public void updateState(Preference preference) {
92         super.updateState(preference);
93         ((RestrictedPreference) mPreference).setDisabledByAdmin(
94                 MobileNetworkUtils.isDpcApnEnforced(mContext)
95                         ? RestrictedLockUtilsInternal.getDeviceOwner(mContext)
96                         : null);
97     }
98 
99     @Override
handlePreferenceTreeClick(Preference preference)100     public boolean handlePreferenceTreeClick(Preference preference) {
101         if (getPreferenceKey().equals(preference.getKey())) {
102             // This activity runs in phone process, we must use intent to start
103             final Intent intent = new Intent(Settings.ACTION_APN_SETTINGS);
104             // This will setup the Home and Search affordance
105             intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, true);
106             intent.putExtra(ApnSettings.SUB_ID, mSubId);
107             mContext.startActivity(intent);
108             return true;
109         }
110 
111         return false;
112     }
113 
init(int subId)114     public void init(int subId) {
115         mSubId = subId;
116     }
117 
118     @VisibleForTesting
setPreference(Preference preference)119     void setPreference(Preference preference) {
120         mPreference = preference;
121     }
122 
123     private class DpcApnEnforcedObserver extends ContentObserver {
DpcApnEnforcedObserver(Handler handler)124         DpcApnEnforcedObserver(Handler handler) {
125             super(handler);
126         }
127 
register(Context context)128         public void register(Context context) {
129             context.getContentResolver().registerContentObserver(ENFORCE_MANAGED_URI, false, this);
130 
131         }
132 
unRegister(Context context)133         public void unRegister(Context context) {
134             context.getContentResolver().unregisterContentObserver(this);
135         }
136 
137         @Override
onChange(boolean selfChange)138         public void onChange(boolean selfChange) {
139             updateState(mPreference);
140         }
141     }
142 }
143