Changeset View
Changeset View
Standalone View
Standalone View
src/org/kde/knotifications/NotifyByAndroid.java
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright (C) 2018 Volker Krause <vkrause@kde.org> | ||||
3 | | ||||
4 | This program is free software; you can redistribute it and/or modify it | ||||
5 | under the terms of the GNU Library General Public License as published by | ||||
6 | the Free Software Foundation; either version 2 of the License, or (at your | ||||
7 | option) any later version. | ||||
8 | | ||||
9 | This program is distributed in the hope that it will be useful, but WITHOUT | ||||
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||||
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public | ||||
12 | License for more details. | ||||
13 | | ||||
14 | You should have received a copy of the GNU General Public License | ||||
15 | along with this program. If not, see <https://www.gnu.org/licenses/>. | ||||
16 | */ | ||||
17 | | ||||
18 | package org.kde.knotifications; | ||||
19 | | ||||
20 | import android.app.Activity; | ||||
21 | import android.app.Notification; | ||||
22 | import android.app.NotificationManager; | ||||
23 | import android.app.PendingIntent; | ||||
24 | import android.content.BroadcastReceiver; | ||||
25 | import android.content.Context; | ||||
26 | import android.content.Intent; | ||||
27 | import android.content.IntentFilter; | ||||
28 | import android.util.Log; | ||||
29 | | ||||
30 | /** Java side of the Android notfication backend. */ | ||||
31 | public class NotifyByAndroid extends BroadcastReceiver | ||||
32 | { | ||||
33 | private static final String TAG = "org.kde.knotifications"; | ||||
34 | | ||||
35 | private static final String NOTIFICATION_ACTION = ".org.kde.knotifications.NOTIFICATION_ACTION"; | ||||
36 | private static final String NOTIFICATION_DELETED = ".org.kde.knotifications.NOTIFICATION_DELETED"; | ||||
37 | private static final String NOTIFICATION_ID_EXTRA = "org.kde.knotifications.NOTIFICATION_ID"; | ||||
38 | private static final String NOTIFICATION_ACTION_ID_EXTRA = "org.kde.knotifications.NOTIFICATION_ACTION_ID"; | ||||
39 | | ||||
40 | private android.content.Context m_ctx; | ||||
41 | private NotificationManager m_notificationManager; | ||||
42 | private int m_uniquePendingIntentId = 0; | ||||
43 | | ||||
44 | public NotifyByAndroid(android.content.Context context) | ||||
45 | { | ||||
46 | Log.i(TAG, context.getPackageName()); | ||||
47 | m_ctx = context; | ||||
48 | m_notificationManager = (NotificationManager)m_ctx.getSystemService(Context.NOTIFICATION_SERVICE); | ||||
49 | | ||||
50 | IntentFilter filter = new IntentFilter(); | ||||
51 | filter.addAction(m_ctx.getPackageName() + NOTIFICATION_ACTION); | ||||
52 | filter.addAction(m_ctx.getPackageName() + NOTIFICATION_DELETED); | ||||
53 | m_ctx.registerReceiver(this, filter); | ||||
54 | } | ||||
55 | | ||||
56 | public void notify(KNotification notification) | ||||
57 | { | ||||
58 | Log.i(TAG, notification.text); | ||||
59 | | ||||
60 | Notification.Builder builder = new Notification.Builder(m_ctx); | ||||
61 | builder.setSmallIcon(notification.icon); | ||||
62 | builder.setContentTitle(notification.title); | ||||
63 | builder.setContentText(notification.text); | ||||
64 | | ||||
65 | // taping the notification shows the app | ||||
66 | Intent intent = new Intent(m_ctx, m_ctx.getClass()); | ||||
67 | PendingIntent contentIntent = PendingIntent.getActivity(m_ctx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); | ||||
68 | builder.setContentIntent(contentIntent); | ||||
69 | | ||||
70 | // actions | ||||
71 | int actionId = 0; | ||||
72 | for (String actionName : notification.actions) { | ||||
73 | Intent actionIntent = new Intent(m_ctx.getPackageName() + NOTIFICATION_ACTION); | ||||
74 | actionIntent.putExtra(NOTIFICATION_ID_EXTRA, notification.id); | ||||
75 | actionIntent.putExtra(NOTIFICATION_ACTION_ID_EXTRA, actionId); | ||||
76 | PendingIntent pendingIntent = PendingIntent.getBroadcast(m_ctx, m_uniquePendingIntentId++, actionIntent, PendingIntent.FLAG_UPDATE_CURRENT); | ||||
77 | Notification.Action action = new Notification.Action.Builder(0, actionName, pendingIntent).build(); | ||||
78 | builder.addAction(action); | ||||
79 | ++actionId; | ||||
80 | } | ||||
81 | | ||||
82 | // notfication about user closing the notification | ||||
83 | Intent deleteIntent = new Intent(m_ctx.getPackageName() + NOTIFICATION_DELETED); | ||||
84 | deleteIntent.putExtra(NOTIFICATION_ID_EXTRA, notification.id); | ||||
85 | Log.i(TAG, deleteIntent.getExtras() + " " + notification.id); | ||||
86 | builder.setDeleteIntent(PendingIntent.getBroadcast(m_ctx, m_uniquePendingIntentId++, deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT)); | ||||
87 | | ||||
88 | m_notificationManager.notify(notification.id, builder.build()); | ||||
89 | } | ||||
90 | | ||||
91 | public void close(int id) | ||||
92 | { | ||||
93 | m_notificationManager.cancel(id); | ||||
94 | } | ||||
95 | | ||||
96 | @Override | ||||
97 | public void onReceive(Context context, Intent intent) | ||||
98 | { | ||||
99 | String action = intent.getAction(); | ||||
100 | int id = intent.getIntExtra(NOTIFICATION_ID_EXTRA, -1); | ||||
101 | Log.i(TAG, action + ": " + id + " " + intent.getExtras()); | ||||
102 | | ||||
103 | if (action.equals(m_ctx.getPackageName() + NOTIFICATION_ACTION)) { | ||||
104 | int actionId = intent.getIntExtra(NOTIFICATION_ACTION_ID_EXTRA, -1); | ||||
105 | notificationActionInvoked(id, actionId); | ||||
106 | } else if (action.equals(m_ctx.getPackageName() + NOTIFICATION_DELETED)) { | ||||
107 | notificationFinished(id); | ||||
108 | } | ||||
109 | } | ||||
110 | | ||||
111 | public native void notificationFinished(int notificationId); | ||||
112 | public native void notificationActionInvoked(int notificationId, int action); | ||||
113 | } |