-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCrocsonService.java
More file actions
98 lines (88 loc) · 3.34 KB
/
Copy pathCrocsonService.java
File metadata and controls
98 lines (88 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.github.abakum.crocson;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ServiceInfo;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
public class CrocsonService extends Service {
private static final String TAG = "croc";
private static final String CHANNEL_ID = "crocson_fg";
private static final int NOTIFICATION_ID = 100;
@Override
public void onCreate() {
super.onCreate();
createNotificationChannel();
Log.d(TAG, "CrocsonService onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "CrocsonService onStartCommand");
Intent launchIntent = getPackageManager()
.getLaunchIntentForPackage(getPackageName());
PendingIntent pendingIntent = null;
if (launchIntent != null) {
int pendingFlags = PendingIntent.FLAG_UPDATE_CURRENT;
if (Build.VERSION.SDK_INT >= 23) {
pendingFlags |= PendingIntent.FLAG_IMMUTABLE;
}
pendingIntent = PendingIntent.getActivity(this, 0, launchIntent, pendingFlags);
}
Notification notification;
if (Build.VERSION.SDK_INT >= 26) {
notification = new Notification.Builder(this, CHANNEL_ID)
.setContentTitle("crocson")
.setContentText("WebDAV server running")
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentIntent(pendingIntent)
.setOngoing(true)
.build();
} else {
notification = new Notification.Builder(this)
.setContentTitle("crocson")
.setContentText("WebDAV server running")
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentIntent(pendingIntent)
.setOngoing(true)
.build();
}
if (Build.VERSION.SDK_INT >= 34) {
startForeground(NOTIFICATION_ID, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK);
} else {
startForeground(NOTIFICATION_ID, notification);
}
Log.d(TAG, "CrocsonService startForeground called");
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "CrocsonService onDestroy");
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= 26) {
NotificationChannel channel = new NotificationChannel(
CHANNEL_ID,
"Crocson Foreground Service",
NotificationManager.IMPORTANCE_LOW
);
channel.setDescription("WebDAV server foreground service");
channel.setSound(null, null);
NotificationManager manager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (manager != null) {
manager.createNotificationChannel(channel);
}
}
}
}