blob: 5f93589c92dbca17203890e7738ea21d29887a69 [file] [log] [blame]
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -08001/*
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
17package com.android.settings;
18
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080019import android.content.Context;
20import android.content.Intent;
Amith Yamasanib98c00e2011-01-26 09:56:44 -080021import android.database.ContentObserver;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080022import android.os.Handler;
23import android.os.Message;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050024import android.os.SystemProperties;
Christopher Tate6a5929b2012-09-10 15:39:05 -070025import android.os.UserHandle;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080026import android.provider.Settings;
Jason Monk39b46742015-09-10 15:52:51 -040027import android.support.v14.preference.SwitchPreference;
28import android.support.v7.preference.Preference;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080029
Tamas Berghammer265d3c22016-06-22 15:34:45 +010030import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Gilles Debunnedfc26372011-07-07 18:23:04 -070031import com.android.internal.telephony.PhoneStateIntentReceiver;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050032import com.android.internal.telephony.TelephonyProperties;
Jason Monkfc1b00c2015-01-28 10:35:53 -050033import com.android.settingslib.WirelessUtils;
Leif Hendrik Wilden28dee1f2018-01-11 10:15:36 -080034import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050035
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080036public class AirplaneModeEnabler implements Preference.OnPreferenceChangeListener {
37
Fan Zhangaa71afe2016-09-22 10:43:12 -070038 private static final int EVENT_SERVICE_STATE_CHANGED = 3;
39
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080040 private final Context mContext;
Fan Zhangaa71afe2016-09-22 10:43:12 -070041 private final SwitchPreference mSwitchPref;
42 private final MetricsFeatureProvider mMetricsFeatureProvider;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080043
44 private PhoneStateIntentReceiver mPhoneStateReceiver;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080045
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 Yamasanib98c00e2011-01-26 09:56:44 -080057 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
58 @Override
59 public void onChange(boolean selfChange) {
60 onAirplaneModeChanged();
61 }
62 };
63
Fan Zhangaa71afe2016-09-22 10:43:12 -070064 public AirplaneModeEnabler(Context context, SwitchPreference airplaneModeSwitchPreference,
65 MetricsFeatureProvider metricsFeatureProvider) {
66
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080067 mContext = context;
Fabrice Di Megliodaef2e22014-10-15 19:00:35 -070068 mSwitchPref = airplaneModeSwitchPreference;
Fan Zhangaa71afe2016-09-22 10:43:12 -070069 mMetricsFeatureProvider = metricsFeatureProvider;
Fabrice Di Megliodaef2e22014-10-15 19:00:35 -070070
71 airplaneModeSwitchPreference.setPersistent(false);
Fan Zhangaa71afe2016-09-22 10:43:12 -070072
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080073 mPhoneStateReceiver = new PhoneStateIntentReceiver(mContext, mHandler);
74 mPhoneStateReceiver.notifyServiceState(EVENT_SERVICE_STATE_CHANGED);
75 }
76
77 public void resume() {
Fan Zhangaa71afe2016-09-22 10:43:12 -070078
Jason Monkfc1b00c2015-01-28 10:35:53 -050079 mSwitchPref.setChecked(WirelessUtils.isAirplaneModeOn(mContext));
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080080
81 mPhoneStateReceiver.registerIntent();
Fabrice Di Meglioe3bced22014-08-06 10:22:52 -070082 mSwitchPref.setOnPreferenceChangeListener(this);
Amith Yamasanib98c00e2011-01-26 09:56:44 -080083 mContext.getContentResolver().registerContentObserver(
Christopher Tate6a5929b2012-09-10 15:39:05 -070084 Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true,
Amith Yamasanib98c00e2011-01-26 09:56:44 -080085 mAirplaneModeObserver);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080086 }
Fan Zhangaa71afe2016-09-22 10:43:12 -070087
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080088 public void pause() {
89 mPhoneStateReceiver.unregisterIntent();
Fabrice Di Meglioe3bced22014-08-06 10:22:52 -070090 mSwitchPref.setOnPreferenceChangeListener(null);
Amith Yamasanib98c00e2011-01-26 09:56:44 -080091 mContext.getContentResolver().unregisterContentObserver(mAirplaneModeObserver);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080092 }
Amith Yamasanib98c00e2011-01-26 09:56:44 -080093
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080094 private void setAirplaneModeOn(boolean enabling) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080095 // Change the system setting
Fan Zhangaa71afe2016-09-22 10:43:12 -070096 Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON,
97 enabling ? 1 : 0);
Irfan Sheriffecea11f2010-08-02 19:11:16 -070098 // Update the UI to reflect system setting
Fabrice Di Meglioe3bced22014-08-06 10:22:52 -070099 mSwitchPref.setChecked(enabling);
Fan Zhangaa71afe2016-09-22 10:43:12 -0700100
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800101 // Post the intent
102 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
103 intent.putExtra("state", enabling);
Christopher Tate6a5929b2012-09-10 15:39:05 -0700104 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800105 }
106
107 /**
108 * Called when we've received confirmation that the airplane mode was set.
Irfan Sheriffecea11f2010-08-02 19:11:16 -0700109 * 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 Projectafc4ab22009-03-03 19:32:34 -0800114 */
115 private void onAirplaneModeChanged() {
Jason Monkfc1b00c2015-01-28 10:35:53 -0500116 mSwitchPref.setChecked(WirelessUtils.isAirplaneModeOn(mContext));
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800117 }
Fan Zhangaa71afe2016-09-22 10:43:12 -0700118
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800119 /**
120 * Called when someone clicks on the checkbox preference.
121 */
122 public boolean onPreferenceChange(Preference preference, Object newValue) {
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500123 if (Boolean.parseBoolean(
Fan Zhangaa71afe2016-09-22 10:43:12 -0700124 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500125 // In ECM mode, do not update database at this point
126 } else {
Chris Wren1b6ffba2015-05-08 17:10:01 -0400127 Boolean value = (Boolean) newValue;
Fan Zhangaa71afe2016-09-22 10:43:12 -0700128 mMetricsFeatureProvider.action(mContext, MetricsEvent.ACTION_AIRPLANE_TOGGLE, value);
Chris Wren1b6ffba2015-05-08 17:10:01 -0400129 setAirplaneModeOn(value);
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500130 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800131 return true;
132 }
133
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500134 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 Sheriffecea11f2010-08-02 19:11:16 -0700139 // update summary
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500140 onAirplaneModeChanged();
141 }
142 }
143
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800144}