OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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.bookmarkimport; |
| 6 |
| 7 import android.content.ContentProviderClient; |
| 8 import android.content.ContentResolver; |
| 9 import android.database.Cursor; |
| 10 import android.provider.Browser; |
| 11 import android.provider.Browser.BookmarkColumns; |
| 12 |
| 13 import java.util.NoSuchElementException; |
| 14 |
| 15 /** |
| 16 * Imports bookmarks from Android Browser using the public provider API. |
| 17 */ |
| 18 class AndroidBrowserProviderIterator implements BookmarkImporter.BookmarkIterato
r { |
| 19 private static final String SELECT_IS_BOOKMARK = BookmarkColumns.BOOKMARK +
"=1"; |
| 20 |
| 21 private final Cursor mCursor; |
| 22 private long mNextId = BookmarkImporter.ROOT_FOLDER_ID + 1; |
| 23 |
| 24 static boolean isProviderAvailable(ContentResolver contentResolver) { |
| 25 ContentProviderClient providerClient = contentResolver.acquireContentPro
viderClient( |
| 26 Browser.BOOKMARKS_URI); |
| 27 if (providerClient != null) { |
| 28 providerClient.release(); |
| 29 return true; |
| 30 } |
| 31 return false; |
| 32 } |
| 33 |
| 34 static AndroidBrowserProviderIterator createIfAvailable( |
| 35 ContentResolver contentResolver) { |
| 36 Cursor cursor = contentResolver.query(Browser.BOOKMARKS_URI, null, SELEC
T_IS_BOOKMARK, |
| 37 null, null); |
| 38 return cursor == null ? null : new AndroidBrowserProviderIterator(cursor
); |
| 39 } |
| 40 |
| 41 private AndroidBrowserProviderIterator(Cursor cursor) { |
| 42 mCursor = cursor; |
| 43 } |
| 44 |
| 45 @Override |
| 46 public void remove() { |
| 47 throw new UnsupportedOperationException(); |
| 48 } |
| 49 |
| 50 @Override |
| 51 public void close() { |
| 52 if (mCursor == null) throw new IllegalStateException(); |
| 53 mCursor.close(); |
| 54 } |
| 55 |
| 56 @Override |
| 57 public boolean hasNext() { |
| 58 if (mCursor == null) throw new IllegalStateException(); |
| 59 return mCursor.getCount() > 0 && !mCursor.isLast() && !mCursor.isAfterLa
st(); |
| 60 } |
| 61 |
| 62 @Override |
| 63 public BookmarkImporter.Bookmark next() { |
| 64 if (mCursor == null) throw new IllegalStateException(); |
| 65 if (!mCursor.moveToNext()) throw new NoSuchElementException(); |
| 66 |
| 67 BookmarkImporter.Bookmark bookmark = new BookmarkImporter.Bookmark(); |
| 68 try { |
| 69 bookmark.url = mCursor.getString(mCursor.getColumnIndexOrThrow(Bookm
arkColumns.URL)); |
| 70 bookmark.title = |
| 71 mCursor.getString(mCursor.getColumnIndexOrThrow(BookmarkColu
mns.TITLE)); |
| 72 } catch (IllegalArgumentException e) { |
| 73 return null; |
| 74 } |
| 75 |
| 76 if (bookmark.url == null || bookmark.title == null) { |
| 77 return null; |
| 78 } |
| 79 |
| 80 int index = mCursor.getColumnIndex(BookmarkColumns.CREATED); |
| 81 if (index != -1 && !mCursor.isNull(index)) bookmark.created = mCursor.ge
tLong(index); |
| 82 |
| 83 index = mCursor.getColumnIndex(BookmarkColumns.DATE); |
| 84 if (index != -1 && !mCursor.isNull(index)) bookmark.lastVisit = mCursor.
getLong(index); |
| 85 |
| 86 index = mCursor.getColumnIndex(BookmarkColumns.VISITS); |
| 87 if (index != -1 && !mCursor.isNull(index)) bookmark.visits = mCursor.get
Long(index); |
| 88 |
| 89 index = mCursor.getColumnIndex(BookmarkColumns.FAVICON); |
| 90 if (index != -1 && !mCursor.isNull(index)) bookmark.favicon = mCursor.ge
tBlob(index); |
| 91 |
| 92 // Add hierarchy information (flat). |
| 93 bookmark.id = mNextId++; |
| 94 bookmark.parentId = BookmarkImporter.ROOT_FOLDER_ID; |
| 95 bookmark.isFolder = false; |
| 96 |
| 97 return bookmark; |
| 98 } |
| 99 } |
OLD | NEW |