1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/background/db/Tab.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,81 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +package org.mozilla.gecko.background.db; 1.9 + 1.10 +import org.json.simple.JSONArray; 1.11 +import org.mozilla.gecko.db.BrowserContract; 1.12 +import org.mozilla.gecko.db.BrowserContract.Tabs; 1.13 +import org.mozilla.gecko.sync.Utils; 1.14 +import org.mozilla.gecko.sync.repositories.android.RepoUtils; 1.15 + 1.16 +import android.content.ContentValues; 1.17 +import android.database.Cursor; 1.18 + 1.19 +// Immutable. 1.20 +public class Tab { 1.21 + public final String title; 1.22 + public final String icon; 1.23 + public final JSONArray history; 1.24 + public final long lastUsed; 1.25 + 1.26 + public Tab(String title, String icon, JSONArray history, long lastUsed) { 1.27 + this.title = title; 1.28 + this.icon = icon; 1.29 + this.history = history; 1.30 + this.lastUsed = lastUsed; 1.31 + } 1.32 + 1.33 + public ContentValues toContentValues(String clientGUID, int position) { 1.34 + ContentValues out = new ContentValues(); 1.35 + out.put(BrowserContract.Tabs.POSITION, position); 1.36 + out.put(BrowserContract.Tabs.CLIENT_GUID, clientGUID); 1.37 + 1.38 + out.put(BrowserContract.Tabs.FAVICON, this.icon); 1.39 + out.put(BrowserContract.Tabs.LAST_USED, this.lastUsed); 1.40 + out.put(BrowserContract.Tabs.TITLE, this.title); 1.41 + out.put(BrowserContract.Tabs.URL, (String) this.history.get(0)); 1.42 + out.put(BrowserContract.Tabs.HISTORY, this.history.toJSONString()); 1.43 + return out; 1.44 + } 1.45 + 1.46 + @Override 1.47 + public boolean equals(Object o) { 1.48 + if (!(o instanceof Tab)) { 1.49 + return false; 1.50 + } 1.51 + final Tab other = (Tab) o; 1.52 + 1.53 + if (!RepoUtils.stringsEqual(this.title, other.title)) { 1.54 + return false; 1.55 + } 1.56 + if (!RepoUtils.stringsEqual(this.icon, other.icon)) { 1.57 + return false; 1.58 + } 1.59 + 1.60 + if (!(this.lastUsed == other.lastUsed)) { 1.61 + return false; 1.62 + } 1.63 + 1.64 + return Utils.sameArrays(this.history, other.history); 1.65 + } 1.66 + 1.67 + /** 1.68 + * Extract a <code>Tab</code> from a cursor row. 1.69 + * <p> 1.70 + * Caller is responsible for creating, positioning, and closing the cursor. 1.71 + * 1.72 + * @param cursor 1.73 + * to inspect. 1.74 + * @return <code>Tab</code> instance. 1.75 + */ 1.76 + public static Tab fromCursor(final Cursor cursor) { 1.77 + final String title = RepoUtils.getStringFromCursor(cursor, Tabs.TITLE); 1.78 + final String icon = RepoUtils.getStringFromCursor(cursor, Tabs.FAVICON); 1.79 + final JSONArray history = RepoUtils.getJSONArrayFromCursor(cursor, Tabs.HISTORY); 1.80 + final long lastUsed = RepoUtils.getLongFromCursor(cursor, Tabs.LAST_USED); 1.81 + 1.82 + return new Tab(title, icon, history, lastUsed); 1.83 + } 1.84 +}