/* * Copyright (C) 2014 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License */ package com.android.systemui.qs.tiles; import android.app.ActivityManager; import android.os.SystemClock; import com.android.systemui.R; import com.android.systemui.qs.QSTile; import com.android.systemui.statusbar.policy.FlashlightController; /** Quick settings tile: Control flashlight **/ public class FlashlightTile extends QSTile implements FlashlightController.FlashlightListener { /** Grace period for which we consider the flashlight * still available because it was recently on. */ private static final long RECENTLY_ON_DURATION_MILLIS = 500; private final FlashlightController mFlashlightController; private long mWasLastOn; public FlashlightTile(Host host) { super(host); mFlashlightController = host.getFlashlightController(); mFlashlightController.addListener(this); } @Override protected void handleDestroy() { super.handleDestroy(); mFlashlightController.removeListener(this); } @Override protected BooleanState newTileState() { return new BooleanState(); } @Override public void setListening(boolean listening) { } @Override protected void handleUserSwitch(int newUserId) { } @Override protected void handleClick() { if (ActivityManager.isUserAMonkey()) { return; } boolean newState = !mState.value; mFlashlightController.setFlashlight(newState); refreshState(newState); } @Override protected void handleUpdateState(BooleanState state, Object arg) { if (state.value) { mWasLastOn = SystemClock.uptimeMillis(); } if (arg instanceof Boolean) { state.value = (Boolean) arg; } if (!state.value && mWasLastOn != 0) { if (SystemClock.uptimeMillis() > mWasLastOn + RECENTLY_ON_DURATION_MILLIS) { mWasLastOn = 0; } else { mHandler.removeCallbacks(mRecentlyOnTimeout); mHandler.postAtTime(mRecentlyOnTimeout, mWasLastOn + RECENTLY_ON_DURATION_MILLIS); } } // Always show the tile when the flashlight is or was recently on. This is needed because // the camera is not available while it is being used for the flashlight. state.visible = mWasLastOn != 0 || mFlashlightController.isAvailable(); state.label = mHost.getContext().getString(R.string.quick_settings_flashlight_label); state.iconId = state.value ? R.drawable.ic_qs_flashlight_on : R.drawable.ic_qs_flashlight_off; int onOrOffId = state.value ? R.string.accessibility_quick_settings_flashlight_on : R.string.accessibility_quick_settings_flashlight_off; state.contentDescription = mContext.getString(onOrOffId); } @Override protected String composeChangeAnnouncement() { if (mState.value) { return mContext.getString(R.string.accessibility_quick_settings_flashlight_changed_on); } else { return mContext.getString(R.string.accessibility_quick_settings_flashlight_changed_off); } } @Override public void onFlashlightOff() { refreshState(false); } @Override public void onFlashlightError() { refreshState(false); } @Override public void onFlashlightAvailabilityChanged(boolean available) { refreshState(); } private Runnable mRecentlyOnTimeout = new Runnable() { @Override public void run() { refreshState(); } }; }