Changeset View
Changeset View
Standalone View
Standalone View
src/android/org/kde/knotifications/NotifyByAndroid.java
Show All 32 Lines | |||||
33 | 33 | | |||
34 | /** Java side of the Android notfication backend. */ | 34 | /** Java side of the Android notfication backend. */ | ||
35 | public class NotifyByAndroid extends BroadcastReceiver | 35 | public class NotifyByAndroid extends BroadcastReceiver | ||
36 | { | 36 | { | ||
37 | private static final String TAG = "org.kde.knotifications"; | 37 | private static final String TAG = "org.kde.knotifications"; | ||
38 | 38 | | |||
39 | private static final String NOTIFICATION_ACTION = ".org.kde.knotifications.NOTIFICATION_ACTION"; | 39 | private static final String NOTIFICATION_ACTION = ".org.kde.knotifications.NOTIFICATION_ACTION"; | ||
40 | private static final String NOTIFICATION_DELETED = ".org.kde.knotifications.NOTIFICATION_DELETED"; | 40 | private static final String NOTIFICATION_DELETED = ".org.kde.knotifications.NOTIFICATION_DELETED"; | ||
41 | private static final String NOTIFICATION_OPENED = ".org.kde.knotifications.NOTIFICATION_OPENED"; | ||||
41 | private static final String NOTIFICATION_ID_EXTRA = "org.kde.knotifications.NOTIFICATION_ID"; | 42 | private static final String NOTIFICATION_ID_EXTRA = "org.kde.knotifications.NOTIFICATION_ID"; | ||
42 | private static final String NOTIFICATION_ACTION_ID_EXTRA = "org.kde.knotifications.NOTIFICATION_ACTION_ID"; | 43 | private static final String NOTIFICATION_ACTION_ID_EXTRA = "org.kde.knotifications.NOTIFICATION_ACTION_ID"; | ||
43 | 44 | | |||
44 | private android.content.Context m_ctx; | 45 | private android.content.Context m_ctx; | ||
45 | private NotificationManager m_notificationManager; | 46 | private NotificationManager m_notificationManager; | ||
46 | private int m_uniquePendingIntentId = 0; | 47 | private int m_uniquePendingIntentId = 0; | ||
47 | private HashSet<String> m_channels = new HashSet(); | 48 | private HashSet<String> m_channels = new HashSet(); | ||
48 | 49 | | |||
49 | public NotifyByAndroid(android.content.Context context) | 50 | public NotifyByAndroid(android.content.Context context) | ||
50 | { | 51 | { | ||
51 | Log.i(TAG, context.getPackageName()); | 52 | Log.i(TAG, context.getPackageName()); | ||
52 | m_ctx = context; | 53 | m_ctx = context; | ||
53 | m_notificationManager = (NotificationManager)m_ctx.getSystemService(Context.NOTIFICATION_SERVICE); | 54 | m_notificationManager = (NotificationManager)m_ctx.getSystemService(Context.NOTIFICATION_SERVICE); | ||
54 | 55 | | |||
55 | IntentFilter filter = new IntentFilter(); | 56 | IntentFilter filter = new IntentFilter(); | ||
56 | filter.addAction(m_ctx.getPackageName() + NOTIFICATION_ACTION); | 57 | filter.addAction(m_ctx.getPackageName() + NOTIFICATION_ACTION); | ||
57 | filter.addAction(m_ctx.getPackageName() + NOTIFICATION_DELETED); | 58 | filter.addAction(m_ctx.getPackageName() + NOTIFICATION_DELETED); | ||
59 | filter.addAction(m_ctx.getPackageName() + NOTIFICATION_OPENED); | ||||
58 | m_ctx.registerReceiver(this, filter); | 60 | m_ctx.registerReceiver(this, filter); | ||
59 | } | 61 | } | ||
60 | 62 | | |||
61 | public void notify(KNotification notification) | 63 | public void notify(KNotification notification) | ||
62 | { | 64 | { | ||
63 | Log.i(TAG, notification.text); | 65 | Log.i(TAG, notification.text); | ||
64 | 66 | | |||
65 | // notification channel | 67 | // notification channel | ||
Show All 18 Lines | 85 | if (Build.VERSION.SDK_INT >= 23) { | |||
84 | builder.setSmallIcon((Icon)notification.icon); | 86 | builder.setSmallIcon((Icon)notification.icon); | ||
85 | } else { | 87 | } else { | ||
86 | builder.setSmallIcon(m_ctx.getApplicationInfo().icon); | 88 | builder.setSmallIcon(m_ctx.getApplicationInfo().icon); | ||
87 | } | 89 | } | ||
88 | builder.setContentTitle(notification.title); | 90 | builder.setContentTitle(notification.title); | ||
89 | builder.setContentText(notification.text); | 91 | builder.setContentText(notification.text); | ||
90 | 92 | | |||
91 | // taping the notification shows the app | 93 | // taping the notification shows the app | ||
92 | Intent intent = new Intent(m_ctx, m_ctx.getClass()); | 94 | Intent intent = new Intent(m_ctx.getPackageName() + NOTIFICATION_OPENED); | ||
93 | PendingIntent contentIntent = PendingIntent.getActivity(m_ctx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); | 95 | intent.putExtra(NOTIFICATION_ID_EXTRA, notification.id); | ||
96 | PendingIntent contentIntent = PendingIntent.getActivity(m_ctx, m_uniquePendingIntentId++, intent, PendingIntent.FLAG_UPDATE_CURRENT); | ||||
94 | builder.setContentIntent(contentIntent); | 97 | builder.setContentIntent(contentIntent); | ||
95 | 98 | | |||
96 | // actions | 99 | // actions | ||
97 | int actionId = 0; | 100 | int actionId = 0; | ||
98 | for (String actionName : notification.actions) { | 101 | for (String actionName : notification.actions) { | ||
99 | Intent actionIntent = new Intent(m_ctx.getPackageName() + NOTIFICATION_ACTION); | 102 | Intent actionIntent = new Intent(m_ctx.getPackageName() + NOTIFICATION_ACTION); | ||
100 | actionIntent.putExtra(NOTIFICATION_ID_EXTRA, notification.id); | 103 | actionIntent.putExtra(NOTIFICATION_ID_EXTRA, notification.id); | ||
101 | actionIntent.putExtra(NOTIFICATION_ACTION_ID_EXTRA, actionId); | 104 | actionIntent.putExtra(NOTIFICATION_ACTION_ID_EXTRA, actionId); | ||
Show All 24 Lines | 127 | { | |||
126 | int id = intent.getIntExtra(NOTIFICATION_ID_EXTRA, -1); | 129 | int id = intent.getIntExtra(NOTIFICATION_ID_EXTRA, -1); | ||
127 | Log.i(TAG, action + ": " + id + " " + intent.getExtras()); | 130 | Log.i(TAG, action + ": " + id + " " + intent.getExtras()); | ||
128 | 131 | | |||
129 | if (action.equals(m_ctx.getPackageName() + NOTIFICATION_ACTION)) { | 132 | if (action.equals(m_ctx.getPackageName() + NOTIFICATION_ACTION)) { | ||
130 | int actionId = intent.getIntExtra(NOTIFICATION_ACTION_ID_EXTRA, -1); | 133 | int actionId = intent.getIntExtra(NOTIFICATION_ACTION_ID_EXTRA, -1); | ||
131 | notificationActionInvoked(id, actionId); | 134 | notificationActionInvoked(id, actionId); | ||
132 | } else if (action.equals(m_ctx.getPackageName() + NOTIFICATION_DELETED)) { | 135 | } else if (action.equals(m_ctx.getPackageName() + NOTIFICATION_DELETED)) { | ||
133 | notificationFinished(id); | 136 | notificationFinished(id); | ||
137 | } else if (action.equals(m_ctx.getPackageName() + NOTIFICATION_OPENED)) { | ||||
138 | Intent newintent = new Intent(m_ctx, m_ctx.getClass()); | ||||
139 | m_ctx.startActivity(newintent); | ||||
140 | notificationActionInvoked(id, 0); | ||||
134 | } | 141 | } | ||
135 | } | 142 | } | ||
136 | 143 | | |||
137 | public native void notificationFinished(int notificationId); | 144 | public native void notificationFinished(int notificationId); | ||
138 | public native void notificationActionInvoked(int notificationId, int action); | 145 | public native void notificationActionInvoked(int notificationId, int action); | ||
139 | } | 146 | } |