blob: 5f5a87e3cb87b73e4e924567d4d63e127d24fea5 [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;
26
Chris Wren457a21c2015-05-06 17:50:34 -040027import com.android.internal.logging.MetricsLogger;
Chris Wrenf6e9228b2016-01-26 18:04:35 -050028import com.android.internal.logging.MetricsProto.MetricsEvent;
John Spurlockaf8d6c42014-05-07 17:49:08 -040029import com.android.systemui.R;
30import com.android.systemui.qs.GlobalSetting;
31import com.android.systemui.qs.QSTile;
32
33/** Quick settings tile: Airplane mode **/
34public class AirplaneModeTile extends QSTile<QSTile.BooleanState> {
John Spurlockc6df3cf2014-11-04 19:36:44 -050035 private final AnimationIcon mEnable =
Jason Monk1aec93f2016-03-01 09:39:30 -050036 new AnimationIcon(R.drawable.ic_signal_airplane_enable_animation,
37 R.drawable.ic_signal_airplane_disable);
John Spurlockc6df3cf2014-11-04 19:36:44 -050038 private final AnimationIcon mDisable =
Jason Monk1aec93f2016-03-01 09:39:30 -050039 new AnimationIcon(R.drawable.ic_signal_airplane_disable_animation,
40 R.drawable.ic_signal_airplane_enable);
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
John Spurlockaf8d6c42014-05-07 17:49:08 -040045 public AirplaneModeTile(Host host) {
46 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);
John Spurlockaf8d6c42014-05-07 17:49:08 -040089 if (airplaneMode) {
John Spurlockc6df3cf2014-11-04 19:36:44 -050090 state.icon = mEnable;
John Spurlockaf8d6c42014-05-07 17:49:08 -040091 state.contentDescription = mContext.getString(
Selim Cinek4fda7b22014-08-18 22:07:25 +020092 R.string.accessibility_quick_settings_airplane_on);
John Spurlockaf8d6c42014-05-07 17:49:08 -040093 } else {
John Spurlockc6df3cf2014-11-04 19:36:44 -050094 state.icon = mDisable;
John Spurlockaf8d6c42014-05-07 17:49:08 -040095 state.contentDescription = mContext.getString(
Selim Cinek4fda7b22014-08-18 22:07:25 +020096 R.string.accessibility_quick_settings_airplane_off);
97 }
98 }
99
100 @Override
Chris Wren457a21c2015-05-06 17:50:34 -0400101 public int getMetricsCategory() {
Chris Wrenf6e9228b2016-01-26 18:04:35 -0500102 return MetricsEvent.QS_AIRPLANEMODE;
Chris Wren457a21c2015-05-06 17:50:34 -0400103 }
104
105 @Override
Selim Cinek4fda7b22014-08-18 22:07:25 +0200106 protected String composeChangeAnnouncement() {
107 if (mState.value) {
108 return mContext.getString(R.string.accessibility_quick_settings_airplane_changed_on);
109 } else {
110 return mContext.getString(R.string.accessibility_quick_settings_airplane_changed_off);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400111 }
112 }
113
John Spurlockccb6b9a2014-05-17 15:54:40 -0400114 public void setListening(boolean listening) {
John Spurlockbceed062014-08-10 18:04:16 -0400115 if (mListening == listening) return;
116 mListening = listening;
John Spurlockccb6b9a2014-05-17 15:54:40 -0400117 if (listening) {
118 final IntentFilter filter = new IntentFilter();
119 filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
120 mContext.registerReceiver(mReceiver, filter);
121 } else {
122 mContext.unregisterReceiver(mReceiver);
123 }
124 mSetting.setListening(listening);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400125 }
126
127 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
128 @Override
129 public void onReceive(Context context, Intent intent) {
130 if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(intent.getAction())) {
131 refreshState();
132 }
133 }
134 };
135}