blob: 950d52a04b5a086a0d37583b1135e2a1cd3ef4c4 [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;
Chris Wren9d1bfd12016-01-26 18:04:01 -050031import com.android.internal.logging.MetricsProto.MetricsEvent;
Gilles Debunnedfc26372011-07-07 18:23:04 -070032import com.android.internal.telephony.PhoneStateIntentReceiver;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050033import com.android.internal.telephony.TelephonyProperties;
Jason Monkfc1b00c2015-01-28 10:35:53 -050034import com.android.settingslib.WirelessUtils;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050035
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080036public class AirplaneModeEnabler implements Preference.OnPreferenceChangeListener {
37
38 private final Context mContext;
39
40 private PhoneStateIntentReceiver mPhoneStateReceiver;
41
Fabrice Di Meglioe3bced22014-08-06 10:22:52 -070042 private final SwitchPreference mSwitchPref;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080043
44 private static final int EVENT_SERVICE_STATE_CHANGED = 3;
45
46 private Handler mHandler = new Handler() {
47 @Override
48 public void handleMessage(Message msg) {
49 switch (msg.what) {
50 case EVENT_SERVICE_STATE_CHANGED:
51 onAirplaneModeChanged();
52 break;
53 }
54 }
55 };
56
Amith Yamasanib98c00e2011-01-26 09:56:44 -080057 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
58 @Override
59 public void onChange(boolean selfChange) {
60 onAirplaneModeChanged();
61 }
62 };
63
Fabrice Di Megliodaef2e22014-10-15 19:00:35 -070064 public AirplaneModeEnabler(Context context, SwitchPreference airplaneModeSwitchPreference) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080065
66 mContext = context;
Fabrice Di Megliodaef2e22014-10-15 19:00:35 -070067 mSwitchPref = airplaneModeSwitchPreference;
68
69 airplaneModeSwitchPreference.setPersistent(false);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080070
71 mPhoneStateReceiver = new PhoneStateIntentReceiver(mContext, mHandler);
72 mPhoneStateReceiver.notifyServiceState(EVENT_SERVICE_STATE_CHANGED);
73 }
74
75 public void resume() {
76
Jason Monkfc1b00c2015-01-28 10:35:53 -050077 mSwitchPref.setChecked(WirelessUtils.isAirplaneModeOn(mContext));
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080078
79 mPhoneStateReceiver.registerIntent();
Fabrice Di Meglioe3bced22014-08-06 10:22:52 -070080 mSwitchPref.setOnPreferenceChangeListener(this);
Amith Yamasanib98c00e2011-01-26 09:56:44 -080081 mContext.getContentResolver().registerContentObserver(
Christopher Tate6a5929b2012-09-10 15:39:05 -070082 Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true,
Amith Yamasanib98c00e2011-01-26 09:56:44 -080083 mAirplaneModeObserver);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080084 }
85
86 public void pause() {
87 mPhoneStateReceiver.unregisterIntent();
Fabrice Di Meglioe3bced22014-08-06 10:22:52 -070088 mSwitchPref.setOnPreferenceChangeListener(null);
Amith Yamasanib98c00e2011-01-26 09:56:44 -080089 mContext.getContentResolver().unregisterContentObserver(mAirplaneModeObserver);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080090 }
Amith Yamasanib98c00e2011-01-26 09:56:44 -080091
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080092 private void setAirplaneModeOn(boolean enabling) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080093 // Change the system setting
Christopher Tate6a5929b2012-09-10 15:39:05 -070094 Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON,
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080095 enabling ? 1 : 0);
Irfan Sheriffecea11f2010-08-02 19:11:16 -070096 // Update the UI to reflect system setting
Fabrice Di Meglioe3bced22014-08-06 10:22:52 -070097 mSwitchPref.setChecked(enabling);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080098
99 // Post the intent
100 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
101 intent.putExtra("state", enabling);
Christopher Tate6a5929b2012-09-10 15:39:05 -0700102 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800103 }
104
105 /**
106 * Called when we've received confirmation that the airplane mode was set.
Irfan Sheriffecea11f2010-08-02 19:11:16 -0700107 * TODO: We update the checkbox summary when we get notified
108 * that mobile radio is powered up/down. We should not have dependency
109 * on one radio alone. We need to do the following:
110 * - handle the case of wifi/bluetooth failures
111 * - mobile does not send failure notification, fail on timeout.
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800112 */
113 private void onAirplaneModeChanged() {
Jason Monkfc1b00c2015-01-28 10:35:53 -0500114 mSwitchPref.setChecked(WirelessUtils.isAirplaneModeOn(mContext));
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800115 }
116
117 /**
118 * Called when someone clicks on the checkbox preference.
119 */
120 public boolean onPreferenceChange(Preference preference, Object newValue) {
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500121 if (Boolean.parseBoolean(
122 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
123 // In ECM mode, do not update database at this point
124 } else {
Chris Wren1b6ffba2015-05-08 17:10:01 -0400125 Boolean value = (Boolean) newValue;
Chris Wren9d1bfd12016-01-26 18:04:01 -0500126 MetricsLogger.action(mContext, MetricsEvent.ACTION_AIRPLANE_TOGGLE, value);
Chris Wren1b6ffba2015-05-08 17:10:01 -0400127 setAirplaneModeOn(value);
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500128 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800129 return true;
130 }
131
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500132 public void setAirplaneModeInECM(boolean isECMExit, boolean isAirplaneModeOn) {
133 if (isECMExit) {
134 // update database based on the current checkbox state
135 setAirplaneModeOn(isAirplaneModeOn);
136 } else {
Irfan Sheriffecea11f2010-08-02 19:11:16 -0700137 // update summary
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500138 onAirplaneModeChanged();
139 }
140 }
141
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800142}