blob: ee3eaa456cb6eb654c00bc0aafdb963920009538 [file] [log] [blame]
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -08001/*
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
17package com.android.settings;
18
Fan Zhang31b21002019-01-16 13:49:47 -080019import android.app.settings.SettingsEnums;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080020import android.content.Context;
21import android.content.Intent;
Christopher Tate6a5929b2012-09-10 15:39:05 -070022import android.os.UserHandle;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080023import android.provider.Settings;
Bonian Chen5980b802019-11-12 17:36:00 +080024import android.telephony.PhoneStateListener;
25import android.telephony.ServiceState;
26import android.telephony.SubscriptionInfo;
27import android.telephony.SubscriptionManager;
28import android.telephony.TelephonyManager;
29import android.util.Log;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080030
Bonian Chen5980b802019-11-12 17:36:00 +080031import com.android.settings.network.GlobalSettingsChangeListener;
32import com.android.settings.network.ProxySubscriptionManager;
33import com.android.settings.overlay.FeatureFactory;
Jason Monkfc1b00c2015-01-28 10:35:53 -050034import com.android.settingslib.WirelessUtils;
Leif Hendrik Wilden28dee1f2018-01-11 10:15:36 -080035import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050036
Bonian Chen5980b802019-11-12 17:36:00 +080037import java.util.ArrayList;
38import java.util.List;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080039
Bonian Chen5980b802019-11-12 17:36:00 +080040/**
41 * Monitor and update configuration of airplane mode settings
42 */
43public class AirplaneModeEnabler extends GlobalSettingsChangeListener {
44
45 private static final String LOG_TAG = "AirplaneModeEnabler";
46 private static final boolean DEBUG = false;
Fan Zhangaa71afe2016-09-22 10:43:12 -070047
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080048 private final Context mContext;
Fan Zhangaa71afe2016-09-22 10:43:12 -070049 private final MetricsFeatureProvider mMetricsFeatureProvider;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080050
CY Chengfee48072018-03-26 18:38:59 +080051 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 Chen5980b802019-11-12 17:36:00 +080062 private TelephonyManager mTelephonyManager;
63 private ProxySubscriptionManager mProxySubscriptionMgr;
64 private List<ServiceStateListener> mServiceStateListeners;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080065
Bonian Chen5980b802019-11-12 17:36:00 +080066 private GlobalSettingsChangeListener mAirplaneModeObserver;
Amith Yamasanib98c00e2011-01-26 09:56:44 -080067
Bonian Chen5980b802019-11-12 17:36:00 +080068 public AirplaneModeEnabler(Context context,
CY Chengfee48072018-03-26 18:38:59 +080069 OnAirplaneModeChangedListener listener) {
Bonian Chen5980b802019-11-12 17:36:00 +080070 super(context, Settings.Global.AIRPLANE_MODE_ON);
Fan Zhangaa71afe2016-09-22 10:43:12 -070071
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080072 mContext = context;
Bonian Chen5980b802019-11-12 17:36:00 +080073 mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
CY Chengfee48072018-03-26 18:38:59 +080074 mOnAirplaneModeChangedListener = listener;
Fan Zhangaa71afe2016-09-22 10:43:12 -070075
Bonian Chen5980b802019-11-12 17:36:00 +080076 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 Projectafc4ab22009-03-03 19:32:34 -080088 }
89
90 public void resume() {
Bonian Chen5980b802019-11-12 17:36:00 +080091 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
Bonian Chen8a512ca2019-12-09 13:39:27 +0800100 if (subInfoList != null) {
Bonian Chen5980b802019-11-12 17:36:00 +0800101 for (SubscriptionInfo subInfo : subInfoList) {
102 mServiceStateListeners.add(new ServiceStateListener(mTelephonyManager,
103 subInfo.getSubscriptionId(), this));
104 }
105 }
106
Bonian Chenf980a292019-12-30 11:24:58 +0800107 for (ServiceStateListener listener : mServiceStateListeners) {
108 listener.start();
Bonian Chen5980b802019-11-12 17:36:00 +0800109 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800110 }
Fan Zhangaa71afe2016-09-22 10:43:12 -0700111
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800112 public void pause() {
Bonian Chen5980b802019-11-12 17:36:00 +0800113 if (mServiceStateListeners == null) {
114 return;
115 }
Bonian Chenf980a292019-12-30 11:24:58 +0800116 for (ServiceStateListener listener : mServiceStateListeners) {
117 listener.stop();
Bonian Chen5980b802019-11-12 17:36:00 +0800118 }
119 mServiceStateListeners = null;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800120 }
Amith Yamasanib98c00e2011-01-26 09:56:44 -0800121
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800122 private void setAirplaneModeOn(boolean enabling) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800123 // Change the system setting
Fan Zhangaa71afe2016-09-22 10:43:12 -0700124 Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON,
125 enabling ? 1 : 0);
CY Chengfee48072018-03-26 18:38:59 +0800126
127 // Notify listener the system setting is changed.
128 if (mOnAirplaneModeChangedListener != null) {
129 mOnAirplaneModeChangedListener.onAirplaneModeChanged(enabling);
130 }
Fan Zhangaa71afe2016-09-22 10:43:12 -0700131
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800132 // Post the intent
Bonian Chen5980b802019-11-12 17:36:00 +0800133 final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800134 intent.putExtra("state", enabling);
Christopher Tate6a5929b2012-09-10 15:39:05 -0700135 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800136 }
137
138 /**
139 * Called when we've received confirmation that the airplane mode was set.
Irfan Sheriffecea11f2010-08-02 19:11:16 -0700140 * 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 Projectafc4ab22009-03-03 19:32:34 -0800145 */
146 private void onAirplaneModeChanged() {
CY Chengfee48072018-03-26 18:38:59 +0800147 if (mOnAirplaneModeChangedListener != null) {
148 mOnAirplaneModeChangedListener.onAirplaneModeChanged(isAirplaneModeOn());
149 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800150 }
Fan Zhangaa71afe2016-09-22 10:43:12 -0700151
Bonian Chen5980b802019-11-12 17:36:00 +0800152 /**
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 Chengfee48072018-03-26 18:38:59 +0800178 public void setAirplaneMode(boolean isAirplaneModeOn) {
Bonian Chen5980b802019-11-12 17:36:00 +0800179 if (isInEcmMode()) {
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500180 // In ECM mode, do not update database at this point
Bonian Chen5980b802019-11-12 17:36:00 +0800181 Log.d(LOG_TAG, "ECM airplane mode=" + isAirplaneModeOn);
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500182 } else {
Fan Zhang31b21002019-01-16 13:49:47 -0800183 mMetricsFeatureProvider.action(mContext, SettingsEnums.ACTION_AIRPLANE_TOGGLE,
CY Chengfee48072018-03-26 18:38:59 +0800184 isAirplaneModeOn);
185 setAirplaneModeOn(isAirplaneModeOn);
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500186 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800187 }
188
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500189 public void setAirplaneModeInECM(boolean isECMExit, boolean isAirplaneModeOn) {
Bonian Chen5980b802019-11-12 17:36:00 +0800190 Log.d(LOG_TAG, "Exist ECM=" + isECMExit + ", with airplane mode=" + isAirplaneModeOn);
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500191 if (isECMExit) {
192 // update database based on the current checkbox state
193 setAirplaneModeOn(isAirplaneModeOn);
194 } else {
Irfan Sheriffecea11f2010-08-02 19:11:16 -0700195 // update summary
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500196 onAirplaneModeChanged();
197 }
198 }
199
CY Chengfee48072018-03-26 18:38:59 +0800200 public boolean isAirplaneModeOn() {
201 return WirelessUtils.isAirplaneModeOn(mContext);
202 }
Bonian Chen5980b802019-11-12 17:36:00 +0800203
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
Bonian Chenf980a292019-12-30 11:24:58 +0800213 private final int mSubId;
214 private final TelephonyManager mTelephonyManager;
215 private final AirplaneModeEnabler mEnabler;
Bonian Chen5980b802019-11-12 17:36:00 +0800216
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 Projectafc4ab22009-03-03 19:32:34 -0800250}