1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.launcher3;
18 
19 import android.content.ContentResolver;
20 import android.database.sqlite.SQLiteDatabase;
21 import android.net.Uri;
22 import android.os.Bundle;
23 import android.provider.BaseColumns;
24 
25 import com.android.launcher3.model.data.ItemInfo;
26 
27 /**
28  * Settings related utilities.
29  */
30 public class LauncherSettings {
31 
32     /**
33      * Favorites.
34      */
35     public static final class Favorites implements BaseColumns {
36         /**
37          * The time of the last update to this row.
38          * <P>Type: INTEGER</P>
39          */
40         public static final String MODIFIED = "modified";
41 
42         /**
43          * Descriptive name of the gesture that can be displayed to the user.
44          * <P>Type: TEXT</P>
45          */
46         public static final String TITLE = "title";
47 
48         /**
49          * The Intent URL of the gesture, describing what it points to. This
50          * value is given to {@link android.content.Intent#parseUri(String, int)} to create
51          * an Intent that can be launched.
52          * <P>Type: TEXT</P>
53          */
54         public static final String INTENT = "intent";
55 
56         /**
57          * The type of the gesture
58          *
59          * <P>Type: INTEGER</P>
60          */
61         public static final String ITEM_TYPE = "itemType";
62 
63         /**
64          * The gesture is a package
65          */
66         public static final int ITEM_TYPE_NON_ACTIONABLE = -1;
67         /**
68          * The gesture is an application
69          */
70         public static final int ITEM_TYPE_APPLICATION = 0;
71 
72         /**
73          * The gesture is an application created shortcut
74          */
75         public static final int ITEM_TYPE_SHORTCUT = 1;
76 
77         /**
78          * The favorite is a user created folder
79          */
80         public static final int ITEM_TYPE_FOLDER = 2;
81 
82         /**
83          * The favorite is a widget
84          */
85         public static final int ITEM_TYPE_APPWIDGET = 4;
86 
87         /**
88          * The favorite is a custom widget provided by the launcher
89          */
90         public static final int ITEM_TYPE_CUSTOM_APPWIDGET = 5;
91 
92         /**
93          * The gesture is an application created deep shortcut
94          */
95         public static final int ITEM_TYPE_DEEP_SHORTCUT = 6;
96 
97         /**
98          * The favroite is a search action
99          */
100         public static final int ITEM_TYPE_SEARCH_ACTION = 7;
101 
102 
103         /**
104          * Type of the item is recents task.
105          * TODO(hyunyoungs): move constants not related to Favorites DB to a better location.
106          */
107         public static final int ITEM_TYPE_TASK = 7;
108 
109         /**
110          * The item is QSB
111          */
112         public static final int ITEM_TYPE_QSB = 8;
113 
114         /**
115          * The icon package name in Intent.ShortcutIconResource
116          * <P>Type: TEXT</P>
117          */
118         public static final String ICON_PACKAGE = "iconPackage";
119 
120         /**
121          * The icon resource name in Intent.ShortcutIconResource
122          * <P>Type: TEXT</P>
123          */
124         public static final String ICON_RESOURCE = "iconResource";
125 
126         /**
127          * The custom icon bitmap.
128          * <P>Type: BLOB</P>
129          */
130         public static final String ICON = "icon";
131 
132         public static final String TABLE_NAME = "favorites";
133 
134         /**
135          * Backup table created when the favorites table is modified during grid migration
136          */
137         public static final String BACKUP_TABLE_NAME = "favorites_bakup";
138 
139         /**
140          * Backup table created when user hotseat is moved to workspace for hybrid hotseat
141          */
142         public static final String HYBRID_HOTSEAT_BACKUP_TABLE = "hotseat_restore_backup";
143 
144         /**
145          * Temporary table used specifically for grid migrations during wallpaper preview
146          */
147         public static final String PREVIEW_TABLE_NAME = "favorites_preview";
148 
149         /**
150          * Temporary table used specifically for multi-db grid migrations
151          */
152         public static final String TMP_TABLE = "favorites_tmp";
153 
154         /**
155          * The content:// style URL for "favorites" table
156          */
157         public static final Uri CONTENT_URI = Uri.parse("content://"
158                 + LauncherProvider.AUTHORITY + "/" + TABLE_NAME);
159 
160         /**
161          * The content:// style URL for "favorites_bakup" table
162          */
163         public static final Uri BACKUP_CONTENT_URI = Uri.parse("content://"
164                 + LauncherProvider.AUTHORITY + "/" + BACKUP_TABLE_NAME);
165 
166         /**
167          * The content:// style URL for "favorites_preview" table
168          */
169         public static final Uri PREVIEW_CONTENT_URI = Uri.parse("content://"
170                 + LauncherProvider.AUTHORITY + "/" + PREVIEW_TABLE_NAME);
171 
172         /**
173          * The content:// style URL for "favorites_tmp" table
174          */
175         public static final Uri TMP_CONTENT_URI = Uri.parse("content://"
176                 + LauncherProvider.AUTHORITY + "/" + TMP_TABLE);
177 
178         /**
179          * The content:// style URL for a given row, identified by its id.
180          *
181          * @param id The row id.
182          *
183          * @return The unique content URL for the specified row.
184          */
getContentUri(int id)185         public static Uri getContentUri(int id) {
186             return Uri.parse("content://" + LauncherProvider.AUTHORITY
187                     + "/" + TABLE_NAME + "/" + id);
188         }
189 
190         /**
191          * The container holding the favorite
192          * <P>Type: INTEGER</P>
193          */
194         public static final String CONTAINER = "container";
195 
196         /**
197          * The icon is a resource identified by a package name and an integer id.
198          */
199         public static final int CONTAINER_DESKTOP = -100;
200         public static final int CONTAINER_HOTSEAT = -101;
201         public static final int CONTAINER_PREDICTION = -102;
202         public static final int CONTAINER_WIDGETS_PREDICTION = -111;
203         public static final int CONTAINER_HOTSEAT_PREDICTION = -103;
204         public static final int CONTAINER_ALL_APPS = -104;
205         public static final int CONTAINER_WIDGETS_TRAY = -105;
206         public static final int CONTAINER_BOTTOM_WIDGETS_TRAY = -112;
207         public static final int CONTAINER_PIN_WIDGETS = -113;
208         public static final int CONTAINER_WALLPAPERS = -114;
209         // Represents search results view.
210         public static final int CONTAINER_SEARCH_RESULTS = -106;
211         public static final int CONTAINER_SHORTCUTS = -107;
212         public static final int CONTAINER_SETTINGS = -108;
213         public static final int CONTAINER_TASKSWITCHER = -109;
214         public static final int CONTAINER_QSB = -110;
215 
216         // Represents any of the extended containers implemented in non-AOSP variants.
217         public static final int EXTENDED_CONTAINERS = -200;
218 
219         public static final int CONTAINER_UNKNOWN = -1;
220 
containerToString(int container)221         public static final String containerToString(int container) {
222             switch (container) {
223                 case CONTAINER_DESKTOP: return "desktop";
224                 case CONTAINER_HOTSEAT: return "hotseat";
225                 case CONTAINER_PREDICTION: return "prediction";
226                 case CONTAINER_ALL_APPS: return "all_apps";
227                 case CONTAINER_WIDGETS_TRAY: return "widgets_tray";
228                 case CONTAINER_SEARCH_RESULTS: return "search_result";
229                 case CONTAINER_SHORTCUTS: return "shortcuts";
230                 default: return String.valueOf(container);
231             }
232         }
233 
itemTypeToString(int type)234         public static final String itemTypeToString(int type) {
235             switch(type) {
236                 case ITEM_TYPE_APPLICATION: return "APP";
237                 case ITEM_TYPE_SHORTCUT: return "SHORTCUT";
238                 case ITEM_TYPE_FOLDER: return "FOLDER";
239                 case ITEM_TYPE_APPWIDGET: return "WIDGET";
240                 case ITEM_TYPE_CUSTOM_APPWIDGET: return "CUSTOMWIDGET";
241                 case ITEM_TYPE_DEEP_SHORTCUT: return "DEEPSHORTCUT";
242                 case ITEM_TYPE_TASK: return "TASK";
243                 case ITEM_TYPE_QSB: return "QSB";
244                 default: return String.valueOf(type);
245             }
246         }
247 
248         /**
249          * The screen holding the favorite (if container is CONTAINER_DESKTOP)
250          * <P>Type: INTEGER</P>
251          */
252         public static final String SCREEN = "screen";
253 
254         /**
255          * The X coordinate of the cell holding the favorite
256          * (if container is CONTAINER_HOTSEAT or CONTAINER_HOTSEAT)
257          * <P>Type: INTEGER</P>
258          */
259         public static final String CELLX = "cellX";
260 
261         /**
262          * The Y coordinate of the cell holding the favorite
263          * (if container is CONTAINER_DESKTOP)
264          * <P>Type: INTEGER</P>
265          */
266         public static final String CELLY = "cellY";
267 
268         /**
269          * The X span of the cell holding the favorite
270          * <P>Type: INTEGER</P>
271          */
272         public static final String SPANX = "spanX";
273 
274         /**
275          * The Y span of the cell holding the favorite
276          * <P>Type: INTEGER</P>
277          */
278         public static final String SPANY = "spanY";
279 
280         /**
281          * The profile id of the item in the cell.
282          * <P>
283          * Type: INTEGER
284          * </P>
285          */
286         public static final String PROFILE_ID = "profileId";
287 
288         /**
289          * The appWidgetId of the widget
290          *
291          * <P>Type: INTEGER</P>
292          */
293         public static final String APPWIDGET_ID = "appWidgetId";
294 
295         /**
296          * The ComponentName of the widget provider
297          *
298          * <P>Type: STRING</P>
299          */
300         public static final String APPWIDGET_PROVIDER = "appWidgetProvider";
301 
302         /**
303          * Boolean indicating that his item was restored and not yet successfully bound.
304          * <P>Type: INTEGER</P>
305          */
306         public static final String RESTORED = "restored";
307 
308         /**
309          * Indicates the position of the item inside an auto-arranged view like folder or hotseat.
310          * <p>Type: INTEGER</p>
311          */
312         public static final String RANK = "rank";
313 
314         /**
315          * Stores general flag based options for {@link ItemInfo}s.
316          * <p>Type: INTEGER</p>
317          */
318         public static final String OPTIONS = "options";
319 
320         /**
321          * Stores the source container that the widget was added from.
322          * <p>Type: INTEGER</p>
323          */
324         public static final String APPWIDGET_SOURCE = "appWidgetSource";
325 
addTableToDb(SQLiteDatabase db, long myProfileId, boolean optional)326         public static void addTableToDb(SQLiteDatabase db, long myProfileId, boolean optional) {
327             addTableToDb(db, myProfileId, optional, TABLE_NAME);
328         }
329 
addTableToDb(SQLiteDatabase db, long myProfileId, boolean optional, String tableName)330         public static void addTableToDb(SQLiteDatabase db, long myProfileId, boolean optional,
331                 String tableName) {
332             String ifNotExists = optional ? " IF NOT EXISTS " : "";
333             db.execSQL("CREATE TABLE " + ifNotExists + tableName + " (" +
334                     "_id INTEGER PRIMARY KEY," +
335                     "title TEXT," +
336                     "intent TEXT," +
337                     "container INTEGER," +
338                     "screen INTEGER," +
339                     "cellX INTEGER," +
340                     "cellY INTEGER," +
341                     "spanX INTEGER," +
342                     "spanY INTEGER," +
343                     "itemType INTEGER," +
344                     "appWidgetId INTEGER NOT NULL DEFAULT -1," +
345                     "iconPackage TEXT," +
346                     "iconResource TEXT," +
347                     "icon BLOB," +
348                     "appWidgetProvider TEXT," +
349                     "modified INTEGER NOT NULL DEFAULT 0," +
350                     "restored INTEGER NOT NULL DEFAULT 0," +
351                     "profileId INTEGER DEFAULT " + myProfileId + "," +
352                     "rank INTEGER NOT NULL DEFAULT 0," +
353                     "options INTEGER NOT NULL DEFAULT 0," +
354                     APPWIDGET_SOURCE + " INTEGER NOT NULL DEFAULT " + CONTAINER_UNKNOWN +
355                     ");");
356         }
357     }
358 
359     /**
360      * Launcher settings
361      */
362     public static final class Settings {
363 
364         public static final Uri CONTENT_URI = Uri.parse("content://" +
365                 LauncherProvider.AUTHORITY + "/settings");
366 
367         public static final String METHOD_CLEAR_EMPTY_DB_FLAG = "clear_empty_db_flag";
368         public static final String METHOD_WAS_EMPTY_DB_CREATED = "get_empty_db_flag";
369 
370         public static final String METHOD_DELETE_EMPTY_FOLDERS = "delete_empty_folders";
371 
372         public static final String METHOD_NEW_ITEM_ID = "generate_new_item_id";
373         public static final String METHOD_NEW_SCREEN_ID = "generate_new_screen_id";
374 
375         public static final String METHOD_CREATE_EMPTY_DB = "create_empty_db";
376 
377         public static final String METHOD_LOAD_DEFAULT_FAVORITES = "load_default_favorites";
378 
379         public static final String METHOD_REMOVE_GHOST_WIDGETS = "remove_ghost_widgets";
380 
381         public static final String METHOD_NEW_TRANSACTION = "new_db_transaction";
382 
383         public static final String METHOD_REFRESH_BACKUP_TABLE = "refresh_backup_table";
384 
385         public static final String METHOD_REFRESH_HOTSEAT_RESTORE_TABLE = "restore_hotseat_table";
386 
387         public static final String METHOD_RESTORE_BACKUP_TABLE = "restore_backup_table";
388 
389         public static final String METHOD_UPDATE_CURRENT_OPEN_HELPER = "update_current_open_helper";
390 
391         public static final String METHOD_PREP_FOR_PREVIEW = "prep_for_preview";
392 
393         public static final String METHOD_SWITCH_DATABASE = "switch_database";
394 
395         public static final String EXTRA_VALUE = "value";
396 
397         public static final String EXTRA_DB_NAME = "db_name";
398 
call(ContentResolver cr, String method)399         public static Bundle call(ContentResolver cr, String method) {
400             return call(cr, method, null /* arg */);
401         }
402 
call(ContentResolver cr, String method, String arg)403         public static Bundle call(ContentResolver cr, String method, String arg) {
404             return call(cr, method, arg, null /* extras */);
405         }
406 
call(ContentResolver cr, String method, String arg, Bundle extras)407         public static Bundle call(ContentResolver cr, String method, String arg, Bundle extras) {
408             return cr.call(CONTENT_URI, method, arg, extras);
409         }
410     }
411 }
412