summaryrefslogtreecommitdiffstats
path: root/src/com/cyngn/theme/util/CursorLoaderHelper.java
blob: 59927799e46e379dfaffa02e79a239125249b7cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
 * Copyright (C) 2015 Cyanogen, Inc.
 */
package com.cyngn.theme.util;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;

import cyanogenmod.app.ThemeVersion;
import cyanogenmod.providers.ThemesContract;
import cyanogenmod.providers.ThemesContract.PreviewColumns;
import cyanogenmod.providers.ThemesContract.ThemesColumns;

import static cyanogenmod.providers.ThemesContract.ThemesColumns.MODIFIES_ALARMS;
import static cyanogenmod.providers.ThemesContract.ThemesColumns.MODIFIES_BOOT_ANIM;
import static cyanogenmod.providers.ThemesContract.ThemesColumns.MODIFIES_LAUNCHER;
import static cyanogenmod.providers.ThemesContract.ThemesColumns.MODIFIES_LIVE_LOCK_SCREEN;
import static cyanogenmod.providers.ThemesContract.ThemesColumns.MODIFIES_LOCKSCREEN;
import static cyanogenmod.providers.ThemesContract.ThemesColumns.MODIFIES_NOTIFICATIONS;
import static cyanogenmod.providers.ThemesContract.ThemesColumns.MODIFIES_OVERLAYS;
import static cyanogenmod.providers.ThemesContract.ThemesColumns.MODIFIES_RINGTONES;
import static cyanogenmod.providers.ThemesContract.ThemesColumns.MODIFIES_STATUS_BAR;
import static cyanogenmod.providers.ThemesContract.ThemesColumns.MODIFIES_NAVIGATION_BAR;
import static cyanogenmod.providers.ThemesContract.ThemesColumns.MODIFIES_ICONS;
import static cyanogenmod.providers.ThemesContract.ThemesColumns.MODIFIES_FONTS;

public class CursorLoaderHelper {

    public static final int LOADER_ID_INVALID = -1;
    public static final int LOADER_ID_ALL = 0;
    public static final int LOADER_ID_STATUS_BAR = 1;
    public static final int LOADER_ID_FONT = 2;
    public static final int LOADER_ID_ICONS = 3;
    public static final int LOADER_ID_WALLPAPER = 4;
    public static final int LOADER_ID_NAVIGATION_BAR = 5;
    public static final int LOADER_ID_LOCKSCREEN = 6;
    public static final int LOADER_ID_STYLE = 7;
    public static final int LOADER_ID_BOOT_ANIMATION = 8;
    public static final int LOADER_ID_RINGTONE = 9;
    public static final int LOADER_ID_NOTIFICATION = 10;
    public static final int LOADER_ID_ALARM = 11;
    public static final int LOADER_ID_LIVE_LOCK_SCREEN = 12;
    public static final int LOADER_ID_INSTALLED_THEMES = 1000;
    public static final int LOADER_ID_APPLIED = 1001;

    private static final long DEFAULT_COMPONENT_ID = 0;

    private static int mThemeVersion = ThemeVersion.getVersion();

    public static Loader<Cursor> chooserActivityCursorLoader(Context context, int id,
            String appliedBaseTheme) {
        String selection = null;
        String selectionArgs[] = null;
        String sortOrder = null;
        String[] projection = null;
        Uri contentUri = null;

        switch (id) {
            case LOADER_ID_INSTALLED_THEMES:
                selection = ThemesColumns.PRESENT_AS_THEME + "=? AND " +
                        ThemesColumns.INSTALL_STATE + "=?";
                selectionArgs = new String[] { "1", "" + ThemesColumns.InstallState.INSTALLED};
                // sort in ascending order but make sure the "default" theme is always first
                sortOrder = "(" + ThemesColumns.IS_DEFAULT_THEME + "=1) DESC, "
                        + "(" + ThemesColumns.PKG_NAME + "='" + appliedBaseTheme + "') DESC, "
                        + ThemesColumns.INSTALL_TIME + " DESC";
                contentUri = ThemesColumns.CONTENT_URI;
                projection = new String[] {ThemesColumns.PKG_NAME, ThemesColumns.TITLE,
                        ThemesColumns.AUTHOR};
                break;
            case LOADER_ID_APPLIED:
                //TODO: Mix n match query should only be done once
                contentUri = ThemesContract.MixnMatchColumns.CONTENT_URI;
                selection = null;
                selectionArgs = null;
                break;
        }

        return new CursorLoader(context, contentUri, projection, selection,
                selectionArgs, sortOrder);
    }

