1 | // Copyright (c) 2012 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.content.Context; |
8 | import android.net.Uri; |
9 | import android.os.Bundle; |
10 | import android.os.Parcelable; |
11 | import android.util.Log; |
12 | |
13 | import org.chromium.chrome.browser.ChromeBrowserProvider.BookmarkNode; |
14 | |
15 | import java.io.Serializable; |
16 | |
17 | /** |
18 | * Exposes the custom API methods for ChromeBrowserProvider. |
19 | */ |
20 | public class ChromeBrowserProviderClient { |
21 | private static final String TAG = "ChromeBrowserProviderClient"; |
22 | |
23 | // Returned by some of the methods in this class. |
24 | public static final long INVALID_BOOKMARK_ID = ChromeBrowserProvider.INVALID_BOOKMARK_ID; |
25 | |
26 | // Flags used with getBookmarkNode. |
27 | /** Retrieve the node corresponding to the id provided in getBookmarkNode. */ |
28 | public static final int GET_NODE = 0x00000000; |
29 | |
30 | /** Retrieve the parent of the node requested in getBookmarkNode. */ |
31 | public static final int GET_PARENT = 0x00000001; |
32 | |
33 | /** Retrieve the immediate children of the node requested in getBookmarkNode. */ |
34 | public static final int GET_CHILDREN = 0x00000002; |
35 | |
36 | /** Retrieve the favicon or touch icon, if any, in all the nodes returned by getBookmarkNode. */ |
37 | public static final int GET_FAVICONS = 0x00000004; |
38 | |
39 | /** Retrieve the thumbnail, if any, in all the nodes returned by getBookmarkNode. */ |
40 | public static final int GET_THUMBNAILS = 0x00000008; |
41 | |
42 | /** |
43 | * Verifies if a bookmark node given by its ID exists in the bookmark model. |
44 | * |
45 | * @return True if the provided bookmark node exists in the bookmark model. |
46 | */ |
47 | public static boolean bookmarkNodeExists(Context context, long nodeId) { |
48 | Boolean result = chromeBrowserProviderCall(Boolean.class, |
49 | ChromeBrowserProvider.CLIENT_API_BOOKMARK_NODE_EXISTS, |
50 | context, argsToBundle(nodeId)); |
51 | return result != null ? result.booleanValue() : false; |
52 | } |
53 | |
54 | /** |
55 | * Creates a bookmark folder or returns its ID if it already exists. |
56 | * This method does not update the last modified folder in the UI. |
57 | * |
58 | * @param title Title of the new or existing bookmark folder. |
59 | * @param parentId ID of the parent folder. Must be in the Mobile Bookmarks branch. |
60 | * @return The ID of the new created folder (or INVALID_BOOKMARK_ID on error). |
61 | * Will return the ID of any existing folder in the same parent with the same name. |
62 | */ |
63 | public static long createBookmarksFolderOnce(Context context, String title, long parentId) { |
64 | Long id = chromeBrowserProviderCall(Long.class, |
65 | ChromeBrowserProvider.CLIENT_API_CREATE_BOOKMARKS_FOLDER_ONCE, context, |
66 | argsToBundle(title, parentId)); |
67 | return id != null ? id.longValue() : INVALID_BOOKMARK_ID; |
68 | } |
69 | |
70 | /** |
71 | * Retrieves the full bookmark folder hierarchy returning its root node. |
72 | * |
73 | * @return The root node of the bookmark folder hierarchy with all its descendant folders |
74 | * populated or null in case of error. Note that only folders are returned. |
75 | */ |
76 | public static BookmarkNode getBookmarkFolderHierarchy(Context context) { |
77 | return chromeBrowserProviderCall(BookmarkNode.class, |
78 | ChromeBrowserProvider.CLIENT_API_GET_BOOKMARK_FOLDER_HIERARCHY, context, |
79 | argsToBundle()); |
80 | } |
81 | |
82 | /** |
83 | * Removes all bookmarks and bookmark folders. |
84 | * Only the permanent bookmark folders remain after this operation. |
85 | */ |
86 | public static void removeAllBookmarks(Context context) { |
87 | chromeBrowserProviderCall(BookmarkNode.class, |
88 | ChromeBrowserProvider.CLIENT_API_DELETE_ALL_BOOKMARKS, context, |
89 | argsToBundle()); |
90 | } |
91 | |
92 | /** |
93 | * Retrieves a bookmark node given its ID or null if no such node exists. |
94 | * The parent and immediate child nodes can be also retrieved by enabling the getParent |
95 | * and getChildren flags. No deeper child nodes can be retrieved with this method. |
96 | * |
97 | * @param nodeId The ID of the bookmark node to be retrieved. |
98 | * @param flags Combination of constants telling what information of the node is required. |
99 | * @return The bookmark node corresponding to the provided ID. |
100 | */ |
101 | public static BookmarkNode getBookmarkNode(Context context, long nodeId, int flags) { |
102 | return chromeBrowserProviderCall(BookmarkNode.class, |
103 | ChromeBrowserProvider.CLIENT_API_GET_BOOKMARK_NODE, context, |
104 | argsToBundle(nodeId, |
105 | (flags & GET_PARENT) != 0, |
106 | (flags & GET_CHILDREN) != 0, |
107 | (flags & GET_FAVICONS) != 0, |
108 | (flags & GET_THUMBNAILS) != 0)); |
109 | } |
110 | |
111 | /** |
112 | * Retrieves the current default folder for UI based bookmark operations. |
113 | * The result depends on where the last successful bookmark operation was performed by the user. |
114 | * |
115 | * @return The default bookmark folder for new bookmarks or null in case of error. |
116 | * No parent or children are populated in the returned node. |
117 | */ |
118 | public static BookmarkNode getDefaultBookmarkFolder(Context context) { |
119 | return chromeBrowserProviderCall(BookmarkNode.class, |
120 | ChromeBrowserProvider.CLIENT_API_GET_DEFAULT_BOOKMARK_FOLDER, context, |
121 | argsToBundle()); |
122 | } |
123 | |
124 | /** |
125 | * Returns the ID of the Mobile Bookmarks folder. |
126 | * |
127 | * @return The ID of the Mobile Bookmarks folder or INVALID_BOOKMARK_ID in case of error. |
128 | */ |
129 | public static long getMobileBookmarksFolderId(Context context) { |
130 | Long id = chromeBrowserProviderCall(Long.class, |
131 | ChromeBrowserProvider.CLIENT_API_GET_MOBILE_BOOKMARKS_FOLDER_ID, context, |
132 | argsToBundle()); |
133 | return id != null ? id.longValue() : INVALID_BOOKMARK_ID; |
134 | } |
135 | |
136 | /** |
137 | * Checks if a bookmark node is in the Mobile Bookmarks folder branch. |
138 | * |
139 | * @return True if the ID belongs to a node in the Mobile Bookmarks folder branch. |
140 | */ |
141 | public static boolean isBookmarkInMobileBookmarksBranch(Context context, long nodeId) { |
142 | Boolean result = chromeBrowserProviderCall(Boolean.class, |
143 | ChromeBrowserProvider.CLIENT_API_IS_BOOKMARK_IN_MOBILE_BOOKMARKS_BRANCH, context, |
144 | argsToBundle(nodeId)); |
145 | return result != null ? result.booleanValue() : false; |
146 | } |
147 | |
148 | // --------------------- End of the client API --------------------- // |
149 | |
150 | private static Uri getPrivateProviderUri(Context context) { |
151 | // The Bookmarks Uri uses the private provider authority. |
152 | return ChromeBrowserProvider.getBookmarksUri(context); |
153 | } |
154 | |
155 | |
156 | private static Bundle argsToBundle(Object ... args) { |
157 | Bundle methodArgs = new Bundle(); |
158 | for (int i = 0; i < args.length; ++i) { |
159 | Class<? extends Object> argClass = args[i].getClass(); |
160 | if (Parcelable.class.isAssignableFrom(argClass)) { |
161 | methodArgs.putParcelable(ChromeBrowserProvider.argKey(i), (Parcelable) args[i]); |
162 | } else if (Serializable.class.isAssignableFrom(argClass)) { |
163 | methodArgs.putSerializable(ChromeBrowserProvider.argKey(i), (Serializable) args[i]); |
164 | } else { |
165 | Log.e(TAG, "Argument implements neither Parcelable nor Serializable."); |
166 | return null; |
167 | } |
168 | } |
169 | return methodArgs; |
170 | } |
171 | |
172 | @SuppressWarnings("unchecked") |
173 | private static <T extends Object> T chromeBrowserProviderCall(Class returnType, String name, |
174 | Context context, Bundle args) { |
175 | Bundle result = context.getContentResolver().call(getPrivateProviderUri(context), |
176 | name, null, args); |
177 | if (result == null) |
178 | return null; |
179 | if (Parcelable.class.isAssignableFrom(returnType)) { |
180 | return (T) result.getParcelable(ChromeBrowserProvider.CLIENT_API_RESULT_KEY); |
181 | } else { |
182 | return (T) result.get(ChromeBrowserProvider.CLIENT_API_RESULT_KEY); |
183 | } |
184 | } |
185 | } |