Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko.background.db; |
michael@0 | 6 | |
michael@0 | 7 | import org.json.simple.JSONArray; |
michael@0 | 8 | import org.mozilla.gecko.db.BrowserContract; |
michael@0 | 9 | import org.mozilla.gecko.db.BrowserContract.Tabs; |
michael@0 | 10 | import org.mozilla.gecko.sync.Utils; |
michael@0 | 11 | import org.mozilla.gecko.sync.repositories.android.RepoUtils; |
michael@0 | 12 | |
michael@0 | 13 | import android.content.ContentValues; |
michael@0 | 14 | import android.database.Cursor; |
michael@0 | 15 | |
michael@0 | 16 | // Immutable. |
michael@0 | 17 | public class Tab { |
michael@0 | 18 | public final String title; |
michael@0 | 19 | public final String icon; |
michael@0 | 20 | public final JSONArray history; |
michael@0 | 21 | public final long lastUsed; |
michael@0 | 22 | |
michael@0 | 23 | public Tab(String title, String icon, JSONArray history, long lastUsed) { |
michael@0 | 24 | this.title = title; |
michael@0 | 25 | this.icon = icon; |
michael@0 | 26 | this.history = history; |
michael@0 | 27 | this.lastUsed = lastUsed; |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | public ContentValues toContentValues(String clientGUID, int position) { |
michael@0 | 31 | ContentValues out = new ContentValues(); |
michael@0 | 32 | out.put(BrowserContract.Tabs.POSITION, position); |
michael@0 | 33 | out.put(BrowserContract.Tabs.CLIENT_GUID, clientGUID); |
michael@0 | 34 | |
michael@0 | 35 | out.put(BrowserContract.Tabs.FAVICON, this.icon); |
michael@0 | 36 | out.put(BrowserContract.Tabs.LAST_USED, this.lastUsed); |
michael@0 | 37 | out.put(BrowserContract.Tabs.TITLE, this.title); |
michael@0 | 38 | out.put(BrowserContract.Tabs.URL, (String) this.history.get(0)); |
michael@0 | 39 | out.put(BrowserContract.Tabs.HISTORY, this.history.toJSONString()); |
michael@0 | 40 | return out; |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | @Override |
michael@0 | 44 | public boolean equals(Object o) { |
michael@0 | 45 | if (!(o instanceof Tab)) { |
michael@0 | 46 | return false; |
michael@0 | 47 | } |
michael@0 | 48 | final Tab other = (Tab) o; |
michael@0 | 49 | |
michael@0 | 50 | if (!RepoUtils.stringsEqual(this.title, other.title)) { |
michael@0 | 51 | return false; |
michael@0 | 52 | } |
michael@0 | 53 | if (!RepoUtils.stringsEqual(this.icon, other.icon)) { |
michael@0 | 54 | return false; |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | if (!(this.lastUsed == other.lastUsed)) { |
michael@0 | 58 | return false; |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | return Utils.sameArrays(this.history, other.history); |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | /** |
michael@0 | 65 | * Extract a <code>Tab</code> from a cursor row. |
michael@0 | 66 | * <p> |
michael@0 | 67 | * Caller is responsible for creating, positioning, and closing the cursor. |
michael@0 | 68 | * |
michael@0 | 69 | * @param cursor |
michael@0 | 70 | * to inspect. |
michael@0 | 71 | * @return <code>Tab</code> instance. |
michael@0 | 72 | */ |
michael@0 | 73 | public static Tab fromCursor(final Cursor cursor) { |
michael@0 | 74 | final String title = RepoUtils.getStringFromCursor(cursor, Tabs.TITLE); |
michael@0 | 75 | final String icon = RepoUtils.getStringFromCursor(cursor, Tabs.FAVICON); |
michael@0 | 76 | final JSONArray history = RepoUtils.getJSONArrayFromCursor(cursor, Tabs.HISTORY); |
michael@0 | 77 | final long lastUsed = RepoUtils.getLongFromCursor(cursor, Tabs.LAST_USED); |
michael@0 | 78 | |
michael@0 | 79 | return new Tab(title, icon, history, lastUsed); |
michael@0 | 80 | } |
michael@0 | 81 | } |