HotspotTile.java revision a2bf987b742b328f56e71eab32a9fa37ec1961ca
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 17package com.android.systemui.qs.tiles; 18 19import android.content.BroadcastReceiver; 20import android.content.Context; 21import android.content.Intent; 22 23import com.android.systemui.R; 24import com.android.systemui.qs.UsageTracker; 25import com.android.systemui.qs.QSTile; 26import com.android.systemui.statusbar.policy.HotspotController; 27import com.android.systemui.statusbar.policy.KeyguardMonitor; 28 29/** Quick settings tile: Hotspot **/ 30public 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 && !(mController.isProvisioningNeeded() && mKeyguard.isSecure() 93 && mKeyguard.isShowing()); 94 state.label = mContext.getString(R.string.quick_settings_hotspot_label); 95 96 state.value = mController.isHotspotEnabled(); 97 state.icon = state.visible && state.value ? mEnable : mDisable; 98 } 99 100 @Override 101 protected String composeChangeAnnouncement() { 102 if (mState.value) { 103 return mContext.getString(R.string.accessibility_quick_settings_hotspot_changed_on); 104 } else { 105 return mContext.getString(R.string.accessibility_quick_settings_hotspot_changed_off); 106 } 107 } 108 109 private static UsageTracker newUsageTracker(Context context) { 110 return new UsageTracker(context, HotspotTile.class, R.integer.days_to_show_hotspot_tile); 111 } 112 113 private final class Callback implements HotspotController.Callback { 114 @Override 115 public void onHotspotChanged(boolean enabled) { 116 refreshState(); 117 } 118 }; 119 120 /** 121 * This will catch broadcasts for changes in hotspot state so we can show 122 * the hotspot tile for a number of days after use. 123 */ 124 public static class APChangedReceiver extends BroadcastReceiver { 125 private UsageTracker mUsageTracker; 126 127 @Override 128 public void onReceive(Context context, Intent intent) { 129 if (mUsageTracker == null) { 130 mUsageTracker = newUsageTracker(context); 131 } 132 mUsageTracker.trackUsage(); 133 } 134 } 135} 136