blob: dde7ce0fcf67d2fe019c2f313d4d961ffe085d61 [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;
Amith Yamasanib98c00e2011-01-26 09:56:44 -080022import android.database.ContentObserver;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080023import android.os.Handler;
Jason Monkfcd5f0f2018-04-20 14:08:57 -040024import android.os.Looper;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080025import android.os.Message;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050026import android.os.SystemProperties;
Christopher Tate6a5929b2012-09-10 15:39:05 -070027import android.os.UserHandle;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080028import android.provider.Settings;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080029
Gilles Debunnedfc26372011-07-07 18:23:04 -070030import com.android.internal.telephony.PhoneStateIntentReceiver;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050031import com.android.internal.telephony.TelephonyProperties;
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
CY Chengfee48072018-03-26 18:38:59 +080035public class AirplaneModeEnabler {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080036
Fan Zhangaa71afe2016-09-22 10:43:12 -070037 private static final int EVENT_SERVICE_STATE_CHANGED = 3;
38
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080039 private final Context mContext;
Fan Zhangaa71afe2016-09-22 10:43:12 -070040 private final MetricsFeatureProvider mMetricsFeatureProvider;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080041
42 private PhoneStateIntentReceiver mPhoneStateReceiver;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080043
CY Chengfee48072018-03-26 18:38:59 +080044 private OnAirplaneModeChangedListener mOnAirplaneModeChangedListener;
45
46 public interface OnAirplaneModeChangedListener {
47 /**
48 * Called when airplane mode status is changed.
49 *
50 * @param isAirplaneModeOn the airplane mode is on
51 */
52 void onAirplaneModeChanged(boolean isAirplaneModeOn);
53 }
54
Jason Monkfcd5f0f2018-04-20 14:08:57 -040055 private Handler mHandler = new Handler(Looper.getMainLooper()) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080056 @Override
57 public void handleMessage(Message msg) {
58 switch (msg.what) {
59 case EVENT_SERVICE_STATE_CHANGED:
60 onAirplaneModeChanged();
61 break;
62 }
63 }
64 };
65
Jason Monkfcd5f0f2018-04-20 14:08:57 -040066 private ContentObserver mAirplaneModeObserver = new ContentObserver(
67 new Handler(Looper.getMainLooper())) {
Amith Yamasanib98c00e2011-01-26 09:56:44 -080068 @Override
69 public void onChange(boolean selfChange) {
70 onAirplaneModeChanged();
71 }
72 };
73
CY Chengfee48072018-03-26 18:38:59 +080074 public AirplaneModeEnabler(Context context, MetricsFeatureProvider metricsFeatureProvider,
75 OnAirplaneModeChangedListener listener) {
Fan Zhangaa71afe2016-09-22 10:43:12 -070076
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080077 mContext = context;
Fan Zhangaa71afe2016-09-22 10:43:12 -070078 mMetricsFeatureProvider = metricsFeatureProvider;
CY Chengfee48072018-03-26 18:38:59 +080079 mOnAirplaneModeChangedListener = listener;
Fan Zhangaa71afe2016-09-22 10:43:12 -070080
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080081 mPhoneStateReceiver = new PhoneStateIntentReceiver(mContext, mHandler);
82 mPhoneStateReceiver.notifyServiceState(EVENT_SERVICE_STATE_CHANGED);
83 }
84
85 public void resume() {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080086 mPhoneStateReceiver.registerIntent();
Amith Yamasanib98c00e2011-01-26 09:56:44 -080087 mContext.getContentResolver().registerContentObserver(
Christopher Tate6a5929b2012-09-10 15:39:05 -070088 Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true,
Amith Yamasanib98c00e2011-01-26 09:56:44 -080089 mAirplaneModeObserver);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080090 }
Fan Zhangaa71afe2016-09-22 10:43:12 -070091
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080092 public void pause() {
93 mPhoneStateReceiver.unregisterIntent();
Amith Yamasanib98c00e2011-01-26 09:56:44 -080094 mContext.getContentResolver().unregisterContentObserver(mAirplaneModeObserver);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080095 }
Amith Yamasanib98c00e2011-01-26 09:56:44 -080096
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080097 private void setAirplaneModeOn(boolean enabling) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080098 // Change the system setting
Fan Zhangaa71afe2016-09-22 10:43:12 -070099 Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON,
100 enabling ? 1 : 0);
CY Chengfee48072018-03-26 18:38:59 +0800101
102 // Notify listener the system setting is changed.
103 if (mOnAirplaneModeChangedListener != null) {
104 mOnAirplaneModeChangedListener.onAirplaneModeChanged(enabling);
105 }
Fan Zhangaa71afe2016-09-22 10:43:12 -0700106
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800107 // Post the intent
108 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
109 intent.putExtra("state", enabling);
Christopher Tate6a5929b2012-09-10 15:39:05 -0700110 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800111 }
112
113 /**
114 * Called when we've received confirmation that the airplane mode was set.
Irfan Sheriffecea11f2010-08-02 19:11:16 -0700115 * TODO: We update the checkbox summary when we get notified
116 * that mobile radio is powered up/down. We should not have dependency
117 * on one radio alone. We need to do the following:
118 * - handle the case of wifi/bluetooth failures
119 * - mobile does not send failure notification, fail on timeout.
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800120 */
121 private void onAirplaneModeChanged() {
CY Chengfee48072018-03-26 18:38:59 +0800122 if (mOnAirplaneModeChangedListener != null) {
123 mOnAirplaneModeChangedListener.onAirplaneModeChanged(isAirplaneModeOn());
124 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800125 }
Fan Zhangaa71afe2016-09-22 10:43:12 -0700126
CY Chengfee48072018-03-26 18:38:59 +0800127 public void setAirplaneMode(boolean isAirplaneModeOn) {
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500128 if (Boolean.parseBoolean(
Fan Zhangaa71afe2016-09-22 10:43:12 -0700129 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500130 // In ECM mode, do not update database at this point
131 } else {
Fan Zhang31b21002019-01-16 13:49:47 -0800132 mMetricsFeatureProvider.action(mContext, SettingsEnums.ACTION_AIRPLANE_TOGGLE,
CY Chengfee48072018-03-26 18:38:59 +0800133 isAirplaneModeOn);
134 setAirplaneModeOn(isAirplaneModeOn);
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500135 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800136 }
137
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500138 public void setAirplaneModeInECM(boolean isECMExit, boolean isAirplaneModeOn) {
139 if (isECMExit) {
140 // update database based on the current checkbox state
141 setAirplaneModeOn(isAirplaneModeOn);
142 } else {
Irfan Sheriffecea11f2010-08-02 19:11:16 -0700143 // update summary
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500144 onAirplaneModeChanged();
145 }
146 }
147
CY Chengfee48072018-03-26 18:38:59 +0800148 public boolean isAirplaneModeOn() {
149 return WirelessUtils.isAirplaneModeOn(mContext);
150 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800151}