Differential D10165 Diff 27997 src/org/kde/kdeconnect/Plugins/NotificationsPlugin/NotificationsPlugin.java
Changeset View
Changeset View
Standalone View
Standalone View
src/org/kde/kdeconnect/Plugins/NotificationsPlugin/NotificationsPlugin.java
Show All 20 Lines | |||||
21 | package org.kde.kdeconnect.Plugins.NotificationsPlugin; | 21 | package org.kde.kdeconnect.Plugins.NotificationsPlugin; | ||
22 | 22 | | |||
23 | import android.annotation.TargetApi; | 23 | import android.annotation.TargetApi; | ||
24 | import android.app.Activity; | 24 | import android.app.Activity; | ||
25 | import android.app.AlertDialog; | 25 | import android.app.AlertDialog; | ||
26 | import android.app.Notification; | 26 | import android.app.Notification; | ||
27 | import android.app.PendingIntent; | 27 | import android.app.PendingIntent; | ||
28 | import android.app.RemoteInput; | 28 | import android.app.RemoteInput; | ||
29 | import android.content.Context; | ||||
29 | import android.content.DialogInterface; | 30 | import android.content.DialogInterface; | ||
30 | import android.content.Intent; | 31 | import android.content.Intent; | ||
32 | import android.content.pm.PackageManager; | ||||
33 | import android.content.res.Resources; | ||||
31 | import android.graphics.Bitmap; | 34 | import android.graphics.Bitmap; | ||
35 | import android.graphics.Canvas; | ||||
36 | import android.graphics.drawable.Drawable; | ||||
37 | import android.graphics.drawable.Icon; | ||||
32 | import android.os.Build; | 38 | import android.os.Build; | ||
33 | import android.os.Bundle; | 39 | import android.os.Bundle; | ||
34 | import android.provider.Settings; | 40 | import android.provider.Settings; | ||
35 | import android.service.notification.StatusBarNotification; | 41 | import android.service.notification.StatusBarNotification; | ||
36 | import android.support.annotation.RequiresApi; | 42 | import android.support.annotation.RequiresApi; | ||
37 | import android.util.Log; | 43 | import android.util.Log; | ||
38 | 44 | | |||
39 | import org.kde.kdeconnect.Helpers.AppsHelper; | 45 | import org.kde.kdeconnect.Helpers.AppsHelper; | ||
▲ Show 20 Lines • Show All 151 Lines • ▼ Show 20 Line(s) | 158 | private void sendNotification(StatusBarNotification statusBarNotification) { | |||
191 | 197 | | |||
192 | if (packageName.equals("org.kde.kdeconnect_tp")) { | 198 | if (packageName.equals("org.kde.kdeconnect_tp")) { | ||
193 | //Make our own notifications silent :) | 199 | //Make our own notifications silent :) | ||
194 | np.set("silent", true); | 200 | np.set("silent", true); | ||
195 | np.set("requestAnswer", true); //For compatibility with old desktop versions of KDE Connect that don't support "silent" | 201 | np.set("requestAnswer", true); //For compatibility with old desktop versions of KDE Connect that don't support "silent" | ||
196 | } | 202 | } | ||
197 | 203 | | |||
198 | try { | 204 | try { | ||
199 | Bitmap appIcon = notification.largeIcon; | 205 | Bitmap appIcon = null; | ||
206 | Context foreignContext = context.createPackageContext(statusBarNotification.getPackageName(), 0); | ||||
207 | if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) { | ||||
208 | appIcon = iconToBitmap(foreignContext, notification.getLargeIcon()); | ||||
209 | } else { | ||||
210 | appIcon = notification.largeIcon; | ||||
211 | } | ||||
212 | //appIcon = drawableToBitmap(context.getResources().getDrawable(R.drawable.icon)); | ||||
213 | if (appIcon == null) { | ||||
214 | if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) { | ||||
215 | appIcon = iconToBitmap(foreignContext, notification.getSmallIcon()); | ||||
216 | } else { | ||||
217 | PackageManager pm = context.getPackageManager(); | ||||
218 | Resources foreignResources = pm.getResourcesForApplication(statusBarNotification.getPackageName()); | ||||
219 | Drawable foreignIcon = foreignResources.getDrawable(notification.icon); | ||||
220 | appIcon = drawableToBitmap(foreignIcon); | ||||
221 | } | ||||
222 | } | ||||
200 | 223 | | |||
201 | if (appIcon != null) { | 224 | if (appIcon != null) { | ||
202 | ByteArrayOutputStream outStream = new ByteArrayOutputStream(); | 225 | ByteArrayOutputStream outStream = new ByteArrayOutputStream(); | ||
203 | if (appIcon.getWidth() > 128) { | | |||
204 | appIcon = Bitmap.createScaledBitmap(appIcon, 96, 96, true); | | |||
205 | } | | |||
206 | appIcon.compress(Bitmap.CompressFormat.PNG, 90, outStream); | 226 | appIcon.compress(Bitmap.CompressFormat.PNG, 90, outStream); | ||
207 | byte[] bitmapData = outStream.toByteArray(); | 227 | byte[] bitmapData = outStream.toByteArray(); | ||
208 | 228 | | |||
229 | | ||||
apol: empty line? | |||||
209 | np.setPayload(bitmapData); | 230 | np.setPayload(bitmapData); | ||
210 | 231 | | |||
211 | np.set("payloadHash", getChecksum(bitmapData)); | 232 | np.set("payloadHash", getChecksum(bitmapData)); | ||
212 | } | 233 | } | ||
213 | } catch (Exception e) { | 234 | } catch (Exception e) { | ||
214 | e.printStackTrace(); | 235 | e.printStackTrace(); | ||
215 | Log.e("NotificationsPlugin", "Error retrieving icon"); | 236 | Log.e("NotificationsPlugin", "Error retrieving icon"); | ||
216 | } | 237 | } | ||
Show All 10 Lines | |||||
227 | np.set("ticker", getTickerText(notification)); | 248 | np.set("ticker", getTickerText(notification)); | ||
228 | np.set("title", getNotificationTitle(notification)); | 249 | np.set("title", getNotificationTitle(notification)); | ||
229 | np.set("text", getNotificationText(notification)); | 250 | np.set("text", getNotificationText(notification)); | ||
230 | np.set("time", Long.toString(statusBarNotification.getPostTime())); | 251 | np.set("time", Long.toString(statusBarNotification.getPostTime())); | ||
231 | 252 | | |||
232 | device.sendPackage(np); | 253 | device.sendPackage(np); | ||
233 | } | 254 | } | ||
234 | 255 | | |||
256 | private Bitmap drawableToBitmap(Drawable drawable) { | ||||
257 | if (drawable == null) return null; | ||||
258 | | ||||
259 | Bitmap res; | ||||
260 | if (drawable.getIntrinsicWidth() > 128 || drawable.getIntrinsicHeight() > 128) { | ||||
261 | res = Bitmap.createBitmap(96, 96, Bitmap.Config.ARGB_8888); | ||||
262 | } else if (drawable.getIntrinsicWidth() <= 64 || drawable.getIntrinsicHeight() <= 64) { | ||||
263 | res = Bitmap.createBitmap(96, 96, Bitmap.Config.ARGB_8888); | ||||
264 | } else { | ||||
265 | res = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); | ||||
266 | } | ||||
267 | | ||||
268 | Canvas canvas = new Canvas(res); | ||||
269 | drawable.setBounds(0, 0, res.getWidth(), res.getHeight()); | ||||
270 | drawable.draw(canvas); | ||||
271 | return res; | ||||
272 | } | ||||
273 | | ||||
274 | @RequiresApi(Build.VERSION_CODES.M) | ||||
275 | private Bitmap iconToBitmap(Context foreignContext, Icon icon) { | ||||
276 | if (icon == null) return null; | ||||
277 | | ||||
278 | return drawableToBitmap(icon.loadDrawable(foreignContext)); | ||||
279 | } | ||||
280 | | ||||
235 | @RequiresApi(api = Build.VERSION_CODES.KITKAT_WATCH) | 281 | @RequiresApi(api = Build.VERSION_CODES.KITKAT_WATCH) | ||
236 | private void replyToNotification(String id, String message) { | 282 | private void replyToNotification(String id, String message) { | ||
237 | if (pendingIntents.isEmpty() || !pendingIntents.containsKey(id)) { | 283 | if (pendingIntents.isEmpty() || !pendingIntents.containsKey(id)) { | ||
238 | Log.e("NotificationsPlugin", "No such notification"); | 284 | Log.e("NotificationsPlugin", "No such notification"); | ||
239 | return; | 285 | return; | ||
240 | } | 286 | } | ||
241 | 287 | | |||
242 | RepliableNotification repliableNotification = pendingIntents.get(id); | 288 | RepliableNotification repliableNotification = pendingIntents.get(id); | ||
▲ Show 20 Lines • Show All 306 Lines • Show Last 20 Lines |
empty line?