30
30
31
31
import com .radio .codec2talkie .MainActivity ;
32
32
import com .radio .codec2talkie .R ;
33
+ import com .radio .codec2talkie .protocol .message .TextMessage ;
33
34
import com .radio .codec2talkie .settings .PreferenceKeys ;
35
+ import com .radio .codec2talkie .storage .message .MessageItemActivity ;
34
36
import com .radio .codec2talkie .tracker .Tracker ;
35
37
import com .radio .codec2talkie .tracker .TrackerFactory ;
36
38
import com .radio .codec2talkie .transport .TransportFactory ;
@@ -46,12 +48,13 @@ public class AppService extends Service {
46
48
47
49
private final int SERVICE_NOTIFICATION_ID = 1 ;
48
50
private final int VOICE_NOTIFICATION_ID = 2 ;
51
+ private final int MESSAGE_NOTIFICATION_ID = 3 ;
49
52
50
53
private AppWorker _appWorker ;
51
54
private Messenger _callbackMessenger ;
52
55
private Tracker _tracker ;
53
56
private NotificationManager _notificationManager ;
54
- PowerManager .WakeLock _serviceWakeLock ;
57
+ private PowerManager .WakeLock _serviceWakeLock ;
55
58
56
59
private boolean _voiceNotificationsEnabled = false ;
57
60
@@ -99,6 +102,11 @@ public void stopTracking() {
99
102
_onProcess .sendMessage (msg );
100
103
}
101
104
105
+ public void sendTextMessage (TextMessage textMessage ) {
106
+ if (_appWorker != null )
107
+ _appWorker .sendTextMessage (textMessage );
108
+ }
109
+
102
110
public boolean isTracking () {
103
111
if (_tracker == null ) return false ;
104
112
return _tracker .isTracking ();
@@ -155,7 +163,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
155
163
transportType = (TransportFactory .TransportType ) extras .get ("transportType" );
156
164
startAppWorker (transportType );
157
165
158
- Notification notification = buildNotification (getString (R .string .app_service_notif_text_ptt_ready ), R .drawable .ic_app_action );
166
+ Notification notification = buildServiceNotification (getString (R .string .app_service_notif_text_ptt_ready ), R .drawable .ic_app_action );
159
167
startForeground (SERVICE_NOTIFICATION_ID , notification );
160
168
PowerManager powerManager = (PowerManager ) getSystemService (POWER_SERVICE );
161
169
@@ -214,6 +222,9 @@ public void handleMessage(Message msg) {
214
222
case EV_VOICE_RECEIVED :
215
223
showVoiceNotification (R .string .app_notifications_voice_title , R .string .app_notifications_voice_summary );
216
224
break ;
225
+ case EV_TEXT_MESSAGE_RECEIVED :
226
+ showMessageNotification (R .string .app_notifications_text_title , (String )msg .obj );
227
+ break ;
217
228
case EV_LISTENING :
218
229
hideVoiceNotification ();
219
230
break ;
@@ -245,7 +256,7 @@ public void handleMessage(Message msg) {
245
256
246
257
private void showServiceNotification (int resId , int iconResId ) {
247
258
String text = getString (resId );
248
- _notificationManager .notify (SERVICE_NOTIFICATION_ID , buildNotification (text , iconResId ));
259
+ _notificationManager .notify (SERVICE_NOTIFICATION_ID , buildServiceNotification (text , iconResId ));
249
260
}
250
261
251
262
private void showVoiceNotification (int titleResId , int textResId ) {
@@ -255,6 +266,12 @@ private void showVoiceNotification(int titleResId, int textResId) {
255
266
_notificationManager .notify (VOICE_NOTIFICATION_ID , buildFullScreenNotification (title , text ));
256
267
}
257
268
269
+ private void showMessageNotification (int titleResId , String note ) {
270
+ if (!MessageItemActivity .isPaused || !_voiceNotificationsEnabled ) return ;
271
+ String title = getString (titleResId );
272
+ _notificationManager .notify (MESSAGE_NOTIFICATION_ID , buildMessageNotification (title , note ));
273
+ }
274
+
258
275
private void hideVoiceNotification () {
259
276
if (!_voiceNotificationsEnabled ) return ;
260
277
_notificationManager .cancel (VOICE_NOTIFICATION_ID );
@@ -269,7 +286,7 @@ private String createNotificationChannel(String channelId, String channelName, i
269
286
return channelId ;
270
287
}
271
288
272
- private Notification buildNotification (String text , int iconResId ) {
289
+ private Notification buildServiceNotification (String text , int iconResId ) {
273
290
String channelId = "" ;
274
291
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O )
275
292
channelId = createNotificationChannel ("alpha" , "Service" , NotificationManager .IMPORTANCE_LOW );
@@ -312,4 +329,31 @@ private Notification buildFullScreenNotification(String title, String text) {
312
329
.setAutoCancel (true )
313
330
.build ();
314
331
}
332
+
333
+ private Notification buildMessageNotification (String title , String text ) {
334
+ String channelId = "" ;
335
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O )
336
+ channelId = createNotificationChannel ("gamma" , "Message" , NotificationManager .IMPORTANCE_HIGH );
337
+
338
+ // extract group name from the note
339
+ String [] srcDstText = text .split (": " );
340
+ String groupName = srcDstText [0 ].split ("→" )[1 ];
341
+
342
+ Intent notificationIntent = new Intent (this , MessageItemActivity .class );
343
+ notificationIntent .putExtra ("groupName" , groupName );
344
+ notificationIntent .setFlags (Intent .FLAG_ACTIVITY_CLEAR_TOP | Intent .FLAG_ACTIVITY_SINGLE_TOP );
345
+ PendingIntent pendingIntent = PendingIntent .getActivity (this , 0 ,
346
+ notificationIntent , PendingIntent .FLAG_IMMUTABLE | PendingIntent .FLAG_UPDATE_CURRENT );
347
+
348
+ return new NotificationCompat .Builder (this , channelId )
349
+ .setContentTitle (title )
350
+ .setContentText (text )
351
+ .setSmallIcon (R .drawable .ic_app_action )
352
+ .setChannelId (channelId )
353
+ .setPriority (NotificationCompat .PRIORITY_HIGH )
354
+ .setCategory (NotificationCompat .CATEGORY_MESSAGE )
355
+ .setContentIntent (pendingIntent )
356
+ .setAutoCancel (true )
357
+ .build ();
358
+ }
315
359
}
0 commit comments