Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / chrome / browser / notifications / notification_ui_manager_android.cc
blob0529ff203fbf5b32f50b25d0bf1dada4fcc779ef
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"
7 #include <utility>
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
31 // the class.
32 static void InitializeNotificationUIManager(JNIEnv* env, jclass clazz) {
33 g_browser_process->notification_ui_manager();
36 // static
37 NotificationUIManager* NotificationUIManager::Create(PrefService* local_state) {
38 return new NotificationUIManagerAndroid();
41 NotificationUIManagerAndroid::NotificationUIManagerAndroid() {
42 java_object_.Reset(
43 Java_NotificationUIManager_create(
44 AttachCurrentThread(),
45 reinterpret_cast<intptr_t>(this),
46 base::android::GetApplicationContext()));
49 NotificationUIManagerAndroid::~NotificationUIManagerAndroid() {
50 Java_NotificationUIManager_destroy(AttachCurrentThread(),
51 java_object_.obj());
54 bool NotificationUIManagerAndroid::OnNotificationClicked(
55 JNIEnv* env,
56 jobject java_object,
57 jlong persistent_notification_id,
58 jstring java_origin,
59 jstring java_tag) {
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,
72 origin,
73 -1 /* action_index */);
75 return true;
78 bool NotificationUIManagerAndroid::OnNotificationClosed(
79 JNIEnv* env,
80 jobject java_object,
81 jlong persistent_notification_id,
82 jstring java_origin,
83 jstring java_tag) {
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,
96 origin);
98 return true;
101 void NotificationUIManagerAndroid::Add(const Notification& notification,
102 Profile* profile) {
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());
114 DCHECK(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(
138 env,
139 java_object_.obj(),
140 persistent_notification_id,
141 origin.obj(),
142 tag.obj(),
143 title.obj(),
144 body.obj(),
145 icon.obj(),
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,
156 Profile* profile) {
157 NOTREACHED();
158 return false;
161 const Notification* NotificationUIManagerAndroid::FindById(
162 const std::string& delegate_id,
163 ProfileID profile_id) const {
164 NOTREACHED();
165 return nullptr;
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))
175 return false;
177 const auto iterator =
178 regenerated_notification_infos_.find(persistent_notification_id);
179 if (iterator == regenerated_notification_infos_.end())
180 return false;
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,
194 java_object_.obj(),
195 persistent_notification_id,
196 origin.obj(),
197 tag.obj());
198 return true;
201 std::set<std::string>
202 NotificationUIManagerAndroid::GetAllIdsByProfileAndSourceOrigin(
203 ProfileID profile_id,
204 const GURL& source) {
205 NOTREACHED();
206 return std::set<std::string>();
209 std::set<std::string> NotificationUIManagerAndroid::GetAllIdsByProfile(
210 ProfileID profile_id) {
211 NOTREACHED();
212 return std::set<std::string>();
215 bool NotificationUIManagerAndroid::CancelAllBySourceOrigin(
216 const GURL& source_origin) {
217 NOTREACHED();
218 return false;
221 bool NotificationUIManagerAndroid::CancelAllByProfile(ProfileID profile_id) {
222 NOTREACHED();
223 return false;
226 void NotificationUIManagerAndroid::CancelAll() {
227 NOTREACHED();
230 bool NotificationUIManagerAndroid::RegisterNotificationUIManager(JNIEnv* env) {
231 return RegisterNativesImpl(env);