blob: 4ce51988763385a068ec6e900d9092703239daeb [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.preference.CheckBoxPreference;
27import android.preference.Preference;
Fabrice Di Meglioe3bced22014-08-06 10:22:52 -070028import android.preference.SwitchPreference;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080029import android.provider.Settings;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080030
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;
33
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080034public class AirplaneModeEnabler implements Preference.OnPreferenceChangeListener {
35
36 private final Context mContext;
37
38 private PhoneStateIntentReceiver mPhoneStateReceiver;
39
Fabrice Di Meglioe3bced22014-08-06 10:22:52 -070040 private final SwitchPreference mSwitchPref;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080041
42 private static final int EVENT_SERVICE_STATE_CHANGED = 3;
43
44 private Handler mHandler = new Handler() {
45 @Override
46 public void handleMessage(Message msg) {
47 switch (msg.what) {
48 case EVENT_SERVICE_STATE_CHANGED:
49 onAirplaneModeChanged();
50 break;
51 }
52 }
53 };
54
Amith Yamasanib98c00e2011-01-26 09:56:44 -080055 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
56 @Override
57 public void onChange(boolean selfChange) {
58 onAirplaneModeChanged();
59 }
60 };
61
Fabrice Di Meglioe3bced22014-08-06 10:22:52 -070062 public AirplaneModeEnabler(Context context, SwitchPreference airplaneModeCheckBoxPreference) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080063
64 mContext = context;
Fabrice Di Meglioe3bced22014-08-06 10:22:52 -070065 mSwitchPref = airplaneModeCheckBoxPreference;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080066
67 airplaneModeCheckBoxPreference.setPersistent(false);
68
69 mPhoneStateReceiver = new PhoneStateIntentReceiver(mContext, mHandler);
70 mPhoneStateReceiver.notifyServiceState(EVENT_SERVICE_STATE_CHANGED);
71 }
72
73 public void resume() {
74
Fabrice Di Meglioe3bced22014-08-06 10:22:52 -070075 mSwitchPref.setChecked(isAirplaneModeOn(mContext));
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080076
77 mPhoneStateReceiver.registerIntent();
Fabrice Di Meglioe3bced22014-08-06 10:22:52 -070078 mSwitchPref.setOnPreferenceChangeListener(this);
Amith Yamasanib98c00e2011-01-26 09:56:44 -080079 mContext.getContentResolver().registerContentObserver(
Christopher Tate6a5929b2012-09-10 15:39:05 -070080 Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true,
Amith Yamasanib98c00e2011-01-26 09:56:44 -080081 mAirplaneModeObserver);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080082 }
83
84 public void pause() {
85 mPhoneStateReceiver.unregisterIntent();
Fabrice Di Meglioe3bced22014-08-06 10:22:52 -070086 mSwitchPref.setOnPreferenceChangeListener(null);
Amith Yamasanib98c00e2011-01-26 09:56:44 -080087 mContext.getContentResolver().unregisterContentObserver(mAirplaneModeObserver);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080088 }
Amith Yamasanib98c00e2011-01-26 09:56:44 -080089
Mike Lockwood83bcc982009-07-29 23:25:10 -070090 public static boolean isAirplaneModeOn(Context context) {
Christopher Tate6a5929b2012-09-10 15:39:05 -070091 return Settings.Global.getInt(context.getContentResolver(),
92 Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080093 }
94
95 private void setAirplaneModeOn(boolean enabling) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080096 // Change the system setting
Christopher Tate6a5929b2012-09-10 15:39:05 -070097 Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON,
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080098 enabling ? 1 : 0);
Irfan Sheriffecea11f2010-08-02 19:11:16 -070099 // Update the UI to reflect system setting
Fabrice Di Meglioe3bced22014-08-06 10:22:52 -0700100 mSwitchPref.setChecked(enabling);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800101
102 // Post the intent
103 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
104 intent.putExtra("state", enabling);
Christopher Tate6a5929b2012-09-10 15:39:05 -0700105 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800106 }
107
108 /**
109 * Called when we've received confirmation that the airplane mode was set.
Irfan Sheriffecea11f2010-08-02 19:11:16 -0700110 * TODO: We update the checkbox summary when we get notified
111 * that mobile radio is powered up/down. We should not have dependency
112 * on one radio alone. We need to do the following:
113 * - handle the case of wifi/bluetooth failures
114 * - mobile does not send failure notification, fail on timeout.
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800115 */
116 private void onAirplaneModeChanged() {
Fabrice Di Meglioe3bced22014-08-06 10:22:52 -0700117 mSwitchPref.setChecked(isAirplaneModeOn(mContext));
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800118 }
119
120 /**
121 * Called when someone clicks on the checkbox preference.
122 */
123 public boolean onPreferenceChange(Preference preference, Object newValue) {
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500124 if (Boolean.parseBoolean(
125 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
126 // In ECM mode, do not update database at this point
127 } else {
128 setAirplaneModeOn((Boolean) newValue);
129 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800130 return true;
131 }
132
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500133 public void setAirplaneModeInECM(boolean isECMExit, boolean isAirplaneModeOn) {
134 if (isECMExit) {
135 // update database based on the current checkbox state
136 setAirplaneModeOn(isAirplaneModeOn);
137 } else {
Irfan Sheriffecea11f2010-08-02 19:11:16 -0700138 // update summary
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500139 onAirplaneModeChanged();
140 }
141 }
142
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800143}