    public static Loader<Cursor> componentSelectorCursorLoader(Context context, int id) {
        Uri uri = PreviewColumns.CONTENT_URI;
        String selection;
        String[] selectionArgs = { "1" };
        String[] projection = { ThemesColumns.TITLE, ThemesColumns.PKG_NAME };
        switch(id) {
            case LOADER_ID_STATUS_BAR:
                selection = MODIFIES_STATUS_BAR + "=?";
                projection = new String[] {
                        PreviewColumns.STATUSBAR_WIFI_ICON,
                        PreviewColumns.STATUSBAR_SIGNAL_ICON,
                        PreviewColumns.STATUSBAR_BLUETOOTH_ICON,
                        PreviewColumns.STATUSBAR_BACKGROUND,
                        PreviewColumns.STATUSBAR_BATTERY_CIRCLE,
                        PreviewColumns.STATUSBAR_BATTERY_LANDSCAPE,
                        PreviewColumns.STATUSBAR_BATTERY_PORTRAIT,
                        ThemesColumns.TITLE,
                        ThemesColumns.PKG_NAME
                };
                break;
            case LOADER_ID_NAVIGATION_BAR:
                selection = MODIFIES_NAVIGATION_BAR + "=?";
                projection = new String[] {
                        PreviewColumns.NAVBAR_BACK_BUTTON,
                        PreviewColumns.STATUSBAR_BACKGROUND,
                        ThemesColumns.TITLE,
                        ThemesColumns.PKG_NAME,
                };
                break;
            case LOADER_ID_FONT:
                // fonts don't have generated previews so use the ThemesColumns.CONTENT_URI
                uri = ThemesColumns.CONTENT_URI;
                selection = MODIFIES_FONTS + "=?";
                break;
            case LOADER_ID_ICONS:
                selection = MODIFIES_ICONS + "=?";
                projection = new String[] {
                        PreviewColumns.ICON_PREVIEW_1,
                        ThemesColumns.TITLE,
                        ThemesColumns.PKG_NAME
                };
                break;
            case LOADER_ID_STYLE:
                selection = MODIFIES_OVERLAYS + "=?";
                projection = new String[] {
                        PreviewColumns.STYLE_THUMBNAIL,
                        ThemesColumns.TITLE,
                        ThemesColumns.PKG_NAME
                };
                break;
            case LOADER_ID_WALLPAPER:
                selection = MODIFIES_LAUNCHER + "=?";
                if (mThemeVersion >= 3) {
                    uri = PreviewColumns.COMPONENTS_URI;
                    projection = new String[]{
                            PreviewColumns.WALLPAPER_THUMBNAIL,
                            ThemesColumns.TITLE,
                            ThemesColumns.PKG_NAME,
                            PreviewColumns.COMPONENT_ID
                    };
                } else {
                    projection = new String[]{
                            PreviewColumns.WALLPAPER_THUMBNAIL,
                            ThemesColumns.TITLE,
                            ThemesColumns.PKG_NAME
                    };
                }
                break;
            case LOADER_ID_BOOT_ANIMATION:
                selection = MODIFIES_BOOT_ANIM + "=?";
                projection = new String[] {
                        PreviewColumns.BOOTANIMATION_THUMBNAIL,
                        ThemesColumns.TITLE,
                        ThemesColumns.PKG_NAME
                };
                break;
            case LOADER_ID_RINGTONE:
                selection = MODIFIES_RINGTONES + "=?";
                break;
            case LOADER_ID_NOTIFICATION:
                selection = MODIFIES_NOTIFICATIONS + "=?";
                break;
            case LOADER_ID_ALARM:
                selection = MODIFIES_ALARMS + "=?";
                break;
            case LOADER_ID_LOCKSCREEN:
                selection = MODIFIES_LOCKSCREEN + "=?";
                selectionArgs = new String[] { "1" };
                if (mThemeVersion >= 3) {
                    projection = new String[]{
                            PreviewColumns.LOCK_WALLPAPER_THUMBNAIL,
                            PreviewColumns.LIVE_LOCK_SCREEN_THUMBNAIL,
                            ThemesColumns.TITLE,
                            ThemesColumns.PKG_NAME,
                            ThemesColumns.MODIFIES_LIVE_LOCK_SCREEN,
                            PreviewColumns.COMPONENT_ID
                    };
                } else {
                    projection = new String[]{
                            PreviewColumns.LOCK_WALLPAPER_THUMBNAIL,
                            PreviewColumns.LIVE_LOCK_SCREEN_THUMBNAIL,
                            ThemesColumns.TITLE,
                            ThemesColumns.MODIFIES_LIVE_LOCK_SCREEN,
                            ThemesColumns.PKG_NAME
                    };
                }
                break;
            case LOADER_ID_LIVE_LOCK_SCREEN:
                selection = MODIFIES_LIVE_LOCK_SCREEN + "=?";
                selectionArgs = new String[] { "1" };
                if (mThemeVersion >= 3) {
                    projection = new String[]{
                            PreviewColumns.LIVE_LOCK_SCREEN_THUMBNAIL,
                            ThemesColumns.TITLE,
                            ThemesColumns.PKG_NAME,
                            ThemesColumns.MODIFIES_LIVE_LOCK_SCREEN,
                            PreviewColumns.COMPONENT_ID
                    };
                } else {
                    projection = new String[]{
                            PreviewColumns.LOCK_WALLPAPER_THUMBNAIL,
                            PreviewColumns.LIVE_LOCK_SCREEN_THUMBNAIL,
                            ThemesColumns.TITLE,
                            ThemesColumns.MODIFIES_LIVE_LOCK_SCREEN,
                            ThemesColumns.PKG_NAME
                    };
                }
                break;
            default:
                return null;
        }
        // sort in ascending order but make sure the "default" theme is always first
        String sortOrder = "(" + ThemesContract.ThemesColumns.IS_DEFAULT_THEME + "=1) DESC, "
                + ThemesContract.ThemesColumns.TITLE + " ASC";
        return new CursorLoader(context, uri, projection, selection, selectionArgs, sortOrder);
    }

