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 com.android.internal.telephony.PhoneStateIntentReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.os.Handler;
24 import android.os.Message;
25 import android.os.SystemProperties;
26 import android.preference.CheckBoxPreference;
27 import android.preference.Preference;
28 import android.provider.Settings;
29 import android.telephony.ServiceState;
31 import com.android.internal.telephony.TelephonyProperties;
33 public class AirplaneModeEnabler implements Preference.OnPreferenceChangeListener {
35 private final Context mContext;
37 private PhoneStateIntentReceiver mPhoneStateReceiver;
39 private final CheckBoxPreference mCheckBoxPref;
41 private static final int EVENT_SERVICE_STATE_CHANGED = 3;
43 private Handler mHandler = new Handler() {
45 public void handleMessage(Message msg) {
47 case EVENT_SERVICE_STATE_CHANGED:
48 onAirplaneModeChanged();
54 public AirplaneModeEnabler(Context context, CheckBoxPreference airplaneModeCheckBoxPreference) {
57 mCheckBoxPref = airplaneModeCheckBoxPreference;
59 airplaneModeCheckBoxPreference.setPersistent(false);
61 mPhoneStateReceiver = new PhoneStateIntentReceiver(mContext, mHandler);
62 mPhoneStateReceiver.notifyServiceState(EVENT_SERVICE_STATE_CHANGED);
65 public void resume() {
67 mCheckBoxPref.setChecked(isAirplaneModeOn(mContext));
69 mPhoneStateReceiver.registerIntent();
70 mCheckBoxPref.setOnPreferenceChangeListener(this);
74 mPhoneStateReceiver.unregisterIntent();
75 mCheckBoxPref.setOnPreferenceChangeListener(null);
78 public static boolean isAirplaneModeOn(Context context) {
79 return Settings.System.getInt(context.getContentResolver(),
80 Settings.System.AIRPLANE_MODE_ON, 0) != 0;
83 private void setAirplaneModeOn(boolean enabling) {
85 mCheckBoxPref.setSummary(enabling ? R.string.airplane_mode_turning_on
86 : R.string.airplane_mode_turning_off);
88 // Change the system setting
89 Settings.System.putInt(mContext.getContentResolver(), Settings.System.AIRPLANE_MODE_ON,
91 // Update the UI to reflect system setting
92 mCheckBoxPref.setChecked(enabling);
95 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
96 intent.putExtra("state", enabling);
97 mContext.sendBroadcast(intent);
101 * Called when we've received confirmation that the airplane mode was set.
102 * TODO: We update the checkbox summary when we get notified
103 * that mobile radio is powered up/down. We should not have dependency
104 * on one radio alone. We need to do the following:
105 * - handle the case of wifi/bluetooth failures
106 * - mobile does not send failure notification, fail on timeout.
108 private void onAirplaneModeChanged() {
109 ServiceState serviceState = mPhoneStateReceiver.getServiceState();
110 boolean airplaneModeEnabled = serviceState.getState() == ServiceState.STATE_POWER_OFF;
111 mCheckBoxPref.setSummary(airplaneModeEnabled ? null :
112 mContext.getString(R.string.airplane_mode_summary));
116 * Called when someone clicks on the checkbox preference.
118 public boolean onPreferenceChange(Preference preference, Object newValue) {
119 if (Boolean.parseBoolean(
120 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
121 // In ECM mode, do not update database at this point
123 setAirplaneModeOn((Boolean) newValue);
128 public void setAirplaneModeInECM(boolean isECMExit, boolean isAirplaneModeOn) {
130 // update database based on the current checkbox state
131 setAirplaneModeOn(isAirplaneModeOn);
134 onAirplaneModeChanged();