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.apn.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 = context.getSystemService(CarrierConfigManager.class); 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 if (mPreference == null) { 97 return; 98 } 99 ((RestrictedPreference) mPreference).setDisabledByAdmin( 100 MobileNetworkUtils.isDpcApnEnforced(mContext) 101 ? RestrictedLockUtilsInternal.getDeviceOwner(mContext) 102 : null); 103 } 104 105 @Override handlePreferenceTreeClick(Preference preference)106 public boolean handlePreferenceTreeClick(Preference preference) { 107 if (getPreferenceKey().equals(preference.getKey())) { 108 // This activity runs in phone process, we must use intent to start 109 final Intent intent = new Intent(Settings.ACTION_APN_SETTINGS); 110 // This will setup the Home and Search affordance 111 intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, true); 112 intent.putExtra(ApnSettings.SUB_ID, mSubId); 113 mContext.startActivity(intent); 114 return true; 115 } 116 117 return false; 118 } 119 init(int subId)120 public void init(int subId) { 121 mSubId = subId; 122 } 123 124 @VisibleForTesting setPreference(Preference preference)125 void setPreference(Preference preference) { 126 mPreference = preference; 127 } 128 129 private class DpcApnEnforcedObserver extends ContentObserver { DpcApnEnforcedObserver(Handler handler)130 DpcApnEnforcedObserver(Handler handler) { 131 super(handler); 132 } 133 register(Context context)134 public void register(Context context) { 135 context.getContentResolver().registerContentObserver(ENFORCE_MANAGED_URI, false, this); 136 137 } 138 unRegister(Context context)139 public void unRegister(Context context) { 140 context.getContentResolver().unregisterContentObserver(this); 141 } 142 143 @Override onChange(boolean selfChange)144 public void onChange(boolean selfChange) { 145 updateState(mPreference); 146 } 147 } 148 } 149