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 android.content.Context; 19 import android.os.UserManager; 20 import android.support.annotation.VisibleForTesting; 21 import android.support.v7.preference.Preference; 22 import android.support.v7.preference.PreferenceScreen; 23 import android.telephony.PhoneStateListener; 24 import android.telephony.ServiceState; 25 import android.telephony.TelephonyManager; 26 27 import com.android.settings.Utils; 28 import com.android.settings.core.PreferenceController; 29 import com.android.settings.core.lifecycle.LifecycleObserver; 30 import com.android.settings.core.lifecycle.events.OnPause; 31 import com.android.settings.core.lifecycle.events.OnResume; 32 33 import static android.os.UserHandle.myUserId; 34 import static android.os.UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS; 35 import static com.android.settingslib.RestrictedLockUtils.hasBaseUserRestriction; 36 37 public class MobileNetworkPreferenceController extends PreferenceController implements 38 LifecycleObserver, OnResume, OnPause { 39 40 private static final String KEY_MOBILE_NETWORK_SETTINGS = "mobile_network_settings"; 41 42 private final UserManager mUserManager; 43 private final boolean mIsSecondaryUser; 44 private final TelephonyManager mTelephonyManager; 45 private Preference mPreference; 46 @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) 47 PhoneStateListener mPhoneStateListener; 48 49 public MobileNetworkPreferenceController(Context context) { 50 super(context); 51 mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE); 52 mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 53 mIsSecondaryUser = !mUserManager.isAdminUser(); 54 } 55 56 @Override 57 public boolean isAvailable() { 58 return !mIsSecondaryUser 59 && !Utils.isWifiOnly(mContext) 60 && !hasBaseUserRestriction(mContext, DISALLOW_CONFIG_MOBILE_NETWORKS, myUserId()); 61 } 62 63 @Override 64 public void displayPreference(PreferenceScreen screen) { 65 super.displayPreference(screen); 66 if (isAvailable()) { 67 mPreference = screen.findPreference(getPreferenceKey()); 68 } 69 } 70 71 @Override 72 public String getPreferenceKey() { 73 return KEY_MOBILE_NETWORK_SETTINGS; 74 } 75 76 @Override 77 public void onResume() { 78 if (isAvailable()) { 79 if (mPhoneStateListener == null) { 80 mPhoneStateListener = new PhoneStateListener() { 81 @Override 82 public void onServiceStateChanged(ServiceState serviceState) { 83 if (mPreference != null) { 84 mPreference.setSummary(mTelephonyManager.getNetworkOperatorName()); 85 } 86 } 87 }; 88 } 89 mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE); 90 } 91 } 92 93 @Override 94 public void onPause() { 95 if (mPhoneStateListener != null) { 96 mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE); 97 } 98 } 99 } 100