blob: 9883da6f3a3528c6c64e28ea47911e2712284a72 [file] [log] [blame]
John Spurlockaf8d6c42014-05-07 17:49:08 -04001/*
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
17package com.android.systemui.qs.tiles;
18
19import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
John Spurlock6627ff62014-05-23 12:59:27 -040023import android.net.ConnectivityManager;
yuemingwcb5daff2018-01-09 11:24:44 +000024import android.os.UserManager;
Jason Monk76c67aa2016-02-19 14:49:42 -050025import android.provider.Settings;
John Spurlockaf8d6c42014-05-07 17:49:08 -040026import android.provider.Settings.Global;
Jason Monk32508852017-01-18 09:17:13 -050027import android.service.quicksettings.Tile;
Julia Reynolds20aef8a2016-05-04 16:44:08 -040028import android.widget.Switch;
John Spurlockaf8d6c42014-05-07 17:49:08 -040029
Chris Wren457a21c2015-05-06 17:50:34 -040030import com.android.internal.logging.MetricsLogger;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010031import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
John Spurlockaf8d6c42014-05-07 17:49:08 -040032import com.android.systemui.R;
Jason Monk395617f2017-05-05 14:07:58 -040033import com.android.systemui.plugins.qs.QSTile.BooleanState;
John Spurlockaf8d6c42014-05-07 17:49:08 -040034import com.android.systemui.qs.GlobalSetting;
Jason Monk702e2eb2017-03-03 16:53:44 -050035import com.android.systemui.qs.QSHost;
Jason Monk702e2eb2017-03-03 16:53:44 -050036import com.android.systemui.qs.tileimpl.QSTileImpl;
John Spurlockaf8d6c42014-05-07 17:49:08 -040037
38/** Quick settings tile: Airplane mode **/
Jason Monk702e2eb2017-03-03 16:53:44 -050039public class AirplaneModeTile extends QSTileImpl<BooleanState> {
Jason Monk395617f2017-05-05 14:07:58 -040040 private final Icon mIcon =
Evan Lairdbcdd0462017-05-15 12:51:34 -040041 ResourceIcon.get(R.drawable.ic_signal_airplane);
John Spurlockaf8d6c42014-05-07 17:49:08 -040042 private final GlobalSetting mSetting;
43
John Spurlockbceed062014-08-10 18:04:16 -040044 private boolean mListening;
45
Jason Monk702e2eb2017-03-03 16:53:44 -050046 public AirplaneModeTile(QSHost host) {
John Spurlockaf8d6c42014-05-07 17:49:08 -040047 super(host);
48
49 mSetting = new GlobalSetting(mContext, mHandler, Global.AIRPLANE_MODE_ON) {
50 @Override
51 protected void handleValueChanged(int value) {
52 handleRefreshState(value);
53 }
54 };
John Spurlockaf8d6c42014-05-07 17:49:08 -040055 }
56
57 @Override
Jason Monk62b63a02016-02-02 15:15:31 -050058 public BooleanState newTileState() {
John Spurlockaf8d6c42014-05-07 17:49:08 -040059 return new BooleanState();
60 }
61
62 @Override
63 public void handleClick() {
Chris Wren9e7283f2015-05-08 17:23:47 -040064 MetricsLogger.action(mContext, getMetricsCategory(), !mState.value);
John Spurlockaf8d6c42014-05-07 17:49:08 -040065 setEnabled(!mState.value);
66 }
67
68 private void setEnabled(boolean enabled) {
John Spurlock6627ff62014-05-23 12:59:27 -040069 final ConnectivityManager mgr =
70 (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
71 mgr.setAirplaneMode(enabled);
John Spurlockaf8d6c42014-05-07 17:49:08 -040072 }
73
74 @Override
Jason Monk76c67aa2016-02-19 14:49:42 -050075 public Intent getLongClickIntent() {
76 return new Intent(Settings.ACTION_AIRPLANE_MODE_SETTINGS);
77 }
78
79 @Override
Jason Monk39c98e62016-03-16 09:18:35 -040080 public CharSequence getTileLabel() {
81 return mContext.getString(R.string.airplane_mode);
82 }
83
84 @Override
John Spurlockaf8d6c42014-05-07 17:49:08 -040085 protected void handleUpdateState(BooleanState state, Object arg) {
yuemingwcb5daff2018-01-09 11:24:44 +000086 checkIfRestrictionEnforcedByAdminOnly(state, UserManager.DISALLOW_AIRPLANE_MODE);
John Spurlockaf8d6c42014-05-07 17:49:08 -040087 final int value = arg instanceof Integer ? (Integer)arg : mSetting.getValue();
88 final boolean airplaneMode = value != 0;
89 state.value = airplaneMode;
Jason Monk726fb282015-03-31 12:56:11 -040090 state.label = mContext.getString(R.string.airplane_mode);
Jason Monk395617f2017-05-05 14:07:58 -040091 state.icon = mIcon;
92 if (state.slash == null) {
93 state.slash = new SlashState();
Selim Cinek4fda7b22014-08-18 22:07:25 +020094 }
Jason Monk395617f2017-05-05 14:07:58 -040095 state.slash.isSlashed = !airplaneMode;
Jason Monk32508852017-01-18 09:17:13 -050096 state.state = airplaneMode ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE;
Julia Reynolds20aef8a2016-05-04 16:44:08 -040097 state.contentDescription = state.label;
Jason Monk702e2eb2017-03-03 16:53:44 -050098 state.expandedAccessibilityClassName = Switch.class.getName();
Selim Cinek4fda7b22014-08-18 22:07:25 +020099 }
100
101 @Override
Chris Wren457a21c2015-05-06 17:50:34 -0400102 public int getMetricsCategory() {
Chris Wrenf6e9228b2016-01-26 18:04:35 -0500103 return MetricsEvent.QS_AIRPLANEMODE;
Chris Wren457a21c2015-05-06 17:50:34 -0400104 }
105
106 @Override
Selim Cinek4fda7b22014-08-18 22:07:25 +0200107 protected String composeChangeAnnouncement() {
108 if (mState.value) {
109 return mContext.getString(R.string.accessibility_quick_settings_airplane_changed_on);
110 } else {
111 return mContext.getString(R.string.accessibility_quick_settings_airplane_changed_off);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400112 }
113 }
114
Jason Monk1c6116c2017-09-06 17:33:01 -0400115 public void handleSetListening(boolean listening) {
John Spurlockbceed062014-08-10 18:04:16 -0400116 if (mListening == listening) return;
117 mListening = listening;
John Spurlockccb6b9a2014-05-17 15:54:40 -0400118 if (listening) {
119 final IntentFilter filter = new IntentFilter();
120 filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
121 mContext.registerReceiver(mReceiver, filter);
122 } else {
123 mContext.unregisterReceiver(mReceiver);
124 }
125 mSetting.setListening(listening);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400126 }
127
128 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
129 @Override
130 public void onReceive(Context context, Intent intent) {
131 if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(intent.getAction())) {
132 refreshState();
133 }
134 }
135 };
136}