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 23 import com.android.internal.logging.MetricsLogger; 24 import com.android.systemui.Prefs; 25 import com.android.systemui.R; 26 import com.android.systemui.qs.QSTile; 27 import com.android.systemui.qs.UsageTracker; 28 import com.android.systemui.statusbar.policy.HotspotController; 29 30 /** Quick settings tile: Hotspot **/ 31 public class HotspotTile extends QSTile<QSTile.BooleanState> { 32 private final AnimationIcon mEnable = 33 new AnimationIcon(R.drawable.ic_hotspot_enable_animation); 34 private final AnimationIcon mDisable = 35 new AnimationIcon(R.drawable.ic_hotspot_disable_animation); 36 private final HotspotController mController; 37 private final Callback mCallback = new Callback(); 38 private final UsageTracker mUsageTracker; 39 40 public HotspotTile(Host host) { 41 super(host); 42 mController = host.getHotspotController(); 43 mUsageTracker = newUsageTracker(host.getContext()); 44 mUsageTracker.setListening(true); 45 } 46 47 @Override 48 protected void handleDestroy() { 49 super.handleDestroy(); 50 mUsageTracker.setListening(false); 51 } 52 53 @Override 54 protected BooleanState newTileState() { 55 return new BooleanState(); 56 } 57 58 @Override 59 public void setListening(boolean listening) { 60 if (listening) { 61 mController.addCallback(mCallback); 62 } else { 63 mController.removeCallback(mCallback); 64 } 65 } 66 67 @Override 68 protected void handleClick() { 69 final boolean isEnabled = (Boolean) mState.value; 70 MetricsLogger.action(mContext, getMetricsCategory(), !isEnabled); 71 mController.setHotspotEnabled(!isEnabled); 72 mEnable.setAllowAnimation(true); 73 mDisable.setAllowAnimation(true); 74 } 75 76 @Override 77 protected void handleLongClick() { 78 if (mState.value) return; // don't allow usage reset if hotspot is active 79 final String title = mContext.getString(R.string.quick_settings_reset_confirmation_title, 80 mState.label); 81 mUsageTracker.showResetConfirmation(title, new Runnable() { 82 @Override 83 public void run() { 84 refreshState(); 85 } 86 }); 87 } 88 89 @Override 90 protected void handleUpdateState(BooleanState state, Object arg) { 91 state.visible = mController.isHotspotSupported() && mUsageTracker.isRecentlyUsed(); 92 state.label = mContext.getString(R.string.quick_settings_hotspot_label); 93 94 if (arg instanceof Boolean) { 95 state.value = (boolean) arg; 96 } else { 97 state.value = mController.isHotspotEnabled(); 98 } 99 state.icon = state.visible && state.value ? mEnable : mDisable; 100 } 101 102 @Override 103 public int getMetricsCategory() { 104 return MetricsLogger.QS_HOTSPOT; 105 } 106 107 @Override 108 protected String composeChangeAnnouncement() { 109 if (mState.value) { 110 return mContext.getString(R.string.accessibility_quick_settings_hotspot_changed_on); 111 } else { 112 return mContext.getString(R.string.accessibility_quick_settings_hotspot_changed_off); 113 } 114 } 115 116 private static UsageTracker newUsageTracker(Context context) { 117 return new UsageTracker(context, Prefs.Key.HOTSPOT_TILE_LAST_USED, HotspotTile.class, 118 R.integer.days_to_show_hotspot_tile); 119 } 120 121 private final class Callback implements HotspotController.Callback { 122 @Override 123 public void onHotspotChanged(boolean enabled) { 124 refreshState(enabled); 125 } 126 }; 127 128 /** 129 * This will catch broadcasts for changes in hotspot state so we can show 130 * the hotspot tile for a number of days after use. 131 */ 132 public static class APChangedReceiver extends BroadcastReceiver { 133 private UsageTracker mUsageTracker; 134 135 @Override 136 public void onReceive(Context context, Intent intent) { 137 if (mUsageTracker == null) { 138 mUsageTracker = newUsageTracker(context); 139 } 140 mUsageTracker.trackUsage(); 141 } 142 } 143 } 144