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

Commit 810d240d authored by Harry Youd's avatar Harry Youd Committed by Bruno Martins
Browse files

Recorder: Add notification channels

Apps targeting API 26 (Android O), must send notifications through
"channels".

Create a channel in both sound and screen recording services, each one
aggregating the following notifications:
 - persistent notification(s) - eg. "xxx is being recorded"
 - recording completed notifications

Also create a channel for screen recording overlay, when it's ready
to start.

This way, a user can turn off particular channels if they don't want
certain types of notifications from Recorder.

Change-Id: I35301f8c300d05f6ba15ab769878a2ac4d4924e7
parent 6039d8d7
Loading
Loading
Loading
Loading
+22 −1
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@
package org.lineageos.recorder.screen;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
@@ -28,6 +30,10 @@ import org.lineageos.recorder.ui.OverlayLayer;
import org.lineageos.recorder.utils.Utils;

public class OverlayService extends Service {

    private static final String SCREENCAST_OVERLAY_NOTIFICATION_CHANNEL =
            "screencast_overlay_notification_channel";

    public static final String EXTRA_HAS_AUDIO = "extra_audio";
    private final static int FG_ID = 123;

@@ -46,7 +52,8 @@ public class OverlayService extends Service {
            onDestroy();
        });

        Notification notification = new NotificationCompat.Builder(this)
        Notification notification = new NotificationCompat.Builder(
                this, SCREENCAST_OVERLAY_NOTIFICATION_CHANNEL)
                .setContentTitle(getString(R.string.screen_overlay_notif_title))
                .setContentText(getString(R.string.screen_overlay_notif_message))
                .setSmallIcon(R.drawable.ic_action_screen_record)
