blob: 11f1a2832205b5d950b3666e3cdb9cd4abe2f8a1 [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
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080019import android.content.Context;
20import android.content.Intent;
Amith Yamasanib98c00e2011-01-26 09:56:44 -080021import android.database.ContentObserver;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080022import android.os.Handler;
23import android.os.Message;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050024import android.os.SystemProperties;
Christopher Tate6a5929b2012-09-10 15:39:05 -070025import android.os.UserHandle;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080026import android.provider.Settings;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080027
Tamas Berghammer265d3c22016-06-22 15:34:45 +010028import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Gilles Debunnedfc26372011-07-07 18:23:04 -070029import com.android.internal.telephony.PhoneStateIntentReceiver;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050030import com.android.internal.telephony.TelephonyProperties;
Jason Monkfc1b00c2015-01-28 10:35:53 -050031import com.android.settingslib.WirelessUtils;
Leif Hendrik Wilden28dee1f2018-01-11 10:15:36 -080032import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050033
CY Chengfee48072018-03-26 18:38:59 +080034public class AirplaneModeEnabler {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080035
Fan Zhangaa71afe2016-09-22 10:43:12 -070036 private static final int EVENT_SERVICE_STATE_CHANGED = 3;
37
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080038 private final Context mContext;
Fan Zhangaa71afe2016-09-22 10:43:12 -070039 private final MetricsFeatureProvider mMetricsFeatureProvider;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080040
41 private PhoneStateIntentReceiver mPhoneStateReceiver;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080042
CY Chengfee48072018-03-26 18:38:59 +080043 private OnAirplaneModeChangedListener mOnAirplaneModeChangedListener;
44
45 public interface OnAirplaneModeChangedListener {
46 /**
47 * Called when airplane mode status is changed.
48 *
49 * @param isAirplaneModeOn the airplane mode is on
50 */
51 void onAirplaneModeChanged(boolean isAirplaneModeOn);
52 }
53
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080054 private Handler mHandler = new Handler() {
55 @Override
56 public void handleMessage(Message msg) {
57 switch (msg.what) {
58 case EVENT_SERVICE_STATE_CHANGED:
59 onAirplaneModeChanged();
60 break;
61 }
62 }
63 };
64
Amith Yamasanib98c00e2011-01-26 09:56:44 -080065 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
66 @Override
67 public void onChange(boolean selfChange) {
68 onAirplaneModeChanged();
69 }
70 };
71
CY Chengfee48072018-03-26 18:38:59 +080072 public AirplaneModeEnabler(Context context, MetricsFeatureProvider metricsFeatureProvider,
73 OnAirplaneModeChangedListener listener) {
Fan Zhangaa71afe2016-09-22 10:43:12 -070074
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080075 mContext = context;
Fan Zhangaa71afe2016-09-22 10:43:12 -070076 mMetricsFeatureProvider = metricsFeatureProvider;
CY Chengfee48072018-03-26 18:38:59 +080077 mOnAirplaneModeChangedListener = listener;
Fan Zhangaa71afe2016-09-22 10:43:12 -070078
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080079 mPhoneStateReceiver = new PhoneStateIntentReceiver(mContext, mHandler);
80 mPhoneStateReceiver.notifyServiceState(EVENT_SERVICE_STATE_CHANGED);
81 }
82
83 public void resume() {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080084 mPhoneStateReceiver.registerIntent();
Amith Yamasanib98c00e2011-01-26 09:56:44 -080085 mContext.getContentResolver().registerContentObserver(
Christopher Tate6a5929b2012-09-10 15:39:05 -070086 Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true,
Amith Yamasanib98c00e2011-01-26 09:56:44 -080087 mAirplaneModeObserver);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080088 }
Fan Zhangaa71afe2016-09-22 10:43:12 -070089
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080090 public void pause() {
91 mPhoneStateReceiver.unregisterIntent();
Amith Yamasanib98c00e2011-01-26 09:56:44 -080092 mContext.getContentResolver().unregisterContentObserver(mAirplaneModeObserver);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080093 }
Amith Yamasanib98c00e2011-01-26 09:56:44 -080094
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080095 private void setAirplaneModeOn(boolean enabling) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080096 // Change the system setting
Fan Zhangaa71afe2016-09-22 10:43:12 -070097 Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON,
98 enabling ? 1 : 0);
CY Chengfee48072018-03-26 18:38:59 +080099
100 // Notify listener the system setting is changed.
101 if (mOnAirplaneModeChangedListener != null) {
102 mOnAirplaneModeChangedListener.onAirplaneModeChanged(enabling);
103 }
Fan Zhangaa71afe2016-09-22 10:43:12 -0700104
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800105 // Post the intent
106 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
107 intent.putExtra("state", enabling);
Christopher Tate6a5929b2012-09-10 15:39:05 -0700108 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800109 }
110
111 /**
112 * Called when we've received confirmation that the airplane mode was set.
Irfan Sheriffecea11f2010-08-02 19:11:16 -0700113 * TODO: We update the checkbox summary when we get notified
114 * that mobile radio is powered up/down. We should not have dependency
115 * on one radio alone. We need to do the following:
116 * - handle the case of wifi/bluetooth failures
117 * - mobile does not send failure notification, fail on timeout.
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800118 */
119 private void onAirplaneModeChanged() {
CY Chengfee48072018-03-26 18:38:59 +0800120 if (mOnAirplaneModeChangedListener != null) {
121 mOnAirplaneModeChangedListener.onAirplaneModeChanged(isAirplaneModeOn());
122 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800123 }
Fan Zhangaa71afe2016-09-22 10:43:12 -0700124
CY Chengfee48072018-03-26 18:38:59 +0800125 public void setAirplaneMode(boolean isAirplaneModeOn) {
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500126 if (Boolean.parseBoolean(
Fan Zhangaa71afe2016-09-22 10:43:12 -0700127 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500128 // In ECM mode, do not update database at this point
129 } else {
CY Chengfee48072018-03-26 18:38:59 +0800130 mMetricsFeatureProvider.action(mContext, MetricsEvent.ACTION_AIRPLANE_TOGGLE,
131 isAirplaneModeOn);
132 setAirplaneModeOn(isAirplaneModeOn);
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500133 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800134 }
135
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500136 public void setAirplaneModeInECM(boolean isECMExit, boolean isAirplaneModeOn) {
137 if (isECMExit) {
138 // update database based on the current checkbox state
139 setAirplaneModeOn(isAirplaneModeOn);
140 } else {
Irfan Sheriffecea11f2010-08-02 19:11:16 -0700141 // update summary
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500142 onAirplaneModeChanged();
143 }
144 }
145
CY Chengfee48072018-03-26 18:38:59 +0800146 public boolean isAirplaneModeOn() {
147 return WirelessUtils.isAirplaneModeOn(mContext);
148 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800149}