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