Skip to content

Commit 9b0031c

Browse files
committed
Use notification channel on Oreo
1 parent 1b5fede commit 9b0031c

File tree

1 file changed

+63
-19
lines changed

1 file changed

+63
-19
lines changed

app/src/main/java/net/yrom/screenrecorder/Notifications.java

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,57 +16,101 @@
1616

1717
package net.yrom.screenrecorder;
1818

19+
import android.annotation.TargetApi;
1920
import android.app.Notification;
21+
import android.app.NotificationChannel;
2022
import android.app.NotificationManager;
2123
import android.app.PendingIntent;
2224
import android.content.Context;
25+
import android.content.ContextWrapper;
2326
import android.content.Intent;
27+
import android.os.Build;
2428
import android.os.SystemClock;
2529
import android.text.format.DateUtils;
2630

31+
import static android.os.Build.VERSION_CODES.O;
2732
import static net.yrom.screenrecorder.MainActivity.ACTION_STOP;
2833

2934
/**
3035
* @author yrom
3136
* @version 2017/12/1
3237
*/
33-
class Notifications {
34-
private final Context mContext;
38+
class Notifications extends ContextWrapper {
3539
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+
3643
private long mLastFiredTime = 0;
44+
private NotificationManager mManager;
45+
private Notification.Action mStopAction;
46+
private Notification.Builder mBuilder;
47+
3748
Notifications(Context context) {
38-
this.mContext = context;
49+
super(context);
50+
if (Build.VERSION.SDK_INT >= O) {
51+
createNotificationChannel();
52+
}
3953
}
4054

4155
public void recording(long timeMs) {
4256
if (SystemClock.elapsedRealtime() - mLastFiredTime < 1000) {
4357
return;
4458
}
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()
5160
.setContentText("Length: " + DateUtils.formatElapsedTime(timeMs / 1000))
52-
.addAction(stopAction())
53-
.setWhen(System.currentTimeMillis())
54-
.setSmallIcon(R.drawable.ic_stat_recording)
5561
.build();
56-
nMan.notify(id, notification);
62+
getNotificationManager().notify(id, notification);
5763
mLastFiredTime = SystemClock.elapsedRealtime();
5864
}
5965

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+
6093
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;
64101
}
65102

66103
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);
70113
}
114+
return mManager;
71115
}
72116
}

0 commit comments

Comments
 (0)