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