1 /* 2 * Copyright (C) 2016 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 package com.android.settings.network; 17 18 import static android.os.UserHandle.myUserId; 19 import static android.os.UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS; 20 21 import android.content.BroadcastReceiver; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.content.IntentFilter; 25 import android.os.UserManager; 26 import android.provider.Settings; 27 import android.support.annotation.VisibleForTesting; 28 import android.support.v7.preference.Preference; 29 import android.support.v7.preference.PreferenceScreen; 30 import android.telephony.PhoneStateListener; 31 import android.telephony.ServiceState; 32 import android.telephony.TelephonyManager; 33 34 import com.android.settings.core.PreferenceControllerMixin; 35 import com.android.settingslib.RestrictedLockUtils; 36 import com.android.settingslib.RestrictedPreference; 37 import com.android.settingslib.Utils; 38 import com.android.settingslib.core.AbstractPreferenceController; 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 public class MobileNetworkPreferenceController extends AbstractPreferenceController 44 implements PreferenceControllerMixin, LifecycleObserver, OnStart, OnStop { 45 46 private static final String KEY_MOBILE_NETWORK_SETTINGS = "mobile_network_settings"; 47 48 private final boolean mIsSecondaryUser; 49 private final TelephonyManager mTelephonyManager; 50 private final UserManager mUserManager; 51 private Preference mPreference; 52 @VisibleForTesting 53 PhoneStateListener mPhoneStateListener; 54 55 private BroadcastReceiver mAirplanModeChangedReceiver; 56 MobileNetworkPreferenceController(Context context)57 public MobileNetworkPreferenceController(Context context) { 58 super(context); 59 mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE); 60 mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 61 mIsSecondaryUser = !mUserManager.isAdminUser(); 62 63 mAirplanModeChangedReceiver = new BroadcastReceiver() { 64 @Override 65 public void onReceive(Context context, Intent intent) { 66 updateState(mPreference); 67 } 68 }; 69 } 70 71 @Override isAvailable()72 public boolean isAvailable() { 73 return !isUserRestricted() && !Utils.isWifiOnly(mContext); 74 } 75 isUserRestricted()76 public boolean isUserRestricted() { 77 return mIsSecondaryUser || 78 RestrictedLockUtils.hasBaseUserRestriction( 79 mContext, 80 DISALLOW_CONFIG_MOBILE_NETWORKS, 81 myUserId()); 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 getPreferenceKey()91 public String getPreferenceKey() { 92 return KEY_MOBILE_NETWORK_SETTINGS; 93 } 94 95 @Override onStart()96 public void onStart() { 97 if (isAvailable()) { 98 if (mPhoneStateListener == null) { 99 mPhoneStateListener = new PhoneStateListener() { 100 @Override 101 public void onServiceStateChanged(ServiceState serviceState) { 102 updateState(mPreference); 103 } 104 }; 105 } 106 mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE); 107 } 108 if (mAirplanModeChangedReceiver != null) { 109 mContext.registerReceiver(mAirplanModeChangedReceiver, 110 new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED)); 111 } 112 } 113 114 @Override onStop()115 public void onStop() { 116 if (mPhoneStateListener != null) { 117 mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE); 118 } 119 if (mAirplanModeChangedReceiver != null) { 120 mContext.unregisterReceiver(mAirplanModeChangedReceiver); 121 } 122 } 123 124 @Override updateState(Preference preference)125 public void updateState(Preference preference) { 126 super.updateState(preference); 127 128 if (preference instanceof RestrictedPreference && 129 ((RestrictedPreference) preference).isDisabledByAdmin()) { 130 return; 131 } 132 preference.setEnabled(Settings.Global.getInt( 133 mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0) == 0); 134 } 135 136 @Override getSummary()137 public CharSequence getSummary() { 138 return mTelephonyManager.getNetworkOperatorName(); 139 } 140 } 141