blob: bef1aff571f3f104b4789ab955932ad0ca75ce1e [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;
Jason Monk76c67aa2016-02-19 14:49:42 -050024import android.provider.Settings;
John Spurlockaf8d6c42014-05-07 17:49:08 -040025import android.provider.Settings.Global;
Jason Monk32508852017-01-18 09:17:13 -050026import android.service.quicksettings.Tile;
Julia Reynolds20aef8a2016-05-04 16:44:08 -040027import android.widget.Switch;
John Spurlockaf8d6c42014-05-07 17:49:08 -040028
Chris Wren457a21c2015-05-06 17:50:34 -040029import com.android.internal.logging.MetricsLogger;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010030import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
John Spurlockaf8d6c42014-05-07 17:49:08 -040031import com.android.systemui.R;
Jason Monk395617f2017-05-05 14:07:58 -040032import com.android.systemui.plugins.qs.QSTile.BooleanState;
John Spurlockaf8d6c42014-05-07 17:49:08 -040033import com.android.systemui.qs.GlobalSetting;
Jason Monk702e2eb2017-03-03 16:53:44 -050034import com.android.systemui.qs.QSHost;
Jason Monk702e2eb2017-03-03 16:53:44 -050035import com.android.systemui.qs.tileimpl.QSTileImpl;
John Spurlockaf8d6c42014-05-07 17:49:08 -040036
37/** Quick settings tile: Airplane mode **/
Jason Monk702e2eb2017-03-03 16:53:44 -050038public class AirplaneModeTile extends QSTileImpl<BooleanState> {
Jason Monk395617f2017-05-05 14:07:58 -040039 private final Icon mIcon =
Evan Lairdbcdd0462017-05-15 12:51:34 -040040 ResourceIcon.get(R.drawable.ic_signal_airplane);
John Spurlockaf8d6c42014-05-07 17:49:08 -040041 private final GlobalSetting mSetting;
42
John Spurlockbceed062014-08-10 18:04:16 -040043 private boolean mListening;
44
Jason Monk702e2eb2017-03-03 16:53:44 -050045 public AirplaneModeTile(QSHost host) {
John Spurlockaf8d6c42014-05-07 17:49:08 -040046 super(host);
47
48 mSetting = new GlobalSetting(mContext, mHandler, Global.AIRPLANE_MODE_ON) {
49 @Override
50 protected void handleValueChanged(int value) {
51 handleRefreshState(value);
52 }
53 };
John Spurlockaf8d6c42014-05-07 17:49:08 -040054 }
55
56 @Override
Jason Monk62b63a02016-02-02 15:15:31 -050057 public BooleanState newTileState() {
John Spurlockaf8d6c42014-05-07 17:49:08 -040058 return new BooleanState();
59 }
60
61 @Override
62 public void handleClick() {
Chris Wren9e7283f2015-05-08 17:23:47 -040063 MetricsLogger.action(mContext, getMetricsCategory(), !mState.value);
John Spurlockaf8d6c42014-05-07 17:49:08 -040064 setEnabled(!mState.value);
65 }
66
67 private void setEnabled(boolean enabled) {
John Spurlock6627ff62014-05-23 12:59:27 -040068 final ConnectivityManager mgr =
69 (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
70 mgr.setAirplaneMode(enabled);
John Spurlockaf8d6c42014-05-07 17:49:08 -040071 }
72
73 @Override
Jason Monk76c67aa2016-02-19 14:49:42 -050074 public Intent getLongClickIntent() {
75 return new Intent(Settings.ACTION_AIRPLANE_MODE_SETTINGS);
76 }
77
78 @Override
Jason Monk39c98e62016-03-16 09:18:35 -040079 public CharSequence getTileLabel() {
80 return mContext.getString(R.string.airplane_mode);
81 }
82
83 @Override
John Spurlockaf8d6c42014-05-07 17:49:08 -040084 protected void handleUpdateState(BooleanState state, Object arg) {
85 final int value = arg instanceof Integer ? (Integer)arg : mSetting.getValue();
86 final boolean airplaneMode = value != 0;
87 state.value = airplaneMode;
Jason Monk726fb282015-03-31 12:56:11 -040088 state.label = mContext.getString(R.string.airplane_mode);
Jason Monk395617f2017-05-05 14:07:58 -040089 state.icon = mIcon;
90 if (state.slash == null) {
91 state.slash = new SlashState();
Selim Cinek4fda7b22014-08-18 22:07:25 +020092 }
Jason Monk395617f2017-05-05 14:07:58 -040093 state.slash.isSlashed = !airplaneMode;
Jason Monk32508852017-01-18 09:17:13 -050094 state.state = airplaneMode ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE;
Julia Reynolds20aef8a2016-05-04 16:44:08 -040095 state.contentDescription = state.label;
Jason Monk702e2eb2017-03-03 16:53:44 -050096 state.expandedAccessibilityClassName = Switch.class.getName();
Selim Cinek4fda7b22014-08-18 22:07:25 +020097 }
98
99 @Override
Chris Wren457a21c2015-05-06 17:50:34 -0400100 public int getMetricsCategory() {
Chris Wrenf6e9228b2016-01-26 18:04:35 -0500101 return MetricsEvent.QS_AIRPLANEMODE;
Chris Wren457a21c2015-05-06 17:50:34 -0400102 }
103
104 @Override
Selim Cinek4fda7b22014-08-18 22:07:25 +0200105 protected String composeChangeAnnouncement() {
106 if (mState.value) {
107 return mContext.getString(R.string.accessibility_quick_settings_airplane_changed_on);
108 } else {
109 return mContext.getString(R.string.accessibility_quick_settings_airplane_changed_off);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400110 }
111 }
112
Jason Monk1c6116c2017-09-06 17:33:01 -0400113 public void handleSetListening(boolean listening) {
John Spurlockbceed062014-08-10 18:04:16 -0400114 if (mListening == listening) return;
115 mListening = listening;
John Spurlockccb6b9a2014-05-17 15:54:40 -0400116 if (listening) {
117 final IntentFilter filter = new IntentFilter();
118 filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
119 mContext.registerReceiver(mReceiver, filter);
120 } else {
121 mContext.unregisterReceiver(mReceiver);
122 }
123 mSetting.setListening(listening);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400124 }
125
126 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
127 @Override
128 public void onReceive(Context context, Intent intent) {
129 if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(intent.getAction())) {
130 refreshState();
131 }
132 }
133 };
134}