blob: a843a046c98ecc8c4aafd64f2cc735be42f5757a [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;
Bonian Chen5980b802019-11-12 17:36:00 +080025import android.telephony.SubscriptionInfo;
Bonian Chen5980b802019-11-12 17:36:00 +080026import android.telephony.TelephonyManager;
27import android.util.Log;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080028
Bonian Chen717e0762020-01-02 12:01:10 +080029import androidx.annotation.VisibleForTesting;
30
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.List;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080038
Bonian Chen5980b802019-11-12 17:36:00 +080039/**
40 * Monitor and update configuration of airplane mode settings
41 */
42public class AirplaneModeEnabler extends GlobalSettingsChangeListener {
43
44 private static final String LOG_TAG = "AirplaneModeEnabler";
45 private static final boolean DEBUG = false;
Fan Zhangaa71afe2016-09-22 10:43:12 -070046
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080047 private final Context mContext;
Fan Zhangaa71afe2016-09-22 10:43:12 -070048 private final MetricsFeatureProvider mMetricsFeatureProvider;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080049
CY Chengfee48072018-03-26 18:38:59 +080050 private OnAirplaneModeChangedListener mOnAirplaneModeChangedListener;
51
52 public interface OnAirplaneModeChangedListener {
53 /**
54 * Called when airplane mode status is changed.
55 *
56 * @param isAirplaneModeOn the airplane mode is on
57 */
58 void onAirplaneModeChanged(boolean isAirplaneModeOn);
59 }
60
Bonian Chen5980b802019-11-12 17:36:00 +080061 private TelephonyManager mTelephonyManager;
Bonian Chen717e0762020-01-02 12:01:10 +080062 @VisibleForTesting
63 PhoneStateListener mPhoneStateListener;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080064
Bonian Chen5980b802019-11-12 17:36:00 +080065 private GlobalSettingsChangeListener mAirplaneModeObserver;
Amith Yamasanib98c00e2011-01-26 09:56:44 -080066
Bonian Chena72a0382019-12-31 16:59:37 +080067 public AirplaneModeEnabler(Context context, OnAirplaneModeChangedListener listener) {
Bonian Chen5980b802019-11-12 17:36:00 +080068 super(context, Settings.Global.AIRPLANE_MODE_ON);
Fan Zhangaa71afe2016-09-22 10:43:12 -070069
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080070 mContext = context;
Bonian Chen5980b802019-11-12 17:36:00 +080071 mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
CY Chengfee48072018-03-26 18:38:59 +080072 mOnAirplaneModeChangedListener = listener;
Fan Zhangaa71afe2016-09-22 10:43:12 -070073
Bonian Chen5980b802019-11-12 17:36:00 +080074 mTelephonyManager = context.getSystemService(TelephonyManager.class);
Bonian Chena72a0382019-12-31 16:59:37 +080075
76 mPhoneStateListener = new PhoneStateListener() {
77 @Override
78 public void onRadioPowerStateChanged(int state) {
79 if (DEBUG) {
80 Log.d(LOG_TAG, "RadioPower: " + state);
81 }
82 onAirplaneModeChanged();
83 }
84 };
Bonian Chen5980b802019-11-12 17:36:00 +080085 }
86
87 /**
88 * Implementation of GlobalSettingsChangeListener.onChanged
89 */
90 public void onChanged(String field) {
91 if (DEBUG) {
92 Log.d(LOG_TAG, "Airplane mode configuration update");
93 }
94 onAirplaneModeChanged();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080095 }
96
97 public void resume() {
Bonian Chena72a0382019-12-31 16:59:37 +080098 mTelephonyManager.listen(mPhoneStateListener,
99 PhoneStateListener.LISTEN_RADIO_POWER_STATE_CHANGED);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800100 }
Fan Zhangaa71afe2016-09-22 10:43:12 -0700101
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800102 public void pause() {
Bonian Chena72a0382019-12-31 16:59:37 +0800103 mTelephonyManager.listen(mPhoneStateListener,
104 PhoneStateListener.LISTEN_NONE);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800105 }
Amith Yamasanib98c00e2011-01-26 09:56:44 -0800106
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800107 private void setAirplaneModeOn(boolean enabling) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800108 // Change the system setting
Fan Zhangaa71afe2016-09-22 10:43:12 -0700109 Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON,
110 enabling ? 1 : 0);
CY Chengfee48072018-03-26 18:38:59 +0800111
112 // Notify listener the system setting is changed.
113 if (mOnAirplaneModeChangedListener != null) {
114 mOnAirplaneModeChangedListener.onAirplaneModeChanged(enabling);
115 }
Fan Zhangaa71afe2016-09-22 10:43:12 -0700116
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800117 // Post the intent
Bonian Chen5980b802019-11-12 17:36:00 +0800118 final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800119 intent.putExtra("state", enabling);
Christopher Tate6a5929b2012-09-10 15:39:05 -0700120 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800121 }
122
123 /**
124 * Called when we've received confirmation that the airplane mode was set.
Irfan Sheriffecea11f2010-08-02 19:11:16 -0700125 * TODO: We update the checkbox summary when we get notified
126 * that mobile radio is powered up/down. We should not have dependency
127 * on one radio alone. We need to do the following:
128 * - handle the case of wifi/bluetooth failures
129 * - mobile does not send failure notification, fail on timeout.
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800130 */
131 private void onAirplaneModeChanged() {
CY Chengfee48072018-03-26 18:38:59 +0800132 if (mOnAirplaneModeChangedListener != null) {
133 mOnAirplaneModeChangedListener.onAirplaneModeChanged(isAirplaneModeOn());
134 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800135 }
Fan Zhangaa71afe2016-09-22 10:43:12 -0700136
Bonian Chen5980b802019-11-12 17:36:00 +0800137 /**
138 * Check the status of ECM mode
139 *
140 * @return any subscription within device is under ECM mode
141 */
142 public boolean isInEcmMode() {
143 if (mTelephonyManager.getEmergencyCallbackMode()) {
144 return true;
145 }
146 final List<SubscriptionInfo> subInfoList =
Bonian Chen717e0762020-01-02 12:01:10 +0800147 ProxySubscriptionManager.getInstance(mContext).getActiveSubscriptionsInfo();
Bonian Chen5980b802019-11-12 17:36:00 +0800148 if (subInfoList == null) {
149 return false;
150 }
151 for (SubscriptionInfo subInfo : subInfoList) {
152 final TelephonyManager telephonyManager =
153 mTelephonyManager.createForSubscriptionId(subInfo.getSubscriptionId());
154 if (telephonyManager != null) {
155 if (telephonyManager.getEmergencyCallbackMode()) {
156 return true;
157 }
158 }
159 }
160 return false;
161 }
162
CY Chengfee48072018-03-26 18:38:59 +0800163 public void setAirplaneMode(boolean isAirplaneModeOn) {
Bonian Chen5980b802019-11-12 17:36:00 +0800164 if (isInEcmMode()) {
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500165 // In ECM mode, do not update database at this point
Bonian Chen5980b802019-11-12 17:36:00 +0800166 Log.d(LOG_TAG, "ECM airplane mode=" + isAirplaneModeOn);
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500167 } else {
Fan Zhang31b21002019-01-16 13:49:47 -0800168 mMetricsFeatureProvider.action(mContext, SettingsEnums.ACTION_AIRPLANE_TOGGLE,
CY Chengfee48072018-03-26 18:38:59 +0800169 isAirplaneModeOn);
170 setAirplaneModeOn(isAirplaneModeOn);
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500171 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800172 }
173
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500174 public void setAirplaneModeInECM(boolean isECMExit, boolean isAirplaneModeOn) {
Bonian Chen5980b802019-11-12 17:36:00 +0800175 Log.d(LOG_TAG, "Exist ECM=" + isECMExit + ", with airplane mode=" + isAirplaneModeOn);
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500176 if (isECMExit) {
177 // update database based on the current checkbox state
178 setAirplaneModeOn(isAirplaneModeOn);
179 } else {
Irfan Sheriffecea11f2010-08-02 19:11:16 -0700180 // update summary
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500181 onAirplaneModeChanged();
182 }
183 }
184
CY Chengfee48072018-03-26 18:38:59 +0800185 public boolean isAirplaneModeOn() {
186 return WirelessUtils.isAirplaneModeOn(mContext);
187 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800188}