Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / notifications / notification_ui_manager_android.cc
blobcee5ffaea34865560555a9ae33e6b3a9457156ba
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);
74 return true;
77 bool NotificationUIManagerAndroid::OnNotificationClosed(
78 JNIEnv* env,
79 jobject java_object,
80 jlong persistent_notification_id,
81 jstring java_origin,
82 jstring java_tag) {
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,
95 origin);
97 return true;
100 void NotificationUIManagerAndroid::Add(const Notification& notification,
101 Profile* profile) {
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());
113 DCHECK(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(
137 env,
138 java_object_.obj(),
139 persistent_notification_id,
140 origin.obj(),
141 tag.obj(),
142 title.obj(),
143 body.obj(),
144 icon.obj(),
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,
155 Profile* profile) {
156 NOTREACHED();
157 return false;
160 const Notification* NotificationUIManagerAndroid::FindById(
161 const std::string& delegate_id,
162 ProfileID profile_id) const {
163 NOTREACHED();
164 return nullptr;
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))
174 return false;
176 const auto iterator =
177 regenerated_notification_infos_.find(persistent_notification_id);
178 if (iterator == regenerated_notification_infos_.end())
179 return false;
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,
193 java_object_.obj(),
194 persistent_notification_id,
195 origin.obj(),
196 tag.obj());
197 return true;
200 std::set<std::string>
201 NotificationUIManagerAndroid::GetAllIdsByProfileAndSourceOrigin(
202 Profile* profile,
203 const GURL& source) {
204 NOTREACHED();
205 return std::set<std::string>();
208 std::set<std::string> NotificationUIManagerAndroid::GetAllIdsByProfile(
209 Profile* profile) {
210 NOTREACHED();
211 return std::set<std::string>();
214 bool NotificationUIManagerAndroid::CancelAllBySourceOrigin(
215 const GURL& source_origin) {
216 NOTREACHED();
217 return false;
220 bool NotificationUIManagerAndroid::CancelAllByProfile(ProfileID profile_id) {
221 NOTREACHED();
222 return false;
225 void NotificationUIManagerAndroid::CancelAll() {
226 NOTREACHED();
229 bool NotificationUIManagerAndroid::RegisterNotificationUIManager(JNIEnv* env) {
230 return RegisterNativesImpl(env);