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