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 | |
| 23 | import com.android.systemui.R; |
John Spurlock | 3976f3d | 2014-08-04 17:45:16 -0400 | [diff] [blame] | 24 | import com.android.systemui.qs.UsageTracker; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 25 | import com.android.systemui.qs.QSTile; |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 26 | import com.android.systemui.statusbar.policy.HotspotController; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 27 | |
| 28 | /** Quick settings tile: Hotspot **/ |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 29 | public class HotspotTile extends QSTile<QSTile.BooleanState> { |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 30 | private final HotspotController mController; |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 31 | private final Callback mCallback = new Callback(); |
John Spurlock | 3976f3d | 2014-08-04 17:45:16 -0400 | [diff] [blame] | 32 | private final UsageTracker mUsageTracker; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 33 | |
| 34 | public HotspotTile(Host host) { |
| 35 | super(host); |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 36 | mController = host.getHotspotController(); |
John Spurlock | 3976f3d | 2014-08-04 17:45:16 -0400 | [diff] [blame] | 37 | mUsageTracker = new UsageTracker(host.getContext(), HotspotTile.class); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 38 | mUsageTracker.setListening(true); |
| 39 | } |
| 40 | |
| 41 | @Override |
| 42 | protected void handleDestroy() { |
| 43 | super.handleDestroy(); |
| 44 | mUsageTracker.setListening(false); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | @Override |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 48 | protected BooleanState newTileState() { |
| 49 | return new BooleanState(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | @Override |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 53 | public void setListening(boolean listening) { |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 54 | if (listening) { |
| 55 | mController.addCallback(mCallback); |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 56 | } else { |
| 57 | mController.removeCallback(mCallback); |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 58 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | @Override |
| 62 | protected void handleClick() { |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 63 | final boolean isEnabled = (Boolean) mState.value; |
| 64 | mController.setHotspotEnabled(!isEnabled); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | @Override |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 68 | protected void handleUpdateState(BooleanState state, Object arg) { |
John Spurlock | 3976f3d | 2014-08-04 17:45:16 -0400 | [diff] [blame] | 69 | state.visible = mController.isHotspotSupported() && mUsageTracker.isRecentlyUsed(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 70 | state.label = mContext.getString(R.string.quick_settings_hotspot_label); |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 71 | |
| 72 | state.value = mController.isHotspotEnabled(); |
| 73 | state.iconId = state.visible && state.value ? R.drawable.ic_qs_hotspot_on |
| 74 | : R.drawable.ic_qs_hotspot_off; |
| 75 | } |
| 76 | |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame^] | 77 | @Override |
| 78 | protected String composeChangeAnnouncement() { |
| 79 | if (mState.value) { |
| 80 | return mContext.getString(R.string.accessibility_quick_settings_hotspot_changed_on); |
| 81 | } else { |
| 82 | return mContext.getString(R.string.accessibility_quick_settings_hotspot_changed_off); |
| 83 | } |
| 84 | } |
| 85 | |
John Spurlock | e2efefe | 2014-07-28 11:17:12 -0400 | [diff] [blame] | 86 | private final class Callback implements HotspotController.Callback { |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 87 | @Override |
| 88 | public void onHotspotChanged(boolean enabled) { |
| 89 | refreshState(); |
| 90 | } |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | /** |
| 94 | * This will catch broadcasts for changes in hotspot state so we can show |
| 95 | * the hotspot tile for a number of days after use. |
| 96 | */ |
| 97 | public static class APChangedReceiver extends BroadcastReceiver { |
John Spurlock | 3976f3d | 2014-08-04 17:45:16 -0400 | [diff] [blame] | 98 | private UsageTracker mUsageTracker; |
| 99 | |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 100 | @Override |
| 101 | public void onReceive(Context context, Intent intent) { |
John Spurlock | 3976f3d | 2014-08-04 17:45:16 -0400 | [diff] [blame] | 102 | if (mUsageTracker == null) { |
| 103 | mUsageTracker = new UsageTracker(context, HotspotTile.class); |
| 104 | } |
| 105 | mUsageTracker.trackUsage(); |
Jason Monk | 51e4dc0 | 2014-07-22 12:00:47 -0400 | [diff] [blame] | 106 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 107 | } |
| 108 | } |