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