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
3 // found in the LICENSE file.
5 #include "chrome/browser/notifications/notification_ui_manager_android.h"
9 #include "base/android/jni_array.h"
10 #include "base/android/jni_string.h"
11 #include "base/logging.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/notifications/notification.h"
15 #include "chrome/browser/notifications/persistent_notification_delegate.h"
16 #include "chrome/browser/notifications/platform_notification_service_impl.h"
17 #include "chrome/browser/profiles/profile_manager.h"
18 #include "content/public/common/persistent_notification_status.h"
19 #include "content/public/common/platform_notification_data.h"
20 #include "jni/NotificationUIManager_jni.h"
21 #include "ui/gfx/android/java_bitmap.h"
22 #include "ui/gfx/image/image.h"
24 using base::android::AttachCurrentThread
;
25 using base::android::ConvertJavaStringToUTF8
;
26 using base::android::ConvertUTF16ToJavaString
;
27 using base::android::ConvertUTF8ToJavaString
;
29 // Called by the Java side when a notification event has been received, but the
30 // NotificationUIManager has not been initialized yet. Enforce initialization of
32 static void InitializeNotificationUIManager(JNIEnv
* env
, jclass clazz
) {
33 g_browser_process
->notification_ui_manager();
37 NotificationUIManager
* NotificationUIManager::Create(PrefService
* local_state
) {
38 return new NotificationUIManagerAndroid();
41 NotificationUIManagerAndroid::NotificationUIManagerAndroid() {
43 Java_NotificationUIManager_create(
44 AttachCurrentThread(),
45 reinterpret_cast<intptr_t>(this),
46 base::android::GetApplicationContext()));
49 NotificationUIManagerAndroid::~NotificationUIManagerAndroid() {
50 Java_NotificationUIManager_destroy(AttachCurrentThread(),
54 bool NotificationUIManagerAndroid::OnNotificationClicked(
57 jlong persistent_notification_id
,
60 GURL
origin(ConvertJavaStringToUTF8(env
, java_origin
));
61 std::string tag
= ConvertJavaStringToUTF8(env
, java_tag
);
63 regenerated_notification_infos_
[persistent_notification_id
] =
64 std::make_pair(origin
.spec(), tag
);
66 // TODO(peter): Rather than assuming that the last used profile is the
67 // appropriate one for this notification, the used profile should be
68 // stored as part of the notification's data. See https://crbug.com/437574.
69 PlatformNotificationServiceImpl::GetInstance()->OnPersistentNotificationClick(
70 ProfileManager::GetLastUsedProfile(),
71 persistent_notification_id
,
77 bool NotificationUIManagerAndroid::OnNotificationClosed(
80 jlong persistent_notification_id
,
83 GURL
origin(ConvertJavaStringToUTF8(env
, java_origin
));
84 std::string tag
= ConvertJavaStringToUTF8(env
, java_tag
);
86 // The notification was closed by the platform, so clear all local state.
87 regenerated_notification_infos_
.erase(persistent_notification_id
);
89 // TODO(peter): Rather than assuming that the last used profile is the
90 // appropriate one for this notification, the used profile should be
91 // stored as part of the notification's data. See https://crbug.com/437574.
92 PlatformNotificationServiceImpl::GetInstance()->OnPersistentNotificationClose(
93 ProfileManager::GetLastUsedProfile(),
94 persistent_notification_id
,
100 void NotificationUIManagerAndroid::Add(const Notification
& notification
,
102 JNIEnv
* env
= AttachCurrentThread();
104 // The Android notification UI manager only supports Web Notifications, which
105 // have a PersistentNotificationDelegate. The persistent id of the
106 // notification is exposed through it's interface.
108 // TODO(peter): When content/ passes a message_center::Notification to the
109 // chrome/ layer, the persistent notification id should be captured as a
110 // property on that object instead, making this cast unnecessary.
111 PersistentNotificationDelegate
* delegate
=
112 static_cast<PersistentNotificationDelegate
*>(notification
.delegate());
115 int64_t persistent_notification_id
= delegate
->persistent_notification_id();
116 GURL
origin_url(notification
.origin_url().GetOrigin());
118 ScopedJavaLocalRef
<jstring
> origin
= ConvertUTF8ToJavaString(
119 env
, origin_url
.spec());
120 ScopedJavaLocalRef
<jstring
> tag
=
121 ConvertUTF8ToJavaString(env
, notification
.tag());
122 ScopedJavaLocalRef
<jstring
> title
= ConvertUTF16ToJavaString(
123 env
, notification
.title());
124 ScopedJavaLocalRef
<jstring
> body
= ConvertUTF16ToJavaString(
125 env
, notification
.message());
127 ScopedJavaLocalRef
<jobject
> icon
;
129 SkBitmap icon_bitmap
= notification
.icon().AsBitmap();
130 if (!icon_bitmap
.isNull())
131 icon
= gfx::ConvertToJavaBitmap(&icon_bitmap
);
133 ScopedJavaLocalRef
<jintArray
> vibration_pattern
=
134 base::android::ToJavaIntArray(env
, notification
.vibration_pattern());
136 Java_NotificationUIManager_displayNotification(
139 persistent_notification_id
,
145 vibration_pattern
.obj(),
146 notification
.silent());
148 regenerated_notification_infos_
[persistent_notification_id
] =
149 std::make_pair(origin_url
.spec(), notification
.tag());
151 notification
.delegate()->Display();
154 bool NotificationUIManagerAndroid::Update(const Notification
& notification
,
160 const Notification
* NotificationUIManagerAndroid::FindById(
161 const std::string
& delegate_id
,
162 ProfileID profile_id
) const {
167 bool NotificationUIManagerAndroid::CancelById(const std::string
& delegate_id
,
168 ProfileID profile_id
) {
169 int64_t persistent_notification_id
= 0;
171 // TODO(peter): Use the |delegate_id| directly when notification ids are being
172 // generated by content/ instead of us.
173 if (!base::StringToInt64(delegate_id
, &persistent_notification_id
))
176 const auto iterator
=
177 regenerated_notification_infos_
.find(persistent_notification_id
);
178 if (iterator
== regenerated_notification_infos_
.end())
181 const RegeneratedNotificationInfo
& notification_info
= iterator
->second
;
183 JNIEnv
* env
= AttachCurrentThread();
185 ScopedJavaLocalRef
<jstring
> origin
=
186 ConvertUTF8ToJavaString(env
, notification_info
.first
);
187 ScopedJavaLocalRef
<jstring
> tag
=
188 ConvertUTF8ToJavaString(env
, notification_info
.second
);
190 regenerated_notification_infos_
.erase(iterator
);
192 Java_NotificationUIManager_closeNotification(env
,
194 persistent_notification_id
,
200 std::set
<std::string
>
201 NotificationUIManagerAndroid::GetAllIdsByProfileAndSourceOrigin(
203 const GURL
& source
) {
205 return std::set
<std::string
>();
208 std::set
<std::string
> NotificationUIManagerAndroid::GetAllIdsByProfile(
211 return std::set
<std::string
>();
214 bool NotificationUIManagerAndroid::CancelAllBySourceOrigin(
215 const GURL
& source_origin
) {
220 bool NotificationUIManagerAndroid::CancelAllByProfile(ProfileID profile_id
) {
225 void NotificationUIManagerAndroid::CancelAll() {
229 bool NotificationUIManagerAndroid::RegisterNotificationUIManager(JNIEnv
* env
) {
230 return RegisterNativesImpl(env
);