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 | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 19 | import android.content.BroadcastReceiver; |
| 20 | import android.content.Context; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 21 | import android.content.Intent; |
| 22 | |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame^] | 23 | import com.android.internal.logging.MetricsLogger; |
Andrew Flynn | 8286257 | 2015-04-01 14:22:37 -0400 | [diff] [blame] | 24 | import com.android.systemui.Prefs; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 25 | import com.android.systemui.R; |
John Spurlock | 3976f3d | 2014-08-04 17:45:16 -0400 | [diff] [blame] | 26 | import com.android.systemui.qs.UsageTracker; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 27 | import com.android.systemui.qs.QSTile; |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 28 | import com.android.systemui.statusbar.policy.HotspotController; |
Sanket Padawe | a2bf987 | 2014-12-04 13:41:17 -0800 | [diff] [blame] | 29 | import com.android.systemui.statusbar.policy.KeyguardMonitor; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 30 | |
| 31 | /** Quick settings tile: Hotspot **/ |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 32 | public class HotspotTile extends QSTile<QSTile.BooleanState> { |
John Spurlock | 1832d2f | 2014-11-09 18:37:19 -0500 | [diff] [blame] | 33 | private final AnimationIcon mEnable = |
| 34 | new AnimationIcon(R.drawable.ic_hotspot_enable_animation); |
| 35 | private final AnimationIcon mDisable = |
| 36 | new AnimationIcon(R.drawable.ic_hotspot_disable_animation); |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 37 | private final HotspotController mController; |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 38 | private final Callback mCallback = new Callback(); |
John Spurlock | 3976f3d | 2014-08-04 17:45:16 -0400 | [diff] [blame] | 39 | private final UsageTracker mUsageTracker; |
Sanket Padawe | a2bf987 | 2014-12-04 13:41:17 -0800 | [diff] [blame] | 40 | private final KeyguardMonitor mKeyguard; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 41 | |
| 42 | public HotspotTile(Host host) { |
| 43 | super(host); |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 44 | mController = host.getHotspotController(); |
John Spurlock | 289d797 | 2014-10-17 12:06:38 -0400 | [diff] [blame] | 45 | mUsageTracker = newUsageTracker(host.getContext()); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 46 | mUsageTracker.setListening(true); |
Sanket Padawe | a2bf987 | 2014-12-04 13:41:17 -0800 | [diff] [blame] | 47 | mKeyguard = host.getKeyguardMonitor(); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | @Override |
| 51 | protected void handleDestroy() { |
| 52 | super.handleDestroy(); |
| 53 | mUsageTracker.setListening(false); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | @Override |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 57 | protected BooleanState newTileState() { |
| 58 | return new BooleanState(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | @Override |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 62 | public void setListening(boolean listening) { |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 63 | if (listening) { |
| 64 | mController.addCallback(mCallback); |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 65 | } else { |
| 66 | mController.removeCallback(mCallback); |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 67 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | @Override |
| 71 | protected void handleClick() { |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame^] | 72 | super.handleClick(); |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 73 | final boolean isEnabled = (Boolean) mState.value; |
| 74 | mController.setHotspotEnabled(!isEnabled); |
John Spurlock | 1832d2f | 2014-11-09 18:37:19 -0500 | [diff] [blame] | 75 | mEnable.setAllowAnimation(true); |
| 76 | mDisable.setAllowAnimation(true); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Override |
John Spurlock | c247b8f | 2014-11-06 23:06:25 -0500 | [diff] [blame] | 80 | protected void handleLongClick() { |
| 81 | if (mState.value) return; // don't allow usage reset if hotspot is active |
| 82 | final String title = mContext.getString(R.string.quick_settings_reset_confirmation_title, |
| 83 | mState.label); |
| 84 | mUsageTracker.showResetConfirmation(title, new Runnable() { |
| 85 | @Override |
| 86 | public void run() { |
| 87 | refreshState(); |
| 88 | } |
| 89 | }); |
| 90 | } |
| 91 | |
| 92 | @Override |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 93 | protected void handleUpdateState(BooleanState state, Object arg) { |
Jason Monk | e69e5f8 | 2014-12-10 17:24:18 -0500 | [diff] [blame] | 94 | state.visible = mController.isHotspotSupported() && mUsageTracker.isRecentlyUsed(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 95 | state.label = mContext.getString(R.string.quick_settings_hotspot_label); |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 96 | |
| 97 | state.value = mController.isHotspotEnabled(); |
John Spurlock | 1832d2f | 2014-11-09 18:37:19 -0500 | [diff] [blame] | 98 | state.icon = state.visible && state.value ? mEnable : mDisable; |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 99 | } |
| 100 | |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 101 | @Override |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame^] | 102 | public int getMetricsCategory() { |
| 103 | return MetricsLogger.QS_HOTSPOT; |
| 104 | } |
| 105 | |
| 106 | @Override |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 107 | protected String composeChangeAnnouncement() { |
| 108 | if (mState.value) { |
| 109 | return mContext.getString(R.string.accessibility_quick_settings_hotspot_changed_on); |
| 110 | } else { |
| 111 | return mContext.getString(R.string.accessibility_quick_settings_hotspot_changed_off); |
| 112 | } |
| 113 | } |
| 114 | |
John Spurlock | 289d797 | 2014-10-17 12:06:38 -0400 | [diff] [blame] | 115 | private static UsageTracker newUsageTracker(Context context) { |
Andrew Flynn | 8286257 | 2015-04-01 14:22:37 -0400 | [diff] [blame] | 116 | return new UsageTracker(context, Prefs.Key.HOTSPOT_TILE_LAST_USED, HotspotTile.class, |
| 117 | R.integer.days_to_show_hotspot_tile); |
John Spurlock | 289d797 | 2014-10-17 12:06:38 -0400 | [diff] [blame] | 118 | } |
| 119 | |
John Spurlock | e2efefe | 2014-07-28 11:17:12 -0400 | [diff] [blame] | 120 | private final class Callback implements HotspotController.Callback { |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 121 | @Override |
| 122 | public void onHotspotChanged(boolean enabled) { |
| 123 | refreshState(); |
| 124 | } |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | /** |
| 128 | * This will catch broadcasts for changes in hotspot state so we can show |
| 129 | * the hotspot tile for a number of days after use. |
| 130 | */ |
| 131 | public static class APChangedReceiver extends BroadcastReceiver { |
John Spurlock | 3976f3d | 2014-08-04 17:45:16 -0400 | [diff] [blame] | 132 | private UsageTracker mUsageTracker; |
| 133 | |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 134 | @Override |
| 135 | public void onReceive(Context context, Intent intent) { |
John Spurlock | 3976f3d | 2014-08-04 17:45:16 -0400 | [diff] [blame] | 136 | if (mUsageTracker == null) { |
John Spurlock | 289d797 | 2014-10-17 12:06:38 -0400 | [diff] [blame] | 137 | mUsageTracker = newUsageTracker(context); |
John Spurlock | 3976f3d | 2014-08-04 17:45:16 -0400 | [diff] [blame] | 138 | } |
| 139 | mUsageTracker.trackUsage(); |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 140 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 141 | } |
| 142 | } |