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.systemui.R; 24 import com.android.systemui.qs.UsageTracker; 25 import com.android.systemui.qs.QSTile; 26 import com.android.systemui.statusbar.policy.HotspotController; 27 import com.android.systemui.statusbar.policy.KeyguardMonitor; 28 29 /** Quick settings tile: Hotspot **/ 30 public class HotspotTile extends QSTile<QSTile.BooleanState> { 31 private final AnimationIcon mEnable = 32 new AnimationIcon(R.drawable.ic_hotspot_enable_animation); 33 private final AnimationIcon mDisable = 34 new AnimationIcon(R.drawable.ic_hotspot_disable_animation); 35 private final HotspotController mController; 36 private final Callback mCallback = new Callback(); 37 private final UsageTracker mUsageTracker; 38 private final KeyguardMonitor mKeyguard; 39 40 public HotspotTile(Host host) { 41 super(host); 42 mController = host.getHotspotController(); 43 mUsageTracker = newUsageTracker(host.getContext()); 44 mUsageTracker.setListening(true); 45 mKeyguard = host.getKeyguardMonitor(); 46 } 47 48 @Override 49 protected void handleDestroy() { 50 super.handleDestroy(); 51 mUsageTracker.setListening(false); 52 } 53 54 @Override 55 protected BooleanState newTileState() { 56 return new BooleanState(); 57 } 58 59 @Override 60 public void setListening(boolean listening) { 61 if (listening) { 62 mController.addCallback(mCallback); 63 } else { 64 mController.removeCallback(mCallback); 65 } 66 } 67 68 @Override 69 protected void handleClick() { 70 final boolean isEnabled = (Boolean) mState.value; 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 state.value = mController.isHotspotEnabled(); 95 state.icon = state.visible && state.value ? mEnable : mDisable; 96 } 97 98 @Override 99 protected String composeChangeAnnouncement() { 100 if (mState.value) { 101 return mContext.getString(R.string.accessibility_quick_settings_hotspot_changed_on); 102 } else { 103 return mContext.getString(R.string.accessibility_quick_settings_hotspot_changed_off); 104 } 105 } 106 107 private static UsageTracker newUsageTracker(Context context) { 108 return new UsageTracker(context, HotspotTile.class, R.integer.days_to_show_hotspot_tile); 109 } 110 111 private final class Callback implements HotspotController.Callback { 112 @Override 113 public void onHotspotChanged(boolean enabled) { 114 refreshState(); 115 } 116 }; 117 118 /** 119 * This will catch broadcasts for changes in hotspot state so we can show 120 * the hotspot tile for a number of days after use. 121 */ 122 public static class APChangedReceiver extends BroadcastReceiver { 123 private UsageTracker mUsageTracker; 124 125 @Override 126 public void onReceive(Context context, Intent intent) { 127 if (mUsageTracker == null) { 128 mUsageTracker = newUsageTracker(context); 129 } 130 mUsageTracker.trackUsage(); 131 } 132 } 133 } 134