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

Commit 8b7fa883 authored by Roman Birg's avatar Roman Birg
Browse files

SystemUI: add flashlight notification reminder



CM13 port of change-id I3689ff6498b97b813ccc10dc7dca3527fc8455aa.

We now only post a notification when the user initiated the flashlight
via the QS tiles (or "V" gesture) since there is a common API for flashlight apps to hook
into (and possibly place their own notification, which we don't want to
ever double up on).

Ref: CYNGNOS-1670

Change-Id: Id532e8449932fefb216c10798aab6241f6e491ea
Signed-off-by: default avatarRoman Birg <roman@cyngn.com>
parent 6542c71b
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -241,4 +241,8 @@

    <!-- Hotspot dialog message -->
    <string name="hotspot_apm_message">Unable to connect to mobile networks while Airplane mode is enabled. Disable Airplane mode and try again.</string>

    <!-- Notification which notifies user flashlight is enabled -->
    <string name="quick_settings_tile_flashlight_not_title">Flashlight is on</string>
    <string name="quick_settings_tile_flashlight_not_summary">Tap to turn off</string>
</resources>
+83 −0
Original line number Diff line number Diff line
@@ -16,7 +16,13 @@

package com.android.systemui.statusbar.policy;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraManager;
@@ -26,6 +32,8 @@ import android.os.Process;
import android.text.TextUtils;
import android.util.Log;

import com.android.systemui.R;

import java.lang.ref.WeakReference;
import java.util.ArrayList;

@@ -41,6 +49,10 @@ public class FlashlightController {
    private static final int DISPATCH_CHANGED = 1;
    private static final int DISPATCH_AVAILABILITY_CHANGED = 2;

    private static final String ACTION_TURN_FLASHLIGHT_OFF =
            "com.android.systemui.action.TURN_FLASHLIGHT_OFF";

    private Context mContext;
    private final CameraManager mCameraManager;
    /** Call {@link #ensureHandler()} before using */
    private Handler mHandler;
@@ -54,7 +66,26 @@ public class FlashlightController {
    private final String mCameraId;
    private boolean mTorchAvailable;

    private Notification mNotification = null;
    private boolean mReceiverRegistered;
    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (ACTION_TURN_FLASHLIGHT_OFF.equals(intent.getAction())) {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        setFlashlight(false);
                    }
                });
            } else if (Intent.ACTION_SCREEN_ON.equals(intent.getAction())) {
                setNotificationShown(true);
            }
        }
    };

    public FlashlightController(Context mContext) {
        this.mContext = mContext;
        mCameraManager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE);

        String cameraId = null;
@@ -93,6 +124,56 @@ public class FlashlightController {
        }
    }

    private void setNotificationShown(boolean showNotification) {
        NotificationManager nm = (NotificationManager)
                mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        if (showNotification) {
            nm.notify(R.string.quick_settings_tile_flashlight_not_title, buildNotification());
        } else {
            nm.cancel(R.string.quick_settings_tile_flashlight_not_title);
            mNotification = null;
        }
    }

    private void setListenForScreenOff(boolean listen) {
        if (listen && !mReceiverRegistered) {
            IntentFilter filter = new IntentFilter();
            filter.addAction(ACTION_TURN_FLASHLIGHT_OFF);
            filter.addAction(Intent.ACTION_SCREEN_ON);
            mContext.registerReceiver(mReceiver, filter);
            mReceiverRegistered = true;
        } else if (!listen) {
            if (mReceiverRegistered) {
                mContext.unregisterReceiver(mReceiver);
                mReceiverRegistered = false;
            }
            setNotificationShown(false);
        }
    }

    private Notification buildNotification() {
        if (mNotification == null) {
            Intent fireMe = new Intent(ACTION_TURN_FLASHLIGHT_OFF);
            fireMe.addFlags(Intent.FLAG_FROM_BACKGROUND);
            fireMe.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
            fireMe.setPackage(mContext.getPackageName());

            final PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, fireMe, 0);
            mNotification = new Notification.Builder(mContext)
                    .setContentTitle(
                            mContext.getString(R.string.quick_settings_tile_flashlight_not_title))
                    .setContentText(
                            mContext.getString(R.string.quick_settings_tile_flashlight_not_summary))
                    .setAutoCancel(false)
                    .setOngoing(true)
                    .setVisibility(Notification.VISIBILITY_PUBLIC)
                    .setSmallIcon(R.drawable.ic_signal_flashlight_disable)
                    .setContentIntent(pendingIntent)
                    .build();
        }
        return mNotification;
    }

    public synchronized boolean isEnabled() {
        return mFlashlightEnabled;
    }
@@ -188,6 +269,7 @@ public class FlashlightController {
        public void onTorchModeUnavailable(String cameraId) {
            if (TextUtils.equals(cameraId, mCameraId)) {
                setCameraAvailable(false);
                setListenForScreenOff(false);
            }
        }

@@ -196,6 +278,7 @@ public class FlashlightController {
            if (TextUtils.equals(cameraId, mCameraId)) {
                setCameraAvailable(true);
                setTorchMode(enabled);
                setListenForScreenOff(enabled);
            }
        }