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.os.UserHandle;
26 import android.preference.CheckBoxPreference;
27 import android.preference.Preference;
28 import android.preference.SwitchPreference;
29 import android.provider.Settings;
31 import com.android.internal.telephony.PhoneStateIntentReceiver;
32 import com.android.internal.telephony.TelephonyProperties;
34 public class AirplaneModeEnabler implements Preference.OnPreferenceChangeListener {
36 private final Context mContext;
38 private PhoneStateIntentReceiver mPhoneStateReceiver;
40 private final SwitchPreference mSwitchPref;
42 private static final int EVENT_SERVICE_STATE_CHANGED = 3;
44 private Handler mHandler = new Handler() {
46 public void handleMessage(Message msg) {
48 case EVENT_SERVICE_STATE_CHANGED:
49 onAirplaneModeChanged();
55 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
57 public void onChange(boolean selfChange) {
58 onAirplaneModeChanged();
62 public AirplaneModeEnabler(Context context, SwitchPreference airplaneModeCheckBoxPreference) {
65 mSwitchPref = airplaneModeCheckBoxPreference;
67 airplaneModeCheckBoxPreference.setPersistent(false);
69 mPhoneStateReceiver = new PhoneStateIntentReceiver(mContext, mHandler);
70 mPhoneStateReceiver.notifyServiceState(EVENT_SERVICE_STATE_CHANGED);
73 public void resume() {
75 mSwitchPref.setChecked(isAirplaneModeOn(mContext));
77 mPhoneStateReceiver.registerIntent();
78 mSwitchPref.setOnPreferenceChangeListener(this);
79 mContext.getContentResolver().registerContentObserver(
80 Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true,
81 mAirplaneModeObserver);
85 mPhoneStateReceiver.unregisterIntent();
86 mSwitchPref.setOnPreferenceChangeListener(null);
87 mContext.getContentResolver().unregisterContentObserver(mAirplaneModeObserver);
90 public static boolean isAirplaneModeOn(Context context) {
91 return Settings.Global.getInt(context.getContentResolver(),
92 Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
95 private void setAirplaneModeOn(boolean enabling) {
96 // Change the system setting
97 Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON,
99 // Update the UI to reflect system setting
100 mSwitchPref.setChecked(enabling);
103 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
104 intent.putExtra("state", enabling);
105 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
109 * Called when we've received confirmation that the airplane mode was set.
110 * 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.
116 private void onAirplaneModeChanged() {
117 mSwitchPref.setChecked(isAirplaneModeOn(mContext));
121 * Called when someone clicks on the checkbox preference.
123 public boolean onPreferenceChange(Preference preference, Object newValue) {
124 if (Boolean.parseBoolean(
125 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
126 // In ECM mode, do not update database at this point
128 setAirplaneModeOn((Boolean) newValue);
133 public void setAirplaneModeInECM(boolean isECMExit, boolean isAirplaneModeOn) {
135 // update database based on the current checkbox state
136 setAirplaneModeOn(isAirplaneModeOn);
139 onAirplaneModeChanged();