Merge "QS: Enable availability listener for flashlight" into lmp-dev

This commit is contained in:
Adrian Roos
2014-08-05 15:06:17 +00:00
committed by Android (Google) Code Review
2 changed files with 35 additions and 9 deletions

View File

@@ -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<QSTile.BooleanState> 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<QSTile.BooleanState> 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<QSTile.BooleanState> implements
public void onFlashlightAvailabilityChanged(boolean available) {
refreshState();
}
private Runnable mRecentlyOnTimeout = new Runnable() {
@Override
public void run() {
refreshState();
}
};
}

View File

@@ -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) {