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 | |
| 19 | import com.android.internal.telephony.PhoneStateIntentReceiver; |
| 20 | |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.os.Handler; |
| 24 | import android.os.Message; |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame^] | 25 | import android.os.SystemProperties; |
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; |
| 28 | import android.provider.Settings; |
| 29 | import android.telephony.ServiceState; |
| 30 | |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame^] | 31 | import com.android.internal.telephony.TelephonyProperties; |
| 32 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 33 | public class AirplaneModeEnabler implements Preference.OnPreferenceChangeListener { |
| 34 | |
| 35 | private final Context mContext; |
| 36 | |
| 37 | private PhoneStateIntentReceiver mPhoneStateReceiver; |
| 38 | |
| 39 | private final CheckBoxPreference mCheckBoxPref; |
| 40 | |
| 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 | |
| 54 | public AirplaneModeEnabler(Context context, CheckBoxPreference airplaneModeCheckBoxPreference) { |
| 55 | |
| 56 | mContext = context; |
| 57 | mCheckBoxPref = airplaneModeCheckBoxPreference; |
| 58 | |
| 59 | airplaneModeCheckBoxPreference.setPersistent(false); |
| 60 | |
| 61 | mPhoneStateReceiver = new PhoneStateIntentReceiver(mContext, mHandler); |
| 62 | mPhoneStateReceiver.notifyServiceState(EVENT_SERVICE_STATE_CHANGED); |
| 63 | } |
| 64 | |
| 65 | public void resume() { |
| 66 | |
| 67 | // This is the widget enabled state, not the preference toggled state |
| 68 | mCheckBoxPref.setEnabled(true); |
| 69 | mCheckBoxPref.setChecked(isAirplaneModeOn(mContext)); |
| 70 | |
| 71 | mPhoneStateReceiver.registerIntent(); |
| 72 | mCheckBoxPref.setOnPreferenceChangeListener(this); |
| 73 | } |
| 74 | |
| 75 | public void pause() { |
| 76 | mPhoneStateReceiver.unregisterIntent(); |
| 77 | mCheckBoxPref.setOnPreferenceChangeListener(null); |
| 78 | } |
| 79 | |
Mike Lockwood | 83bcc98 | 2009-07-29 23:25:10 -0700 | [diff] [blame] | 80 | public static boolean isAirplaneModeOn(Context context) { |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 81 | return Settings.System.getInt(context.getContentResolver(), |
| 82 | Settings.System.AIRPLANE_MODE_ON, 0) != 0; |
| 83 | } |
| 84 | |
| 85 | private void setAirplaneModeOn(boolean enabling) { |
| 86 | |
| 87 | mCheckBoxPref.setEnabled(false); |
| 88 | mCheckBoxPref.setSummary(enabling ? R.string.airplane_mode_turning_on |
| 89 | : R.string.airplane_mode_turning_off); |
| 90 | |
| 91 | // Change the system setting |
| 92 | Settings.System.putInt(mContext.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, |
| 93 | enabling ? 1 : 0); |
| 94 | |
| 95 | // Post the intent |
| 96 | Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
| 97 | intent.putExtra("state", enabling); |
| 98 | mContext.sendBroadcast(intent); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Called when we've received confirmation that the airplane mode was set. |
| 103 | */ |
| 104 | private void onAirplaneModeChanged() { |
| 105 | ServiceState serviceState = mPhoneStateReceiver.getServiceState(); |
| 106 | boolean airplaneModeEnabled = serviceState.getState() == ServiceState.STATE_POWER_OFF; |
| 107 | mCheckBoxPref.setChecked(airplaneModeEnabled); |
| 108 | mCheckBoxPref.setSummary(airplaneModeEnabled ? null : |
| 109 | mContext.getString(R.string.airplane_mode_summary)); |
| 110 | mCheckBoxPref.setEnabled(true); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Called when someone clicks on the checkbox preference. |
| 115 | */ |
| 116 | public boolean onPreferenceChange(Preference preference, Object newValue) { |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame^] | 117 | if (Boolean.parseBoolean( |
| 118 | SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) { |
| 119 | // In ECM mode, do not update database at this point |
| 120 | } else { |
| 121 | setAirplaneModeOn((Boolean) newValue); |
| 122 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 123 | return true; |
| 124 | } |
| 125 | |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame^] | 126 | public void setAirplaneModeInECM(boolean isECMExit, boolean isAirplaneModeOn) { |
| 127 | if (isECMExit) { |
| 128 | // update database based on the current checkbox state |
| 129 | setAirplaneModeOn(isAirplaneModeOn); |
| 130 | } else { |
| 131 | // update checkbox state based on database value |
| 132 | onAirplaneModeChanged(); |
| 133 | } |
| 134 | } |
| 135 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 136 | } |