blob: 00c416fc9ed049b41bdbd46229be9ba8e05f8653 [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
19import com.android.internal.telephony.PhoneStateIntentReceiver;
20
21import android.content.Context;
22import android.content.Intent;
Amith Yamasanib98c00e2011-01-26 09:56:44 -080023import android.database.ContentObserver;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080024import android.os.Handler;
25import android.os.Message;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050026import android.os.SystemProperties;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080027import android.preference.CheckBoxPreference;
28import android.preference.Preference;
29import android.provider.Settings;
30import android.telephony.ServiceState;
31
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
40 private final CheckBoxPreference mCheckBoxPref;
41
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
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080062 public AirplaneModeEnabler(Context context, CheckBoxPreference airplaneModeCheckBoxPreference) {
63
64 mContext = context;
65 mCheckBoxPref = airplaneModeCheckBoxPreference;
66
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
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080075 mCheckBoxPref.setChecked(isAirplaneModeOn(mContext));
76
77 mPhoneStateReceiver.registerIntent();
78 mCheckBoxPref.setOnPreferenceChangeListener(this);
Amith Yamasanib98c00e2011-01-26 09:56:44 -080079 mContext.getContentResolver().registerContentObserver(
80 Settings.System.getUriFor(Settings.System.AIRPLANE_MODE_ON), true,
81 mAirplaneModeObserver);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080082 }
83
84 public void pause() {
85 mPhoneStateReceiver.unregisterIntent();
86 mCheckBoxPref.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) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080091 return Settings.System.getInt(context.getContentResolver(),
92 Settings.System.AIRPLANE_MODE_ON, 0) != 0;
93 }
94
95 private void setAirplaneModeOn(boolean enabling) {
96
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080097 mCheckBoxPref.setSummary(enabling ? R.string.airplane_mode_turning_on
98 : R.string.airplane_mode_turning_off);
Amith Yamasanib98c00e2011-01-26 09:56:44 -080099
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800100 // Change the system setting
101 Settings.System.putInt(mContext.getContentResolver(), Settings.System.AIRPLANE_MODE_ON,
102 enabling ? 1 : 0);
Irfan Sheriffecea11f2010-08-02 19:11:16 -0700103 // Update the UI to reflect system setting
104 mCheckBoxPref.setChecked(enabling);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800105
106 // Post the intent
107 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
108 intent.putExtra("state", enabling);
109 mContext.sendBroadcast(intent);
110 }
111
112 /**
113 * Called when we've received confirmation that the airplane mode was set.
Irfan Sheriffecea11f2010-08-02 19:11:16 -0700114 * TODO: We update the checkbox summary when we get notified
115 * that mobile radio is powered up/down. We should not have dependency
116 * on one radio alone. We need to do the following:
117 * - handle the case of wifi/bluetooth failures
118 * - mobile does not send failure notification, fail on timeout.
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800119 */
120 private void onAirplaneModeChanged() {
Amith Yamasanib98c00e2011-01-26 09:56:44 -0800121 boolean airplaneModeEnabled = isAirplaneModeOn(mContext);
122 mCheckBoxPref.setChecked(isAirplaneModeOn(mContext));
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800123 mCheckBoxPref.setSummary(airplaneModeEnabled ? null :
124 mContext.getString(R.string.airplane_mode_summary));
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800125 }
126
127 /**
128 * Called when someone clicks on the checkbox preference.
129 */
130 public boolean onPreferenceChange(Preference preference, Object newValue) {
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500131 if (Boolean.parseBoolean(
132 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
133 // In ECM mode, do not update database at this point
134 } else {
135 setAirplaneModeOn((Boolean) newValue);
136 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800137 return true;
138 }
139
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500140 public void setAirplaneModeInECM(boolean isECMExit, boolean isAirplaneModeOn) {
141 if (isECMExit) {
142 // update database based on the current checkbox state
143 setAirplaneModeOn(isAirplaneModeOn);
144 } else {
Irfan Sheriffecea11f2010-08-02 19:11:16 -0700145 // update summary
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500146 onAirplaneModeChanged();
147 }
148 }
149
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800150}