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.gsm; 18 19 import static androidx.lifecycle.Lifecycle.Event.ON_START; 20 import static androidx.lifecycle.Lifecycle.Event.ON_STOP; 21 22 import android.app.settings.SettingsEnums; 23 import android.content.Context; 24 import android.os.Bundle; 25 import android.provider.Settings; 26 import android.telephony.ServiceState; 27 import android.telephony.SubscriptionManager; 28 import android.telephony.TelephonyManager; 29 import android.text.TextUtils; 30 31 import androidx.lifecycle.Lifecycle; 32 import androidx.lifecycle.LifecycleObserver; 33 import androidx.lifecycle.OnLifecycleEvent; 34 import androidx.preference.Preference; 35 import androidx.preference.PreferenceScreen; 36 37 import com.android.settings.R; 38 import com.android.settings.core.SubSettingLauncher; 39 import com.android.settings.network.AllowedNetworkTypesListener; 40 import com.android.settings.network.telephony.MobileNetworkUtils; 41 import com.android.settings.network.telephony.NetworkSelectSettings; 42 import com.android.settings.network.telephony.TelephonyBasePreferenceController; 43 44 /** 45 * Preference controller for "Open network select" 46 */ 47 public class OpenNetworkSelectPagePreferenceController extends 48 TelephonyBasePreferenceController implements 49 AutoSelectPreferenceController.OnNetworkSelectModeListener, LifecycleObserver { 50 51 private TelephonyManager mTelephonyManager; 52 private Preference mPreference; 53 private PreferenceScreen mPreferenceScreen; 54 private AllowedNetworkTypesListener mAllowedNetworkTypesListener; 55 OpenNetworkSelectPagePreferenceController(Context context, String key)56 public OpenNetworkSelectPagePreferenceController(Context context, String key) { 57 super(context, key); 58 mTelephonyManager = context.getSystemService(TelephonyManager.class); 59 mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID; 60 mAllowedNetworkTypesListener = new AllowedNetworkTypesListener( 61 context.getMainExecutor()); 62 mAllowedNetworkTypesListener.setAllowedNetworkTypesListener( 63 () -> updatePreference()); 64 65 } 66 updatePreference()67 private void updatePreference() { 68 if (mPreferenceScreen != null) { 69 displayPreference(mPreferenceScreen); 70 } 71 if (mPreference != null) { 72 updateState(mPreference); 73 } 74 } 75 76 @Override getAvailabilityStatus(int subId)77 public int getAvailabilityStatus(int subId) { 78 return MobileNetworkUtils.shouldDisplayNetworkSelectOptions(mContext, subId) 79 ? AVAILABLE 80 : CONDITIONALLY_UNAVAILABLE; 81 } 82 83 @OnLifecycleEvent(ON_START) onStart()84 public void onStart() { 85 mAllowedNetworkTypesListener.register(mContext, mSubId); 86 } 87 88 @OnLifecycleEvent(ON_STOP) onStop()89 public void onStop() { 90 mAllowedNetworkTypesListener.unregister(mContext, mSubId); 91 } 92 93 @Override displayPreference(PreferenceScreen screen)94 public void displayPreference(PreferenceScreen screen) { 95 super.displayPreference(screen); 96 mPreferenceScreen = screen; 97 mPreference = screen.findPreference(getPreferenceKey()); 98 } 99 100 @Override updateState(Preference preference)101 public void updateState(Preference preference) { 102 super.updateState(preference); 103 preference.setEnabled(mTelephonyManager.getNetworkSelectionMode() 104 != TelephonyManager.NETWORK_SELECTION_MODE_AUTO); 105 } 106 107 @Override getSummary()108 public CharSequence getSummary() { 109 final ServiceState ss = mTelephonyManager.getServiceState(); 110 if (ss != null && ss.getState() == ServiceState.STATE_IN_SERVICE) { 111 return MobileNetworkUtils.getCurrentCarrierNameForDisplay(mContext, mSubId); 112 } else { 113 return mContext.getString(R.string.network_disconnected); 114 } 115 } 116 117 @Override handlePreferenceTreeClick(Preference preference)118 public boolean handlePreferenceTreeClick(Preference preference) { 119 if (TextUtils.equals(preference.getKey(), getPreferenceKey())) { 120 final Bundle bundle = new Bundle(); 121 bundle.putInt(Settings.EXTRA_SUB_ID, mSubId); 122 new SubSettingLauncher(mContext) 123 .setDestination(NetworkSelectSettings.class.getName()) 124 .setSourceMetricsCategory(SettingsEnums.MOBILE_NETWORK_SELECT) 125 .setTitleRes(R.string.choose_network_title) 126 .setArguments(bundle) 127 .launch(); 128 return true; 129 } 130 131 return false; 132 } 133 init(Lifecycle lifecycle, int subId)134 public OpenNetworkSelectPagePreferenceController init(Lifecycle lifecycle, int subId) { 135 mSubId = subId; 136 mTelephonyManager = mContext.getSystemService(TelephonyManager.class) 137 .createForSubscriptionId(mSubId); 138 lifecycle.addObserver(this); 139 return this; 140 } 141 142 @Override onNetworkSelectModeChanged()143 public void onNetworkSelectModeChanged() { 144 updateState(mPreference); 145 } 146 } 147