blob: 6cc0d606dd2207f48eca7e5e54289d5e89a0de48 [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;
Jason Monk39b46742015-09-10 15:52:51 -040027import android.support.v14.preference.SwitchPreference;
28import android.support.v7.preference.Preference;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080029
Chris Wren1b6ffba2015-05-08 17:10:01 -040030import com.android.internal.logging.MetricsLogger;
Gilles Debunnedfc26372011-07-07 18:23:04 -070031import com.android.internal.telephony.PhoneStateIntentReceiver;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050032import com.android.internal.telephony.TelephonyProperties;
Jason Monkfc1b00c2015-01-28 10:35:53 -050033import com.android.settingslib.WirelessUtils;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050034
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080035public class AirplaneModeEnabler implements Preference.OnPreferenceChangeListener {
36
37 private final Context mContext;
38
39 private PhoneStateIntentReceiver mPhoneStateReceiver;
40
Fabrice Di Meglioe3bced22014-08-06 10:22:52 -070041 private final SwitchPreference mSwitchPref;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080042
43 private static final int EVENT_SERVICE_STATE_CHANGED = 3;
44
45 private Handler mHandler = new Handler() {
46 @Override
47 public void handleMessage(Message msg) {
48 switch (msg.what) {
49 case EVENT_SERVICE_STATE_CHANGED:
50 onAirplaneModeChanged();
51 break;
52 }
53 }
54 };
55
Amith Yamasanib98c00e2011-01-26 09:56:44 -080056 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
57 @Override
58 public void onChange(boolean selfChange) {
59 onAirplaneModeChanged();
60 }
61 };
62
Fabrice Di Megliodaef2e22014-10-15 19:00:35 -070063 public AirplaneModeEnabler(Context context, SwitchPreference airplaneModeSwitchPreference) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080064
65 mContext = context;
Fabrice Di Megliodaef2e22014-10-15 19:00:35 -070066 mSwitchPref = airplaneModeSwitchPreference;
67
68 airplaneModeSwitchPreference.setPersistent(false);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080069
70 mPhoneStateReceiver = new PhoneStateIntentReceiver(mContext, mHandler);
71 mPhoneStateReceiver.notifyServiceState(EVENT_SERVICE_STATE_CHANGED);
72 }
73
74 public void resume() {
75
Jason Monkfc1b00c2015-01-28 10:35:53 -050076 mSwitchPref.setChecked(WirelessUtils.isAirplaneModeOn(mContext));
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080077
78 mPhoneStateReceiver.registerIntent();
Fabrice Di Meglioe3bced22014-08-06 10:22:52 -070079 mSwitchPref.setOnPreferenceChangeListener(this);
Amith Yamasanib98c00e2011-01-26 09:56:44 -080080 mContext.getContentResolver().registerContentObserver(
Christopher Tate6a5929b2012-09-10 15:39:05 -070081 Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true,
Amith Yamasanib98c00e2011-01-26 09:56:44 -080082 mAirplaneModeObserver);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080083 }
84
85 public void pause() {
86 mPhoneStateReceiver.unregisterIntent();
Fabrice Di Meglioe3bced22014-08-06 10:22:52 -070087 mSwitchPref.setOnPreferenceChangeListener(null);
Amith Yamasanib98c00e2011-01-26 09:56:44 -080088 mContext.getContentResolver().unregisterContentObserver(mAirplaneModeObserver);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080089 }
Amith Yamasanib98c00e2011-01-26 09:56:44 -080090
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080091 private void setAirplaneModeOn(boolean enabling) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080092 // Change the system setting
Christopher Tate6a5929b2012-09-10 15:39:05 -070093 Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON,
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080094 enabling ? 1 : 0);
Irfan Sheriffecea11f2010-08-02 19:11:16 -070095 // Update the UI to reflect system setting
Fabrice Di Meglioe3bced22014-08-06 10:22:52 -070096 mSwitchPref.setChecked(enabling);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080097
98 // Post the intent
99 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
100 intent.putExtra("state", enabling);
Christopher Tate6a5929b2012-09-10 15:39:05 -0700101 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800102 }
103
104 /**
105 * Called when we've received confirmation that the airplane mode was set.
Irfan Sheriffecea11f2010-08-02 19:11:16 -0700106 * TODO: We update the checkbox summary when we get notified
107 * that mobile radio is powered up/down. We should not have dependency
108 * on one radio alone. We need to do the following:
109 * - handle the case of wifi/bluetooth failures
110 * - mobile does not send failure notification, fail on timeout.
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800111 */
112 private void onAirplaneModeChanged() {
Jason Monkfc1b00c2015-01-28 10:35:53 -0500113 mSwitchPref.setChecked(WirelessUtils.isAirplaneModeOn(mContext));
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800114 }
115
116 /**
117 * Called when someone clicks on the checkbox preference.
118 */
119 public boolean onPreferenceChange(Preference preference, Object newValue) {
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500120 if (Boolean.parseBoolean(
121 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
122 // In ECM mode, do not update database at this point
123 } else {
Chris Wren1b6ffba2015-05-08 17:10:01 -0400124 Boolean value = (Boolean) newValue;
125 MetricsLogger.action(mContext, MetricsLogger.ACTION_AIRPLANE_TOGGLE, value);
126 setAirplaneModeOn(value);
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500127 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800128 return true;
129 }
130
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500131 public void setAirplaneModeInECM(boolean isECMExit, boolean isAirplaneModeOn) {
132 if (isECMExit) {
133 // update database based on the current checkbox state
134 setAirplaneModeOn(isAirplaneModeOn);
135 } else {
Irfan Sheriffecea11f2010-08-02 19:11:16 -0700136 // update summary
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500137 onAirplaneModeChanged();
138 }
139 }
140
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800141}