| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.webapk.lib.client; | 5 package org.chromium.webapk.lib.client; |
| 6 | 6 |
| 7 import android.app.PendingIntent; | |
| 8 import android.content.Intent; | |
| 9 import android.graphics.Bitmap; | |
| 10 import android.os.RemoteException; | 7 import android.os.RemoteException; |
| 11 import android.util.Log; | 8 import android.util.Log; |
| 12 | 9 |
| 13 import org.chromium.base.Callback; | 10 import org.chromium.base.Callback; |
| 14 import org.chromium.webapk.lib.runtime_library.IWebApkApi; | 11 import org.chromium.webapk.lib.runtime_library.IWebApkApi; |
| 15 | 12 |
| 16 /** | 13 /** |
| 17 * NotificationClient provides APIs that a WebAPK host can delegate display or c
lose notification | 14 * NotificationClient provides APIs that a WebAPK host can delegate display or c
lose notification |
| 18 * tasks. | 15 * tasks. |
| 19 */ | 16 */ |
| (...skipping 13 matching lines...) Expand all Loading... |
| 33 } | 30 } |
| 34 } | 31 } |
| 35 } | 32 } |
| 36 | 33 |
| 37 public NotificationClient() {} | 34 public NotificationClient() {} |
| 38 | 35 |
| 39 /** | 36 /** |
| 40 * Connect to a bind service of the WebAPK with the given package name, buil
d a notification | 37 * Connect to a bind service of the WebAPK with the given package name, buil
d a notification |
| 41 * and hand it over to the WebAPK to display. | 38 * and hand it over to the WebAPK to display. |
| 42 */ | 39 */ |
| 43 public void displayNotification(final int pendingIntentRequestCode, | 40 public void displayNotification(final NotificationBuilderDelegate notificati
onBuilder, |
| 44 final NotificationBuilderDelegate notificationBuilder, final Intent
clickIntent, | 41 final String platformTag, final int platformID, String webApkPackage
) { |
| 45 final Intent closeIntent, final Intent[] actionIntents, final String
[] actionTitles, | |
| 46 final Bitmap[] actionIcons, final int flags, final String platformTa
g, | |
| 47 final int platformID, String webApkPackage) { | |
| 48 final ApiUseCallback connectionCallback = new ApiUseCallback() { | 42 final ApiUseCallback connectionCallback = new ApiUseCallback() { |
| 49 @Override | 43 @Override |
| 50 public void useApi(IWebApkApi api) throws RemoteException { | 44 public void useApi(IWebApkApi api) throws RemoteException { |
| 51 // Create all the PendingIntents needed to build the notificatio
n. | |
| 52 PendingIntent clickPendingIntent = | |
| 53 api.getBroadcastPendingIntent(pendingIntentRequestCode,
clickIntent, flags); | |
| 54 PendingIntent closePendingIntent = | |
| 55 api.getBroadcastPendingIntent(pendingIntentRequestCode,
closeIntent, flags); | |
| 56 PendingIntent[] actionPendingIntents = null; | |
| 57 if (actionIntents != null) { | |
| 58 actionPendingIntents = new PendingIntent[actionIntents.lengt
h]; | |
| 59 for (int actionIndex = 0; actionIndex < actionIntents.length
; actionIndex++) { | |
| 60 actionPendingIntents[actionIndex] = api.getBroadcastPend
ingIntent( | |
| 61 pendingIntentRequestCode, actionIntents[actionIn
dex], flags); | |
| 62 } | |
| 63 } | |
| 64 notificationBuilder.setSmallIcon(api.getSmallIconId()); | 45 notificationBuilder.setSmallIcon(api.getSmallIconId()); |
| 65 // Build a notification. | |
| 66 buildNotification(notificationBuilder, clickPendingIntent, close
PendingIntent, | |
| 67 actionPendingIntents, actionTitles, actionIcons); | |
| 68 // Display the notification. | |
| 69 api.displayNotification(platformTag, platformID, notificationBui
lder.build()); | 46 api.displayNotification(platformTag, platformID, notificationBui
lder.build()); |
| 70 } | 47 } |
| 71 }; | 48 }; |
| 72 | 49 |
| 73 WebApkServiceConnectionManager.getInstance().connect(webApkPackage, conn
ectionCallback); | 50 WebApkServiceConnectionManager.getInstance().connect(webApkPackage, conn
ectionCallback); |
| 74 } | 51 } |
| 75 | 52 |
| 76 /** | 53 /** |
| 77 * Connect to a bind service of a WebAPK with the given package name, and as
k the it to close a | 54 * Connect to a bind service of a WebAPK with the given package name, and as
k the it to close a |
| 78 * notification. | 55 * notification. |
| 79 */ | 56 */ |
| 80 public void closeNotification( | 57 public void closeNotification( |
| 81 String webApkPackage, final String platformTag, final int platformID
) { | 58 String webApkPackage, final String platformTag, final int platformID
) { |
| 82 final ApiUseCallback connectionCallback = new ApiUseCallback() { | 59 final ApiUseCallback connectionCallback = new ApiUseCallback() { |
| 83 @Override | 60 @Override |
| 84 public void useApi(IWebApkApi api) throws RemoteException { | 61 public void useApi(IWebApkApi api) throws RemoteException { |
| 85 api.closeNotification(platformTag, platformID); | 62 api.closeNotification(platformTag, platformID); |
| 86 } | 63 } |
| 87 }; | 64 }; |
| 88 WebApkServiceConnectionManager.getInstance().connect(webApkPackage, conn
ectionCallback); | 65 WebApkServiceConnectionManager.getInstance().connect(webApkPackage, conn
ectionCallback); |
| 89 } | 66 } |
| 90 | |
| 91 private static void buildNotification(final NotificationBuilderDelegate noti
ficationBuilder, | |
| 92 final PendingIntent clickPendingIntent, final PendingIntent closePen
dingIntent, | |
| 93 final PendingIntent[] actionPendingIntents, | |
| 94 final String[] actionTitles, final Bitmap[] actionIcons) { | |
| 95 notificationBuilder.setContentIntent(clickPendingIntent) | |
| 96 .setDeleteIntent(closePendingIntent); | |
| 97 if (actionPendingIntents != null) { | |
| 98 for (int actionIndex = 0; actionIndex < actionPendingIntents.length; | |
| 99 actionIndex++) { | |
| 100 notificationBuilder.addAction(actionIcons[actionIndex], | |
| 101 actionTitles[actionIndex], | |
| 102 actionPendingIntents[actionIndex]); | |
| 103 } | |
| 104 } | |
| 105 } | |
| 106 } | 67 } |
| OLD | NEW |