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
,
33 const JavaParamRef
<jclass
>& clazz
) {
34 g_browser_process
->notification_ui_manager();
38 NotificationUIManager
* NotificationUIManager::Create(PrefService
* local_state
) {
39 return new NotificationUIManagerAndroid();
42 NotificationUIManagerAndroid::NotificationUIManagerAndroid() {
44 Java_NotificationUIManager_create(
45 AttachCurrentThread(),
46 reinterpret_cast<intptr_t>(this),
47 base::android::GetApplicationContext()));
50 NotificationUIManagerAndroid::~NotificationUIManagerAndroid() {
51 Java_NotificationUIManager_destroy(AttachCurrentThread(),
55 bool NotificationUIManagerAndroid::OnNotificationClicked(
58 jlong persistent_notification_id
,
61 GURL
origin(ConvertJavaStringToUTF8(env
, java_origin
));
62 std::string tag
= ConvertJavaStringToUTF8(env
, java_tag
);
64 regenerated_notification_infos_
[persistent_notification_id
] =
65 std::make_pair(origin
.spec(), tag
);
67 // TODO(peter): Rather than assuming that the last used profile is the
68 // appropriate one for this notification, the used profile should be
69 // stored as part of the notification's data. See https://crbug.com/437574.
70 PlatformNotificationServiceImpl::GetInstance()->OnPersistentNotificationClick(
71 ProfileManager::GetLastUsedProfile(),
72 persistent_notification_id
,
74 -1 /* action_index */);
79 bool NotificationUIManagerAndroid::OnNotificationClosed(
82 jlong persistent_notification_id
,
85 GURL
origin(ConvertJavaStringToUTF8(env
, java_origin
));
86 std::string tag
= ConvertJavaStringToUTF8(env
, java_tag
);
88 // The notification was closed by the platform, so clear all local state.
89 regenerated_notification_infos_
.erase(persistent_notification_id
);
91 // TODO(peter): Rather than assuming that the last used profile is the
92 // appropriate one for this notification, the used profile should be
93 // stored as part of the notification's data. See https://crbug.com/437574.
94 PlatformNotificationServiceImpl::GetInstance()->OnPersistentNotificationClose(
95 ProfileManager::GetLastUsedProfile(),
96 persistent_notification_id
,
102 void NotificationUIManagerAndroid::Add(const Notification
& notification
,
104 JNIEnv
* env
= AttachCurrentThread();
106 // The Android notification UI manager only supports Web Notifications, which
107 // have a PersistentNotificationDelegate. The persistent id of the
108 // notification is exposed through it's interface.
110 // TODO(peter): When content/ passes a message_center::Notification to the
111 // chrome/ layer, the persistent notification id should be captured as a
112 // property on that object instead, making this cast unnecessary.
113 PersistentNotificationDelegate
* delegate
=
114 static_cast<PersistentNotificationDelegate
*>(notification
.delegate());
117 int64_t persistent_notification_id
= delegate
->persistent_notification_id();
118 GURL
origin_url(notification
.origin_url().GetOrigin());
120 ScopedJavaLocalRef
<jstring
> origin
= ConvertUTF8ToJavaString(
121 env
, origin_url
.spec());
122 ScopedJavaLocalRef
<jstring
> tag
=
123 ConvertUTF8ToJavaString(env
, notification
.tag());
124 ScopedJavaLocalRef
<jstring
> title
= ConvertUTF16ToJavaString(
125 env
, notification
.title());
126 ScopedJavaLocalRef
<jstring
> body
= ConvertUTF16ToJavaString(
127 env
, notification
.message());
129 ScopedJavaLocalRef
<jobject
> icon
;
131 SkBitmap icon_bitmap
= notification
.icon().AsBitmap();
132 if (!icon_bitmap
.isNull())
133 icon
= gfx::ConvertToJavaBitmap(&icon_bitmap
);
135 ScopedJavaLocalRef
<jintArray
> vibration_pattern
=
136 base::android::ToJavaIntArray(env
, notification
.vibration_pattern());
138 Java_NotificationUIManager_displayNotification(
141 persistent_notification_id
,
147 vibration_pattern
.obj(),
148 notification
.silent());
150 regenerated_notification_infos_
[persistent_notification_id
] =
151 std::make_pair(origin_url
.spec(), notification
.tag());
153 notification
.delegate()->Display();
156 bool NotificationUIManagerAndroid::Update(const Notification
& notification
,
162 const Notification
* NotificationUIManagerAndroid::FindById(
163 const std::string
& delegate_id
,
164 ProfileID profile_id
) const {
169 bool NotificationUIManagerAndroid::CancelById(const std::string
& delegate_id
,
170 ProfileID profile_id
) {
171 int64_t persistent_notification_id
= 0;
173 // TODO(peter): Use the |delegate_id| directly when notification ids are being
174 // generated by content/ instead of us.
175 if (!base::StringToInt64(delegate_id
, &persistent_notification_id
))
178 const auto iterator
=
179 regenerated_notification_infos_
.find(persistent_notification_id
);
180 if (iterator
== regenerated_notification_infos_
.end())
183 const RegeneratedNotificationInfo
& notification_info
= iterator
->second
;
185 JNIEnv
* env
= AttachCurrentThread();
187 ScopedJavaLocalRef
<jstring
> origin
=
188 ConvertUTF8ToJavaString(env
, notification_info
.first
);
189 ScopedJavaLocalRef
<jstring
> tag
=
190 ConvertUTF8ToJavaString(env
, notification_info
.second
);
192 regenerated_notification_infos_
.erase(iterator
);
194 Java_NotificationUIManager_closeNotification(env
,
196 persistent_notification_id
,
202 std::set
<std::string
>
203 NotificationUIManagerAndroid::GetAllIdsByProfileAndSourceOrigin(
204 ProfileID profile_id
,
205 const GURL
& source
) {
207 return std::set
<std::string
>();
210 std::set
<std::string
> NotificationUIManagerAndroid::GetAllIdsByProfile(
211 ProfileID profile_id
) {
213 return std::set
<std::string
>();
216 bool NotificationUIManagerAndroid::CancelAllBySourceOrigin(
217 const GURL
& source_origin
) {
222 bool NotificationUIManagerAndroid::CancelAllByProfile(ProfileID profile_id
) {
227 void NotificationUIManagerAndroid::CancelAll() {
231 bool NotificationUIManagerAndroid::RegisterNotificationUIManager(JNIEnv
* env
) {
232 return RegisterNativesImpl(env
);