Index: components/bookmarks/common/android/java/src/org/chromium/components/bookmarks/BookmarkId.java |
diff --git a/components/bookmarks/common/android/java/src/org/chromium/components/bookmarks/BookmarkId.java b/components/bookmarks/common/android/java/src/org/chromium/components/bookmarks/BookmarkId.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..87dc550039f300eddd66e5e8eaf72d8e02440768 |
--- /dev/null |
+++ b/components/bookmarks/common/android/java/src/org/chromium/components/bookmarks/BookmarkId.java |
@@ -0,0 +1,120 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package org.chromium.components.bookmarks; |
+ |
+import android.text.TextUtils; |
+import android.util.Log; |
+ |
+import org.chromium.base.CalledByNative; |
+ |
+/** |
+ * Simple object representing the bookmark id. |
+ */ |
+public class BookmarkId { |
+ // Should mirror constants in chrome/browser/android/bookmarks/bookmarks_bridge.cc |
+ public static final int BOOKMARK_TYPE_NORMAL = 0; |
Yaron
2014/08/12 20:15:49
Why generate the c++/native bindings if you're dup
Kibeom Kim (inactive)
2014/08/12 22:33:48
Done. (forgot to update here, also moved Bookmarks
|
+ public static final int BOOKMARK_TYPE_PARTNER = 1; |
+ |
+ public static final int ROOT_FOLDER_ID = -1; |
+ |
+ private static final String LOG_TAG = "BookmarkId"; |
+ private static final char TYPE_PARTNER = 'p'; |
+ |
+ private final long mId; |
+ private final int mType; |
+ |
+ public BookmarkId(long id, int type) { |
+ mId = id; |
+ mType = type; |
+ } |
+ |
+ /** |
+ * @param c The char representing the type. |
+ * @return The Bookmark type from a char representing the type. |
+ */ |
+ private static int getBookmarkTypeFromChar(char c) { |
+ switch (c) { |
+ case TYPE_PARTNER: |
+ return BOOKMARK_TYPE_PARTNER; |
+ default: |
+ return BOOKMARK_TYPE_NORMAL; |
+ } |
+ } |
+ |
+ /** |
+ * @param c The char representing the bookmark type. |
+ * @return Whether the char representing the bookmark type is a valid type. |
+ */ |
+ private static boolean isValidBookmarkTypeFromChar(char c) { |
Yaron
2014/08/12 20:15:49
What char? Is the char some encoding of the bookma
Kibeom Kim (inactive)
2014/08/12 22:33:48
commented below.
|
+ return c == TYPE_PARTNER; |
+ } |
+ |
+ /** |
+ * @param s The bookmark id string (Eg: p1 for partner bookmark id 1). |
+ * @return the Bookmark id from the string which is a concatenation of |
+ * bookmark type and the bookmark id. |
+ */ |
+ public static BookmarkId getBookmarkIdFromString(String s) { |
Yaron
2014/08/12 20:15:49
This and the above method seem like they have a ve
Kibeom Kim (inactive)
2014/08/12 22:33:48
I just looked up. It's used in BookmarksPage.java
|
+ long id = ROOT_FOLDER_ID; |
+ int type = BOOKMARK_TYPE_NORMAL; |
+ if (TextUtils.isEmpty(s)) |
+ return new BookmarkId(id, type); |
+ char folderTypeChar = s.charAt(0); |
+ if (isValidBookmarkTypeFromChar(folderTypeChar)) { |
+ type = getBookmarkTypeFromChar(folderTypeChar); |
+ s = s.substring(1); |
+ } |
+ try { |
+ id = Long.parseLong(s); |
+ } catch (NumberFormatException exception) { |
+ Log.e(LOG_TAG, "Error parsing url to extract the bookmark folder id.", exception); |
+ } |
+ return new BookmarkId(id, type); |
+ } |
+ |
+ /** |
+ * @return The id of the bookmark. |
+ */ |
+ @CalledByNative |
+ public long getId() { |
+ return mId; |
+ } |
+ |
+ /** |
+ * @return The bookmark type. |
+ */ |
+ @CalledByNative |
+ public int getType() { |
+ return mType; |
+ } |
+ |
+ private String getBookmarkTypeString() { |
+ switch (mType) { |
+ case BOOKMARK_TYPE_PARTNER: |
+ return String.valueOf(TYPE_PARTNER); |
+ case BOOKMARK_TYPE_NORMAL: |
+ default: |
+ return ""; |
+ } |
+ } |
+ |
+ @Override |
+ public String toString() { |
+ return getBookmarkTypeString() + mId; |
+ } |
+ |
+ @Override |
+ public boolean equals(Object o) { |
+ if (!(o instanceof BookmarkId)) |
+ return false; |
+ BookmarkId item = (BookmarkId) o; |
+ return (item.mId == mId && item.mType == mType); |
+ } |
+ |
+ @Override |
+ public int hashCode() { |
+ return toString().hashCode(); |
+ } |
+} |