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; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 25 | import android.preference.CheckBoxPreference; |
| 26 | import android.preference.Preference; |
| 27 | import android.provider.Settings; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 28 | |
Gilles Debunne | dfc2637 | 2011-07-07 18:23:04 -0700 | [diff] [blame^] | 29 | import com.android.internal.telephony.PhoneStateIntentReceiver; |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame] | 30 | import com.android.internal.telephony.TelephonyProperties; |
| 31 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 32 | public 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 Yamasani | b98c00e | 2011-01-26 09:56:44 -0800 | [diff] [blame] | 53 | private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) { |
| 54 | @Override |
| 55 | public void onChange(boolean selfChange) { |
| 56 | onAirplaneModeChanged(); |
| 57 | } |
| 58 | }; |
| 59 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 60 | 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 Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 73 | mCheckBoxPref.setChecked(isAirplaneModeOn(mContext)); |
| 74 | |
| 75 | mPhoneStateReceiver.registerIntent(); |
| 76 | mCheckBoxPref.setOnPreferenceChangeListener(this); |
Amith Yamasani | b98c00e | 2011-01-26 09:56:44 -0800 | [diff] [blame] | 77 | mContext.getContentResolver().registerContentObserver( |
| 78 | Settings.System.getUriFor(Settings.System.AIRPLANE_MODE_ON), true, |
| 79 | mAirplaneModeObserver); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | public void pause() { |
| 83 | mPhoneStateReceiver.unregisterIntent(); |
| 84 | mCheckBoxPref.setOnPreferenceChangeListener(null); |
Amith Yamasani | b98c00e | 2011-01-26 09:56:44 -0800 | [diff] [blame] | 85 | mContext.getContentResolver().unregisterContentObserver(mAirplaneModeObserver); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 86 | } |
Amith Yamasani | b98c00e | 2011-01-26 09:56:44 -0800 | [diff] [blame] | 87 | |
Mike Lockwood | 83bcc98 | 2009-07-29 23:25:10 -0700 | [diff] [blame] | 88 | public static boolean isAirplaneModeOn(Context context) { |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 89 | 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 Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 94 | // Change the system setting |
| 95 | Settings.System.putInt(mContext.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, |
| 96 | enabling ? 1 : 0); |
Irfan Sheriff | ecea11f | 2010-08-02 19:11:16 -0700 | [diff] [blame] | 97 | // Update the UI to reflect system setting |
| 98 | mCheckBoxPref.setChecked(enabling); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 99 | |
| 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 Sheriff | ecea11f | 2010-08-02 19:11:16 -0700 | [diff] [blame] | 108 | * 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 Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 113 | */ |
| 114 | private void onAirplaneModeChanged() { |
Amith Yamasani | b98c00e | 2011-01-26 09:56:44 -0800 | [diff] [blame] | 115 | mCheckBoxPref.setChecked(isAirplaneModeOn(mContext)); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Called when someone clicks on the checkbox preference. |
| 120 | */ |
| 121 | public boolean onPreferenceChange(Preference preference, Object newValue) { |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame] | 122 | 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 Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 128 | return true; |
| 129 | } |
| 130 | |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame] | 131 | 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 Sheriff | ecea11f | 2010-08-02 19:11:16 -0700 | [diff] [blame] | 136 | // update summary |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame] | 137 | onAirplaneModeChanged(); |
| 138 | } |
| 139 | } |
| 140 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 141 | } |