blob: ccfe541c511cb6bc30eff27a4bb85ee67020ad3a [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;
23import android.os.Handler;
24import android.os.Message;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050025import android.os.SystemProperties;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080026import android.preference.CheckBoxPreference;
27import android.preference.Preference;
28import android.provider.Settings;
29import android.telephony.ServiceState;
30
Chouting Zhang71cc49e2009-08-28 14:36:35 -050031import com.android.internal.telephony.TelephonyProperties;
32
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080033public class AirplaneModeEnabler implements Preference.OnPreferenceChangeListener {
34
35 private final Context mContext;
36
37 private PhoneStateIntentReceiver mPhoneStateReceiver;
38
39 private final CheckBoxPreference mCheckBoxPref;
40
41 private static final int EVENT_SERVICE_STATE_CHANGED = 3;
42
43 private Handler mHandler = new Handler() {
44 @Override
45 public void handleMessage(Message msg) {
46 switch (msg.what) {
47 case EVENT_SERVICE_STATE_CHANGED:
48 onAirplaneModeChanged();
49 break;
50 }
51 }
52 };
53
54 public AirplaneModeEnabler(Context context, CheckBoxPreference airplaneModeCheckBoxPreference) {
55
56 mContext = context;
57 mCheckBoxPref = airplaneModeCheckBoxPreference;
58
59 airplaneModeCheckBoxPreference.setPersistent(false);
60
61 mPhoneStateReceiver = new PhoneStateIntentReceiver(mContext, mHandler);
62 mPhoneStateReceiver.notifyServiceState(EVENT_SERVICE_STATE_CHANGED);
63 }
64
65 public void resume() {
66
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080067 mCheckBoxPref.setChecked(isAirplaneModeOn(mContext));
68
69 mPhoneStateReceiver.registerIntent();
70 mCheckBoxPref.setOnPreferenceChangeListener(this);
71 }
72
73 public void pause() {
74 mPhoneStateReceiver.unregisterIntent();
75 mCheckBoxPref.setOnPreferenceChangeListener(null);
76 }
77
Mike Lockwood83bcc982009-07-29 23:25:10 -070078 public static boolean isAirplaneModeOn(Context context) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080079 return Settings.System.getInt(context.getContentResolver(),
80 Settings.System.AIRPLANE_MODE_ON, 0) != 0;
81 }
82
83 private void setAirplaneModeOn(boolean enabling) {
84
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080085 mCheckBoxPref.setSummary(enabling ? R.string.airplane_mode_turning_on
86 : R.string.airplane_mode_turning_off);
87
88 // Change the system setting
89 Settings.System.putInt(mContext.getContentResolver(), Settings.System.AIRPLANE_MODE_ON,
90 enabling ? 1 : 0);
Irfan Sheriffecea11f2010-08-02 19:11:16 -070091 // Update the UI to reflect system setting
92 mCheckBoxPref.setChecked(enabling);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080093
94 // Post the intent
95 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
96 intent.putExtra("state", enabling);
97 mContext.sendBroadcast(intent);
98 }
99
100 /**
101 * Called when we've received confirmation that the airplane mode was set.
Irfan Sheriffecea11f2010-08-02 19:11:16 -0700102 * 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.
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800107 */
108 private void onAirplaneModeChanged() {
109 ServiceState serviceState = mPhoneStateReceiver.getServiceState();
110 boolean airplaneModeEnabled = serviceState.getState() == ServiceState.STATE_POWER_OFF;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800111 mCheckBoxPref.setSummary(airplaneModeEnabled ? null :
112 mContext.getString(R.string.airplane_mode_summary));
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800113 }
114
115 /**
116 * Called when someone clicks on the checkbox preference.
117 */
118 public boolean onPreferenceChange(Preference preference, Object newValue) {
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500119 if (Boolean.parseBoolean(
120 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
121 // In ECM mode, do not update database at this point
122 } else {
123 setAirplaneModeOn((Boolean) newValue);
124 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800125 return true;
126 }
127
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500128 public void setAirplaneModeInECM(boolean isECMExit, boolean isAirplaneModeOn) {
129 if (isECMExit) {
130 // update database based on the current checkbox state
131 setAirplaneModeOn(isAirplaneModeOn);
132 } else {
Irfan Sheriffecea11f2010-08-02 19:11:16 -0700133 // update summary
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500134 onAirplaneModeChanged();
135 }
136 }
137
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800138}