Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | package org.mozilla.gecko.tests; |
michael@0 | 2 | |
michael@0 | 3 | import java.util.ArrayList; |
michael@0 | 4 | |
michael@0 | 5 | import org.mozilla.gecko.AppConstants; |
michael@0 | 6 | import org.mozilla.gecko.Assert; |
michael@0 | 7 | import org.mozilla.gecko.GeckoProfile; |
michael@0 | 8 | import org.mozilla.gecko.db.BrowserDB; |
michael@0 | 9 | |
michael@0 | 10 | import android.app.Activity; |
michael@0 | 11 | import android.content.ContentResolver; |
michael@0 | 12 | import android.database.Cursor; |
michael@0 | 13 | import android.net.Uri; |
michael@0 | 14 | |
michael@0 | 15 | class DatabaseHelper { |
michael@0 | 16 | protected enum BrowserDataType {BOOKMARKS, HISTORY}; |
michael@0 | 17 | private Activity mActivity; |
michael@0 | 18 | private Assert mAsserter; |
michael@0 | 19 | |
michael@0 | 20 | public DatabaseHelper(Activity activity, Assert asserter) { |
michael@0 | 21 | mActivity = activity; |
michael@0 | 22 | mAsserter = asserter; |
michael@0 | 23 | } |
michael@0 | 24 | /** |
michael@0 | 25 | * This method can be used to check if an URL is present in the bookmarks database |
michael@0 | 26 | */ |
michael@0 | 27 | protected boolean isBookmark(String url) { |
michael@0 | 28 | final ContentResolver resolver = mActivity.getContentResolver(); |
michael@0 | 29 | return BrowserDB.isBookmark(resolver, url); |
michael@0 | 30 | } |
michael@0 | 31 | |
michael@0 | 32 | protected Uri buildUri(BrowserDataType dataType) { |
michael@0 | 33 | Uri uri = null; |
michael@0 | 34 | if (dataType == BrowserDataType.BOOKMARKS || dataType == BrowserDataType.HISTORY) { |
michael@0 | 35 | uri = Uri.parse("content://" + AppConstants.ANDROID_PACKAGE_NAME + ".db.browser/" + dataType.toString().toLowerCase()); |
michael@0 | 36 | } else { |
michael@0 | 37 | mAsserter.ok(false, "The wrong data type has been provided = " + dataType.toString(), "Please provide the correct data type"); |
michael@0 | 38 | } |
michael@0 | 39 | uri = uri.buildUpon().appendQueryParameter("profile", GeckoProfile.DEFAULT_PROFILE) |
michael@0 | 40 | .appendQueryParameter("sync", "true").build(); |
michael@0 | 41 | return uri; |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | /** |
michael@0 | 45 | * Adds a bookmark, or updates the bookmark title if the url already exists. |
michael@0 | 46 | * |
michael@0 | 47 | * The LocalBrowserDB.addBookmark implementation handles updating existing bookmarks. |
michael@0 | 48 | */ |
michael@0 | 49 | protected void addOrUpdateMobileBookmark(String title, String url) { |
michael@0 | 50 | final ContentResolver resolver = mActivity.getContentResolver(); |
michael@0 | 51 | BrowserDB.addBookmark(resolver, title, url); |
michael@0 | 52 | mAsserter.ok(true, "Inserting/updating a new bookmark", "Inserting/updating the bookmark with the title = " + title + " and the url = " + url); |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | /** |
michael@0 | 56 | * Updates the title and keyword of a bookmark with the given URL. |
michael@0 | 57 | * |
michael@0 | 58 | * Warning: This method assumes that there's only one bookmark with the given URL. |
michael@0 | 59 | */ |
michael@0 | 60 | protected void updateBookmark(String url, String title, String keyword) { |
michael@0 | 61 | final ContentResolver resolver = mActivity.getContentResolver(); |
michael@0 | 62 | // Get the id for the bookmark with the given URL. |
michael@0 | 63 | Cursor c = null; |
michael@0 | 64 | try { |
michael@0 | 65 | c = BrowserDB.getBookmarkForUrl(resolver, url); |
michael@0 | 66 | if (!c.moveToFirst()) { |
michael@0 | 67 | mAsserter.ok(false, "Getting bookmark with url", "Couldn't find bookmark with url = " + url); |
michael@0 | 68 | return; |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | int id = c.getInt(c.getColumnIndexOrThrow("_id")); |
michael@0 | 72 | BrowserDB.updateBookmark(resolver, id, url, title, keyword); |
michael@0 | 73 | |
michael@0 | 74 | mAsserter.ok(true, "Updating bookmark", "Updating bookmark with url = " + url); |
michael@0 | 75 | } finally { |
michael@0 | 76 | if (c != null) { |
michael@0 | 77 | c.close(); |
michael@0 | 78 | } |
michael@0 | 79 | } |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | protected void deleteBookmark(String url) { |
michael@0 | 83 | final ContentResolver resolver = mActivity.getContentResolver(); |
michael@0 | 84 | BrowserDB.removeBookmarksWithURL(resolver, url); |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | protected void deleteHistoryItem(String url) { |
michael@0 | 88 | final ContentResolver resolver = mActivity.getContentResolver(); |
michael@0 | 89 | BrowserDB.removeHistoryEntry(resolver, url); |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | // About the same implementation as getFolderIdFromGuid from LocalBrowserDB because it is declared private and we can't use reflections to access it |
michael@0 | 93 | protected long getFolderIdFromGuid(String guid) { |
michael@0 | 94 | ContentResolver resolver = mActivity.getContentResolver(); |
michael@0 | 95 | long folderId = Long.valueOf(-1); |
michael@0 | 96 | Uri bookmarksUri = buildUri(BrowserDataType.BOOKMARKS); |
michael@0 | 97 | Cursor c = null; |
michael@0 | 98 | try { |
michael@0 | 99 | c = resolver.query(bookmarksUri, |
michael@0 | 100 | new String[] { "_id" }, |
michael@0 | 101 | "guid = ?", |
michael@0 | 102 | new String[] { guid }, |
michael@0 | 103 | null); |
michael@0 | 104 | if (c.moveToFirst()) { |
michael@0 | 105 | folderId = c.getLong(c.getColumnIndexOrThrow("_id")); |
michael@0 | 106 | } |
michael@0 | 107 | if (folderId == -1) { |
michael@0 | 108 | mAsserter.ok(false, "Trying to get the folder id" ,"We did not get the correct folder id"); |
michael@0 | 109 | } |
michael@0 | 110 | } finally { |
michael@0 | 111 | if (c != null) { |
michael@0 | 112 | c.close(); |
michael@0 | 113 | } |
michael@0 | 114 | } |
michael@0 | 115 | return folderId; |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | /** |
michael@0 | 119 | * @param a BrowserDataType value - either HISTORY or BOOKMARKS |
michael@0 | 120 | * @return an ArrayList of the urls in the Firefox for Android Bookmarks or History databases |
michael@0 | 121 | */ |
michael@0 | 122 | protected ArrayList<String> getBrowserDBUrls(BrowserDataType dataType) { |
michael@0 | 123 | ArrayList<String> browserData = new ArrayList<String>(); |
michael@0 | 124 | ContentResolver resolver = mActivity.getContentResolver(); |
michael@0 | 125 | Cursor cursor = null; |
michael@0 | 126 | Uri uri = buildUri(dataType); |
michael@0 | 127 | if (dataType == BrowserDataType.HISTORY) { |
michael@0 | 128 | cursor = BrowserDB.getAllVisitedHistory(resolver); |
michael@0 | 129 | } else if (dataType == BrowserDataType.BOOKMARKS) { |
michael@0 | 130 | cursor = BrowserDB.getBookmarksInFolder(resolver, getFolderIdFromGuid("mobile")); |
michael@0 | 131 | } |
michael@0 | 132 | if (cursor != null) { |
michael@0 | 133 | cursor.moveToFirst(); |
michael@0 | 134 | for (int i = 0; i < cursor.getCount(); i++ ) { |
michael@0 | 135 | // The url field may be null for folders in the structure of the Bookmarks table for Firefox so we should eliminate those |
michael@0 | 136 | if (cursor.getString(cursor.getColumnIndex("url")) != null) { |
michael@0 | 137 | browserData.add(cursor.getString(cursor.getColumnIndex("url"))); |
michael@0 | 138 | } |
michael@0 | 139 | if(!cursor.isLast()) { |
michael@0 | 140 | cursor.moveToNext(); |
michael@0 | 141 | } |
michael@0 | 142 | } |
michael@0 | 143 | } else { |
michael@0 | 144 | mAsserter.ok(false, "We could not retrieve any data from the database", "The cursor was null"); |
michael@0 | 145 | } |
michael@0 | 146 | if (cursor != null) { |
michael@0 | 147 | cursor.close(); |
michael@0 | 148 | } |
michael@0 | 149 | return browserData; |
michael@0 | 150 | } |
michael@0 | 151 | } |