Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 034a6c96 authored by Adrian Roos's avatar Adrian Roos
Browse files

QS: Enable availability listener for flashlight

Enables the availability listener now that the
underlying framwork is stable.

Bug: 16483222
Change-Id: I586a43941ade1ee8a8f3cb1a087e5356539703c1
parent 05204dc2
Loading
Loading
Loading
Loading
+30 −2
Original line number Diff line number Diff line
@@ -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();
        }
    };
}
+5 −7
Original line number Diff line number Diff line
@@ -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,10 +79,11 @@ public class FlashlightController {
            return;
        }

        if (mCameraId != null) {
            ensureHandler();
        mCameraAvailable = true;
            mCameraManager.addAvailabilityListener(mAvailabilityListener, mHandler);
        }
    }

    public synchronized void setFlashlight(boolean enabled) {
        if (mFlashlightEnabled != 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) {