blob: 8b29d67bb288510b4e9c3e9ec0af4ff1c6dfddaa [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;
Jason Chiu1766b3c2020-04-09 14:29:58 +080022import android.os.Looper;
Christopher Tate6a5929b2012-09-10 15:39:05 -070023import android.os.UserHandle;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080024import android.provider.Settings;
Bonian Chen5980b802019-11-12 17:36:00 +080025import android.telephony.PhoneStateListener;
Bonian Chen5980b802019-11-12 17:36:00 +080026import android.telephony.SubscriptionInfo;
Zoey Chenb8a639f2021-03-10 16:01:18 +080027import android.telephony.TelephonyCallback;
Bonian Chen5980b802019-11-12 17:36:00 +080028import android.telephony.TelephonyManager;
29import android.util.Log;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080030
Bonian Chen717e0762020-01-02 12:01:10 +080031import androidx.annotation.VisibleForTesting;
32
Bonian Chen5980b802019-11-12 17:36:00 +080033import com.android.settings.network.GlobalSettingsChangeListener;
34import com.android.settings.network.ProxySubscriptionManager;
35import com.android.settings.overlay.FeatureFactory;
Jason Monkfc1b00c2015-01-28 10:35:53 -050036import com.android.settingslib.WirelessUtils;
Leif Hendrik Wilden28dee1f2018-01-11 10:15:36 -080037import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050038
Bonian Chen5980b802019-11-12 17:36:00 +080039import java.util.List;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080040
Bonian Chen5980b802019-11-12 17:36:00 +080041/**
42 * Monitor and update configuration of airplane mode settings
43 */
44public class AirplaneModeEnabler extends GlobalSettingsChangeListener {
45
46 private static final String LOG_TAG = "AirplaneModeEnabler";
47 private static final boolean DEBUG = false;
Fan Zhangaa71afe2016-09-22 10:43:12 -070048
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080049 private final Context mContext;
Fan Zhangaa71afe2016-09-22 10:43:12 -070050 private final MetricsFeatureProvider mMetricsFeatureProvider;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080051
CY Chengfee48072018-03-26 18:38:59 +080052 private OnAirplaneModeChangedListener mOnAirplaneModeChangedListener;
53
54 public interface OnAirplaneModeChangedListener {
55 /**
56 * Called when airplane mode status is changed.
57 *
58 * @param isAirplaneModeOn the airplane mode is on
59 */
60 void onAirplaneModeChanged(boolean isAirplaneModeOn);
61 }
62
Bonian Chen5980b802019-11-12 17:36:00 +080063 private TelephonyManager mTelephonyManager;
Bonian Chen717e0762020-01-02 12:01:10 +080064 @VisibleForTesting
Zoey Chenb8a639f2021-03-10 16:01:18 +080065 AirplaneModeTelephonyCallback mTelephonyCallback;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -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);
Zoey Chenb8a639f2021-03-10 16:01:18 +080075 mTelephonyCallback = new AirplaneModeTelephonyCallback();
76 }
Bonian Chena72a0382019-12-31 16:59:37 +080077
Zoey Chenb8a639f2021-03-10 16:01:18 +080078 class AirplaneModeTelephonyCallback extends TelephonyCallback implements
79 TelephonyCallback.RadioPowerStateListener {
80 @Override
81 public void onRadioPowerStateChanged(int state) {
82 if (DEBUG) {
83 Log.d(LOG_TAG, "RadioPower: " + state);
Bonian Chena72a0382019-12-31 16:59:37 +080084 }
Zoey Chenb8a639f2021-03-10 16:01:18 +080085 onAirplaneModeChanged();
86 }
Bonian Chen5980b802019-11-12 17:36:00 +080087 }
88
89 /**
90 * Implementation of GlobalSettingsChangeListener.onChanged
91 */
Jason Chiu1766b3c2020-04-09 14:29:58 +080092 @Override
Bonian Chen5980b802019-11-12 17:36:00 +080093 public void onChanged(String field) {
94 if (DEBUG) {
95 Log.d(LOG_TAG, "Airplane mode configuration update");
96 }
97 onAirplaneModeChanged();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080098 }
99
Jason Chiu1766b3c2020-04-09 14:29:58 +0800100 /**
101 * Start listening to the phone state change
102 */
103 public void start() {
Zoey Chenb8a639f2021-03-10 16:01:18 +0800104 mTelephonyManager.registerTelephonyCallback(mContext.getMainExecutor(), mTelephonyCallback);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800105 }
Fan Zhangaa71afe2016-09-22 10:43:12 -0700106
Jason Chiu1766b3c2020-04-09 14:29:58 +0800107 /**
108 * Stop listening to the phone state change
109 */
110 public void stop() {
Zoey Chenb8a639f2021-03-10 16:01:18 +0800111 mTelephonyManager.unregisterTelephonyCallback(mTelephonyCallback);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800112 }
Amith Yamasanib98c00e2011-01-26 09:56:44 -0800113
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800114 private void setAirplaneModeOn(boolean enabling) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800115 // Change the system setting
Fan Zhangaa71afe2016-09-22 10:43:12 -0700116 Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON,
117 enabling ? 1 : 0);
CY Chengfee48072018-03-26 18:38:59 +0800118
119 // Notify listener the system setting is changed.
120 if (mOnAirplaneModeChangedListener != null) {
121 mOnAirplaneModeChangedListener.onAirplaneModeChanged(enabling);
122 }
Fan Zhangaa71afe2016-09-22 10:43:12 -0700123
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800124 // Post the intent
Bonian Chen5980b802019-11-12 17:36:00 +0800125 final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800126 intent.putExtra("state", enabling);
Christopher Tate6a5929b2012-09-10 15:39:05 -0700127 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800128 }
129
130 /**
131 * Called when we've received confirmation that the airplane mode was set.
Irfan Sheriffecea11f2010-08-02 19:11:16 -0700132 * TODO: We update the checkbox summary when we get notified
133 * that mobile radio is powered up/down. We should not have dependency
134 * on one radio alone. We need to do the following:
135 * - handle the case of wifi/bluetooth failures
136 * - mobile does not send failure notification, fail on timeout.
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800137 */
138 private void onAirplaneModeChanged() {
CY Chengfee48072018-03-26 18:38:59 +0800139 if (mOnAirplaneModeChangedListener != null) {
140 mOnAirplaneModeChangedListener.onAirplaneModeChanged(isAirplaneModeOn());
141 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800142 }
Fan Zhangaa71afe2016-09-22 10:43:12 -0700143
Bonian Chen5980b802019-11-12 17:36:00 +0800144 /**
145 * Check the status of ECM mode
146 *
147 * @return any subscription within device is under ECM mode
148 */
149 public boolean isInEcmMode() {
150 if (mTelephonyManager.getEmergencyCallbackMode()) {
151 return true;
152 }
153 final List<SubscriptionInfo> subInfoList =
Bonian Chen717e0762020-01-02 12:01:10 +0800154 ProxySubscriptionManager.getInstance(mContext).getActiveSubscriptionsInfo();
Bonian Chen5980b802019-11-12 17:36:00 +0800155 if (subInfoList == null) {
156 return false;
157 }
158 for (SubscriptionInfo subInfo : subInfoList) {
159 final TelephonyManager telephonyManager =
160 mTelephonyManager.createForSubscriptionId(subInfo.getSubscriptionId());
161 if (telephonyManager != null) {
162 if (telephonyManager.getEmergencyCallbackMode()) {
163 return true;
164 }
165 }
166 }
167 return false;
168 }
169
CY Chengfee48072018-03-26 18:38:59 +0800170 public void setAirplaneMode(boolean isAirplaneModeOn) {
Bonian Chen5980b802019-11-12 17:36:00 +0800171 if (isInEcmMode()) {
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500172 // In ECM mode, do not update database at this point
Bonian Chen5980b802019-11-12 17:36:00 +0800173 Log.d(LOG_TAG, "ECM airplane mode=" + isAirplaneModeOn);
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500174 } else {
Fan Zhang31b21002019-01-16 13:49:47 -0800175 mMetricsFeatureProvider.action(mContext, SettingsEnums.ACTION_AIRPLANE_TOGGLE,
CY Chengfee48072018-03-26 18:38:59 +0800176 isAirplaneModeOn);
177 setAirplaneModeOn(isAirplaneModeOn);
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500178 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800179 }
180
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500181 public void setAirplaneModeInECM(boolean isECMExit, boolean isAirplaneModeOn) {
Bonian Chen5980b802019-11-12 17:36:00 +0800182 Log.d(LOG_TAG, "Exist ECM=" + isECMExit + ", with airplane mode=" + isAirplaneModeOn);
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500183 if (isECMExit) {
184 // update database based on the current checkbox state
185 setAirplaneModeOn(isAirplaneModeOn);
186 } else {
Irfan Sheriffecea11f2010-08-02 19:11:16 -0700187 // update summary
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500188 onAirplaneModeChanged();
189 }
190 }
191
CY Chengfee48072018-03-26 18:38:59 +0800192 public boolean isAirplaneModeOn() {
193 return WirelessUtils.isAirplaneModeOn(mContext);
194 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800195}