The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.settings; |
| 18 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 19 | import android.content.Context; |
| 20 | import android.content.Intent; |
Amith Yamasani | b98c00e | 2011-01-26 09:56:44 -0800 | [diff] [blame] | 21 | import android.database.ContentObserver; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 22 | import android.os.Handler; |
| 23 | import android.os.Message; |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame] | 24 | import android.os.SystemProperties; |
Christopher Tate | 6a5929b | 2012-09-10 15:39:05 -0700 | [diff] [blame] | 25 | import android.os.UserHandle; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 26 | import android.preference.CheckBoxPreference; |
| 27 | import android.preference.Preference; |
Fabrice Di Meglio | e3bced2 | 2014-08-06 10:22:52 -0700 | [diff] [blame^] | 28 | import android.preference.SwitchPreference; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 29 | import android.provider.Settings; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 30 | |
Gilles Debunne | dfc2637 | 2011-07-07 18:23:04 -0700 | [diff] [blame] | 31 | import com.android.internal.telephony.PhoneStateIntentReceiver; |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame] | 32 | import com.android.internal.telephony.TelephonyProperties; |
| 33 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 34 | public class AirplaneModeEnabler implements Preference.OnPreferenceChangeListener { |
| 35 | |
| 36 | private final Context mContext; |
| 37 | |
| 38 | private PhoneStateIntentReceiver mPhoneStateReceiver; |
| 39 | |
Fabrice Di Meglio | e3bced2 | 2014-08-06 10:22:52 -0700 | [diff] [blame^] | 40 | private final SwitchPreference mSwitchPref; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 41 | |
| 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 Yamasani | b98c00e | 2011-01-26 09:56:44 -0800 | [diff] [blame] | 55 | private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) { |
| 56 | @Override |
| 57 | public void onChange(boolean selfChange) { |
| 58 | onAirplaneModeChanged(); |
| 59 | } |
| 60 | }; |
| 61 | |
Fabrice Di Meglio | e3bced2 | 2014-08-06 10:22:52 -0700 | [diff] [blame^] | 62 | public AirplaneModeEnabler(Context context, SwitchPreference airplaneModeCheckBoxPreference) { |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 63 | |
| 64 | mContext = context; |
Fabrice Di Meglio | e3bced2 | 2014-08-06 10:22:52 -0700 | [diff] [blame^] | 65 | mSwitchPref = airplaneModeCheckBoxPreference; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 66 | |
| 67 | airplaneModeCheckBoxPreference.setPersistent(false); |
| 68 | |
| 69 | mPhoneStateReceiver = new PhoneStateIntentReceiver(mContext, mHandler); |
| 70 | mPhoneStateReceiver.notifyServiceState(EVENT_SERVICE_STATE_CHANGED); |
| 71 | } |
| 72 | |
| 73 | public void resume() { |
| 74 | |
Fabrice Di Meglio | e3bced2 | 2014-08-06 10:22:52 -0700 | [diff] [blame^] | 75 | mSwitchPref.setChecked(isAirplaneModeOn(mContext)); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 76 | |
| 77 | mPhoneStateReceiver.registerIntent(); |
Fabrice Di Meglio | e3bced2 | 2014-08-06 10:22:52 -0700 | [diff] [blame^] | 78 | mSwitchPref.setOnPreferenceChangeListener(this); |
Amith Yamasani | b98c00e | 2011-01-26 09:56:44 -0800 | [diff] [blame] | 79 | mContext.getContentResolver().registerContentObserver( |
Christopher Tate | 6a5929b | 2012-09-10 15:39:05 -0700 | [diff] [blame] | 80 | Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true, |
Amith Yamasani | b98c00e | 2011-01-26 09:56:44 -0800 | [diff] [blame] | 81 | mAirplaneModeObserver); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | public void pause() { |
| 85 | mPhoneStateReceiver.unregisterIntent(); |
Fabrice Di Meglio | e3bced2 | 2014-08-06 10:22:52 -0700 | [diff] [blame^] | 86 | mSwitchPref.setOnPreferenceChangeListener(null); |
Amith Yamasani | b98c00e | 2011-01-26 09:56:44 -0800 | [diff] [blame] | 87 | mContext.getContentResolver().unregisterContentObserver(mAirplaneModeObserver); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 88 | } |
Amith Yamasani | b98c00e | 2011-01-26 09:56:44 -0800 | [diff] [blame] | 89 | |
Mike Lockwood | 83bcc98 | 2009-07-29 23:25:10 -0700 | [diff] [blame] | 90 | public static boolean isAirplaneModeOn(Context context) { |
Christopher Tate | 6a5929b | 2012-09-10 15:39:05 -0700 | [diff] [blame] | 91 | return Settings.Global.getInt(context.getContentResolver(), |
| 92 | Settings.Global.AIRPLANE_MODE_ON, 0) != 0; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | private void setAirplaneModeOn(boolean enabling) { |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 96 | // Change the system setting |
Christopher Tate | 6a5929b | 2012-09-10 15:39:05 -0700 | [diff] [blame] | 97 | Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 98 | enabling ? 1 : 0); |
Irfan Sheriff | ecea11f | 2010-08-02 19:11:16 -0700 | [diff] [blame] | 99 | // Update the UI to reflect system setting |
Fabrice Di Meglio | e3bced2 | 2014-08-06 10:22:52 -0700 | [diff] [blame^] | 100 | mSwitchPref.setChecked(enabling); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 101 | |
| 102 | // Post the intent |
| 103 | Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
| 104 | intent.putExtra("state", enabling); |
Christopher Tate | 6a5929b | 2012-09-10 15:39:05 -0700 | [diff] [blame] | 105 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Called when we've received confirmation that the airplane mode was set. |
Irfan Sheriff | ecea11f | 2010-08-02 19:11:16 -0700 | [diff] [blame] | 110 | * TODO: We update the checkbox summary when we get notified |
| 111 | * that mobile radio is powered up/down. We should not have dependency |
| 112 | * on one radio alone. We need to do the following: |
| 113 | * - handle the case of wifi/bluetooth failures |
| 114 | * - mobile does not send failure notification, fail on timeout. |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 115 | */ |
| 116 | private void onAirplaneModeChanged() { |
Fabrice Di Meglio | e3bced2 | 2014-08-06 10:22:52 -0700 | [diff] [blame^] | 117 | mSwitchPref.setChecked(isAirplaneModeOn(mContext)); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Called when someone clicks on the checkbox preference. |
| 122 | */ |
| 123 | public boolean onPreferenceChange(Preference preference, Object newValue) { |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame] | 124 | if (Boolean.parseBoolean( |
| 125 | SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) { |
| 126 | // In ECM mode, do not update database at this point |
| 127 | } else { |
| 128 | setAirplaneModeOn((Boolean) newValue); |
| 129 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 130 | return true; |
| 131 | } |
| 132 | |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame] | 133 | public void setAirplaneModeInECM(boolean isECMExit, boolean isAirplaneModeOn) { |
| 134 | if (isECMExit) { |
| 135 | // update database based on the current checkbox state |
| 136 | setAirplaneModeOn(isAirplaneModeOn); |
| 137 | } else { |
Irfan Sheriff | ecea11f | 2010-08-02 19:11:16 -0700 | [diff] [blame] | 138 | // update summary |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame] | 139 | onAirplaneModeChanged(); |
| 140 | } |
| 141 | } |
| 142 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 143 | } |