blob: f0860fe181d377b95a4ef06a78c6108a910f048e [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 =
36 new AnimationIcon(R.drawable.ic_signal_airplane_enable_animation);
37 private final AnimationIcon mDisable =
38 new AnimationIcon(R.drawable.ic_signal_airplane_disable_animation);
John Spurlockaf8d6c42014-05-07 17:49:08 -040039 private final GlobalSetting mSetting;
40
John Spurlockbceed062014-08-10 18:04:16 -040041 private boolean mListening;
42
John Spurlockaf8d6c42014-05-07 17:49:08 -040043 public AirplaneModeTile(Host host) {
44 super(host);
45
46 mSetting = new GlobalSetting(mContext, mHandler, Global.AIRPLANE_MODE_ON) {
47 @Override
48 protected void handleValueChanged(int value) {
49 handleRefreshState(value);
50 }
51 };
John Spurlockaf8d6c42014-05-07 17:49:08 -040052 }
53
54 @Override
Jason Monk62b63a02016-02-02 15:15:31 -050055 public BooleanState newTileState() {
John Spurlockaf8d6c42014-05-07 17:49:08 -040056 return new BooleanState();
57 }
58
59 @Override
60 public void handleClick() {
Chris Wren9e7283f2015-05-08 17:23:47 -040061 MetricsLogger.action(mContext, getMetricsCategory(), !mState.value);
John Spurlockaf8d6c42014-05-07 17:49:08 -040062 setEnabled(!mState.value);
63 }
64
65 private void setEnabled(boolean enabled) {
John Spurlock6627ff62014-05-23 12:59:27 -040066 final ConnectivityManager mgr =
67 (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
68 mgr.setAirplaneMode(enabled);
John Spurlockaf8d6c42014-05-07 17:49:08 -040069 }
70
71 @Override
Jason Monk76c67aa2016-02-19 14:49:42 -050072 public Intent getLongClickIntent() {
73 return new Intent(Settings.ACTION_AIRPLANE_MODE_SETTINGS);
74 }
75
76 @Override
John Spurlockaf8d6c42014-05-07 17:49:08 -040077 protected void handleUpdateState(BooleanState state, Object arg) {
78 final int value = arg instanceof Integer ? (Integer)arg : mSetting.getValue();
79 final boolean airplaneMode = value != 0;
80 state.value = airplaneMode;
Jason Monk726fb282015-03-31 12:56:11 -040081 state.label = mContext.getString(R.string.airplane_mode);
John Spurlockaf8d6c42014-05-07 17:49:08 -040082 if (airplaneMode) {
John Spurlockc6df3cf2014-11-04 19:36:44 -050083 state.icon = mEnable;
John Spurlockaf8d6c42014-05-07 17:49:08 -040084 state.contentDescription = mContext.getString(
Selim Cinek4fda7b22014-08-18 22:07:25 +020085 R.string.accessibility_quick_settings_airplane_on);
John Spurlockaf8d6c42014-05-07 17:49:08 -040086 } else {
John Spurlockc6df3cf2014-11-04 19:36:44 -050087 state.icon = mDisable;
John Spurlockaf8d6c42014-05-07 17:49:08 -040088 state.contentDescription = mContext.getString(
Selim Cinek4fda7b22014-08-18 22:07:25 +020089 R.string.accessibility_quick_settings_airplane_off);
90 }
91 }
92
93 @Override
Chris Wren457a21c2015-05-06 17:50:34 -040094 public int getMetricsCategory() {
Chris Wrenf6e9228b2016-01-26 18:04:35 -050095 return MetricsEvent.QS_AIRPLANEMODE;
Chris Wren457a21c2015-05-06 17:50:34 -040096 }
97
98 @Override
Selim Cinek4fda7b22014-08-18 22:07:25 +020099 protected String composeChangeAnnouncement() {
100 if (mState.value) {
101 return mContext.getString(R.string.accessibility_quick_settings_airplane_changed_on);
102 } else {
103 return mContext.getString(R.string.accessibility_quick_settings_airplane_changed_off);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400104 }
105 }
106
John Spurlockccb6b9a2014-05-17 15:54:40 -0400107 public void setListening(boolean listening) {
John Spurlockbceed062014-08-10 18:04:16 -0400108 if (mListening == listening) return;
109 mListening = listening;
John Spurlockccb6b9a2014-05-17 15:54:40 -0400110 if (listening) {
111 final IntentFilter filter = new IntentFilter();
112 filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
113 mContext.registerReceiver(mReceiver, filter);
114 } else {
115 mContext.unregisterReceiver(mReceiver);
116 }
117 mSetting.setListening(listening);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400118 }
119
120 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
121 @Override
122 public void onReceive(Context context, Intent intent) {
123 if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(intent.getAction())) {
124 refreshState();
125 }
126 }
127 };
128}