blob: f1d36f2654e2aa4c67b6b555c954c7338d7352d0 [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 Chen5980b802019-11-12 17:36:00 +080029import com.android.settings.network.GlobalSettingsChangeListener;
30import com.android.settings.network.ProxySubscriptionManager;
31import com.android.settings.overlay.FeatureFactory;
Jason Monkfc1b00c2015-01-28 10:35:53 -050032import com.android.settingslib.WirelessUtils;
Leif Hendrik Wilden28dee1f2018-01-11 10:15:36 -080033import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050034
Bonian Chen5980b802019-11-12 17:36:00 +080035import java.util.List;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080036
Bonian Chen5980b802019-11-12 17:36:00 +080037/**
38 * Monitor and update configuration of airplane mode settings
39 */
40public class AirplaneModeEnabler extends GlobalSettingsChangeListener {
41
42 private static final String LOG_TAG = "AirplaneModeEnabler";
43 private static final boolean DEBUG = false;
Fan Zhangaa71afe2016-09-22 10:43:12 -070044
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080045 private final Context mContext;
Fan Zhangaa71afe2016-09-22 10:43:12 -070046 private final MetricsFeatureProvider mMetricsFeatureProvider;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080047
CY Chengfee48072018-03-26 18:38:59 +080048 private OnAirplaneModeChangedListener mOnAirplaneModeChangedListener;
49
50 public interface OnAirplaneModeChangedListener {
51 /**
52 * Called when airplane mode status is changed.
53 *
54 * @param isAirplaneModeOn the airplane mode is on
55 */
56 void onAirplaneModeChanged(boolean isAirplaneModeOn);
57 }
58
Bonian Chen5980b802019-11-12 17:36:00 +080059 private TelephonyManager mTelephonyManager;
60 private ProxySubscriptionManager mProxySubscriptionMgr;
Bonian Chena72a0382019-12-31 16:59:37 +080061 private PhoneStateListener mPhoneStateListener;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080062
Bonian Chen5980b802019-11-12 17:36:00 +080063 private GlobalSettingsChangeListener mAirplaneModeObserver;
Amith Yamasanib98c00e2011-01-26 09:56:44 -080064
Bonian Chena72a0382019-12-31 16:59:37 +080065 public AirplaneModeEnabler(Context context, OnAirplaneModeChangedListener listener) {
Bonian Chen5980b802019-11-12 17:36:00 +080066 super(context, Settings.Global.AIRPLANE_MODE_ON);
Fan Zhangaa71afe2016-09-22 10:43:12 -070067
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080068 mContext = context;
Bonian Chen5980b802019-11-12 17:36:00 +080069 mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
CY Chengfee48072018-03-26 18:38:59 +080070 mOnAirplaneModeChangedListener = listener;
Fan Zhangaa71afe2016-09-22 10:43:12 -070071
Bonian Chen5980b802019-11-12 17:36:00 +080072 mTelephonyManager = context.getSystemService(TelephonyManager.class);
73 mProxySubscriptionMgr = ProxySubscriptionManager.getInstance(context);
Bonian Chena72a0382019-12-31 16:59:37 +080074
75 mPhoneStateListener = new PhoneStateListener() {
76 @Override
77 public void onRadioPowerStateChanged(int state) {
78 if (DEBUG) {
79 Log.d(LOG_TAG, "RadioPower: " + state);
80 }
81 onAirplaneModeChanged();
82 }
83 };
Bonian Chen5980b802019-11-12 17:36:00 +080084 }
85
86 /**
87 * Implementation of GlobalSettingsChangeListener.onChanged
88 */
89 public void onChanged(String field) {
90 if (DEBUG) {
91 Log.d(LOG_TAG, "Airplane mode configuration update");
92 }
93 onAirplaneModeChanged();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080094 }
95
96 public void resume() {
Bonian Chena72a0382019-12-31 16:59:37 +080097 mTelephonyManager.listen(mPhoneStateListener,
98 PhoneStateListener.LISTEN_RADIO_POWER_STATE_CHANGED);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080099 }
Fan Zhangaa71afe2016-09-22 10:43:12 -0700100
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800101 public void pause() {
Bonian Chena72a0382019-12-31 16:59:37 +0800102 mTelephonyManager.listen(mPhoneStateListener,
103 PhoneStateListener.LISTEN_NONE);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800104 }
Amith Yamasanib98c00e2011-01-26 09:56:44 -0800105
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800106 private void setAirplaneModeOn(boolean enabling) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800107 // Change the system setting
Fan Zhangaa71afe2016-09-22 10:43:12 -0700108 Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON,
109 enabling ? 1 : 0);
CY Chengfee48072018-03-26 18:38:59 +0800110
111 // Notify listener the system setting is changed.
112 if (mOnAirplaneModeChangedListener != null) {
113 mOnAirplaneModeChangedListener.onAirplaneModeChanged(enabling);
114 }
Fan Zhangaa71afe2016-09-22 10:43:12 -0700115
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800116 // Post the intent
Bonian Chen5980b802019-11-12 17:36:00 +0800117 final Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800118 intent.putExtra("state", enabling);
Christopher Tate6a5929b2012-09-10 15:39:05 -0700119 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800120 }
121
122 /**
123 * Called when we've received confirmation that the airplane mode was set.
Irfan Sheriffecea11f2010-08-02 19:11:16 -0700124 * TODO: We update the checkbox summary when we get notified
125 * that mobile radio is powered up/down. We should not have dependency
126 * on one radio alone. We need to do the following:
127 * - handle the case of wifi/bluetooth failures
128 * - mobile does not send failure notification, fail on timeout.
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800129 */
130 private void onAirplaneModeChanged() {
CY Chengfee48072018-03-26 18:38:59 +0800131 if (mOnAirplaneModeChangedListener != null) {
132 mOnAirplaneModeChangedListener.onAirplaneModeChanged(isAirplaneModeOn());
133 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800134 }
Fan Zhangaa71afe2016-09-22 10:43:12 -0700135
Bonian Chen5980b802019-11-12 17:36:00 +0800136 /**
137 * Check the status of ECM mode
138 *
139 * @return any subscription within device is under ECM mode
140 */
141 public boolean isInEcmMode() {
142 if (mTelephonyManager.getEmergencyCallbackMode()) {
143 return true;
144 }
145 final List<SubscriptionInfo> subInfoList =
146 mProxySubscriptionMgr.getActiveSubscriptionsInfo();
147 if (subInfoList == null) {
148 return false;
149 }
150 for (SubscriptionInfo subInfo : subInfoList) {
151 final TelephonyManager telephonyManager =
152 mTelephonyManager.createForSubscriptionId(subInfo.getSubscriptionId());
153 if (telephonyManager != null) {
154 if (telephonyManager.getEmergencyCallbackMode()) {
155 return true;
156 }
157 }
158 }
159 return false;
160 }
161
CY Chengfee48072018-03-26 18:38:59 +0800162 public void setAirplaneMode(boolean isAirplaneModeOn) {
Bonian Chen5980b802019-11-12 17:36:00 +0800163 if (isInEcmMode()) {
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500164 // In ECM mode, do not update database at this point
Bonian Chen5980b802019-11-12 17:36:00 +0800165 Log.d(LOG_TAG, "ECM airplane mode=" + isAirplaneModeOn);
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500166 } else {
Fan Zhang31b21002019-01-16 13:49:47 -0800167 mMetricsFeatureProvider.action(mContext, SettingsEnums.ACTION_AIRPLANE_TOGGLE,
CY Chengfee48072018-03-26 18:38:59 +0800168 isAirplaneModeOn);
169 setAirplaneModeOn(isAirplaneModeOn);
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500170 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800171 }
172
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500173 public void setAirplaneModeInECM(boolean isECMExit, boolean isAirplaneModeOn) {
Bonian Chen5980b802019-11-12 17:36:00 +0800174 Log.d(LOG_TAG, "Exist ECM=" + isECMExit + ", with airplane mode=" + isAirplaneModeOn);
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500175 if (isECMExit) {
176 // update database based on the current checkbox state
177 setAirplaneModeOn(isAirplaneModeOn);
178 } else {
Irfan Sheriffecea11f2010-08-02 19:11:16 -0700179 // update summary
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500180 onAirplaneModeChanged();
181 }
182 }
183
CY Chengfee48072018-03-26 18:38:59 +0800184 public boolean isAirplaneModeOn() {
185 return WirelessUtils.isAirplaneModeOn(mContext);
186 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800187}