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.sync.repositories.android;
6 
7 import java.util.Collections;
8 import java.util.HashMap;
9 import java.util.Map;
10 
11 import org.mozilla.gecko.db.BrowserContract;
12 import org.mozilla.gecko.sync.setup.Constants;
13 
14 import android.net.Uri;
15 
16 public class BrowserContractHelpers extends BrowserContract {
17 
withSyncAndDeletedAndProfile(Uri u)18   protected static Uri withSyncAndDeletedAndProfile(Uri u) {
19     return u.buildUpon()
20             .appendQueryParameter(PARAM_PROFILE, Constants.DEFAULT_PROFILE)
21             .appendQueryParameter(PARAM_IS_SYNC, "true")
22             .appendQueryParameter(PARAM_SHOW_DELETED, "true")
23             .build();
24   }
withSyncAndProfile(Uri u)25   protected static Uri withSyncAndProfile(Uri u) {
26     return u.buildUpon()
27         .appendQueryParameter(PARAM_PROFILE, Constants.DEFAULT_PROFILE)
28         .appendQueryParameter(PARAM_IS_SYNC, "true")
29         .build();
30   }
31 
32   public static final Uri BOOKMARKS_CONTENT_URI            = withSyncAndDeletedAndProfile(Bookmarks.CONTENT_URI);
33   public static final Uri BOOKMARKS_PARENTS_CONTENT_URI    = withSyncAndDeletedAndProfile(Bookmarks.PARENTS_CONTENT_URI);
34   public static final Uri BOOKMARKS_POSITIONS_CONTENT_URI  = withSyncAndDeletedAndProfile(Bookmarks.POSITIONS_CONTENT_URI);
35   public static final Uri HISTORY_CONTENT_URI              = withSyncAndDeletedAndProfile(History.CONTENT_URI);
36   public static final Uri VISITS_CONTENT_URI               = withSyncAndDeletedAndProfile(Visits.CONTENT_URI);
37   public static final Uri SCHEMA_CONTENT_URI               = withSyncAndDeletedAndProfile(Schema.CONTENT_URI);
38   public static final Uri PASSWORDS_CONTENT_URI            = withSyncAndDeletedAndProfile(Passwords.CONTENT_URI);
39   public static final Uri DELETED_PASSWORDS_CONTENT_URI    = withSyncAndDeletedAndProfile(DeletedPasswords.CONTENT_URI);
40   public static final Uri FORM_HISTORY_CONTENT_URI         = withSyncAndProfile(FormHistory.CONTENT_URI);
41   public static final Uri DELETED_FORM_HISTORY_CONTENT_URI = withSyncAndProfile(DeletedFormHistory.CONTENT_URI);
42   public static final Uri TABS_CONTENT_URI                 = withSyncAndProfile(Tabs.CONTENT_URI);
43   public static final Uri CLIENTS_CONTENT_URI              = withSyncAndProfile(Clients.CONTENT_URI);
44   public static final Uri LOGINS_CONTENT_URI               = withSyncAndProfile(Logins.CONTENT_URI);
45 
46   public static final String[] PasswordColumns = new String[] {
47     Passwords.ID,
48     Passwords.HOSTNAME,
49     Passwords.HTTP_REALM,
50     Passwords.FORM_SUBMIT_URL,
51     Passwords.USERNAME_FIELD,
52     Passwords.PASSWORD_FIELD,
53     Passwords.ENCRYPTED_USERNAME,
54     Passwords.ENCRYPTED_PASSWORD,
55     Passwords.ENC_TYPE,
56     Passwords.TIME_CREATED,
57     Passwords.TIME_LAST_USED,
58     Passwords.TIME_PASSWORD_CHANGED,
59     Passwords.TIMES_USED,
60     Passwords.GUID
61   };
62 
63   public static final String[] HistoryColumns = new String[] {
64     CommonColumns._ID,
65     SyncColumns.GUID,
66     SyncColumns.DATE_CREATED,
67     SyncColumns.DATE_MODIFIED,
68     SyncColumns.IS_DELETED,
69     History.TITLE,
70     History.URL,
71     History.DATE_LAST_VISITED,
72     History.VISITS
73   };
74 
75   public static final String[] BookmarkColumns = new String[] {
76     CommonColumns._ID,
77     SyncColumns.GUID,
78     SyncColumns.DATE_CREATED,
79     SyncColumns.DATE_MODIFIED,
80     SyncColumns.IS_DELETED,
81     Bookmarks.TITLE,
82     Bookmarks.URL,
83     Bookmarks.TYPE,
84     Bookmarks.PARENT,
85     Bookmarks.POSITION,
86     Bookmarks.TAGS,
87     Bookmarks.DESCRIPTION,
88     Bookmarks.KEYWORD
89   };
90 
91   public static final String[] FormHistoryColumns = new String[] {
92     FormHistory.ID,
93     FormHistory.GUID,
94     FormHistory.FIELD_NAME,
95     FormHistory.VALUE,
96     FormHistory.TIMES_USED,
97     FormHistory.FIRST_USED,
98     FormHistory.LAST_USED
99   };
100 
101   public static final String[] DeletedColumns = new String[] {
102     BrowserContract.DeletedColumns.ID,
103     BrowserContract.DeletedColumns.GUID,
104     BrowserContract.DeletedColumns.TIME_DELETED
105   };
106 
107   // Mapping from Sync types to Fennec types.
108   public static final String[] BOOKMARK_TYPE_CODE_TO_STRING = {
109     // Observe omissions: "microsummary", "item".
110     "folder", "bookmark", "separator", "livemark", "query"
111   };
112   private static final int MAX_BOOKMARK_TYPE_CODE = BOOKMARK_TYPE_CODE_TO_STRING.length - 1;
113   public static final Map<String, Integer> BOOKMARK_TYPE_STRING_TO_CODE;
114   static {
115     HashMap<String, Integer> t = new HashMap<String, Integer>();
116     t.put("folder",    Bookmarks.TYPE_FOLDER);
117     t.put("bookmark",  Bookmarks.TYPE_BOOKMARK);
118     t.put("separator", Bookmarks.TYPE_SEPARATOR);
119     t.put("livemark",  Bookmarks.TYPE_LIVEMARK);
120     t.put("query",     Bookmarks.TYPE_QUERY);
121     BOOKMARK_TYPE_STRING_TO_CODE = Collections.unmodifiableMap(t);
122   }
123 
124   /**
125    * Convert a database bookmark type code into the Sync string equivalent.
126    *
127    * @param code one of the <code>Bookmarks.TYPE_*</code> enumerations.
128    * @return the string equivalent, or null if not found.
129    */
typeStringForCode(int code)130   public static String typeStringForCode(int code) {
131     if (0 <= code && code <= MAX_BOOKMARK_TYPE_CODE) {
132       return BOOKMARK_TYPE_CODE_TO_STRING[code];
133     }
134     return null;
135   }
136 
137   /**
138    * Convert a Sync type string into a Fennec type code.
139    *
140    * @param type a type string, such as "livemark".
141    * @return the type code, or -1 if not found.
142    */
typeCodeForString(String type)143   public static int typeCodeForString(String type) {
144     Integer found = BOOKMARK_TYPE_STRING_TO_CODE.get(type);
145     if (found == null) {
146       return -1;
147     }
148     return found;
149   }
150 
isSupportedType(String type)151   public static boolean isSupportedType(String type) {
152     return BOOKMARK_TYPE_STRING_TO_CODE.containsKey(type);
153   }
154 }
155