blob: 89f1985b3f75ec315afc74de415892dc38324673 [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
John Spurlockaf8d6c42014-05-07 17:49:08 -040079 protected void handleUpdateState(BooleanState state, Object arg) {
80 final int value = arg instanceof Integer ? (Integer)arg : mSetting.getValue();
81 final boolean airplaneMode = value != 0;
82 state.value = airplaneMode;
Jason Monk726fb282015-03-31 12:56:11 -040083 state.label = mContext.getString(R.string.airplane_mode);
John Spurlockaf8d6c42014-05-07 17:49:08 -040084 if (airplaneMode) {
John Spurlockc6df3cf2014-11-04 19:36:44 -050085 state.icon = mEnable;
John Spurlockaf8d6c42014-05-07 17:49:08 -040086 state.contentDescription = mContext.getString(
Selim Cinek4fda7b22014-08-18 22:07:25 +020087 R.string.accessibility_quick_settings_airplane_on);
John Spurlockaf8d6c42014-05-07 17:49:08 -040088 } else {
John Spurlockc6df3cf2014-11-04 19:36:44 -050089 state.icon = mDisable;
John Spurlockaf8d6c42014-05-07 17:49:08 -040090 state.contentDescription = mContext.getString(
Selim Cinek4fda7b22014-08-18 22:07:25 +020091 R.string.accessibility_quick_settings_airplane_off);
92 }
93 }
94
95 @Override
Chris Wren457a21c2015-05-06 17:50:34 -040096 public int getMetricsCategory() {
Chris Wrenf6e9228b2016-01-26 18:04:35 -050097 return MetricsEvent.QS_AIRPLANEMODE;
Chris Wren457a21c2015-05-06 17:50:34 -040098 }
99
100 @Override
Selim Cinek4fda7b22014-08-18 22:07:25 +0200101 protected String composeChangeAnnouncement() {
102 if (mState.value) {
103 return mContext.getString(R.string.accessibility_quick_settings_airplane_changed_on);
104 } else {
105 return mContext.getString(R.string.accessibility_quick_settings_airplane_changed_off);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400106 }
107 }
108
John Spurlockccb6b9a2014-05-17 15:54:40 -0400109 public void setListening(boolean listening) {
John Spurlockbceed062014-08-10 18:04:16 -0400110 if (mListening == listening) return;
111 mListening = listening;
John Spurlockccb6b9a2014-05-17 15:54:40 -0400112 if (listening) {
113 final IntentFilter filter = new IntentFilter();
114 filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
115 mContext.registerReceiver(mReceiver, filter);
116 } else {
117 mContext.unregisterReceiver(mReceiver);
118 }
119 mSetting.setListening(listening);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400120 }
121
122 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
123 @Override
124 public void onReceive(Context context, Intent intent) {
125 if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(intent.getAction())) {
126 refreshState();
127 }
128 }
129 };
130}