blob: 94ba5a1d5c65300f0d6da48804fe275aeb374f52 [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
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080019import android.content.Context;
20import android.content.Intent;
Amith Yamasanib98c00e2011-01-26 09:56:44 -080021import android.database.ContentObserver;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080022import android.os.Handler;
23import android.os.Message;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050024import android.os.SystemProperties;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080025import android.preference.CheckBoxPreference;
26import android.preference.Preference;
27import android.provider.Settings;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080028
Gilles Debunnedfc26372011-07-07 18:23:04 -070029import com.android.internal.telephony.PhoneStateIntentReceiver;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050030import com.android.internal.telephony.TelephonyProperties;
31
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080032public class AirplaneModeEnabler implements Preference.OnPreferenceChangeListener {
33
34 private final Context mContext;
35
36 private PhoneStateIntentReceiver mPhoneStateReceiver;
37
38 private final CheckBoxPreference mCheckBoxPref;
39
40 private static final int EVENT_SERVICE_STATE_CHANGED = 3;
41
42 private Handler mHandler = new Handler() {
43 @Override
44 public void handleMessage(Message msg) {
45 switch (msg.what) {
46 case EVENT_SERVICE_STATE_CHANGED:
47 onAirplaneModeChanged();
48 break;
49 }
50 }
51 };
52
Amith Yamasanib98c00e2011-01-26 09:56:44 -080053 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
54 @Override
55 public void onChange(boolean selfChange) {
56 onAirplaneModeChanged();
57 }
58 };
59
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080060 public AirplaneModeEnabler(Context context, CheckBoxPreference airplaneModeCheckBoxPreference) {
61
62 mContext = context;
63 mCheckBoxPref = airplaneModeCheckBoxPreference;
64
65 airplaneModeCheckBoxPreference.setPersistent(false);
66
67 mPhoneStateReceiver = new PhoneStateIntentReceiver(mContext, mHandler);
68 mPhoneStateReceiver.notifyServiceState(EVENT_SERVICE_STATE_CHANGED);
69 }
70
71 public void resume() {
72
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080073 mCheckBoxPref.setChecked(isAirplaneModeOn(mContext));
74
75 mPhoneStateReceiver.registerIntent();
76 mCheckBoxPref.setOnPreferenceChangeListener(this);
Amith Yamasanib98c00e2011-01-26 09:56:44 -080077 mContext.getContentResolver().registerContentObserver(
78 Settings.System.getUriFor(Settings.System.AIRPLANE_MODE_ON), true,
79 mAirplaneModeObserver);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080080 }
81
82 public void pause() {
83 mPhoneStateReceiver.unregisterIntent();
84 mCheckBoxPref.setOnPreferenceChangeListener(null);
Amith Yamasanib98c00e2011-01-26 09:56:44 -080085 mContext.getContentResolver().unregisterContentObserver(mAirplaneModeObserver);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080086 }
Amith Yamasanib98c00e2011-01-26 09:56:44 -080087
Mike Lockwood83bcc982009-07-29 23:25:10 -070088 public static boolean isAirplaneModeOn(Context context) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080089 return Settings.System.getInt(context.getContentResolver(),
90 Settings.System.AIRPLANE_MODE_ON, 0) != 0;
91 }
92
93 private void setAirplaneModeOn(boolean enabling) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080094 // Change the system setting
95 Settings.System.putInt(mContext.getContentResolver(), Settings.System.AIRPLANE_MODE_ON,
96 enabling ? 1 : 0);
Irfan Sheriffecea11f2010-08-02 19:11:16 -070097 // Update the UI to reflect system setting
98 mCheckBoxPref.setChecked(enabling);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080099
100 // Post the intent
101 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
102 intent.putExtra("state", enabling);
103 mContext.sendBroadcast(intent);
104 }
105
106 /**
107 * Called when we've received confirmation that the airplane mode was set.
Irfan Sheriffecea11f2010-08-02 19:11:16 -0700108 * 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.
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800113 */
114 private void onAirplaneModeChanged() {
Amith Yamasanib98c00e2011-01-26 09:56:44 -0800115 mCheckBoxPref.setChecked(isAirplaneModeOn(mContext));
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800116 }
117
118 /**
119 * Called when someone clicks on the checkbox preference.
120 */
121 public boolean onPreferenceChange(Preference preference, Object newValue) {
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500122 if (Boolean.parseBoolean(
123 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
124 // In ECM mode, do not update database at this point
125 } else {
126 setAirplaneModeOn((Boolean) newValue);
127 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800128 return true;
129 }
130
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500131 public void setAirplaneModeInECM(boolean isECMExit, boolean isAirplaneModeOn) {
132 if (isECMExit) {
133 // update database based on the current checkbox state
134 setAirplaneModeOn(isAirplaneModeOn);
135 } else {
Irfan Sheriffecea11f2010-08-02 19:11:16 -0700136 // update summary
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500137 onAirplaneModeChanged();
138 }
139 }
140
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800141}