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

Unverified Commit affb6a34 authored by Roman Birg's avatar Roman Birg Committed by Michael Bestas
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 b4c599b9
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -28,4 +28,8 @@

    <!-- Content description of the superuser tile -->
    <string name="accessibility_su_active">Superuser session active</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>
+81 −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.content.pm.PackageManager;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCharacteristics;
@@ -27,6 +33,8 @@ import android.os.Process;
import android.text.TextUtils;
import android.util.Log;

import com.android.systemui.R;

import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.lang.ref.WeakReference;
@@ -44,6 +52,9 @@ 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 final CameraManager mCameraManager;
    private final Context mContext;
    /** Call {@link #ensureHandler()} before using */
@@ -58,6 +69,24 @@ public class FlashlightController {
    private 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 context) {
        mContext = context;
        mCameraManager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE);
@@ -100,6 +129,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 boolean hasFlashlight() {
        return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
    }
@@ -202,6 +281,7 @@ public class FlashlightController {
        public void onTorchModeUnavailable(String cameraId) {
            if (TextUtils.equals(cameraId, mCameraId)) {
                setCameraAvailable(false);
                setListenForScreenOff(false);
            }
        }

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