@@ -63,6 +70,20 @@ public class OverlayService extends Service {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        CharSequence name = getString(R.string.screen_overlay_channel_title);
        String description = getString(R.string.screen_overlay_channel_desc);
        NotificationChannel notificationChannel =
                new NotificationChannel(SCREENCAST_OVERLAY_NOTIFICATION_CHANNEL,
                        name, NotificationManager.IMPORTANCE_LOW);
        notificationChannel.setDescription(description);
        notificationManager.createNotificationChannel(notificationChannel);
    }

    @Override
    public void onDestroy() {
        if (mLayer != null) {
+15 −4
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@
 */
package org.lineageos.recorder.screen;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
@@ -47,6 +48,10 @@ import java.util.Timer;
import java.util.TimerTask;

public class ScreencastService extends Service {

    private static final String SCREENCAST_NOTIFICATION_CHANNEL =
            "screencast_notification_channel";

    public static final String EXTRA_WITHAUDIO = "withaudio";
    public static final String ACTION_START_SCREENCAST =
            "org.lineageos.recorder.screen.ACTION_START_SCREENCAST";
@@ -80,8 +85,14 @@ public class ScreencastService extends Service {
    public void onCreate() {
        super.onCreate();

        mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager = getSystemService(NotificationManager.class);
        CharSequence name = getString(R.string.screen_channel_title);
        String description = getString(R.string.screen_channel_desc);
        NotificationChannel notificationChannel =
                new NotificationChannel(SCREENCAST_NOTIFICATION_CHANNEL,
                        name, NotificationManager.IMPORTANCE_LOW);
        notificationChannel.setDescription(description);
        mNotificationManager.createNotificationChannel(notificationChannel);

        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_USER_BACKGROUND);
@@ -210,7 +221,7 @@ public class ScreencastService extends Service {
        Intent stopRecordingIntent = new Intent(ACTION_STOP_SCREENCAST);
        stopRecordingIntent.setClass(this, ScreencastService.class);

        return new NotificationCompat.Builder(this)
        return new NotificationCompat.Builder(this, SCREENCAST_NOTIFICATION_CHANNEL)
                .setOngoing(true)
                .setSmallIcon(R.drawable.ic_action_screen_record)
                .setContentTitle(getString(R.string.screen_notification_title))
@@ -244,7 +255,7 @@ public class ScreencastService extends Service {

        Log.i(LOGTAG, "Video complete: " + file);

        return new NotificationCompat.Builder(this)
        return new NotificationCompat.Builder(this, SCREENCAST_NOTIFICATION_CHANNEL)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.ic_action_screen_record)
                .setContentTitle(getString(R.string.screen_notification_message_done))
+22 −10
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@
package org.lineageos.recorder.sounds;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
@@ -55,6 +56,9 @@ public class SoundRecorderService extends Service {
    private static final String ACTION_STOPPED = "org.lineageos.recorder.sounds.STOPPED_SOUND";
    private static final String EXTRA_FILE = "extra_filename";

    private static final String SOUNDRECORDER_NOTIFICATION_CHANNEL =
            "soundrecorder_notification_channel";

    private static final String TAG = "SoundRecorderService";
    private static final File RECORDINGS_DIR =
            new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC),
@@ -77,6 +81,7 @@ public class SoundRecorderService extends Service {
    private Thread mVisualizerThread;
    private byte[] mData;
    private RecorderStatus mStatus = RecorderStatus.STOPPED;
    private NotificationManager mNotificationManager;
    private final BroadcastReceiver mShutdownReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
@@ -105,6 +110,16 @@ public class SoundRecorderService extends Service {
    @Override
    public void onCreate() {
        super.onCreate();

        mNotificationManager = getSystemService(NotificationManager.class);
        CharSequence name = getString(R.string.sound_channel_title);
        String description = getString(R.string.sound_channel_desc);
        NotificationChannel notificationChannel =
                new NotificationChannel(SOUNDRECORDER_NOTIFICATION_CHANNEL,
                        name, NotificationManager.IMPORTANCE_LOW);
        notificationChannel.setDescription(description);
        mNotificationManager.createNotificationChannel(notificationChannel);

        registerReceiver(mShutdownReceiver, new IntentFilter(Intent.ACTION_SHUTDOWN));
    }

@@ -254,11 +269,8 @@ public class SoundRecorderService extends Service {

    private void startTimer() {
        Timer timer = new Timer();
        mTimerListener = (seconds -> {
            NotificationManager nm = (NotificationManager)
                    getSystemService(Context.NOTIFICATION_SERVICE);
            nm.notify(NOTIFICATION_ID, createRecordingNotification());
        });
        mTimerListener = (seconds ->
                mNotificationManager.notify(NOTIFICATION_ID, createRecordingNotification()));

        mTask = new TimerTask() {
            @Override
@@ -276,7 +288,8 @@ public class SoundRecorderService extends Service {
        Intent intent = new Intent(this, RecorderActivity.class);
        PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
        NotificationCompat.Builder builder = new NotificationCompat.Builder(
                this, SOUNDRECORDER_NOTIFICATION_CHANNEL)
                .setContentTitle(getString(R.string.sound_notification_title))
                .setContentText(getString(R.string.sound_notification_message,
                        DateUtils.formatElapsedTime(mElapsedTime / 1000)))
@@ -304,7 +317,8 @@ public class SoundRecorderService extends Service {

        LastRecordHelper.setLastItem(this, mOutFilePath, mElapsedTime, true);

        Notification notification = new NotificationCompat.Builder(this)
        Notification notification = new NotificationCompat.Builder(
                this, SOUNDRECORDER_NOTIFICATION_CHANNEL)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.ic_action_sound_record)
                .setContentTitle(getString(R.string.sound_notification_title))
@@ -316,9 +330,7 @@ public class SoundRecorderService extends Service {
                .setContentIntent(pi)
                .build();

        NotificationManager nm = (NotificationManager)
                getSystemService(Context.NOTIFICATION_SERVICE);
        nm.notify(NOTIFICATION_ID, notification);
        mNotificationManager.notify(NOTIFICATION_ID, notification);
    }

    public enum RecorderStatus {
+12 −0
Original line number Diff line number Diff line
@@ -45,6 +45,10 @@
    <string name="sound_recording_title_working">Recording</string>
    <!-- Sound recorder last item title -->
    <string name="sound_last_title">Last record</string>
    <!-- Sound recorder notification channel title -->
    <string name="sound_channel_title">Sound recording</string>
    <!-- Sound recorder notification channel description-->
    <string name="sound_channel_desc">Persistent notification when recording sound and notification after it is complete</string>

    <!-- Screen recorder -->
    <!-- Screen recorder enabled audio message -->
@@ -77,6 +81,14 @@
    <string name="screen_overlay_notif_message">Press the record button when you\'re ready to start</string>
    <!-- Screen settings dialog title -->
    <string name="screen_settings_title">Screen recorder settings</string>
    <!-- Screen recorder notification overlay channel title -->
    <string name="screen_overlay_channel_title">Ready to record screen</string>
    <!-- Screen recorder notification overlay channel description-->
    <string name="screen_overlay_channel_desc">Notification showing screen recording is ready to start</string>
    <!-- Screen recorder persistent notification channel title -->
    <string name="screen_channel_title">Screen recording</string>
    <!-- Screen recorder persistent notification channel description-->
    <string name="screen_channel_desc">Persistent notification when recording the screen and notification after it is complete</string>

    <!-- Permissions -->
    <!-- Permissions dialog title -->