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 | |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 19 | import android.content.Intent; |
Jason Monk | 49a2796 | 2016-05-25 14:22:01 -0400 | [diff] [blame] | 20 | import android.content.IntentFilter; |
Sudheer Shanka | b6fc931 | 2016-01-27 19:59:03 +0000 | [diff] [blame] | 21 | import android.os.UserManager; |
| 22 | |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 23 | import android.provider.Settings; |
Jason Monk | 49a2796 | 2016-05-25 14:22:01 -0400 | [diff] [blame] | 24 | import android.provider.Settings.Global; |
jackqdyulei | 76471a5 | 2016-12-05 15:03:56 -0800 | [diff] [blame^] | 25 | import android.service.quicksettings.Tile; |
Jason Monk | 49a2796 | 2016-05-25 14:22:01 -0400 | [diff] [blame] | 26 | import android.text.SpannableStringBuilder; |
| 27 | import android.text.style.ForegroundColorSpan; |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 28 | import android.widget.Switch; |
| 29 | |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 30 | import com.android.internal.logging.MetricsLogger; |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 31 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 32 | import com.android.systemui.R; |
Jason Monk | 49a2796 | 2016-05-25 14:22:01 -0400 | [diff] [blame] | 33 | import com.android.systemui.qs.GlobalSetting; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 34 | import com.android.systemui.qs.QSTile; |
jackqdyulei | 76471a5 | 2016-12-05 15:03:56 -0800 | [diff] [blame^] | 35 | import com.android.systemui.qs.external.TileColorPicker; |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 36 | import com.android.systemui.statusbar.policy.HotspotController; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 37 | |
| 38 | /** Quick settings tile: Hotspot **/ |
Jason Monk | ab17c98 | 2016-06-20 12:39:38 -0400 | [diff] [blame] | 39 | public class HotspotTile extends QSTile<QSTile.AirplaneBooleanState> { |
John Spurlock | 1832d2f | 2014-11-09 18:37:19 -0500 | [diff] [blame] | 40 | private final AnimationIcon mEnable = |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 41 | new AnimationIcon(R.drawable.ic_hotspot_enable_animation, |
| 42 | R.drawable.ic_hotspot_disable); |
John Spurlock | 1832d2f | 2014-11-09 18:37:19 -0500 | [diff] [blame] | 43 | private final AnimationIcon mDisable = |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 44 | new AnimationIcon(R.drawable.ic_hotspot_disable_animation, |
| 45 | R.drawable.ic_hotspot_enable); |
Jason Monk | ab17c98 | 2016-06-20 12:39:38 -0400 | [diff] [blame] | 46 | private final Icon mDisableNoAnimation = ResourceIcon.get(R.drawable.ic_hotspot_enable); |
| 47 | private final Icon mUnavailable = ResourceIcon.get(R.drawable.ic_hotspot_unavailable); |
| 48 | |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 49 | private final HotspotController mController; |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 50 | private final Callback mCallback = new Callback(); |
Jason Monk | 49a2796 | 2016-05-25 14:22:01 -0400 | [diff] [blame] | 51 | private final GlobalSetting mAirplaneMode; |
| 52 | private boolean mListening; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 53 | |
| 54 | public HotspotTile(Host host) { |
| 55 | super(host); |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 56 | mController = host.getHotspotController(); |
Jason Monk | 49a2796 | 2016-05-25 14:22:01 -0400 | [diff] [blame] | 57 | mAirplaneMode = new GlobalSetting(mContext, mHandler, Global.AIRPLANE_MODE_ON) { |
| 58 | @Override |
| 59 | protected void handleValueChanged(int value) { |
| 60 | refreshState(); |
| 61 | } |
| 62 | }; |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | @Override |
Jason Monk | 28680c6 | 2016-04-19 09:31:20 -0400 | [diff] [blame] | 66 | public boolean isAvailable() { |
| 67 | return mController.isHotspotSupported(); |
| 68 | } |
| 69 | |
| 70 | @Override |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 71 | protected void handleDestroy() { |
| 72 | super.handleDestroy(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Override |
Jason Monk | ab17c98 | 2016-06-20 12:39:38 -0400 | [diff] [blame] | 76 | public AirplaneBooleanState newTileState() { |
| 77 | return new AirplaneBooleanState(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Override |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 81 | public void setListening(boolean listening) { |
Jason Monk | 49a2796 | 2016-05-25 14:22:01 -0400 | [diff] [blame] | 82 | if (mListening == listening) return; |
| 83 | mListening = listening; |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 84 | if (listening) { |
| 85 | mController.addCallback(mCallback); |
Jason Monk | 49a2796 | 2016-05-25 14:22:01 -0400 | [diff] [blame] | 86 | final IntentFilter filter = new IntentFilter(); |
| 87 | filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
Jason Monk | 94f682e | 2016-06-23 13:23:01 -0400 | [diff] [blame] | 88 | refreshState(); |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 89 | } else { |
| 90 | mController.removeCallback(mCallback); |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 91 | } |
Jason Monk | 49a2796 | 2016-05-25 14:22:01 -0400 | [diff] [blame] | 92 | mAirplaneMode.setListening(listening); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Override |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 96 | public Intent getLongClickIntent() { |
| 97 | return new Intent(Settings.ACTION_WIRELESS_SETTINGS); |
| 98 | } |
| 99 | |
| 100 | @Override |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 101 | protected void handleClick() { |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 102 | final boolean isEnabled = (Boolean) mState.value; |
Jason Monk | 49a2796 | 2016-05-25 14:22:01 -0400 | [diff] [blame] | 103 | if (!isEnabled && mAirplaneMode.getValue() != 0) { |
| 104 | return; |
| 105 | } |
Chris Wren | 9e7283f | 2015-05-08 17:23:47 -0400 | [diff] [blame] | 106 | MetricsLogger.action(mContext, getMetricsCategory(), !isEnabled); |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 107 | mController.setHotspotEnabled(!isEnabled); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Override |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 111 | public CharSequence getTileLabel() { |
| 112 | return mContext.getString(R.string.quick_settings_hotspot_label); |
| 113 | } |
| 114 | |
| 115 | @Override |
Jason Monk | ab17c98 | 2016-06-20 12:39:38 -0400 | [diff] [blame] | 116 | protected void handleUpdateState(AirplaneBooleanState state, Object arg) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 117 | state.label = mContext.getString(R.string.quick_settings_hotspot_label); |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 118 | |
Sudheer Shanka | a8fbbb3 | 2016-02-11 17:17:57 +0000 | [diff] [blame] | 119 | checkIfRestrictionEnforcedByAdminOnly(state, UserManager.DISALLOW_CONFIG_TETHERING); |
Julia Reynolds | b8a46a6 | 2015-07-14 13:36:30 -0400 | [diff] [blame] | 120 | if (arg instanceof Boolean) { |
| 121 | state.value = (boolean) arg; |
| 122 | } else { |
Jason Monk | dd5bdc6 | 2015-07-20 12:18:38 -0400 | [diff] [blame] | 123 | state.value = mController.isHotspotEnabled(); |
Julia Reynolds | b8a46a6 | 2015-07-14 13:36:30 -0400 | [diff] [blame] | 124 | } |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 125 | state.icon = state.value ? mEnable : mDisable; |
Jason Monk | ab17c98 | 2016-06-20 12:39:38 -0400 | [diff] [blame] | 126 | boolean wasAirplane = state.isAirplaneMode; |
| 127 | state.isAirplaneMode = mAirplaneMode.getValue() != 0; |
| 128 | if (state.isAirplaneMode) { |
jackqdyulei | 76471a5 | 2016-12-05 15:03:56 -0800 | [diff] [blame^] | 129 | final int disabledColor = TileColorPicker.getInstance(mContext) |
| 130 | .getColor(Tile.STATE_UNAVAILABLE); |
Jason Monk | 49a2796 | 2016-05-25 14:22:01 -0400 | [diff] [blame] | 131 | state.label = new SpannableStringBuilder().append(state.label, |
| 132 | new ForegroundColorSpan(disabledColor), |
| 133 | SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE); |
| 134 | state.icon = mUnavailable; |
Jason Monk | ab17c98 | 2016-06-20 12:39:38 -0400 | [diff] [blame] | 135 | } else if (wasAirplane) { |
| 136 | state.icon = mDisableNoAnimation; |
Jason Monk | 49a2796 | 2016-05-25 14:22:01 -0400 | [diff] [blame] | 137 | } |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 138 | state.minimalAccessibilityClassName = state.expandedAccessibilityClassName |
| 139 | = Switch.class.getName(); |
| 140 | state.contentDescription = state.label; |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 141 | } |
| 142 | |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 143 | @Override |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 144 | public int getMetricsCategory() { |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 145 | return MetricsEvent.QS_HOTSPOT; |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | @Override |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 149 | protected String composeChangeAnnouncement() { |
| 150 | if (mState.value) { |
| 151 | return mContext.getString(R.string.accessibility_quick_settings_hotspot_changed_on); |
| 152 | } else { |
| 153 | return mContext.getString(R.string.accessibility_quick_settings_hotspot_changed_off); |
| 154 | } |
| 155 | } |
| 156 | |
John Spurlock | e2efefe | 2014-07-28 11:17:12 -0400 | [diff] [blame] | 157 | private final class Callback implements HotspotController.Callback { |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 158 | @Override |
| 159 | public void onHotspotChanged(boolean enabled) { |
Julia Reynolds | b8a46a6 | 2015-07-14 13:36:30 -0400 | [diff] [blame] | 160 | refreshState(enabled); |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 161 | } |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 162 | }; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 163 | } |