    public static Loader<Cursor> myThemeFragmentCursorLoader(Context context, int id) {
        Uri uri;
        String[] projection;
        projection = new String[]{
                PreviewColumns.WALLPAPER_PREVIEW,
                PreviewColumns.STATUSBAR_BACKGROUND,
                PreviewColumns.STATUSBAR_WIFI_ICON,
                PreviewColumns.STATUSBAR_WIFI_COMBO_MARGIN_END,
                PreviewColumns.STATUSBAR_BLUETOOTH_ICON,
                PreviewColumns.STATUSBAR_SIGNAL_ICON,
                PreviewColumns.STATUSBAR_CLOCK_TEXT_COLOR,
                PreviewColumns.STATUSBAR_BATTERY_CIRCLE,
                PreviewColumns.STATUSBAR_BATTERY_LANDSCAPE,
                PreviewColumns.STATUSBAR_BATTERY_PORTRAIT,
                PreviewColumns.NAVBAR_BACK_BUTTON,
                PreviewColumns.NAVBAR_HOME_BUTTON,
                PreviewColumns.NAVBAR_RECENT_BUTTON,
                PreviewColumns.ICON_PREVIEW_1,
                PreviewColumns.ICON_PREVIEW_2,
                PreviewColumns.ICON_PREVIEW_3,
                PreviewColumns.LOCK_WALLPAPER_PREVIEW,
                PreviewColumns.STYLE_PREVIEW,
                PreviewColumns.NAVBAR_BACKGROUND,
                PreviewColumns.LIVE_LOCK_SCREEN_PREVIEW
        };
        uri = PreviewColumns.APPLIED_URI;
        return new CursorLoader(context, uri, projection, null, null, null);
    }

