2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.database.ContentObserver;
22 import android.os.Handler;
23 import android.os.Message;
24 import android.os.SystemProperties;
25 import android.preference.CheckBoxPreference;
26 import android.preference.Preference;
27 import android.provider.Settings;
29 import com.android.internal.telephony.PhoneStateIntentReceiver;
30 import com.android.internal.telephony.TelephonyProperties;
32 public class AirplaneModeEnabler implements Preference.OnPreferenceChangeListener {
34 private final Context mContext;
36 private PhoneStateIntentReceiver mPhoneStateReceiver;
38 private final CheckBoxPreference mCheckBoxPref;
40 private static final int EVENT_SERVICE_STATE_CHANGED = 3;
42 private Handler mHandler = new Handler() {
44 public void handleMessage(Message msg) {
46 case EVENT_SERVICE_STATE_CHANGED:
47 onAirplaneModeChanged();
53 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
55 public void onChange(boolean selfChange) {
56 onAirplaneModeChanged();
60 public AirplaneModeEnabler(Context context, CheckBoxPreference airplaneModeCheckBoxPreference) {
63 mCheckBoxPref = airplaneModeCheckBoxPreference;
65 airplaneModeCheckBoxPreference.setPersistent(false);
67 mPhoneStateReceiver = new PhoneStateIntentReceiver(mContext, mHandler);
68 mPhoneStateReceiver.notifyServiceState(EVENT_SERVICE_STATE_CHANGED);
71 public void resume() {
73 mCheckBoxPref.setChecked(isAirplaneModeOn(mContext));
75 mPhoneStateReceiver.registerIntent();
76 mCheckBoxPref.setOnPreferenceChangeListener(this);
77 mContext.getContentResolver().registerContentObserver(
78 Settings.System.getUriFor(Settings.System.AIRPLANE_MODE_ON), true,
79 mAirplaneModeObserver);
83 mPhoneStateReceiver.unregisterIntent();
84 mCheckBoxPref.setOnPreferenceChangeListener(null);
85 mContext.getContentResolver().unregisterContentObserver(mAirplaneModeObserver);
88 public static boolean isAirplaneModeOn(Context context) {
89 return Settings.System.getInt(context.getContentResolver(),
90 Settings.System.AIRPLANE_MODE_ON, 0) != 0;
93 private void setAirplaneModeOn(boolean enabling) {
94 // Change the system setting
95 Settings.System.putInt(mContext.getContentResolver(), Settings.System.AIRPLANE_MODE_ON,
97 // Update the UI to reflect system setting
98 mCheckBoxPref.setChecked(enabling);
101 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
102 intent.putExtra("state", enabling);
103 mContext.sendBroadcast(intent);
107 * Called when we've received confirmation that the airplane mode was set.
108 * TODO: We update the checkbox summary when we get notified
109 * that mobile radio is powered up/down. We should not have dependency
110 * on one radio alone. We need to do the following:
111 * - handle the case of wifi/bluetooth failures
112 * - mobile does not send failure notification, fail on timeout.
114 private void onAirplaneModeChanged() {
115 mCheckBoxPref.setChecked(isAirplaneModeOn(mContext));
119 * Called when someone clicks on the checkbox preference.
121 public boolean onPreferenceChange(Preference preference, Object newValue) {
122 if (Boolean.parseBoolean(
123 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
124 // In ECM mode, do not update database at this point
126 setAirplaneModeOn((Boolean) newValue);
131 public void setAirplaneModeInECM(boolean isECMExit, boolean isAirplaneModeOn) {
133 // update database based on the current checkbox state
134 setAirplaneModeOn(isAirplaneModeOn);
137 onAirplaneModeChanged();