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
,
73 -1 /* action_index */);
78 bool NotificationUIManagerAndroid::OnNotificationClosed(
81 jlong persistent_notification_id
,
84 GURL
origin(ConvertJavaStringToUTF8(env
, java_origin
));
85 std::string tag
= ConvertJavaStringToUTF8(env
, java_tag
);
87 // The notification was closed by the platform, so clear all local state.
88 regenerated_notification_infos_
.erase(persistent_notification_id
);
90 // TODO(peter): Rather than assuming that the last used profile is the
91 // appropriate one for this notification, the used profile should be
92 // stored as part of the notification's data. See https://crbug.com/437574.
93 PlatformNotificationServiceImpl::GetInstance()->OnPersistentNotificationClose(
94 ProfileManager::GetLastUsedProfile(),
95 persistent_notification_id
,
101 void NotificationUIManagerAndroid::Add(const Notification
& notification
,
103 JNIEnv
* env
= AttachCurrentThread();
105 // The Android notification UI manager only supports Web Notifications, which
106 // have a PersistentNotificationDelegate. The persistent id of the
107 // notification is exposed through it's interface.
109 // TODO(peter): When content/ passes a message_center::Notification to the
110 // chrome/ layer, the persistent notification id should be captured as a
111 // property on that object instead, making this cast unnecessary.
112 PersistentNotificationDelegate
* delegate
=
113 static_cast<PersistentNotificationDelegate
*>(notification
.delegate());
116 int64_t persistent_notification_id
= delegate
->persistent_notification_id();
117 GURL
origin_url(notification
.origin_url().GetOrigin());
119 ScopedJavaLocalRef
<jstring
> origin
= ConvertUTF8ToJavaString(
120 env
, origin_url
.spec());
121 ScopedJavaLocalRef
<jstring
> tag
=
122 ConvertUTF8ToJavaString(env
, notification
.tag());
123 ScopedJavaLocalRef
<jstring
> title
= ConvertUTF16ToJavaString(
124 env
, notification
.title());
125 ScopedJavaLocalRef
<jstring
> body
= ConvertUTF16ToJavaString(
126 env
, notification
.message());
128 ScopedJavaLocalRef
<jobject
> icon
;
130 SkBitmap icon_bitmap
= notification
.icon().AsBitmap();
131 if (!icon_bitmap
.isNull())
132 icon
= gfx::ConvertToJavaBitmap(&icon_bitmap
);
134 ScopedJavaLocalRef
<jintArray
> vibration_pattern
=
135 base::android::ToJavaIntArray(env
, notification
.vibration_pattern());
137 Java_NotificationUIManager_displayNotification(
140 persistent_notification_id
,
146 vibration_pattern
.obj(),
147 notification
.silent());
149 regenerated_notification_infos_
[persistent_notification_id
] =
150 std::make_pair(origin_url
.spec(), notification
.tag());
152 notification
.delegate()->Display();
155 bool NotificationUIManagerAndroid::Update(const Notification
& notification
,
161 const Notification
* NotificationUIManagerAndroid::FindById(
162 const std::string
& delegate_id
,
163 ProfileID profile_id
) const {
168 bool NotificationUIManagerAndroid::CancelById(const std::string
& delegate_id
,
169 ProfileID profile_id
) {
170 int64_t persistent_notification_id
= 0;
172 // TODO(peter): Use the |delegate_id| directly when notification ids are being
173 // generated by content/ instead of us.
174 if (!base::StringToInt64(delegate_id
, &persistent_notification_id
))
177 const auto iterator
=
178 regenerated_notification_infos_
.find(persistent_notification_id
);
179 if (iterator
== regenerated_notification_infos_
.end())
182 const RegeneratedNotificationInfo
& notification_info
= iterator
->second
;
184 JNIEnv
* env
= AttachCurrentThread();
186 ScopedJavaLocalRef
<jstring
> origin
=
187 ConvertUTF8ToJavaString(env
, notification_info
.first
);
188 ScopedJavaLocalRef
<jstring
> tag
=
189 ConvertUTF8ToJavaString(env
, notification_info
.second
);
191 regenerated_notification_infos_
.erase(iterator
);
193 Java_NotificationUIManager_closeNotification(env
,
195 persistent_notification_id
,
201 std::set
<std::string
>
202 NotificationUIManagerAndroid::GetAllIdsByProfileAndSourceOrigin(
203 ProfileID profile_id
,
204 const GURL
& source
) {
206 return std::set
<std::string
>();
209 std::set
<std::string
> NotificationUIManagerAndroid::GetAllIdsByProfile(
210 ProfileID profile_id
) {
212 return std::set
<std::string
>();
215 bool NotificationUIManagerAndroid::CancelAllBySourceOrigin(
216 const GURL
& source_origin
) {
221 bool NotificationUIManagerAndroid::CancelAllByProfile(ProfileID profile_id
) {
226 void NotificationUIManagerAndroid::CancelAll() {
230 bool NotificationUIManagerAndroid::RegisterNotificationUIManager(JNIEnv
* env
) {
231 return RegisterNativesImpl(env
);