    public static Loader<Cursor> themeFragmentCursorLoader(Context context, int id, String pkgName,
            long componentId) {
        Uri uri = PreviewColumns.CONTENT_URI;
        String selection = ThemesContract.ThemesColumns.PKG_NAME + "= ?";
        String[] selectionArgs = new String[] { pkgName };
        String[] projection = null;
        switch (id) {
            case LOADER_ID_ALL:
                if (mThemeVersion >= 3) {
                    // Load all default component previews (component_id == 0)
                    selection += " AND " + PreviewColumns.COMPONENT_ID + "=?";
                    selectionArgs = new String[] { pkgName, String.valueOf(DEFAULT_COMPONENT_ID) };
                } else {
                    // SQL query will fail if we ask for PreviewColumns.COMPONENT_ID, don't add it.
                    selectionArgs = new String[]{pkgName};
                }
                projection = new String[] {
                        ThemesColumns.PKG_NAME,
                        ThemesColumns.TITLE,
                        ThemesColumns.AUTHOR,
                        ThemesColumns.WALLPAPER_URI,
                        ThemesColumns.HOMESCREEN_URI,
                        ThemesColumns.TARGET_API,
                        // Theme abilities
                        ThemesColumns.MODIFIES_LAUNCHER,
                        ThemesColumns.MODIFIES_LOCKSCREEN,
                        ThemesColumns.MODIFIES_ALARMS,
                        ThemesColumns.MODIFIES_BOOT_ANIM,
                        ThemesColumns.MODIFIES_FONTS,
                        ThemesColumns.MODIFIES_ICONS,
                        ThemesColumns.MODIFIES_NAVIGATION_BAR,
                        ThemesColumns.MODIFIES_OVERLAYS,
                        ThemesColumns.MODIFIES_RINGTONES,
                        ThemesColumns.MODIFIES_STATUS_BAR,
                        ThemesColumns.MODIFIES_NOTIFICATIONS,
                        //Previews
                        PreviewColumns.WALLPAPER_PREVIEW,
                        PreviewColumns.STATUSBAR_BACKGROUND,
                        PreviewColumns.STATUSBAR_WIFI_ICON,
                        PreviewColumns.STATUSBAR_WIFI_COMBO_MARGIN_END,
                        PreviewColumns.STATUSBAR_BLUETOOTH_ICON,
                        PreviewColumns.STATUSBAR_SIGNAL_ICON,
                        PreviewColumns.STATUSBAR_CLOCK_TEXT_COLOR,
                        PreviewColumns.STATUSBAR_BATTERY_CIRCLE,
                        PreviewColumns.STATUSBAR_BATTERY_LANDSCAPE,
                        PreviewColumns.STATUSBAR_BATTERY_PORTRAIT,
                        PreviewColumns.NAVBAR_BACK_BUTTON,
                        PreviewColumns.NAVBAR_HOME_BUTTON,
                        PreviewColumns.NAVBAR_RECENT_BUTTON,
                        PreviewColumns.ICON_PREVIEW_1,
                        PreviewColumns.ICON_PREVIEW_2,
                        PreviewColumns.ICON_PREVIEW_3,
                        PreviewColumns.LOCK_WALLPAPER_PREVIEW,
                        PreviewColumns.STYLE_PREVIEW,
                        PreviewColumns.LIVE_LOCK_SCREEN_PREVIEW
                };
                break;
            case LOADER_ID_STATUS_BAR:
                projection = new String[] {
                        ThemesColumns.PKG_NAME,
                        ThemesColumns.TITLE,
                        PreviewColumns.STATUSBAR_BACKGROUND,
                        PreviewColumns.STATUSBAR_WIFI_ICON,
                        PreviewColumns.STATUSBAR_WIFI_COMBO_MARGIN_END,
                        PreviewColumns.STATUSBAR_BLUETOOTH_ICON,
                        PreviewColumns.STATUSBAR_SIGNAL_ICON,
                        PreviewColumns.STATUSBAR_CLOCK_TEXT_COLOR,
                        PreviewColumns.STATUSBAR_BATTERY_CIRCLE,
                        PreviewColumns.STATUSBAR_BATTERY_LANDSCAPE,
                        PreviewColumns.STATUSBAR_BATTERY_PORTRAIT
                };
                break;
            case LOADER_ID_FONT:
                uri = ThemesColumns.CONTENT_URI;
                projection = new String[] {
                        ThemesColumns.PKG_NAME,
                        ThemesColumns.TITLE
                };
                break;
            case LOADER_ID_ICONS:
                projection = new String[] {
                        ThemesColumns.PKG_NAME,
                        ThemesColumns.TITLE,
                        PreviewColumns.ICON_PREVIEW_1,
                        PreviewColumns.ICON_PREVIEW_2,
                        PreviewColumns.ICON_PREVIEW_3,
                };
                break;
            case LOADER_ID_WALLPAPER:
                if (mThemeVersion >= 3) {
                    uri = PreviewColumns.COMPONENTS_URI;
                    // Load specified wallpaper previews (component_id is specified)
                    selection += " AND " + PreviewColumns.COMPONENT_ID + "=?";
                    selectionArgs = new String[]{pkgName, String.valueOf(componentId)};
                    projection = new String[]{
                            ThemesColumns.PKG_NAME,
                            ThemesColumns.TITLE,
                            PreviewColumns.WALLPAPER_PREVIEW,
                            PreviewColumns.COMPONENT_ID
                    };
                } else {
                    projection = new String[]{
                            ThemesColumns.PKG_NAME,
                            ThemesColumns.TITLE,
                            PreviewColumns.WALLPAPER_PREVIEW
                    };
                }
                break;
            case LOADER_ID_NAVIGATION_BAR:
                projection = new String[] {
                        ThemesColumns.PKG_NAME,
                        ThemesColumns.TITLE,
                        PreviewColumns.STATUSBAR_BACKGROUND,
                        PreviewColumns.NAVBAR_BACK_BUTTON,
                        PreviewColumns.NAVBAR_HOME_BUTTON,
                        PreviewColumns.NAVBAR_RECENT_BUTTON
                };
                break;
            case LOADER_ID_LOCKSCREEN:
                projection = new String[]{
                        ThemesColumns.PKG_NAME,
                        ThemesColumns.TITLE,
                        PreviewColumns.LOCK_WALLPAPER_PREVIEW,
                };
                break;
            case LOADER_ID_LIVE_LOCK_SCREEN:
                projection = new String[]{
                        ThemesColumns.PKG_NAME,
                        ThemesColumns.TITLE,
                        ThemesColumns.MODIFIES_LIVE_LOCK_SCREEN,
                        PreviewColumns.LIVE_LOCK_SCREEN_PREVIEW
                };
                break;
            case LOADER_ID_STYLE:
                projection = new String[] {
                        ThemesColumns.PKG_NAME,
                        ThemesColumns.TITLE,
                        PreviewColumns.STYLE_PREVIEW
                };
                break;
            case LOADER_ID_BOOT_ANIMATION:
                projection = new String[] {
                        ThemesColumns.PKG_NAME,
                        ThemesColumns.TITLE
                };
                break;
            case LOADER_ID_RINGTONE:
            case LOADER_ID_NOTIFICATION:
            case LOADER_ID_ALARM:
                projection = new String[] {
                        ThemesColumns.PKG_NAME,
                        ThemesColumns.TITLE
                };
                break;
        }
        return new CursorLoader(context, uri, projection, selection, selectionArgs, null);
    }

    public static Object[] getRowFromCursor(Cursor cursor) {
        Object[] row = null;
        if (cursor != null) {
            int colCount = cursor.getColumnCount();
            row = new Object[colCount];
            for (int indx = 0; indx < colCount; indx++) {
                row[indx] = getFieldValueFromRow(cursor, indx);
            }
        }
        return row;
    }

    public static Object getFieldValueFromRow(Cursor cursor, int position) {
        switch (cursor.getType(position)) {
            case Cursor.FIELD_TYPE_BLOB: return cursor.getBlob(position);
            case Cursor.FIELD_TYPE_FLOAT: return cursor.getFloat(position);
            case Cursor.FIELD_TYPE_INTEGER: return cursor.getInt(position);
            case Cursor.FIELD_TYPE_STRING: return cursor.getString(position);
            case Cursor.FIELD_TYPE_NULL:
            default:
                return null;
        }
    }
}