Debian Codegraph @

templates/other/AndroidAutoMessagingService/root/src/app_package/MessagingService.java.ftl

raw
1 /*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package ${packageName};
18
19 import android.app.PendingIntent;
20 import android.app.Service;
21 import android.content.Intent;
22 import android.os.Handler;
23 import android.os.IBinder;
24 import android.os.Message;
25 import android.os.Messenger;
26 import android.support.v4.app.NotificationCompat;
27 import android.support.v4.app.NotificationCompat.CarExtender;
28 import android.support.v4.app.NotificationCompat.CarExtender.UnreadConversation;
29 import android.support.v4.app.NotificationManagerCompat;
30 import android.support.v4.app.RemoteInput;
31
32 public class ${serviceName} extends Service {
33 private static final String TAG = ${serviceName}.class.getSimpleName();
34
35 public static final String READ_ACTION =
36 "${packageName}.ACTION_MESSAGE_READ";
37 public static final String REPLY_ACTION =
38 "${packageName}.ACTION_MESSAGE_REPLY";
39 public static final String CONVERSATION_ID = "conversation_id";
40 public static final String EXTRA_VOICE_REPLY = "extra_voice_reply";
41
42 private NotificationManagerCompat mNotificationManager;
43
44 private final Messenger mMessenger = new Messenger(new IncomingHandler());
45
46 /**
47 * Handler of incoming messages from clients.
48 */
49 class IncomingHandler extends Handler {
50 @Override
51 public void handleMessage(Message msg) {
52 sendNotification(1, "This is a sample message", "John Doe",
53 System.currentTimeMillis());
54 }
55 }
56
57 @Override
58 public void onCreate() {
59 mNotificationManager = NotificationManagerCompat.from(getApplicationContext());
60 }
61
62 @Override
63 public IBinder onBind(Intent intent) {
64 return mMessenger.getBinder();
65 }
66
67 @Override
68 public int onStartCommand(Intent intent, int flags, int startId) {
69 return START_STICKY;
70 }
71
72 private Intent createIntent(int conversationId, String action) {
73 return new Intent()
74 .addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES)
75 .setAction(action)
76 .putExtra(CONVERSATION_ID, conversationId);
77 }
78
79 private void sendNotification(int conversationId, String message,
80 String participant, long timestamp) {
81 // A pending Intent for reads
82 PendingIntent readPendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
83 conversationId,
84 createIntent(conversationId, READ_ACTION),
85 PendingIntent.FLAG_UPDATE_CURRENT);
86
87 // Build a RemoteInput for receiving voice input in a Car Notification
88 RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
89 .setLabel("Reply by voice")
90 .build();
91
92 // Building a Pending Intent for the reply action to trigger
93 PendingIntent replyIntent = PendingIntent.getBroadcast(getApplicationContext(),
94 conversationId,
95 createIntent(conversationId, REPLY_ACTION),
96 PendingIntent.FLAG_UPDATE_CURRENT);
97
98 // Create the UnreadConversation and populate it with the participant name,
99 // read and reply intents.
100 UnreadConversation.Builder unreadConvBuilder =
101 new UnreadConversation.Builder(participant)
102 .setLatestTimestamp(timestamp)
103 .setReadPendingIntent(readPendingIntent)
104 .setReplyAction(replyIntent, remoteInput);
105
106 NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
107 // Set the application notification icon:
108 //.setSmallIcon(R.drawable.notification_icon)
109
110 // Set the large icon, for example a picture of the other recipient of the message
111 //.setLargeIcon(personBitmap)
112
113 .setContentText(message)
114 .setWhen(timestamp)
115 .setContentTitle(participant)
116 .setContentIntent(readPendingIntent)
117 .extend(new CarExtender()
118 .setUnreadConversation(unreadConvBuilder.build()));
119
120 mNotificationManager.notify(conversationId, builder.build());
121 }
122 }