blob: 5b8ad0d9e9d1e2b961e88c4112205997f31247db [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;
Christopher Tate6a5929b2012-09-10 15:39:05 -070025import android.os.UserHandle;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080026import android.preference.Preference;
Fabrice Di Meglioe3bced22014-08-06 10:22:52 -070027import android.preference.SwitchPreference;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080028import android.provider.Settings;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080029
Gilles Debunnedfc26372011-07-07 18:23:04 -070030import com.android.internal.telephony.PhoneStateIntentReceiver;
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
Fabrice Di Meglioe3bced22014-08-06 10:22:52 -070039 private final SwitchPreference mSwitchPref;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080040
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
Amith Yamasanib98c00e2011-01-26 09:56:44 -080054 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
55 @Override
56 public void onChange(boolean selfChange) {
57 onAirplaneModeChanged();
58 }
59 };
60
Fabrice Di Megliodaef2e22014-10-15 19:00:35 -070061 public AirplaneModeEnabler(Context context, SwitchPreference airplaneModeSwitchPreference) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080062
63 mContext = context;
Fabrice Di Megliodaef2e22014-10-15 19:00:35 -070064 mSwitchPref = airplaneModeSwitchPreference;
65
66 airplaneModeSwitchPreference.setPersistent(false);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080067
68 mPhoneStateReceiver = new PhoneStateIntentReceiver(mContext, mHandler);
69 mPhoneStateReceiver.notifyServiceState(EVENT_SERVICE_STATE_CHANGED);
70 }
71
72 public void resume() {
73
Fabrice Di Meglioe3bced22014-08-06 10:22:52 -070074 mSwitchPref.setChecked(isAirplaneModeOn(mContext));
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080075
76 mPhoneStateReceiver.registerIntent();
Fabrice Di Meglioe3bced22014-08-06 10:22:52 -070077 mSwitchPref.setOnPreferenceChangeListener(this);
Amith Yamasanib98c00e2011-01-26 09:56:44 -080078 mContext.getContentResolver().registerContentObserver(
Christopher Tate6a5929b2012-09-10 15:39:05 -070079 Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true,
Amith Yamasanib98c00e2011-01-26 09:56:44 -080080 mAirplaneModeObserver);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080081 }
82
83 public void pause() {
84 mPhoneStateReceiver.unregisterIntent();
Fabrice Di Meglioe3bced22014-08-06 10:22:52 -070085 mSwitchPref.setOnPreferenceChangeListener(null);
Amith Yamasanib98c00e2011-01-26 09:56:44 -080086 mContext.getContentResolver().unregisterContentObserver(mAirplaneModeObserver);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080087 }
Amith Yamasanib98c00e2011-01-26 09:56:44 -080088
Mike Lockwood83bcc982009-07-29 23:25:10 -070089 public static boolean isAirplaneModeOn(Context context) {
Christopher Tate6a5929b2012-09-10 15:39:05 -070090 return Settings.Global.getInt(context.getContentResolver(),
91 Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080092 }
93
94 private void setAirplaneModeOn(boolean enabling) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080095 // Change the system setting
Christopher Tate6a5929b2012-09-10 15:39:05 -070096 Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON,
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080097 enabling ? 1 : 0);
Irfan Sheriffecea11f2010-08-02 19:11:16 -070098 // Update the UI to reflect system setting
Fabrice Di Meglioe3bced22014-08-06 10:22:52 -070099 mSwitchPref.setChecked(enabling);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800100
101 // Post the intent
102 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
103 intent.putExtra("state", enabling);
Christopher Tate6a5929b2012-09-10 15:39:05 -0700104 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800105 }
106
107 /**
108 * Called when we've received confirmation that the airplane mode was set.
Irfan Sheriffecea11f2010-08-02 19:11:16 -0700109 * TODO: We update the checkbox summary when we get notified
110 * that mobile radio is powered up/down. We should not have dependency
111 * on one radio alone. We need to do the following:
112 * - handle the case of wifi/bluetooth failures
113 * - mobile does not send failure notification, fail on timeout.
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800114 */
115 private void onAirplaneModeChanged() {
Fabrice Di Meglioe3bced22014-08-06 10:22:52 -0700116 mSwitchPref.setChecked(isAirplaneModeOn(mContext));
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800117 }
118
119 /**
120 * Called when someone clicks on the checkbox preference.
121 */
122 public boolean onPreferenceChange(Preference preference, Object newValue) {
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500123 if (Boolean.parseBoolean(
124 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
125 // In ECM mode, do not update database at this point
126 } else {
127 setAirplaneModeOn((Boolean) newValue);
128 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800129 return true;
130 }
131
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500132 public void setAirplaneModeInECM(boolean isECMExit, boolean isAirplaneModeOn) {
133 if (isECMExit) {
134 // update database based on the current checkbox state
135 setAirplaneModeOn(isAirplaneModeOn);
136 } else {
Irfan Sheriffecea11f2010-08-02 19:11:16 -0700137 // update summary
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500138 onAirplaneModeChanged();
139 }
140 }
141
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800142}