-
Notifications
You must be signed in to change notification settings - Fork 37
/
NotificationService.java
137 lines (119 loc) · 5.47 KB
/
NotificationService.java
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* AsteroidOSSync
* Copyright (c) 2023 AsteroidOS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.asteroidos.sync.connectivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import org.asteroidos.sync.NotificationPreferences;
import org.asteroidos.sync.asteroid.IAsteroidDevice;
import org.asteroidos.sync.dataobjects.Notification;
import org.asteroidos.sync.utils.AsteroidUUIDS;
import java.util.HashMap;
import java.util.Objects;
import java.util.UUID;
public class NotificationService implements IConnectivityService {
public static final String TAG = NotificationService.class.toString();
private final Context mCtx;
private final IAsteroidDevice mDevice;
private NotificationReceiver mNReceiver;
public NotificationService(Context ctx, IAsteroidDevice device) {
this.mDevice = device;
this.mCtx = ctx;
}
@Override
public void sync() {
if (mNReceiver == null) {
IntentFilter filter = new IntentFilter();
filter.addAction("org.asteroidos.sync.NOTIFICATION_LISTENER");
mNReceiver = new NotificationReceiver();
mCtx.registerReceiver(mNReceiver, filter);
Intent i = new Intent("org.asteroidos.sync.NOTIFICATION_LISTENER_SERVICE");
i.putExtra("command", "refresh");
mCtx.sendBroadcast(i);
}
}
@Override
public void unsync() {
if (mNReceiver != null) {
try {
mCtx.unregisterReceiver(mNReceiver);
} catch (IllegalArgumentException ignored) {
}
mNReceiver = null;
}
}
@Override
public final HashMap<UUID, Direction> getCharacteristicUUIDs() {
HashMap<UUID, Direction> chars = new HashMap<>();
chars.put(AsteroidUUIDS.NOTIFICATION_UPDATE_CHAR, Direction.TO_WATCH);
chars.put(AsteroidUUIDS.NOTIFICATION_FEEDBACK_CHAR, Direction.FROM_WATCH);
return chars;
}
@Override
public final UUID getServiceUUID() {
return AsteroidUUIDS.NOTIFICATION_SERVICE_UUID;
}
class NotificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String event = intent.getStringExtra("event");
if (Objects.equals(event, "posted")) {
String packageName = intent.getStringExtra("packageName");
NotificationPreferences.putPackageToSeen(context, packageName);
NotificationPreferences.NotificationOption notificationOption =
NotificationPreferences.getNotificationPreferenceForApp(context, packageName);
if (notificationOption == NotificationPreferences.NotificationOption.NO_NOTIFICATIONS)
return;
int id = intent.getIntExtra("id", 0);
String appName = intent.getStringExtra("appName");
String appIcon = intent.getStringExtra("appIcon");
String summary = intent.getStringExtra("summary");
String body = intent.getStringExtra("body");
String vibration;
if (notificationOption == NotificationPreferences.NotificationOption.SILENT_NOTIFICATION)
vibration = "none";
else if (notificationOption == null
|| notificationOption == NotificationPreferences.NotificationOption.NORMAL_VIBRATION
|| notificationOption == NotificationPreferences.NotificationOption.DEFAULT)
vibration = "normal";
else if (notificationOption == NotificationPreferences.NotificationOption.STRONG_VIBRATION)
vibration = "strong";
else if(notificationOption == NotificationPreferences.NotificationOption.RINGTONE_VIBRATION)
vibration = "ringtone";
else
throw new IllegalArgumentException("Not all options handled");
if(intent.hasExtra("vibration"))
vibration = intent.getStringExtra("vibration");
Notification notification = new Notification(
Notification.MsgType.POSTED,
packageName,
id,
appName,
appIcon,
summary,
body,
vibration);
mDevice.send(AsteroidUUIDS.NOTIFICATION_UPDATE_CHAR, notification.toBytes(), NotificationService.this);
} else if (Objects.equals(event, "removed")) {
int id = intent.getIntExtra("id", 0);
mDevice.send(AsteroidUUIDS.NOTIFICATION_UPDATE_CHAR, new Notification(Notification.MsgType.REMOVED, id).toBytes(), NotificationService.this);
}
}
}
}