michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0:
michael@0: package org.mozilla.gecko.background.db;
michael@0:
michael@0: import org.json.simple.JSONArray;
michael@0: import org.mozilla.gecko.db.BrowserContract;
michael@0: import org.mozilla.gecko.db.BrowserContract.Tabs;
michael@0: import org.mozilla.gecko.sync.Utils;
michael@0: import org.mozilla.gecko.sync.repositories.android.RepoUtils;
michael@0:
michael@0: import android.content.ContentValues;
michael@0: import android.database.Cursor;
michael@0:
michael@0: // Immutable.
michael@0: public class Tab {
michael@0: public final String title;
michael@0: public final String icon;
michael@0: public final JSONArray history;
michael@0: public final long lastUsed;
michael@0:
michael@0: public Tab(String title, String icon, JSONArray history, long lastUsed) {
michael@0: this.title = title;
michael@0: this.icon = icon;
michael@0: this.history = history;
michael@0: this.lastUsed = lastUsed;
michael@0: }
michael@0:
michael@0: public ContentValues toContentValues(String clientGUID, int position) {
michael@0: ContentValues out = new ContentValues();
michael@0: out.put(BrowserContract.Tabs.POSITION, position);
michael@0: out.put(BrowserContract.Tabs.CLIENT_GUID, clientGUID);
michael@0:
michael@0: out.put(BrowserContract.Tabs.FAVICON, this.icon);
michael@0: out.put(BrowserContract.Tabs.LAST_USED, this.lastUsed);
michael@0: out.put(BrowserContract.Tabs.TITLE, this.title);
michael@0: out.put(BrowserContract.Tabs.URL, (String) this.history.get(0));
michael@0: out.put(BrowserContract.Tabs.HISTORY, this.history.toJSONString());
michael@0: return out;
michael@0: }
michael@0:
michael@0: @Override
michael@0: public boolean equals(Object o) {
michael@0: if (!(o instanceof Tab)) {
michael@0: return false;
michael@0: }
michael@0: final Tab other = (Tab) o;
michael@0:
michael@0: if (!RepoUtils.stringsEqual(this.title, other.title)) {
michael@0: return false;
michael@0: }
michael@0: if (!RepoUtils.stringsEqual(this.icon, other.icon)) {
michael@0: return false;
michael@0: }
michael@0:
michael@0: if (!(this.lastUsed == other.lastUsed)) {
michael@0: return false;
michael@0: }
michael@0:
michael@0: return Utils.sameArrays(this.history, other.history);
michael@0: }
michael@0:
michael@0: /**
michael@0: * Extract a Tab
from a cursor row.
michael@0: *
michael@0: * Caller is responsible for creating, positioning, and closing the cursor.
michael@0: *
michael@0: * @param cursor
michael@0: * to inspect.
michael@0: * @return Tab
instance.
michael@0: */
michael@0: public static Tab fromCursor(final Cursor cursor) {
michael@0: final String title = RepoUtils.getStringFromCursor(cursor, Tabs.TITLE);
michael@0: final String icon = RepoUtils.getStringFromCursor(cursor, Tabs.FAVICON);
michael@0: final JSONArray history = RepoUtils.getJSONArrayFromCursor(cursor, Tabs.HISTORY);
michael@0: final long lastUsed = RepoUtils.getLongFromCursor(cursor, Tabs.LAST_USED);
michael@0:
michael@0: return new Tab(title, icon, history, lastUsed);
michael@0: }
michael@0: }