SLAM/mobile/src/main/java/de/vanitasvitae/slam/service/MyMessagingService.java

121 lines
4.8 KiB
Java

/*
* Copyright 2018 Paul Schaub
*
* This code 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, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package de.vanitasvitae.slam.service;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.CarExtender;
import android.support.v4.app.NotificationCompat.CarExtender.UnreadConversation;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.app.RemoteInput;
public class MyMessagingService extends Service {
public static final String READ_ACTION =
"de.vanitasvitae.slam.ACTION_MESSAGE_READ";
public static final String REPLY_ACTION =
"de.vanitasvitae.slam.ACTION_MESSAGE_REPLY";
public static final String CONVERSATION_ID = "conversation_id";
public static final String EXTRA_VOICE_REPLY = "extra_voice_reply";
private static final String TAG = MyMessagingService.class.getSimpleName();
private final Messenger mMessenger = new Messenger(new IncomingHandler());
private NotificationManagerCompat mNotificationManager;
@Override
public void onCreate() {
mNotificationManager = NotificationManagerCompat.from(getApplicationContext());
}
@Override
public IBinder onBind(Intent intent) {
return mMessenger.getBinder();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
private Intent createIntent(int conversationId, String action) {
return new Intent()
.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES)
.setAction(action)
.putExtra(CONVERSATION_ID, conversationId);
}
private void sendNotification(int conversationId, String message,
String participant, long timestamp) {
// A pending Intent for reads
PendingIntent readPendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
conversationId,
createIntent(conversationId, READ_ACTION),
PendingIntent.FLAG_UPDATE_CURRENT);
// Build a RemoteInput for receiving voice input in a Car Notification
RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
.setLabel("Reply by voice")
.build();
// Building a Pending Intent for the reply action to trigger
PendingIntent replyIntent = PendingIntent.getBroadcast(getApplicationContext(),
conversationId,
createIntent(conversationId, REPLY_ACTION),
PendingIntent.FLAG_UPDATE_CURRENT);
// Create the UnreadConversation and populate it with the participant name,
// read and reply intents.
UnreadConversation.Builder unreadConvBuilder =
new UnreadConversation.Builder(participant)
.setLatestTimestamp(timestamp)
.setReadPendingIntent(readPendingIntent)
.setReplyAction(replyIntent, remoteInput);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
// Set the application notification icon:
//.setSmallIcon(R.drawable.notification_icon)
// Set the large icon, for example a picture of the other recipient of the message
//.setLargeIcon(personBitmap)
.setContentText(message)
.setWhen(timestamp)
.setContentTitle(participant)
.setContentIntent(readPendingIntent)
.extend(new CarExtender()
.setUnreadConversation(unreadConvBuilder.build()));
mNotificationManager.notify(conversationId, builder.build());
}
/**
* Handler of incoming messages from clients.
*/
class IncomingHandler extends Handler {
@Override
public void handleMessage(Message msg) {
sendNotification(1, "This is a sample message", "John Doe",
System.currentTimeMillis());
}
}
}