|
16 | 16 |
|
17 | 17 | package net.yrom.screenrecorder;
|
18 | 18 |
|
| 19 | +import android.annotation.TargetApi; |
19 | 20 | import android.app.Notification;
|
| 21 | +import android.app.NotificationChannel; |
20 | 22 | import android.app.NotificationManager;
|
21 | 23 | import android.app.PendingIntent;
|
22 | 24 | import android.content.Context;
|
| 25 | +import android.content.ContextWrapper; |
23 | 26 | import android.content.Intent;
|
| 27 | +import android.os.Build; |
24 | 28 | import android.os.SystemClock;
|
25 | 29 | import android.text.format.DateUtils;
|
26 | 30 |
|
| 31 | +import static android.os.Build.VERSION_CODES.O; |
27 | 32 | import static net.yrom.screenrecorder.MainActivity.ACTION_STOP;
|
28 | 33 |
|
29 | 34 | /**
|
30 | 35 | * @author yrom
|
31 | 36 | * @version 2017/12/1
|
32 | 37 | */
|
33 |
| -class Notifications { |
34 |
| - private final Context mContext; |
| 38 | +class Notifications extends ContextWrapper { |
35 | 39 | private static final int id = 0x1fff;
|
| 40 | + private static final String CHANNEL_ID = "Recording"; |
| 41 | + private static final String CHANNEL_NAME = "Screen Recorder Notifications"; |
| 42 | + |
36 | 43 | private long mLastFiredTime = 0;
|
| 44 | + private NotificationManager mManager; |
| 45 | + private Notification.Action mStopAction; |
| 46 | + private Notification.Builder mBuilder; |
| 47 | + |
37 | 48 | Notifications(Context context) {
|
38 |
| - this.mContext = context; |
| 49 | + super(context); |
| 50 | + if (Build.VERSION.SDK_INT >= O) { |
| 51 | + createNotificationChannel(); |
| 52 | + } |
39 | 53 | }
|
40 | 54 |
|
41 | 55 | public void recording(long timeMs) {
|
42 | 56 | if (SystemClock.elapsedRealtime() - mLastFiredTime < 1000) {
|
43 | 57 | return;
|
44 | 58 | }
|
45 |
| - NotificationManager nMan = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
46 |
| - if (nMan == null) return; |
47 |
| - Notification notification = new Notification.Builder(mContext) |
48 |
| - .setOngoing(true) |
49 |
| - .setTicker("Recording...") |
50 |
| - .setContentTitle("Recording...") |
| 59 | + Notification notification = getBuilder() |
51 | 60 | .setContentText("Length: " + DateUtils.formatElapsedTime(timeMs / 1000))
|
52 |
| - .addAction(stopAction()) |
53 |
| - .setWhen(System.currentTimeMillis()) |
54 |
| - .setSmallIcon(R.drawable.ic_stat_recording) |
55 | 61 | .build();
|
56 |
| - nMan.notify(id, notification); |
| 62 | + getNotificationManager().notify(id, notification); |
57 | 63 | mLastFiredTime = SystemClock.elapsedRealtime();
|
58 | 64 | }
|
59 | 65 |
|
| 66 | + private Notification.Builder getBuilder() { |
| 67 | + if (mBuilder == null) { |
| 68 | + Notification.Builder builder = new Notification.Builder(this) |
| 69 | + .setContentTitle("Recording...") |
| 70 | + .setOngoing(true) |
| 71 | + .setLocalOnly(true) |
| 72 | + .setOnlyAlertOnce(true) |
| 73 | + .addAction(stopAction()) |
| 74 | + .setWhen(System.currentTimeMillis()) |
| 75 | + .setSmallIcon(R.drawable.ic_stat_recording); |
| 76 | + if (Build.VERSION.SDK_INT >= O) { |
| 77 | + builder.setChannelId(CHANNEL_ID) |
| 78 | + .setUsesChronometer(true); |
| 79 | + } |
| 80 | + mBuilder = builder; |
| 81 | + } |
| 82 | + return mBuilder; |
| 83 | + } |
| 84 | + |
| 85 | + @TargetApi(O) |
| 86 | + private void createNotificationChannel() { |
| 87 | + NotificationChannel channel = |
| 88 | + new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT); |
| 89 | + channel.setShowBadge(false); |
| 90 | + getNotificationManager().createNotificationChannel(channel); |
| 91 | + } |
| 92 | + |
60 | 93 | private Notification.Action stopAction() {
|
61 |
| - Intent intent = new Intent(ACTION_STOP).setPackage(mContext.getPackageName()); |
62 |
| - PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 1, intent, PendingIntent.FLAG_ONE_SHOT); |
63 |
| - return new Notification.Action(android.R.drawable.ic_media_pause, "Stop", pendingIntent); |
| 94 | + if (mStopAction == null) { |
| 95 | + Intent intent = new Intent(ACTION_STOP).setPackage(getPackageName()); |
| 96 | + PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, |
| 97 | + intent, PendingIntent.FLAG_ONE_SHOT); |
| 98 | + mStopAction = new Notification.Action(android.R.drawable.ic_media_pause, "Stop", pendingIntent); |
| 99 | + } |
| 100 | + return mStopAction; |
64 | 101 | }
|
65 | 102 |
|
66 | 103 | void clear() {
|
67 |
| - NotificationManager nMan = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
68 |
| - if (nMan != null) { |
69 |
| - nMan.cancelAll(); |
| 104 | + mLastFiredTime = 0; |
| 105 | + mBuilder = null; |
| 106 | + mStopAction = null; |
| 107 | + getNotificationManager().cancelAll(); |
| 108 | + } |
| 109 | + |
| 110 | + NotificationManager getNotificationManager() { |
| 111 | + if (mManager == null) { |
| 112 | + mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); |
70 | 113 | }
|
| 114 | + return mManager; |
71 | 115 | }
|
72 | 116 | }
|
0 commit comments