blob: 06648dbfb7c2f8072d69355f3e442e4ccd9b4d49 [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;
Jason Monkfc1b00c2015-01-28 10:35:53 -050032import com.android.settingslib.WirelessUtils;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050033
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080034public class AirplaneModeEnabler implements Preference.OnPreferenceChangeListener {
35
36 private final Context mContext;
37
38 private PhoneStateIntentReceiver mPhoneStateReceiver;
39
Fabrice Di Meglioe3bced22014-08-06 10:22:52 -070040 private final SwitchPreference mSwitchPref;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080041
42 private static final int EVENT_SERVICE_STATE_CHANGED = 3;
43
44 private Handler mHandler = new Handler() {
45 @Override
46 public void handleMessage(Message msg) {
47 switch (msg.what) {
48 case EVENT_SERVICE_STATE_CHANGED:
49 onAirplaneModeChanged();
50 break;
51 }
52 }
53 };
54
Amith Yamasanib98c00e2011-01-26 09:56:44 -080055 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
56 @Override
57 public void onChange(boolean selfChange) {
58 onAirplaneModeChanged();
59 }
60 };
61
Fabrice Di Megliodaef2e22014-10-15 19:00:35 -070062 public AirplaneModeEnabler(Context context, SwitchPreference airplaneModeSwitchPreference) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080063
64 mContext = context;
Fabrice Di Megliodaef2e22014-10-15 19:00:35 -070065 mSwitchPref = airplaneModeSwitchPreference;
66
67 airplaneModeSwitchPreference.setPersistent(false);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080068
69 mPhoneStateReceiver = new PhoneStateIntentReceiver(mContext, mHandler);
70 mPhoneStateReceiver.notifyServiceState(EVENT_SERVICE_STATE_CHANGED);
71 }
72
73 public void resume() {
74
Jason Monkfc1b00c2015-01-28 10:35:53 -050075 mSwitchPref.setChecked(WirelessUtils.isAirplaneModeOn(mContext));
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080076
77 mPhoneStateReceiver.registerIntent();
Fabrice Di Meglioe3bced22014-08-06 10:22:52 -070078 mSwitchPref.setOnPreferenceChangeListener(this);
Amith Yamasanib98c00e2011-01-26 09:56:44 -080079 mContext.getContentResolver().registerContentObserver(
Christopher Tate6a5929b2012-09-10 15:39:05 -070080 Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true,
Amith Yamasanib98c00e2011-01-26 09:56:44 -080081 mAirplaneModeObserver);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080082 }
83
84 public void pause() {
85 mPhoneStateReceiver.unregisterIntent();
Fabrice Di Meglioe3bced22014-08-06 10:22:52 -070086 mSwitchPref.setOnPreferenceChangeListener(null);
Amith Yamasanib98c00e2011-01-26 09:56:44 -080087 mContext.getContentResolver().unregisterContentObserver(mAirplaneModeObserver);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080088 }
Amith Yamasanib98c00e2011-01-26 09:56:44 -080089
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080090 private void setAirplaneModeOn(boolean enabling) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080091 // Change the system setting
Christopher Tate6a5929b2012-09-10 15:39:05 -070092 Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON,
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080093 enabling ? 1 : 0);
Irfan Sheriffecea11f2010-08-02 19:11:16 -070094 // Update the UI to reflect system setting
Fabrice Di Meglioe3bced22014-08-06 10:22:52 -070095 mSwitchPref.setChecked(enabling);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080096
97 // Post the intent
98 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
99 intent.putExtra("state", enabling);
Christopher Tate6a5929b2012-09-10 15:39:05 -0700100 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800101 }
102
103 /**
104 * Called when we've received confirmation that the airplane mode was set.
Irfan Sheriffecea11f2010-08-02 19:11:16 -0700105 * TODO: We update the checkbox summary when we get notified
106 * that mobile radio is powered up/down. We should not have dependency
107 * on one radio alone. We need to do the following:
108 * - handle the case of wifi/bluetooth failures
109 * - mobile does not send failure notification, fail on timeout.
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800110 */
111 private void onAirplaneModeChanged() {
Jason Monkfc1b00c2015-01-28 10:35:53 -0500112 mSwitchPref.setChecked(WirelessUtils.isAirplaneModeOn(mContext));
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}