The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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; |
| 18 | |
Fan Zhang | 31b2100 | 2019-01-16 13:49:47 -0800 | [diff] [blame] | 19 | import android.app.settings.SettingsEnums; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
Christopher Tate | 6a5929b | 2012-09-10 15:39:05 -0700 | [diff] [blame] | 22 | import android.os.UserHandle; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 23 | import android.provider.Settings; |
Bonian Chen | 5980b80 | 2019-11-12 17:36:00 +0800 | [diff] [blame^] | 24 | import android.telephony.PhoneStateListener; |
| 25 | import android.telephony.ServiceState; |
| 26 | import android.telephony.SubscriptionInfo; |
| 27 | import android.telephony.SubscriptionManager; |
| 28 | import android.telephony.TelephonyManager; |
| 29 | import android.util.Log; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 30 | |
Bonian Chen | 5980b80 | 2019-11-12 17:36:00 +0800 | [diff] [blame^] | 31 | import com.android.settings.network.GlobalSettingsChangeListener; |
| 32 | import com.android.settings.network.ProxySubscriptionManager; |
| 33 | import com.android.settings.overlay.FeatureFactory; |
Jason Monk | fc1b00c | 2015-01-28 10:35:53 -0500 | [diff] [blame] | 34 | import com.android.settingslib.WirelessUtils; |
Leif Hendrik Wilden | 28dee1f | 2018-01-11 10:15:36 -0800 | [diff] [blame] | 35 | import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame] | 36 | |
Bonian Chen | 5980b80 | 2019-11-12 17:36:00 +0800 | [diff] [blame^] | 37 | import java.util.ArrayList; |
| 38 | import java.util.List; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 39 | |
Bonian Chen | 5980b80 | 2019-11-12 17:36:00 +0800 | [diff] [blame^] | 40 | /** |
| 41 | * Monitor and update configuration of airplane mode settings |
| 42 | */ |
| 43 | public class AirplaneModeEnabler extends GlobalSettingsChangeListener { |
| 44 | |
| 45 | private static final String LOG_TAG = "AirplaneModeEnabler"; |
| 46 | private static final boolean DEBUG = false; |
Fan Zhang | aa71afe | 2016-09-22 10:43:12 -0700 | [diff] [blame] | 47 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 48 | private final Context mContext; |
Fan Zhang | aa71afe | 2016-09-22 10:43:12 -0700 | [diff] [blame] | 49 | private final MetricsFeatureProvider mMetricsFeatureProvider; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 50 | |
CY Cheng | fee4807 | 2018-03-26 18:38:59 +0800 | [diff] [blame] | 51 | private OnAirplaneModeChangedListener mOnAirplaneModeChangedListener; |
| 52 | |
| 53 | public interface OnAirplaneModeChangedListener { |
| 54 | /** |
| 55 | * Called when airplane mode status is changed. |
| 56 | * |
| 57 | * @param isAirplaneModeOn the airplane mode is on |
| 58 | */ |
| 59 | void onAirplaneModeChanged(boolean isAirplaneModeOn); |
| 60 | } |
| 61 | |
Bonian Chen | 5980b80 | 2019-11-12 17:36:00 +0800 | [diff] [blame^] | 62 | private TelephonyManager mTelephonyManager; |
| 63 | private ProxySubscriptionManager mProxySubscriptionMgr; |
| 64 | private List<ServiceStateListener> mServiceStateListeners; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 65 | |
Bonian Chen | 5980b80 | 2019-11-12 17:36:00 +0800 | [diff] [blame^] | 66 | private GlobalSettingsChangeListener mAirplaneModeObserver; |
Amith Yamasani | b98c00e | 2011-01-26 09:56:44 -0800 | [diff] [blame] | 67 | |
Bonian Chen | 5980b80 | 2019-11-12 17:36:00 +0800 | [diff] [blame^] | 68 | public AirplaneModeEnabler(Context context, |
CY Cheng | fee4807 | 2018-03-26 18:38:59 +0800 | [diff] [blame] | 69 | OnAirplaneModeChangedListener listener) { |
Bonian Chen | 5980b80 | 2019-11-12 17:36:00 +0800 | [diff] [blame^] | 70 | super(context, Settings.Global.AIRPLANE_MODE_ON); |
Fan Zhang | aa71afe | 2016-09-22 10:43:12 -0700 | [diff] [blame] | 71 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 72 | mContext = context; |
Bonian Chen | 5980b80 | 2019-11-12 17:36:00 +0800 | [diff] [blame^] | 73 | mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider(); |
CY Cheng | fee4807 | 2018-03-26 18:38:59 +0800 | [diff] [blame] | 74 | mOnAirplaneModeChangedListener = listener; |
Fan Zhang | aa71afe | 2016-09-22 10:43:12 -0700 | [diff] [blame] | 75 | |
Bonian Chen | 5980b80 | 2019-11-12 17:36:00 +0800 | [diff] [blame^] | 76 | mTelephonyManager = context.getSystemService(TelephonyManager.class); |
| 77 | mProxySubscriptionMgr = ProxySubscriptionManager.getInstance(context); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Implementation of GlobalSettingsChangeListener.onChanged |
| 82 | */ |
| 83 | public void onChanged(String field) { |
| 84 | if (DEBUG) { |
| 85 | Log.d(LOG_TAG, "Airplane mode configuration update"); |
| 86 | } |
| 87 | onAirplaneModeChanged(); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | public void resume() { |
Bonian Chen | 5980b80 | 2019-11-12 17:36:00 +0800 | [diff] [blame^] | 91 | final List<SubscriptionInfo> subInfoList = |
| 92 | mProxySubscriptionMgr.getActiveSubscriptionsInfo(); |
| 93 | |
| 94 | mServiceStateListeners = new ArrayList<ServiceStateListener>(); |
| 95 | |
| 96 | // add default listener |
| 97 | mServiceStateListeners.add(new ServiceStateListener(mTelephonyManager, |
| 98 | SubscriptionManager.INVALID_SUBSCRIPTION_ID, this)); |
| 99 | |
| 100 | if (subInfoList == null) { |
| 101 | for (SubscriptionInfo subInfo : subInfoList) { |
| 102 | mServiceStateListeners.add(new ServiceStateListener(mTelephonyManager, |
| 103 | subInfo.getSubscriptionId(), this)); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | for (ServiceStateListener ssListener : mServiceStateListeners) { |
| 108 | ssListener.start(); |
| 109 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 110 | } |
Fan Zhang | aa71afe | 2016-09-22 10:43:12 -0700 | [diff] [blame] | 111 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 112 | public void pause() { |
Bonian Chen | 5980b80 | 2019-11-12 17:36:00 +0800 | [diff] [blame^] | 113 | if (mServiceStateListeners == null) { |
| 114 | return; |
| 115 | } |
| 116 | for (ServiceStateListener ssListener : mServiceStateListeners) { |
| 117 | ssListener.stop(); |
| 118 | } |
| 119 | mServiceStateListeners = null; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 120 | } |
Amith Yamasani | b98c00e | 2011-01-26 09:56:44 -0800 | [diff] [blame] | 121 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 122 | private void setAirplaneModeOn(boolean enabling) { |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 123 | // Change the system setting |
Fan Zhang | aa71afe | 2016-09-22 10:43:12 -0700 | [diff] [blame] | 124 | Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, |
| 125 | enabling ? 1 : 0); |
CY Cheng | fee4807 | 2018-03-26 18:38:59 +0800 | [diff] [blame] | 126 | |
| 127 | // Notify listener the system setting is changed. |
| 128 | if (mOnAirplaneModeChangedListener != null) { |
| 129 | mOnAirplaneModeChangedListener.onAirplaneModeChanged(enabling); |
| 130 | } |
Fan Zhang | aa71afe | 2016-09-22 10:43:12 -0700 | [diff] [blame] | 131 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 132 | // Post the intent |
Bonian Chen | 5980b80 | 2019-11-12 17:36:00 +0800 | [diff] [blame^] | 133 | final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 134 | intent.putExtra("state", enabling); |
Christopher Tate | 6a5929b | 2012-09-10 15:39:05 -0700 | [diff] [blame] | 135 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Called when we've received confirmation that the airplane mode was set. |
Irfan Sheriff | ecea11f | 2010-08-02 19:11:16 -0700 | [diff] [blame] | 140 | * TODO: We update the checkbox summary when we get notified |
| 141 | * that mobile radio is powered up/down. We should not have dependency |
| 142 | * on one radio alone. We need to do the following: |
| 143 | * - handle the case of wifi/bluetooth failures |
| 144 | * - mobile does not send failure notification, fail on timeout. |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 145 | */ |
| 146 | private void onAirplaneModeChanged() { |
CY Cheng | fee4807 | 2018-03-26 18:38:59 +0800 | [diff] [blame] | 147 | if (mOnAirplaneModeChangedListener != null) { |
| 148 | mOnAirplaneModeChangedListener.onAirplaneModeChanged(isAirplaneModeOn()); |
| 149 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 150 | } |
Fan Zhang | aa71afe | 2016-09-22 10:43:12 -0700 | [diff] [blame] | 151 | |
Bonian Chen | 5980b80 | 2019-11-12 17:36:00 +0800 | [diff] [blame^] | 152 | /** |
| 153 | * Check the status of ECM mode |
| 154 | * |
| 155 | * @return any subscription within device is under ECM mode |
| 156 | */ |
| 157 | public boolean isInEcmMode() { |
| 158 | if (mTelephonyManager.getEmergencyCallbackMode()) { |
| 159 | return true; |
| 160 | } |
| 161 | final List<SubscriptionInfo> subInfoList = |
| 162 | mProxySubscriptionMgr.getActiveSubscriptionsInfo(); |
| 163 | if (subInfoList == null) { |
| 164 | return false; |
| 165 | } |
| 166 | for (SubscriptionInfo subInfo : subInfoList) { |
| 167 | final TelephonyManager telephonyManager = |
| 168 | mTelephonyManager.createForSubscriptionId(subInfo.getSubscriptionId()); |
| 169 | if (telephonyManager != null) { |
| 170 | if (telephonyManager.getEmergencyCallbackMode()) { |
| 171 | return true; |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | return false; |
| 176 | } |
| 177 | |
CY Cheng | fee4807 | 2018-03-26 18:38:59 +0800 | [diff] [blame] | 178 | public void setAirplaneMode(boolean isAirplaneModeOn) { |
Bonian Chen | 5980b80 | 2019-11-12 17:36:00 +0800 | [diff] [blame^] | 179 | if (isInEcmMode()) { |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame] | 180 | // In ECM mode, do not update database at this point |
Bonian Chen | 5980b80 | 2019-11-12 17:36:00 +0800 | [diff] [blame^] | 181 | Log.d(LOG_TAG, "ECM airplane mode=" + isAirplaneModeOn); |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame] | 182 | } else { |
Fan Zhang | 31b2100 | 2019-01-16 13:49:47 -0800 | [diff] [blame] | 183 | mMetricsFeatureProvider.action(mContext, SettingsEnums.ACTION_AIRPLANE_TOGGLE, |
CY Cheng | fee4807 | 2018-03-26 18:38:59 +0800 | [diff] [blame] | 184 | isAirplaneModeOn); |
| 185 | setAirplaneModeOn(isAirplaneModeOn); |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame] | 186 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 187 | } |
| 188 | |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame] | 189 | public void setAirplaneModeInECM(boolean isECMExit, boolean isAirplaneModeOn) { |
Bonian Chen | 5980b80 | 2019-11-12 17:36:00 +0800 | [diff] [blame^] | 190 | Log.d(LOG_TAG, "Exist ECM=" + isECMExit + ", with airplane mode=" + isAirplaneModeOn); |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame] | 191 | if (isECMExit) { |
| 192 | // update database based on the current checkbox state |
| 193 | setAirplaneModeOn(isAirplaneModeOn); |
| 194 | } else { |
Irfan Sheriff | ecea11f | 2010-08-02 19:11:16 -0700 | [diff] [blame] | 195 | // update summary |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame] | 196 | onAirplaneModeChanged(); |
| 197 | } |
| 198 | } |
| 199 | |
CY Cheng | fee4807 | 2018-03-26 18:38:59 +0800 | [diff] [blame] | 200 | public boolean isAirplaneModeOn() { |
| 201 | return WirelessUtils.isAirplaneModeOn(mContext); |
| 202 | } |
Bonian Chen | 5980b80 | 2019-11-12 17:36:00 +0800 | [diff] [blame^] | 203 | |
| 204 | private static class ServiceStateListener extends PhoneStateListener { |
| 205 | private ServiceStateListener(TelephonyManager telephonyManager, int subscriptionId, |
| 206 | AirplaneModeEnabler enabler) { |
| 207 | super(); |
| 208 | mSubId = subscriptionId; |
| 209 | mTelephonyManager = getSubIdSpecificTelephonyManager(telephonyManager); |
| 210 | mEnabler = enabler; |
| 211 | } |
| 212 | |
| 213 | private int mSubId; |
| 214 | private TelephonyManager mTelephonyManager; |
| 215 | private AirplaneModeEnabler mEnabler; |
| 216 | |
| 217 | int getSubscriptionId() { |
| 218 | return mSubId; |
| 219 | } |
| 220 | |
| 221 | void start() { |
| 222 | if (mTelephonyManager != null) { |
| 223 | mTelephonyManager.listen(this, PhoneStateListener.LISTEN_SERVICE_STATE); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | void stop() { |
| 228 | if (mTelephonyManager != null) { |
| 229 | mTelephonyManager.listen(this, PhoneStateListener.LISTEN_NONE); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public void onServiceStateChanged(ServiceState serviceState) { |
| 235 | if (DEBUG) { |
| 236 | Log.d(LOG_TAG, "ServiceState in sub" + mSubId + ": " + serviceState); |
| 237 | } |
| 238 | mEnabler.onAirplaneModeChanged(); |
| 239 | } |
| 240 | |
| 241 | private TelephonyManager getSubIdSpecificTelephonyManager( |
| 242 | TelephonyManager telephonyManager) { |
| 243 | if (mSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) { |
| 244 | return telephonyManager; |
| 245 | } |
| 246 | return telephonyManager.createForSubscriptionId(mSubId); |
| 247 | } |
| 248 | } |
| 249 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 250 | } |