OLD | NEW |
| (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 package org.chromium.chrome.browser; | |
6 | |
7 import android.graphics.Bitmap; | |
8 import android.util.LruCache; | |
9 import android.util.Pair; | |
10 | |
11 import org.chromium.base.CalledByNative; | |
12 import org.chromium.base.JNINamespace; | |
13 import org.chromium.base.ObserverList; | |
14 import org.chromium.chrome.browser.profiles.Profile; | |
15 import org.chromium.components.bookmarks.BookmarkId; | |
16 | |
17 import java.util.ArrayList; | |
18 import java.util.Arrays; | |
19 import java.util.List; | |
20 import java.util.Map; | |
21 | |
22 /** | |
23 * Access gate to C++ side enhanced bookmarks functionalities. | |
24 */ | |
25 @JNINamespace("enhanced_bookmarks::android") | |
26 public final class EnhancedBookmarksBridge { | |
27 private long mNativeEnhancedBookmarksBridge; | |
28 private final ObserverList<FiltersObserver> mFilterObservers = | |
29 new ObserverList<FiltersObserver>(); | |
30 private final ObserverList<SearchServiceObserver> mSearchObservers = | |
31 new ObserverList<SearchServiceObserver>(); | |
32 | |
33 private LruCache<String, Pair<String, Bitmap>> mSalientImageCache; | |
34 | |
35 /** | |
36 * Interface for getting result back from SalientImageForUrl function. | |
37 */ | |
38 public interface SalientImageCallback { | |
39 /** | |
40 * Callback method for fetching salient image. | |
41 * @param image Salient image. This can be null if the image cannot be f
ound. | |
42 * @param imageUrl Url of the image. Note this is not the same as the ur
l of the website | |
43 * containing the image. | |
44 */ | |
45 @CalledByNative("SalientImageCallback") | |
46 void onSalientImageReady(Bitmap image, String imageUrl); | |
47 } | |
48 | |
49 /** | |
50 * Interface to provide consumers notifications to changes in clusters | |
51 */ | |
52 public interface FiltersObserver { | |
53 /** | |
54 * Invoked when client detects that filters have been added/removed from
the server. | |
55 */ | |
56 void onFiltersChanged(); | |
57 } | |
58 | |
59 /** | |
60 * Interface to provide consumers notifications to changes in search service
results. | |
61 */ | |
62 public interface SearchServiceObserver { | |
63 /** | |
64 * Invoked when client detects that search results have been updated. Th
is callback is | |
65 * guaranteed to be called only once and only for the most recent query. | |
66 */ | |
67 void onSearchResultsReturned(); | |
68 } | |
69 | |
70 public EnhancedBookmarksBridge(Profile profile, int maxCacheSize) { | |
71 this(profile); | |
72 // Do not initialize LruCache if cache size is set to 0. | |
73 if (maxCacheSize != 0) { | |
74 mSalientImageCache = new LruCache<String, Pair<String, Bitmap>>(maxC
acheSize) { | |
75 @Override | |
76 protected int sizeOf(String key, Pair<String, Bitmap> urlImage)
{ | |
77 return urlImage.first.length() + urlImage.second.getByteCoun
t(); | |
78 } | |
79 }; | |
80 } | |
81 } | |
82 | |
83 public EnhancedBookmarksBridge(Profile profile) { | |
84 mNativeEnhancedBookmarksBridge = nativeInit(profile); | |
85 } | |
86 | |
87 public void destroy() { | |
88 assert mNativeEnhancedBookmarksBridge != 0; | |
89 nativeDestroy(mNativeEnhancedBookmarksBridge); | |
90 mNativeEnhancedBookmarksBridge = 0; | |
91 | |
92 if (mSalientImageCache != null) { | |
93 for (Map.Entry<String, Pair<String, Bitmap>> entry : | |
94 mSalientImageCache.snapshot().entrySet()) { | |
95 entry.getValue().second.recycle(); | |
96 } | |
97 mSalientImageCache.evictAll(); | |
98 } | |
99 } | |
100 | |
101 /** | |
102 * Adds a folder to the EnhancedBookmarkModel | |
103 * @param parent The parent of this folder | |
104 * @param index The position this folder should appear within the parent | |
105 * @param title The title of the bookmark | |
106 * @return The ID of the newly created folder. | |
107 */ | |
108 public BookmarkId addFolder(BookmarkId parent, int index, String title) { | |
109 return nativeAddFolder(mNativeEnhancedBookmarksBridge, parent, index, ti
tle); | |
110 } | |
111 | |
112 /** | |
113 * Adds a Bookmark to the EnhancedBookmarkModel | |
114 * @param parent The parent of this bookmark | |
115 * @param index The position this bookmark should appear within the parent | |
116 * @param title The title of the bookmark | |
117 * @param url URL of the bookmark | |
118 * @return The ID of the newly created bookmark | |
119 */ | |
120 public BookmarkId addBookmark(BookmarkId parent, int index, String title, St
ring url) { | |
121 return nativeAddBookmark(mNativeEnhancedBookmarksBridge, parent, index,
title, url); | |
122 } | |
123 | |
124 /** | |
125 * Moves a bookmark to another folder, and append it at the end of the list
of all children. | |
126 * @param bookmarkId The item to be be moved | |
127 * @param newParentId The new parent of the item | |
128 */ | |
129 public void moveBookmark(BookmarkId bookmarkId, BookmarkId newParentId) { | |
130 nativeMoveBookmark(mNativeEnhancedBookmarksBridge, bookmarkId, newParent
Id); | |
131 } | |
132 | |
133 /** | |
134 * Get descriptions of a given bookmark. | |
135 * @param id The id of the bookmark to look at. | |
136 * @return Description of the bookmark. If given a partner bookmark, this me
thod will return an | |
137 * empty list. | |
138 */ | |
139 public String getBookmarkDescription(BookmarkId id) { | |
140 return nativeGetBookmarkDescription(mNativeEnhancedBookmarksBridge, id.g
etId(), | |
141 id.getType()); | |
142 } | |
143 | |
144 /** | |
145 * Sets the description of the given bookmark. | |
146 */ | |
147 public void setBookmarkDescription(BookmarkId id, String description) { | |
148 nativeSetBookmarkDescription(mNativeEnhancedBookmarksBridge, id.getId(),
id.getType(), | |
149 description); | |
150 } | |
151 | |
152 /** | |
153 * Registers a FiltersObserver to listen for filter change notifications. | |
154 * @param observer Observer to add | |
155 */ | |
156 public void addFiltersObserver(FiltersObserver observer) { | |
157 mFilterObservers.addObserver(observer); | |
158 } | |
159 | |
160 /** | |
161 * Unregisters a FiltersObserver from listening to filter change notificatio
ns. | |
162 * @param observer Observer to remove | |
163 */ | |
164 public void removeFiltersObserver(FiltersObserver observer) { | |
165 mFilterObservers.removeObserver(observer); | |
166 } | |
167 | |
168 /** | |
169 * Gets all the bookmark ids associated with a filter string. | |
170 * @param filter The filter string | |
171 * @return List of bookmark ids | |
172 */ | |
173 public List<BookmarkId> getBookmarksForFilter(String filter) { | |
174 List<BookmarkId> list = new ArrayList<BookmarkId>(); | |
175 nativeGetBookmarksForFilter(mNativeEnhancedBookmarksBridge, filter, list
); | |
176 return list; | |
177 } | |
178 | |
179 /** | |
180 * Sends request to search server for querying related bookmarks. | |
181 * @param query Keyword used to find related bookmarks. | |
182 */ | |
183 public void sendSearchRequest(String query) { | |
184 nativeSendSearchRequest(mNativeEnhancedBookmarksBridge, query); | |
185 } | |
186 | |
187 /** | |
188 * Get list of bookmarks as result of a search request that was sent before
in | |
189 * {@link EnhancedBookmarksBridge#sendSearchRequest(String)}. Normally this
function should be | |
190 * called after {@link SearchServiceObserver#onSearchResultsReturned()} | |
191 * @param query Keyword used to find related bookmarks. | |
192 * @return List of BookmarkIds that are related to query. It will be null if
the request is | |
193 * still on the fly, or empty list if there are no results for the q
uery. | |
194 */ | |
195 public List<BookmarkId> getSearchResultsForQuery(String query) { | |
196 return nativeGetSearchResults(mNativeEnhancedBookmarksBridge, query); | |
197 } | |
198 | |
199 /** | |
200 * Registers a SearchObserver that listens to search request updates. | |
201 * @param observer Observer to add | |
202 */ | |
203 public void addSearchObserver(SearchServiceObserver observer) { | |
204 mSearchObservers.addObserver(observer); | |
205 } | |
206 | |
207 /** | |
208 * Unregisters a SearchObserver that listens to search request updates. | |
209 * @param observer Observer to remove | |
210 */ | |
211 public void removeSearchObserver(SearchServiceObserver observer) { | |
212 mSearchObservers.removeObserver(observer); | |
213 } | |
214 | |
215 /** | |
216 * Request bookmark salient image for the given URL. Please refer to | |
217 * |BookmarkImageService::SalientImageForUrl|. | |
218 * @return True if this method is executed synchronously. False if | |
219 * {@link SalientImageCallback#onSalientImageReady(Bitmap, String)}
is called later | |
220 * (asynchronously). | |
221 */ | |
222 public boolean salientImageForUrl(final String url, final SalientImageCallba
ck callback) { | |
223 assert callback != null; | |
224 SalientImageCallback callbackWrapper = callback; | |
225 | |
226 if (mSalientImageCache != null) { | |
227 Pair<String, Bitmap> cached = mSalientImageCache.get(url); | |
228 if (cached != null) { | |
229 callback.onSalientImageReady(cached.second, cached.first); | |
230 return true; | |
231 } | |
232 | |
233 callbackWrapper = new SalientImageCallback() { | |
234 @Override | |
235 public void onSalientImageReady(Bitmap image, String imageUrl) { | |
236 if (image != null) { | |
237 mSalientImageCache.put(url, new Pair<String, Bitmap>(ima
geUrl, image)); | |
238 } | |
239 callback.onSalientImageReady(image, imageUrl); | |
240 } | |
241 }; | |
242 } | |
243 | |
244 nativeSalientImageForUrl(mNativeEnhancedBookmarksBridge, url, callbackWr
apper); | |
245 return false; | |
246 } | |
247 | |
248 /** | |
249 * Get all filters associated with the given bookmark. | |
250 * | |
251 * @param bookmark The bookmark to find filters for. | |
252 * @return Array of Strings, each representing a filter. If given a partner
bookmark, this | |
253 * method will return an empty array. | |
254 */ | |
255 public String[] getFiltersForBookmark(BookmarkId bookmark) { | |
256 return nativeGetFiltersForBookmark(mNativeEnhancedBookmarksBridge, bookm
ark.getId(), | |
257 bookmark.getType()); | |
258 } | |
259 | |
260 /** | |
261 * @return Current set of known auto-filters for bookmarks. | |
262 */ | |
263 public List<String> getFilters() { | |
264 List<String> list = | |
265 Arrays.asList(nativeGetFilters(mNativeEnhancedBookmarksBridge)); | |
266 return list; | |
267 } | |
268 | |
269 @CalledByNative | |
270 private void onFiltersChanged() { | |
271 for (FiltersObserver observer : mFilterObservers) { | |
272 observer.onFiltersChanged(); | |
273 } | |
274 } | |
275 | |
276 @CalledByNative | |
277 private void onSearchResultReturned() { | |
278 for (SearchServiceObserver observer : mSearchObservers) { | |
279 observer.onSearchResultsReturned(); | |
280 } | |
281 } | |
282 | |
283 @CalledByNative | |
284 private static List<BookmarkId> createBookmarkIdList() { | |
285 return new ArrayList<BookmarkId>(); | |
286 } | |
287 | |
288 @CalledByNative | |
289 private static void addToBookmarkIdList(List<BookmarkId> bookmarkIdList, lon
g id, int type) { | |
290 bookmarkIdList.add(new BookmarkId(id, type)); | |
291 } | |
292 | |
293 private native long nativeInit(Profile profile); | |
294 private native void nativeDestroy(long nativeEnhancedBookmarksBridge); | |
295 private native String nativeGetBookmarkDescription(long nativeEnhancedBookma
rksBridge, long id, | |
296 int type); | |
297 private native void nativeSetBookmarkDescription(long nativeEnhancedBookmark
sBridge, long id, | |
298 int type, String description); | |
299 private native void nativeGetBookmarksForFilter(long nativeEnhancedBookmarks
Bridge, | |
300 String filter, List<BookmarkId> list); | |
301 private native List<BookmarkId> nativeGetSearchResults(long nativeEnhancedBo
okmarksBridge, | |
302 String query); | |
303 private native String[] nativeGetFilters(long nativeEnhancedBookmarksBridge)
; | |
304 private native String[] nativeGetFiltersForBookmark(long nativeEnhancedBookm
arksBridge, long id, | |
305 int type); | |
306 private native BookmarkId nativeAddFolder(long nativeEnhancedBookmarksBridge
, BookmarkId parent, | |
307 int index, String title); | |
308 private native void nativeMoveBookmark(long nativeEnhancedBookmarksBridge, | |
309 BookmarkId bookmarkId, BookmarkId newParentId); | |
310 private native BookmarkId nativeAddBookmark(long nativeEnhancedBookmarksBrid
ge, | |
311 BookmarkId parent, int index, String title, String url); | |
312 private native void nativeSendSearchRequest(long nativeEnhancedBookmarksBrid
ge, String query); | |
313 private static native void nativeSalientImageForUrl(long nativeEnhancedBookm
arksBridge, | |
314 String url, SalientImageCallback callback); | |
315 } | |
OLD | NEW |