John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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.systemui.qs.tiles; |
| 18 | |
| 19 | import android.content.BroadcastReceiver; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.content.IntentFilter; |
John Spurlock | 6627ff6 | 2014-05-23 12:59:27 -0400 | [diff] [blame] | 23 | import android.net.ConnectivityManager; |
Amin Shaikh | df13e92 | 2018-04-12 13:29:32 -0400 | [diff] [blame] | 24 | import android.os.SystemProperties; |
yuemingw | cb5daff | 2018-01-09 11:24:44 +0000 | [diff] [blame] | 25 | import android.os.UserManager; |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 26 | import android.provider.Settings; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 27 | import android.provider.Settings.Global; |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 28 | import android.service.quicksettings.Tile; |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 29 | import android.widget.Switch; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 30 | |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 31 | import com.android.internal.logging.MetricsLogger; |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 32 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Amin Shaikh | df13e92 | 2018-04-12 13:29:32 -0400 | [diff] [blame] | 33 | import com.android.internal.telephony.TelephonyIntents; |
| 34 | import com.android.internal.telephony.TelephonyProperties; |
| 35 | import com.android.systemui.Dependency; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 36 | import com.android.systemui.R; |
Amin Shaikh | df13e92 | 2018-04-12 13:29:32 -0400 | [diff] [blame] | 37 | import com.android.systemui.plugins.ActivityStarter; |
Jason Monk | 395617f | 2017-05-05 14:07:58 -0400 | [diff] [blame] | 38 | import com.android.systemui.plugins.qs.QSTile.BooleanState; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 39 | import com.android.systemui.qs.GlobalSetting; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 40 | import com.android.systemui.qs.QSHost; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 41 | import com.android.systemui.qs.tileimpl.QSTileImpl; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 42 | |
| 43 | /** Quick settings tile: Airplane mode **/ |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 44 | public class AirplaneModeTile extends QSTileImpl<BooleanState> { |
Jason Monk | 395617f | 2017-05-05 14:07:58 -0400 | [diff] [blame] | 45 | private final Icon mIcon = |
Evan Laird | bcdd046 | 2017-05-15 12:51:34 -0400 | [diff] [blame] | 46 | ResourceIcon.get(R.drawable.ic_signal_airplane); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 47 | private final GlobalSetting mSetting; |
| 48 | |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 49 | private boolean mListening; |
| 50 | |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 51 | public AirplaneModeTile(QSHost host) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 52 | super(host); |
| 53 | |
| 54 | mSetting = new GlobalSetting(mContext, mHandler, Global.AIRPLANE_MODE_ON) { |
| 55 | @Override |
| 56 | protected void handleValueChanged(int value) { |
| 57 | handleRefreshState(value); |
| 58 | } |
| 59 | }; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Override |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 63 | public BooleanState newTileState() { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 64 | return new BooleanState(); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public void handleClick() { |
Amin Shaikh | df13e92 | 2018-04-12 13:29:32 -0400 | [diff] [blame] | 69 | boolean airplaneModeEnabled = mState.value; |
| 70 | MetricsLogger.action(mContext, getMetricsCategory(), !airplaneModeEnabled); |
| 71 | if (!airplaneModeEnabled && Boolean.parseBoolean( |
| 72 | SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) { |
| 73 | Dependency.get(ActivityStarter.class).postStartActivityDismissingKeyguard( |
| 74 | new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS), 0); |
| 75 | return; |
| 76 | } |
| 77 | setEnabled(!airplaneModeEnabled); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | private void setEnabled(boolean enabled) { |
John Spurlock | 6627ff6 | 2014-05-23 12:59:27 -0400 | [diff] [blame] | 81 | final ConnectivityManager mgr = |
| 82 | (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 83 | mgr.setAirplaneMode(enabled); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | @Override |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 87 | public Intent getLongClickIntent() { |
| 88 | return new Intent(Settings.ACTION_AIRPLANE_MODE_SETTINGS); |
| 89 | } |
| 90 | |
| 91 | @Override |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 92 | public CharSequence getTileLabel() { |
| 93 | return mContext.getString(R.string.airplane_mode); |
| 94 | } |
| 95 | |
| 96 | @Override |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 97 | protected void handleUpdateState(BooleanState state, Object arg) { |
yuemingw | cb5daff | 2018-01-09 11:24:44 +0000 | [diff] [blame] | 98 | checkIfRestrictionEnforcedByAdminOnly(state, UserManager.DISALLOW_AIRPLANE_MODE); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 99 | final int value = arg instanceof Integer ? (Integer)arg : mSetting.getValue(); |
| 100 | final boolean airplaneMode = value != 0; |
| 101 | state.value = airplaneMode; |
Jason Monk | 726fb28 | 2015-03-31 12:56:11 -0400 | [diff] [blame] | 102 | state.label = mContext.getString(R.string.airplane_mode); |
Jason Monk | 395617f | 2017-05-05 14:07:58 -0400 | [diff] [blame] | 103 | state.icon = mIcon; |
| 104 | if (state.slash == null) { |
| 105 | state.slash = new SlashState(); |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 106 | } |
Jason Monk | 395617f | 2017-05-05 14:07:58 -0400 | [diff] [blame] | 107 | state.slash.isSlashed = !airplaneMode; |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 108 | state.state = airplaneMode ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE; |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 109 | state.contentDescription = state.label; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 110 | state.expandedAccessibilityClassName = Switch.class.getName(); |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | @Override |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 114 | public int getMetricsCategory() { |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 115 | return MetricsEvent.QS_AIRPLANEMODE; |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | @Override |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 119 | protected String composeChangeAnnouncement() { |
| 120 | if (mState.value) { |
| 121 | return mContext.getString(R.string.accessibility_quick_settings_airplane_changed_on); |
| 122 | } else { |
| 123 | return mContext.getString(R.string.accessibility_quick_settings_airplane_changed_off); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
Jason Monk | 1c6116c | 2017-09-06 17:33:01 -0400 | [diff] [blame] | 127 | public void handleSetListening(boolean listening) { |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 128 | if (mListening == listening) return; |
| 129 | mListening = listening; |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 130 | if (listening) { |
| 131 | final IntentFilter filter = new IntentFilter(); |
| 132 | filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
| 133 | mContext.registerReceiver(mReceiver, filter); |
| 134 | } else { |
| 135 | mContext.unregisterReceiver(mReceiver); |
| 136 | } |
| 137 | mSetting.setListening(listening); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | private final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 141 | @Override |
| 142 | public void onReceive(Context context, Intent intent) { |
| 143 | if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(intent.getAction())) { |
| 144 | refreshState(); |
| 145 | } |
| 146 | } |
| 147 | }; |
| 148 | } |