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; |
Jason Monk | fcd5f0f | 2018-04-20 14:08:57 -0400 | [diff] [blame^] | 23 | import android.os.Looper; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 24 | import android.os.Message; |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame] | 25 | import android.os.SystemProperties; |
Christopher Tate | 6a5929b | 2012-09-10 15:39:05 -0700 | [diff] [blame] | 26 | import android.os.UserHandle; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 27 | import android.provider.Settings; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 28 | |
Tamas Berghammer | 265d3c2 | 2016-06-22 15:34:45 +0100 | [diff] [blame] | 29 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Gilles Debunne | dfc2637 | 2011-07-07 18:23:04 -0700 | [diff] [blame] | 30 | import com.android.internal.telephony.PhoneStateIntentReceiver; |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame] | 31 | import com.android.internal.telephony.TelephonyProperties; |
Jason Monk | fc1b00c | 2015-01-28 10:35:53 -0500 | [diff] [blame] | 32 | import com.android.settingslib.WirelessUtils; |
Leif Hendrik Wilden | 28dee1f | 2018-01-11 10:15:36 -0800 | [diff] [blame] | 33 | import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame] | 34 | |
CY Cheng | fee4807 | 2018-03-26 18:38:59 +0800 | [diff] [blame] | 35 | public class AirplaneModeEnabler { |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 36 | |
Fan Zhang | aa71afe | 2016-09-22 10:43:12 -0700 | [diff] [blame] | 37 | private static final int EVENT_SERVICE_STATE_CHANGED = 3; |
| 38 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 39 | private final Context mContext; |
Fan Zhang | aa71afe | 2016-09-22 10:43:12 -0700 | [diff] [blame] | 40 | private final MetricsFeatureProvider mMetricsFeatureProvider; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 41 | |
| 42 | private PhoneStateIntentReceiver mPhoneStateReceiver; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 43 | |
CY Cheng | fee4807 | 2018-03-26 18:38:59 +0800 | [diff] [blame] | 44 | private OnAirplaneModeChangedListener mOnAirplaneModeChangedListener; |
| 45 | |
| 46 | public interface OnAirplaneModeChangedListener { |
| 47 | /** |
| 48 | * Called when airplane mode status is changed. |
| 49 | * |
| 50 | * @param isAirplaneModeOn the airplane mode is on |
| 51 | */ |
| 52 | void onAirplaneModeChanged(boolean isAirplaneModeOn); |
| 53 | } |
| 54 | |
Jason Monk | fcd5f0f | 2018-04-20 14:08:57 -0400 | [diff] [blame^] | 55 | private Handler mHandler = new Handler(Looper.getMainLooper()) { |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 56 | @Override |
| 57 | public void handleMessage(Message msg) { |
| 58 | switch (msg.what) { |
| 59 | case EVENT_SERVICE_STATE_CHANGED: |
| 60 | onAirplaneModeChanged(); |
| 61 | break; |
| 62 | } |
| 63 | } |
| 64 | }; |
| 65 | |
Jason Monk | fcd5f0f | 2018-04-20 14:08:57 -0400 | [diff] [blame^] | 66 | private ContentObserver mAirplaneModeObserver = new ContentObserver( |
| 67 | new Handler(Looper.getMainLooper())) { |
Amith Yamasani | b98c00e | 2011-01-26 09:56:44 -0800 | [diff] [blame] | 68 | @Override |
| 69 | public void onChange(boolean selfChange) { |
| 70 | onAirplaneModeChanged(); |
| 71 | } |
| 72 | }; |
| 73 | |
CY Cheng | fee4807 | 2018-03-26 18:38:59 +0800 | [diff] [blame] | 74 | public AirplaneModeEnabler(Context context, MetricsFeatureProvider metricsFeatureProvider, |
| 75 | OnAirplaneModeChangedListener listener) { |
Fan Zhang | aa71afe | 2016-09-22 10:43:12 -0700 | [diff] [blame] | 76 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 77 | mContext = context; |
Fan Zhang | aa71afe | 2016-09-22 10:43:12 -0700 | [diff] [blame] | 78 | mMetricsFeatureProvider = metricsFeatureProvider; |
CY Cheng | fee4807 | 2018-03-26 18:38:59 +0800 | [diff] [blame] | 79 | mOnAirplaneModeChangedListener = listener; |
Fan Zhang | aa71afe | 2016-09-22 10:43:12 -0700 | [diff] [blame] | 80 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 81 | mPhoneStateReceiver = new PhoneStateIntentReceiver(mContext, mHandler); |
| 82 | mPhoneStateReceiver.notifyServiceState(EVENT_SERVICE_STATE_CHANGED); |
| 83 | } |
| 84 | |
| 85 | public void resume() { |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 86 | mPhoneStateReceiver.registerIntent(); |
Amith Yamasani | b98c00e | 2011-01-26 09:56:44 -0800 | [diff] [blame] | 87 | mContext.getContentResolver().registerContentObserver( |
Christopher Tate | 6a5929b | 2012-09-10 15:39:05 -0700 | [diff] [blame] | 88 | Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true, |
Amith Yamasani | b98c00e | 2011-01-26 09:56:44 -0800 | [diff] [blame] | 89 | mAirplaneModeObserver); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 90 | } |
Fan Zhang | aa71afe | 2016-09-22 10:43:12 -0700 | [diff] [blame] | 91 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 92 | public void pause() { |
| 93 | mPhoneStateReceiver.unregisterIntent(); |
Amith Yamasani | b98c00e | 2011-01-26 09:56:44 -0800 | [diff] [blame] | 94 | mContext.getContentResolver().unregisterContentObserver(mAirplaneModeObserver); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 95 | } |
Amith Yamasani | b98c00e | 2011-01-26 09:56:44 -0800 | [diff] [blame] | 96 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 97 | private void setAirplaneModeOn(boolean enabling) { |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 98 | // Change the system setting |
Fan Zhang | aa71afe | 2016-09-22 10:43:12 -0700 | [diff] [blame] | 99 | Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, |
| 100 | enabling ? 1 : 0); |
CY Cheng | fee4807 | 2018-03-26 18:38:59 +0800 | [diff] [blame] | 101 | |
| 102 | // Notify listener the system setting is changed. |
| 103 | if (mOnAirplaneModeChangedListener != null) { |
| 104 | mOnAirplaneModeChangedListener.onAirplaneModeChanged(enabling); |
| 105 | } |
Fan Zhang | aa71afe | 2016-09-22 10:43:12 -0700 | [diff] [blame] | 106 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 107 | // Post the intent |
| 108 | Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
| 109 | intent.putExtra("state", enabling); |
Christopher Tate | 6a5929b | 2012-09-10 15:39:05 -0700 | [diff] [blame] | 110 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Called when we've received confirmation that the airplane mode was set. |
Irfan Sheriff | ecea11f | 2010-08-02 19:11:16 -0700 | [diff] [blame] | 115 | * TODO: We update the checkbox summary when we get notified |
| 116 | * that mobile radio is powered up/down. We should not have dependency |
| 117 | * on one radio alone. We need to do the following: |
| 118 | * - handle the case of wifi/bluetooth failures |
| 119 | * - mobile does not send failure notification, fail on timeout. |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 120 | */ |
| 121 | private void onAirplaneModeChanged() { |
CY Cheng | fee4807 | 2018-03-26 18:38:59 +0800 | [diff] [blame] | 122 | if (mOnAirplaneModeChangedListener != null) { |
| 123 | mOnAirplaneModeChangedListener.onAirplaneModeChanged(isAirplaneModeOn()); |
| 124 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 125 | } |
Fan Zhang | aa71afe | 2016-09-22 10:43:12 -0700 | [diff] [blame] | 126 | |
CY Cheng | fee4807 | 2018-03-26 18:38:59 +0800 | [diff] [blame] | 127 | public void setAirplaneMode(boolean isAirplaneModeOn) { |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame] | 128 | if (Boolean.parseBoolean( |
Fan Zhang | aa71afe | 2016-09-22 10:43:12 -0700 | [diff] [blame] | 129 | SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) { |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame] | 130 | // In ECM mode, do not update database at this point |
| 131 | } else { |
CY Cheng | fee4807 | 2018-03-26 18:38:59 +0800 | [diff] [blame] | 132 | mMetricsFeatureProvider.action(mContext, MetricsEvent.ACTION_AIRPLANE_TOGGLE, |
| 133 | isAirplaneModeOn); |
| 134 | setAirplaneModeOn(isAirplaneModeOn); |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame] | 135 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame] | 138 | public void setAirplaneModeInECM(boolean isECMExit, boolean isAirplaneModeOn) { |
| 139 | if (isECMExit) { |
| 140 | // update database based on the current checkbox state |
| 141 | setAirplaneModeOn(isAirplaneModeOn); |
| 142 | } else { |
Irfan Sheriff | ecea11f | 2010-08-02 19:11:16 -0700 | [diff] [blame] | 143 | // update summary |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame] | 144 | onAirplaneModeChanged(); |
| 145 | } |
| 146 | } |
| 147 | |
CY Cheng | fee4807 | 2018-03-26 18:38:59 +0800 | [diff] [blame] | 148 | public boolean isAirplaneModeOn() { |
| 149 | return WirelessUtils.isAirplaneModeOn(mContext); |
| 150 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 151 | } |