OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "chrome/browser/notifications/notification_platform_bridge_android.h" | 5 #include "chrome/browser/notifications/notification_platform_bridge_android.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/android/jni_array.h" | 10 #include "base/android/jni_array.h" |
11 #include "base/android/jni_string.h" | 11 #include "base/android/jni_string.h" |
12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/strings/nullable_string16.h" | 15 #include "base/strings/nullable_string16.h" |
16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
17 #include "chrome/browser/browser_process.h" | 17 #include "chrome/browser/browser_process.h" |
18 #include "chrome/browser/notifications/native_notification_display_service.h" | 18 #include "chrome/browser/notifications/native_notification_display_service.h" |
19 #include "chrome/browser/notifications/notification.h" | 19 #include "chrome/browser/notifications/notification.h" |
20 #include "chrome/browser/notifications/notification_common.h" | 20 #include "chrome/browser/notifications/notification_common.h" |
21 #include "chrome/browser/notifications/notification_display_service_factory.h" | 21 #include "chrome/browser/notifications/notification_display_service_factory.h" |
22 #include "chrome/browser/notifications/persistent_notification_delegate.h" | 22 #include "chrome/browser/notifications/persistent_notification_delegate.h" |
23 #include "chrome/browser/notifications/platform_notification_service_impl.h" | 23 #include "chrome/browser/notifications/platform_notification_service_impl.h" |
24 #include "chrome/browser/profiles/profile_manager.h" | 24 #include "chrome/browser/profiles/profile_manager.h" |
25 #include "chrome/common/chrome_switches.h" | 25 #include "chrome/common/chrome_switches.h" |
26 #include "chrome/common/pref_names.h" | 26 #include "chrome/common/pref_names.h" |
27 #include "components/pref_registry/pref_registry_syncable.h" | 27 #include "components/pref_registry/pref_registry_syncable.h" |
28 #include "content/public/common/persistent_notification_status.h" | 28 #include "content/public/common/persistent_notification_status.h" |
29 #include "content/public/common/platform_notification_data.h" | 29 #include "content/public/common/platform_notification_data.h" |
| 30 #include "jni/ActionInfo_jni.h" |
30 #include "jni/NotificationPlatformBridge_jni.h" | 31 #include "jni/NotificationPlatformBridge_jni.h" |
31 #include "third_party/skia/include/core/SkBitmap.h" | 32 #include "third_party/skia/include/core/SkBitmap.h" |
32 #include "ui/gfx/android/java_bitmap.h" | 33 #include "ui/gfx/android/java_bitmap.h" |
33 #include "ui/gfx/image/image.h" | 34 #include "ui/gfx/image/image.h" |
34 | 35 |
35 using base::android::AttachCurrentThread; | 36 using base::android::AttachCurrentThread; |
36 using base::android::ConvertJavaStringToUTF8; | 37 using base::android::ConvertJavaStringToUTF8; |
37 using base::android::ConvertJavaStringToUTF16; | 38 using base::android::ConvertJavaStringToUTF16; |
38 using base::android::ConvertUTF16ToJavaString; | 39 using base::android::ConvertUTF16ToJavaString; |
39 using base::android::ConvertUTF8ToJavaString; | 40 using base::android::ConvertUTF8ToJavaString; |
40 using base::android::JavaParamRef; | 41 using base::android::JavaParamRef; |
41 using base::android::ScopedJavaLocalRef; | 42 using base::android::ScopedJavaLocalRef; |
42 | 43 |
43 namespace { | 44 namespace { |
44 | 45 |
45 ScopedJavaLocalRef<jobjectArray> ConvertToJavaBitmaps( | 46 // A Java counterpart will be generated for this enum. |
| 47 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.chrome.browser.notifications |
| 48 enum NotificationActionType { |
| 49 // NB. Making this a one-line enum breaks code generation! crbug.com/657847 |
| 50 BUTTON, |
| 51 TEXT |
| 52 }; |
| 53 |
| 54 ScopedJavaLocalRef<jobject> ConvertToJavaBitmap(JNIEnv* env, |
| 55 const gfx::Image& icon) { |
| 56 SkBitmap skbitmap = icon.AsBitmap(); |
| 57 ScopedJavaLocalRef<jobject> j_bitmap; |
| 58 if (!skbitmap.drawsNothing()) |
| 59 j_bitmap = gfx::ConvertToJavaBitmap(&skbitmap); |
| 60 return j_bitmap; |
| 61 } |
| 62 |
| 63 NotificationActionType GetNotificationActionType( |
| 64 message_center::ButtonInfo button) { |
| 65 switch (button.type) { |
| 66 case message_center::ButtonType::BUTTON: |
| 67 return NotificationActionType::BUTTON; |
| 68 case message_center::ButtonType::TEXT: |
| 69 return NotificationActionType::TEXT; |
| 70 } |
| 71 NOTREACHED(); |
| 72 return NotificationActionType::TEXT; |
| 73 } |
| 74 |
| 75 ScopedJavaLocalRef<jobjectArray> ConvertToJavaActionInfos( |
46 const std::vector<message_center::ButtonInfo>& buttons) { | 76 const std::vector<message_center::ButtonInfo>& buttons) { |
47 std::vector<SkBitmap> skbitmaps; | |
48 for (const message_center::ButtonInfo& button : buttons) | |
49 skbitmaps.push_back(button.icon.AsBitmap()); | |
50 | |
51 JNIEnv* env = AttachCurrentThread(); | 77 JNIEnv* env = AttachCurrentThread(); |
52 ScopedJavaLocalRef<jclass> clazz = | 78 ScopedJavaLocalRef<jclass> clazz = base::android::GetClass( |
53 base::android::GetClass(env, "android/graphics/Bitmap"); | 79 env, "org/chromium/chrome/browser/notifications/ActionInfo"); |
54 jobjectArray array = env->NewObjectArray(skbitmaps.size(), clazz.obj(), | 80 jobjectArray actions = env->NewObjectArray(buttons.size(), clazz.obj(), |
55 nullptr /* initialElement */); | 81 nullptr /* initialElement */); |
56 base::android::CheckException(env); | 82 base::android::CheckException(env); |
57 | 83 |
58 for (size_t i = 0; i < skbitmaps.size(); ++i) { | 84 for (size_t i = 0; i < buttons.size(); ++i) { |
59 if (!skbitmaps[i].drawsNothing()) { | 85 const auto& button = buttons[i]; |
60 env->SetObjectArrayElement( | 86 ScopedJavaLocalRef<jstring> title = |
61 array, i, gfx::ConvertToJavaBitmap(&(skbitmaps[i])).obj()); | 87 base::android::ConvertUTF16ToJavaString(env, button.title); |
62 } | 88 int type = GetNotificationActionType(button); |
| 89 ScopedJavaLocalRef<jstring> placeholder = |
| 90 base::android::ConvertUTF16ToJavaString(env, button.placeholder); |
| 91 ScopedJavaLocalRef<jobject> icon = ConvertToJavaBitmap(env, button.icon); |
| 92 ScopedJavaLocalRef<jobject> action_info = |
| 93 Java_ActionInfo_createActionInfo(AttachCurrentThread(), title.obj(), |
| 94 icon.obj(), type, placeholder.obj()); |
| 95 env->SetObjectArrayElement(actions, i, action_info.obj()); |
63 } | 96 } |
64 | 97 |
65 return ScopedJavaLocalRef<jobjectArray>(env, array); | 98 return ScopedJavaLocalRef<jobjectArray>(env, actions); |
66 } | 99 } |
67 | 100 |
68 // Callback to run once the profile has been loaded in order to perform a | 101 // Callback to run once the profile has been loaded in order to perform a |
69 // given |operation| in a notification. | 102 // given |operation| in a notification. |
70 // TODO(miguelg) move it to notification_common? | 103 // TODO(miguelg) move it to notification_common? |
71 void ProfileLoadedCallback(NotificationCommon::Operation operation, | 104 void ProfileLoadedCallback(NotificationCommon::Operation operation, |
72 NotificationCommon::Type notification_type, | 105 NotificationCommon::Type notification_type, |
73 const std::string& origin, | 106 const std::string& origin, |
74 const std::string& notification_id, | 107 const std::string& notification_id, |
75 int action_index, | 108 int action_index, |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 ScopedJavaLocalRef<jobject> notification_icon; | 254 ScopedJavaLocalRef<jobject> notification_icon; |
222 SkBitmap notification_icon_bitmap = notification.icon().AsBitmap(); | 255 SkBitmap notification_icon_bitmap = notification.icon().AsBitmap(); |
223 if (!notification_icon_bitmap.drawsNothing()) | 256 if (!notification_icon_bitmap.drawsNothing()) |
224 notification_icon = gfx::ConvertToJavaBitmap(¬ification_icon_bitmap); | 257 notification_icon = gfx::ConvertToJavaBitmap(¬ification_icon_bitmap); |
225 | 258 |
226 ScopedJavaLocalRef<jobject> badge; | 259 ScopedJavaLocalRef<jobject> badge; |
227 SkBitmap badge_bitmap = notification.small_image().AsBitmap(); | 260 SkBitmap badge_bitmap = notification.small_image().AsBitmap(); |
228 if (!badge_bitmap.drawsNothing()) | 261 if (!badge_bitmap.drawsNothing()) |
229 badge = gfx::ConvertToJavaBitmap(&badge_bitmap); | 262 badge = gfx::ConvertToJavaBitmap(&badge_bitmap); |
230 | 263 |
231 // TODO(crbug.com/650302): Combine these action_* vectors into a single vector | 264 ScopedJavaLocalRef<jobjectArray> actions = |
232 // of objects. | 265 ConvertToJavaActionInfos(notification.buttons()); |
233 std::vector<base::string16> action_titles_vector; | |
234 std::vector<base::string16> action_types_vector; | |
235 std::vector<base::string16> action_placeholders_vector; | |
236 for (const message_center::ButtonInfo& button : notification.buttons()) { | |
237 action_titles_vector.push_back(button.title); | |
238 action_placeholders_vector.push_back(button.placeholder); | |
239 base::string16 type; | |
240 switch (button.type) { | |
241 case message_center::ButtonType::BUTTON: | |
242 type = base::ASCIIToUTF16("button"); | |
243 break; | |
244 case message_center::ButtonType::TEXT: | |
245 type = base::ASCIIToUTF16("text"); | |
246 break; | |
247 } | |
248 action_types_vector.push_back(std::move(type)); | |
249 } | |
250 ScopedJavaLocalRef<jobjectArray> action_titles = | |
251 base::android::ToJavaArrayOfStrings(env, action_titles_vector); | |
252 | |
253 ScopedJavaLocalRef<jobjectArray> action_types = | |
254 base::android::ToJavaArrayOfStrings(env, action_types_vector); | |
255 | |
256 ScopedJavaLocalRef<jobjectArray> action_placeholders = | |
257 base::android::ToJavaArrayOfStrings(env, action_placeholders_vector); | |
258 | |
259 ScopedJavaLocalRef<jobjectArray> action_icons = | |
260 ConvertToJavaBitmaps(notification.buttons()); | |
261 | 266 |
262 ScopedJavaLocalRef<jintArray> vibration_pattern = | 267 ScopedJavaLocalRef<jintArray> vibration_pattern = |
263 base::android::ToJavaIntArray(env, notification.vibration_pattern()); | 268 base::android::ToJavaIntArray(env, notification.vibration_pattern()); |
264 | 269 |
265 ScopedJavaLocalRef<jstring> j_profile_id = | 270 ScopedJavaLocalRef<jstring> j_profile_id = |
266 ConvertUTF8ToJavaString(env, profile_id); | 271 ConvertUTF8ToJavaString(env, profile_id); |
267 | 272 |
268 Java_NotificationPlatformBridge_displayNotification( | 273 Java_NotificationPlatformBridge_displayNotification( |
269 env, java_object_, j_notification_id, j_origin, j_profile_id, incognito, | 274 env, java_object_, j_notification_id, j_origin, j_profile_id, incognito, |
270 tag, webapk_package, title, body, image, notification_icon, badge, | 275 tag, webapk_package, title, body, image, notification_icon, badge, |
271 vibration_pattern, notification.timestamp().ToJavaTime(), | 276 vibration_pattern, notification.timestamp().ToJavaTime(), |
272 notification.renotify(), notification.silent(), action_titles, | 277 notification.renotify(), notification.silent(), actions); |
273 action_icons, action_types, action_placeholders); | |
274 | 278 |
275 regenerated_notification_infos_[notification_id] = | 279 regenerated_notification_infos_[notification_id] = |
276 RegeneratedNotificationInfo(origin_url.spec(), notification.tag(), | 280 RegeneratedNotificationInfo(origin_url.spec(), notification.tag(), |
277 ConvertJavaStringToUTF8(env, webapk_package)); | 281 ConvertJavaStringToUTF8(env, webapk_package)); |
278 } | 282 } |
279 | 283 |
280 void NotificationPlatformBridgeAndroid::Close( | 284 void NotificationPlatformBridgeAndroid::Close( |
281 const std::string& profile_id, | 285 const std::string& profile_id, |
282 const std::string& notification_id) { | 286 const std::string& notification_id) { |
283 const auto iterator = regenerated_notification_infos_.find(notification_id); | 287 const auto iterator = regenerated_notification_infos_.find(notification_id); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
331 RegeneratedNotificationInfo() {} | 335 RegeneratedNotificationInfo() {} |
332 | 336 |
333 NotificationPlatformBridgeAndroid::RegeneratedNotificationInfo:: | 337 NotificationPlatformBridgeAndroid::RegeneratedNotificationInfo:: |
334 RegeneratedNotificationInfo(const std::string& origin, | 338 RegeneratedNotificationInfo(const std::string& origin, |
335 const std::string& tag, | 339 const std::string& tag, |
336 const std::string& webapk_package) | 340 const std::string& webapk_package) |
337 : origin(origin), tag(tag), webapk_package(webapk_package) {} | 341 : origin(origin), tag(tag), webapk_package(webapk_package) {} |
338 | 342 |
339 NotificationPlatformBridgeAndroid::RegeneratedNotificationInfo:: | 343 NotificationPlatformBridgeAndroid::RegeneratedNotificationInfo:: |
340 ~RegeneratedNotificationInfo() {} | 344 ~RegeneratedNotificationInfo() {} |
OLD | NEW |