John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.qs.tiles; |
| 18 | |
| 19 | import android.content.BroadcastReceiver; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.content.IntentFilter; |
John Spurlock | 6627ff6 | 2014-05-23 12:59:27 -0400 | [diff] [blame] | 23 | import android.net.ConnectivityManager; |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 24 | import android.provider.Settings; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 25 | import android.provider.Settings.Global; |
| 26 | |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 27 | import com.android.internal.logging.MetricsLogger; |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 28 | import com.android.internal.logging.MetricsProto.MetricsEvent; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 29 | import com.android.systemui.R; |
| 30 | import com.android.systemui.qs.GlobalSetting; |
| 31 | import com.android.systemui.qs.QSTile; |
| 32 | |
| 33 | /** Quick settings tile: Airplane mode **/ |
| 34 | public class AirplaneModeTile extends QSTile<QSTile.BooleanState> { |
John Spurlock | c6df3cf | 2014-11-04 19:36:44 -0500 | [diff] [blame] | 35 | 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 Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 39 | private final GlobalSetting mSetting; |
| 40 | |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 41 | private boolean mListening; |
| 42 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 43 | 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 Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | @Override |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 55 | public BooleanState newTileState() { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 56 | return new BooleanState(); |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public void handleClick() { |
Chris Wren | 9e7283f | 2015-05-08 17:23:47 -0400 | [diff] [blame] | 61 | MetricsLogger.action(mContext, getMetricsCategory(), !mState.value); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 62 | setEnabled(!mState.value); |
| 63 | } |
| 64 | |
| 65 | private void setEnabled(boolean enabled) { |
John Spurlock | 6627ff6 | 2014-05-23 12:59:27 -0400 | [diff] [blame] | 66 | final ConnectivityManager mgr = |
| 67 | (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 68 | mgr.setAirplaneMode(enabled); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Override |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 72 | public Intent getLongClickIntent() { |
| 73 | return new Intent(Settings.ACTION_AIRPLANE_MODE_SETTINGS); |
| 74 | } |
| 75 | |
| 76 | @Override |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 77 | 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 Monk | 726fb28 | 2015-03-31 12:56:11 -0400 | [diff] [blame] | 81 | state.label = mContext.getString(R.string.airplane_mode); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 82 | if (airplaneMode) { |
John Spurlock | c6df3cf | 2014-11-04 19:36:44 -0500 | [diff] [blame] | 83 | state.icon = mEnable; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 84 | state.contentDescription = mContext.getString( |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 85 | R.string.accessibility_quick_settings_airplane_on); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 86 | } else { |
John Spurlock | c6df3cf | 2014-11-04 19:36:44 -0500 | [diff] [blame] | 87 | state.icon = mDisable; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 88 | state.contentDescription = mContext.getString( |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 89 | R.string.accessibility_quick_settings_airplane_off); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | @Override |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 94 | public int getMetricsCategory() { |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 95 | return MetricsEvent.QS_AIRPLANEMODE; |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Override |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 99 | 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 Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 107 | public void setListening(boolean listening) { |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 108 | if (mListening == listening) return; |
| 109 | mListening = listening; |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 110 | 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 Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 118 | } |
| 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 | } |