OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.webapk.lib.client; | |
6 | |
7 import android.app.PendingIntent; | |
8 import android.content.ComponentName; | |
9 import android.content.Context; | |
10 import android.content.Intent; | |
11 import android.content.ServiceConnection; | |
12 import android.graphics.Bitmap; | |
13 import android.os.IBinder; | |
14 import android.os.RemoteException; | |
15 import android.util.Log; | |
16 | |
17 import org.chromium.webapk.lib.runtime_library.IWebApkApi; | |
18 | |
19 /** | |
20 * NotificationClient provides APIs that a WebAPK host can delegate display or c
lose notification | |
21 * tasks. | |
22 */ | |
23 public class NotificationClient { | |
24 private static final String CATEGORY_MINTED_API = "android.intent.category.M
INTED_API"; | |
25 private static final String TAG = "cr_NotificationClient"; | |
26 | |
27 private IWebApkApi mMintedApi; | |
28 | |
29 public NotificationClient() {} | |
30 | |
31 /** | |
32 * Connect to a bind service of the WebAPK with the given package name, buil
d a notification | |
33 * and hand it over to the WebAPK to display. | |
34 */ | |
35 public void displayNotification(Context context, final int pendingIntentRequ
estCode, | |
36 final NotificationBuilderDelegate notificationBuilder, | |
37 final Intent clickIntent, final Intent closeIntent, final Intent[] a
ctionIntents, | |
38 final String[] actionTitles, final Bitmap[] actionIcons, final int f
lags, | |
39 final String platformTag, final int platformID, String packageNameOf
MintedAPK) { | |
40 Intent intent = createIntentToBindMintedAPI(packageNameOfMintedAPK); | |
41 try { | |
42 context.bindService(intent, new ServiceConnection() { | |
43 @Override | |
44 public void onServiceDisconnected(ComponentName name) { | |
45 } | |
46 @Override | |
47 public void onServiceConnected(ComponentName name, IBinder servi
ce) { | |
48 mMintedApi = IWebApkApi.Stub.asInterface(service); | |
49 Log.d(TAG, "Got minted api: " + mMintedApi); | |
50 try { | |
51 // Create all the PendingIntents needed to build the not
ification. | |
52 PendingIntent clickPendingIntent = mMintedApi.getBroadca
stPendingIntent( | |
53 pendingIntentRequestCode, clickIntent, flags); | |
54 PendingIntent closePendingIntent = mMintedApi.getBroadca
stPendingIntent( | |
55 pendingIntentRequestCode, closeIntent, flags); | |
56 PendingIntent[] actionPendingIntents = null; | |
57 if (actionIntents != null) { | |
58 actionPendingIntents = new PendingIntent[actionInten
ts.length]; | |
59 for (int actionIndex = 0; actionIndex < actionIntent
s.length; | |
60 actionIndex++) { | |
61 actionPendingIntents[actionIndex] = | |
62 mMintedApi.getBroadcastPendingIntent( | |
63 pendingIntentRequestCode, | |
64 actionIntents[actionIndex], flag
s); | |
65 } | |
66 } | |
67 notificationBuilder.setSmallIcon(mMintedApi.getSmallIcon
Id()); | |
68 // Build a notification. | |
69 buildNotification(notificationBuilder, clickPendingInten
t, | |
70 closePendingIntent, actionPendingIntents, action
Titles, | |
71 actionIcons); | |
72 // Dispaly the notification. | |
73 mMintedApi.displayNotification(platformTag, platformID, | |
74 notificationBuilder.build()); | |
75 } catch (RemoteException e) { | |
76 Log.w(TAG, "MintedAPI use failed, exception: " + e); | |
77 } | |
78 } | |
79 }, Context.BIND_AUTO_CREATE); | |
80 } catch (SecurityException e) { | |
81 Log.w(TAG, "Security failed binding."); | |
82 } | |
83 } | |
84 | |
85 /** | |
86 * Connect to a bind service of a WebAPK with the given package name, and as
k the it to close a | |
87 * notification. | |
88 */ | |
89 public void closeNotification(Context context, String packageNameOfMintedAPK
, | |
90 final String platformTag, final int platformID) { | |
91 Intent intent = NotificationClient.createIntentToBindMintedAPI(packageNa
meOfMintedAPK); | |
92 try { | |
93 context.bindService(intent, new ServiceConnection() { | |
94 @Override | |
95 public void onServiceDisconnected(ComponentName name) { | |
96 } | |
97 @Override | |
98 public void onServiceConnected(ComponentName name, IBinder servi
ce) { | |
99 mMintedApi = IWebApkApi.Stub.asInterface(service); | |
100 Log.d(TAG, "Got minted api: " + mMintedApi); | |
101 try { | |
102 mMintedApi.closeNotification(platformTag, platformID); | |
103 } catch (RemoteException e) { | |
104 Log.w(TAG, "MintedAPI use failed, exception: " + e); | |
105 } | |
106 } | |
107 }, Context.BIND_AUTO_CREATE); | |
108 } catch (SecurityException e) { | |
109 Log.w(TAG, "Security failed binding."); | |
110 } | |
111 } | |
112 | |
113 private static Intent createIntentToBindMintedAPI(String packageNameOfMinted
APK) { | |
114 Intent intent = new Intent(); | |
115 intent.addCategory(CATEGORY_MINTED_API); | |
116 intent.setPackage(packageNameOfMintedAPK); | |
117 return intent; | |
118 } | |
119 | |
120 private static void buildNotification(final NotificationBuilderDelegate noti
ficationBuilder, | |
121 final PendingIntent clickPendingIntent, final PendingIntent closePen
dingIntent, | |
122 final PendingIntent[] actionPendingIntents, | |
123 final String[] actionTitles, final Bitmap[] actionIcons) { | |
124 notificationBuilder.setContentIntent(clickPendingIntent) | |
125 .setDeleteIntent(closePendingIntent); | |
126 if (actionPendingIntents != null) { | |
127 for (int actionIndex = 0; actionIndex < actionPendingIntents.length; | |
128 actionIndex++) { | |
129 notificationBuilder.addAction(actionIcons[actionIndex], | |
130 actionTitles[actionIndex], | |
131 actionPendingIntents[actionIndex]); | |
132 } | |
133 } | |
134 } | |
135 } | |
OLD | NEW |