Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(302)

Side by Side Diff: chrome/browser/notifications/notification_ui_manager_android.cc

Issue 1919183003: Rename NotificationUIManager to NotificationPlatformBridge (1/2) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "chrome/browser/notifications/notification_ui_manager_android.h"
6
7 #include <utility>
8 #include <vector>
9
10 #include "base/android/context_utils.h"
11 #include "base/android/jni_array.h"
12 #include "base/android/jni_string.h"
13 #include "base/files/file_path.h"
14 #include "base/logging.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "chrome/browser/browser_process.h"
17 #include "chrome/browser/notifications/notification.h"
18 #include "chrome/browser/notifications/persistent_notification_delegate.h"
19 #include "chrome/browser/notifications/platform_notification_service_impl.h"
20 #include "chrome/browser/profiles/profile_manager.h"
21 #include "content/public/common/persistent_notification_status.h"
22 #include "content/public/common/platform_notification_data.h"
23 #include "jni/NotificationUIManager_jni.h"
24 #include "third_party/skia/include/core/SkBitmap.h"
25 #include "ui/gfx/android/java_bitmap.h"
26 #include "ui/gfx/image/image.h"
27
28 using base::android::AttachCurrentThread;
29 using base::android::ConvertJavaStringToUTF8;
30 using base::android::ConvertUTF16ToJavaString;
31 using base::android::ConvertUTF8ToJavaString;
32
33 namespace {
34
35 ScopedJavaLocalRef<jobjectArray> ConvertToJavaBitmaps(
36 const std::vector<message_center::ButtonInfo>& buttons) {
37 std::vector<SkBitmap> skbitmaps;
38 for (const message_center::ButtonInfo& button : buttons)
39 skbitmaps.push_back(button.icon.AsBitmap());
40
41 JNIEnv* env = AttachCurrentThread();
42 ScopedJavaLocalRef<jclass> clazz =
43 base::android::GetClass(env, "android/graphics/Bitmap");
44 jobjectArray array = env->NewObjectArray(skbitmaps.size(), clazz.obj(),
45 nullptr /* initialElement */);
46 base::android::CheckException(env);
47
48 for (size_t i = 0; i < skbitmaps.size(); ++i) {
49 if (!skbitmaps[i].drawsNothing()) {
50 env->SetObjectArrayElement(
51 array, i, gfx::ConvertToJavaBitmap(&(skbitmaps[i])).obj());
52 }
53 }
54
55 return ScopedJavaLocalRef<jobjectArray>(env, array);
56 }
57
58 } // namespace
59
60 // Called by the Java side when a notification event has been received, but the
61 // NotificationUIManager has not been initialized yet. Enforce initialization of
62 // the class.
63 static void InitializeNotificationUIManager(JNIEnv* env,
64 const JavaParamRef<jclass>& clazz) {
65 g_browser_process->notification_platform_bridge();
66 }
67
68 // static
69 NotificationPlatformBridge* NotificationPlatformBridge::Create() {
70 return new NotificationUIManagerAndroid();
71 }
72
73 NotificationUIManagerAndroid::NotificationUIManagerAndroid() {
74 java_object_.Reset(
75 Java_NotificationUIManager_create(
76 AttachCurrentThread(),
77 reinterpret_cast<intptr_t>(this),
78 base::android::GetApplicationContext()));
79 }
80
81 NotificationUIManagerAndroid::~NotificationUIManagerAndroid() {
82 Java_NotificationUIManager_destroy(AttachCurrentThread(),
83 java_object_.obj());
84 }
85
86 void NotificationUIManagerAndroid::OnNotificationClicked(
87 JNIEnv* env,
88 const JavaParamRef<jobject>& java_object,
89 jlong persistent_notification_id,
90 const JavaParamRef<jstring>& java_origin,
91 const JavaParamRef<jstring>& java_profile_id,
92 jboolean incognito,
93 const JavaParamRef<jstring>& java_tag,
94 jint action_index) {
95 GURL origin(ConvertJavaStringToUTF8(env, java_origin));
96 std::string tag = ConvertJavaStringToUTF8(env, java_tag);
97 std::string profile_id = ConvertJavaStringToUTF8(env, java_profile_id);
98
99 regenerated_notification_infos_[persistent_notification_id] =
100 std::make_pair(origin.spec(), tag);
101
102 PlatformNotificationServiceImpl::GetInstance()
103 ->ProcessPersistentNotificationOperation(
104 PlatformNotificationServiceImpl::NOTIFICATION_CLICK, profile_id,
105 incognito, origin, persistent_notification_id, action_index);
106 }
107
108 void NotificationUIManagerAndroid::OnNotificationClosed(
109 JNIEnv* env,
110 const JavaParamRef<jobject>& java_object,
111 jlong persistent_notification_id,
112 const JavaParamRef<jstring>& java_origin,
113 const JavaParamRef<jstring>& java_profile_id,
114 jboolean incognito,
115 const JavaParamRef<jstring>& java_tag,
116 jboolean by_user) {
117 GURL origin(ConvertJavaStringToUTF8(env, java_origin));
118 std::string profile_id = ConvertJavaStringToUTF8(env, java_profile_id);
119 std::string tag = ConvertJavaStringToUTF8(env, java_tag);
120
121 // The notification was closed by the platform, so clear all local state.
122 regenerated_notification_infos_.erase(persistent_notification_id);
123 PlatformNotificationServiceImpl::GetInstance()
124 ->ProcessPersistentNotificationOperation(
125 PlatformNotificationServiceImpl::NOTIFICATION_CLOSE, profile_id,
126 incognito, origin, persistent_notification_id, -1);
127 }
128
129 void NotificationUIManagerAndroid::Display(const std::string& notification_id,
130 const std::string& profile_id,
131 bool incognito,
132 const Notification& notification) {
133 JNIEnv* env = AttachCurrentThread();
134
135 // The Android notification UI manager only supports Web Notifications, which
136 // have a PersistentNotificationDelegate. The persistent id of the
137 // notification is exposed through it's interface.
138 //
139 // TODO(peter): When content/ passes a message_center::Notification to the
140 // chrome/ layer, the persistent notification id should be captured as a
141 // property on that object instead, making this cast unnecessary.
142 PersistentNotificationDelegate* delegate =
143 static_cast<PersistentNotificationDelegate*>(notification.delegate());
144 DCHECK(delegate);
145
146 int64_t persistent_notification_id = delegate->persistent_notification_id();
147 GURL origin_url(notification.origin_url().GetOrigin());
148
149 ScopedJavaLocalRef<jstring> origin = ConvertUTF8ToJavaString(
150 env, origin_url.spec());
151 ScopedJavaLocalRef<jstring> tag =
152 ConvertUTF8ToJavaString(env, notification.tag());
153 ScopedJavaLocalRef<jstring> title = ConvertUTF16ToJavaString(
154 env, notification.title());
155 ScopedJavaLocalRef<jstring> body = ConvertUTF16ToJavaString(
156 env, notification.message());
157
158 ScopedJavaLocalRef<jobject> notification_icon;
159 SkBitmap notification_icon_bitmap = notification.icon().AsBitmap();
160 if (!notification_icon_bitmap.drawsNothing())
161 notification_icon = gfx::ConvertToJavaBitmap(&notification_icon_bitmap);
162
163 ScopedJavaLocalRef<jobject> badge;
164 SkBitmap badge_bitmap = notification.small_image().AsBitmap();
165 if (!badge_bitmap.drawsNothing())
166 badge = gfx::ConvertToJavaBitmap(&badge_bitmap);
167
168 std::vector<base::string16> action_titles_vector;
169 for (const message_center::ButtonInfo& button : notification.buttons())
170 action_titles_vector.push_back(button.title);
171 ScopedJavaLocalRef<jobjectArray> action_titles =
172 base::android::ToJavaArrayOfStrings(env, action_titles_vector);
173
174 ScopedJavaLocalRef<jobjectArray> action_icons =
175 ConvertToJavaBitmaps(notification.buttons());
176
177 ScopedJavaLocalRef<jintArray> vibration_pattern =
178 base::android::ToJavaIntArray(env, notification.vibration_pattern());
179
180 ScopedJavaLocalRef<jstring> j_profile_id =
181 ConvertUTF8ToJavaString(env, profile_id);
182
183 Java_NotificationUIManager_displayNotification(
184 env, java_object_.obj(), persistent_notification_id, origin.obj(),
185 j_profile_id.obj(), incognito, tag.obj(), title.obj(), body.obj(),
186 notification_icon.obj(), badge.obj(), vibration_pattern.obj(),
187 notification.timestamp().ToJavaTime(), notification.renotify(),
188 notification.silent(), action_titles.obj(), action_icons.obj());
189
190 regenerated_notification_infos_[persistent_notification_id] =
191 std::make_pair(origin_url.spec(), notification.tag());
192 }
193
194 void NotificationUIManagerAndroid::Close(const std::string& profile_id,
195 const std::string& notification_id) {
196 int64_t persistent_notification_id = 0;
197
198 // TODO(peter): Use the |delegate_id| directly when notification ids are being
199 // generated by content/ instead of us.
200 if (!base::StringToInt64(notification_id, &persistent_notification_id)) {
201 LOG(WARNING) << "Unable to decode notification_id " << notification_id;
202 return;
203 }
204
205 const auto iterator =
206 regenerated_notification_infos_.find(persistent_notification_id);
207 if (iterator == regenerated_notification_infos_.end())
208 return;
209
210 const RegeneratedNotificationInfo& notification_info = iterator->second;
211
212 JNIEnv* env = AttachCurrentThread();
213
214 ScopedJavaLocalRef<jstring> origin =
215 ConvertUTF8ToJavaString(env, notification_info.first);
216 ScopedJavaLocalRef<jstring> tag =
217 ConvertUTF8ToJavaString(env, notification_info.second);
218 ScopedJavaLocalRef<jstring> j_profile_id =
219 ConvertUTF8ToJavaString(env, profile_id);
220
221 regenerated_notification_infos_.erase(iterator);
222
223 Java_NotificationUIManager_closeNotification(
224 env, java_object_.obj(), j_profile_id.obj(), persistent_notification_id,
225 origin.obj(), tag.obj());
226 }
227
228 bool NotificationUIManagerAndroid::GetDisplayed(
229 const std::string& profile_id,
230 bool incognito,
231 std::set<std::string>* notifications) const {
232 // TODO(miguelg): This can actually be implemented for M+
233 return false;
234 }
235
236 bool NotificationUIManagerAndroid::SupportsNotificationCenter() const {
237 return true;
238 }
239
240 bool NotificationUIManagerAndroid::RegisterNotificationPlatformBridge(
241 JNIEnv* env) {
242 return RegisterNativesImpl(env);
243 }
OLDNEW
« no previous file with comments | « chrome/browser/notifications/notification_ui_manager_android.h ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698