From 034a6c9667b41506b0f6fa94617037717d7a9cd6 Mon Sep 17 00:00:00 2001 From: Adrian Roos Date: Tue, 5 Aug 2014 16:49:34 +0200 Subject: [PATCH] QS: Enable availability listener for flashlight Enables the availability listener now that the underlying framwork is stable. Bug: 16483222 Change-Id: I586a43941ade1ee8a8f3cb1a087e5356539703c1 --- .../systemui/qs/tiles/FlashlightTile.java | 32 +++++++++++++++++-- .../policy/FlashlightController.java | 12 +++---- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/FlashlightTile.java b/packages/SystemUI/src/com/android/systemui/qs/tiles/FlashlightTile.java index c46632db5ee37..f4ddd8424ff3f 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/tiles/FlashlightTile.java +++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/FlashlightTile.java @@ -24,13 +24,20 @@ import com.android.systemui.statusbar.policy.FlashlightController; import android.app.ActivityManager; import android.os.Handler; import android.os.Looper; +import android.os.SystemClock; import android.provider.Settings.Secure; +import android.util.Log; /** 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); @@ -63,12 +70,26 @@ public class FlashlightTile extends QSTile implements @Override protected void handleUpdateState(BooleanState state, Object arg) { + if (state.value) { + mWasLastOn = SystemClock.uptimeMillis(); + } + if (arg instanceof Boolean) { state.value = (Boolean) arg; } - // Always show the tile when the flashlight is on. This is needed because + + 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 = state.value || mFlashlightController.isAvailable(); + 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; @@ -88,4 +109,11 @@ public class FlashlightTile extends QSTile implements public void onFlashlightAvailabilityChanged(boolean available) { refreshState(); } + + private Runnable mRecentlyOnTimeout = new Runnable() { + @Override + public void run() { + refreshState(); + } + }; } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/FlashlightController.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/FlashlightController.java index 36cfb86d60e7b..70eaa5c6adfa1 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/FlashlightController.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/FlashlightController.java @@ -44,9 +44,6 @@ public class FlashlightController { private static final String TAG = "FlashlightController"; private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); - private static final boolean ENFORCE_AVAILABILITY_LISTENER = - SystemProperties.getBoolean("persist.sysui.flash.listener", false); - private static final int DISPATCH_ERROR = 0; private static final int DISPATCH_OFF = 1; private static final int DISPATCH_AVAILABILITY_CHANGED = 2; @@ -82,9 +79,10 @@ public class FlashlightController { return; } - ensureHandler(); - mCameraAvailable = true; - mCameraManager.addAvailabilityListener(mAvailabilityListener, mHandler); + if (mCameraId != null) { + ensureHandler(); + mCameraManager.addAvailabilityListener(mAvailabilityListener, mHandler); + } } public synchronized void setFlashlight(boolean enabled) { @@ -105,7 +103,7 @@ public class FlashlightController { } public synchronized boolean isAvailable() { - return ENFORCE_AVAILABILITY_LISTENER ? mCameraAvailable : (mCameraId != null); + return mCameraAvailable; } public void addListener(FlashlightListener l) {