summaryrefslogtreecommitdiffstats
path: root/src/org/cyanogenmod/themes/provider/ThemesOpenHelper.java
blob: b0835de652a3f034f291d96e669ac5e413db8a47 (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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/*
 * Copyright (C) 2014 The CyanogenMod Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.cyanogenmod.themes.provider;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ThemeUtils;
import android.content.res.ThemeConfig;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Build;
import android.provider.ThemesContract;
import android.provider.ThemesContract.ThemesColumns;
import android.provider.ThemesContract.MixnMatchColumns;
import android.provider.ThemesContract.PreviewColumns;
import android.util.Log;

public class ThemesOpenHelper extends SQLiteOpenHelper {
    private static final String TAG = ThemesOpenHelper.class.getName();

    private static final int DATABASE_VERSION = 14;
    private static final String DATABASE_NAME = "themes.db";
    private static final String SYSTEM_THEME_PKG_NAME = ThemeConfig.SYSTEM_DEFAULT;

    private Context mContext;

    public ThemesOpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        mContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(ThemesTable.THEMES_TABLE_CREATE);
        db.execSQL(MixnMatchTable.MIXNMATCH_TABLE_CREATE);
        db.execSQL(PreviewsTable.PREVIEWS_TABLE_CREATE);

        ThemesTable.insertSystemDefaults(db, mContext);
        MixnMatchTable.insertDefaults(db);
        PreviewsTable.insertDefaults(mContext);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.i(TAG, "Upgrading DB from version " + oldVersion + " to " + newVersion);
        try {
            if (oldVersion == 1) {
                upgradeToVersion2(db);
                oldVersion = 2;
            }
            if (oldVersion == 2) {
                upgradeToVersion3(db);
                oldVersion = 3;
            }
            if (oldVersion == 3) {
                upgradeToVersion4(db);
                oldVersion = 4;
            }
            if (oldVersion == 4) {
                upgradeToVersion5(db);
                oldVersion = 5;
            }
            if (oldVersion == 5) {
                upgradeToVersion6(db);
                oldVersion = 6;
            }
            if (oldVersion == 6) {
                upgradeToVersion7(db);
                oldVersion = 7;
            }
            if (oldVersion == 7) {
                upgradeToVersion8(db);
                oldVersion = 8;
            }
            if (oldVersion == 8) {
                upgradeToVersion9(db);
                oldVersion = 9;
            }
            if (oldVersion == 9) {
                upgradeToVersion10(db);
                oldVersion = 10;
            }
            if (oldVersion == 10) {
                upgradeToVersion11(db);
                oldVersion = 11;
            }
            if (oldVersion == 11) {
                upgradeToVersion12(db);
                oldVersion = 12;
            }
            if (oldVersion == 12) {
                upgradeToVersion13(db);
                oldVersion = 13;
            }
            if (oldVersion == 13) {
                upgradeToVersion14(db);
                oldVersion = 14;
            }
            if (oldVersion != DATABASE_VERSION) {
                Log.e(TAG, "Recreating db because unknown database version: " + oldVersion);
                dropTables(db);
                onCreate(db);
            }
        } catch(SQLiteException e) {
            Log.e(TAG, "onUpgrade: SQLiteException, recreating db. ", e);
            Log.e(TAG, "(oldVersion was " + oldVersion + ")");
            dropTables(db);
            onCreate(db);
            return;
        }
    }

    private void upgradeToVersion2(SQLiteDatabase db) {
        String addStyleColumn = String.format("ALTER TABLE %s ADD COLUMN %s TEXT",
                ThemesTable.TABLE_NAME, ThemesColumns.STYLE_URI);
        db.execSQL(addStyleColumn);
    }

    private void upgradeToVersion3(SQLiteDatabase db) {
        // Add default value to mixnmatch for KEY_ALARM
        ContentValues values = new ContentValues();
        values.put(MixnMatchColumns.COL_KEY, ThemesContract.MixnMatchColumns.KEY_ALARM);
        values.put(MixnMatchColumns.COL_VALUE, SYSTEM_THEME_PKG_NAME);
        db.insert(MixnMatchTable.TABLE_NAME, null, values);
    }

    private void upgradeToVersion4(SQLiteDatabase db) {
        String isLegacyIconPackColumn = String.format("ALTER TABLE %s" +
                        " ADD COLUMN %s INTEGER DEFAULT 0",
                ThemesTable.TABLE_NAME, ThemesColumns.IS_LEGACY_ICONPACK);
        db.execSQL(isLegacyIconPackColumn);
    }

    private void upgradeToVersion5(SQLiteDatabase db) {
        String addIsDefault = String.format("ALTER TABLE %s ADD COLUMN %s TEXT",
                ThemesTable.TABLE_NAME, ThemesColumns.IS_DEFAULT_THEME);
        db.execSQL(addIsDefault);

        // change default package name to holo
        String changeDefaultToSystem = String.format("UPDATE %s SET %s='%s' WHERE" +
                        " %s='%s'", ThemesTable.TABLE_NAME, ThemesColumns.PKG_NAME,
                SYSTEM_THEME_PKG_NAME, ThemesColumns.PKG_NAME, "default");
        db.execSQL(changeDefaultToSystem);

        if (isSystemDefault(mContext)) {
            // flag holo as default if
            String makeHoloDefault = String.format("UPDATE %s SET %s=%d WHERE" +
                            " %s='%s'", ThemesTable.TABLE_NAME, ThemesColumns.IS_DEFAULT_THEME, 1,
                    ThemesColumns.PKG_NAME, SYSTEM_THEME_PKG_NAME);
            db.execSQL(makeHoloDefault);
        }

        // change any existing mixnmatch values set to "default" to "holo"
        db.execSQL(String.format("UPDATE %s SET %s='%s' WHERE %s='%s'",
                MixnMatchTable.TABLE_NAME, MixnMatchColumns.COL_VALUE, SYSTEM_THEME_PKG_NAME,
                MixnMatchColumns.COL_VALUE, "default"));
    }

    private void upgradeToVersion6(SQLiteDatabase db) {
        db.execSQL(PreviewsTable.PREVIEWS_TABLE_CREATE);

        // remove (Default) from Holo's title
        db.execSQL(String.format("UPDATE %s SET %s='%s' WHERE %s='%s'", ThemesTable.TABLE_NAME,
                ThemesColumns.TITLE, "Holo", ThemesColumns.PKG_NAME, "holo"));

        // we need to update any existing themes
        final String[] projection = { ThemesColumns.PKG_NAME, ThemesColumns.MODIFIES_STATUS_BAR,
                ThemesColumns.MODIFIES_ICONS, ThemesColumns.MODIFIES_OVERLAYS,
                ThemesColumns.MODIFIES_LAUNCHER, ThemesColumns.MODIFIES_BOOT_ANIM };
        final String selection = ThemesColumns.MODIFIES_OVERLAYS + "=?";
        final String[] selectionArgs = { "1" };
        final Cursor c = db.query(ThemesTable.TABLE_NAME, projection, selection, selectionArgs,
                null, null, null);
        if (c != null) {
            while(c.moveToNext()) {
                Intent intent = new Intent(mContext, PreviewGenerationService.class);
                intent.setAction(PreviewGenerationService.ACTION_INSERT);
                intent.putExtra(PreviewGenerationService.EXTRA_PKG_NAME, c.getString(0));
                intent.putExtra(PreviewGenerationService.EXTRA_HAS_SYSTEMUI, c.getInt(1) == 1);
                intent.putExtra(PreviewGenerationService.EXTRA_HAS_ICONS, c.getInt(2) == 1);
                intent.putExtra(PreviewGenerationService.EXTRA_HAS_STYLES, c.getInt(3) == 1);
                intent.putExtra(PreviewGenerationService.EXTRA_HAS_WALLPAPER, c.getInt(4) == 1);
                intent.putExtra(PreviewGenerationService.EXTRA_HAS_BOOTANIMATION, c.getInt(5) == 1);
                mContext.startService(intent);
            }
            c.close();
        }
    }

    private void upgradeToVersion7(SQLiteDatabase db) {
        String addStatusBar = String.format("ALTER TABLE %s ADD COLUMN %s INTEGER",
                ThemesTable.TABLE_NAME, ThemesColumns.MODIFIES_STATUS_BAR);
        String addNavBar = String.format("ALTER TABLE %s ADD COLUMN %s INTEGER",
                ThemesTable.TABLE_NAME, ThemesColumns.MODIFIES_NAVIGATION_BAR);
        db.execSQL(addStatusBar);
        db.execSQL(addNavBar);

        // we need to update any existing themes
        final String[] projection = { ThemesColumns.PKG_NAME, ThemesColumns.IS_LEGACY_THEME };
        final String selection = ThemesColumns.MODIFIES_OVERLAYS + "=? OR " +
                ThemesColumns.IS_LEGACY_THEME + "=?";
        final String[] selectionArgs = { "1", "1" };
        final Cursor c = db.query(ThemesTable.TABLE_NAME, projection, selection, selectionArgs,
                null, null, null);
        if (c != null) {
            while(c.moveToNext()) {
                final String pkgName = c.getString(0);
                final boolean isLegacyTheme = c.getInt(1) == 1;
                boolean hasSystemUi = false;
                if (SYSTEM_THEME_PKG_NAME.equals(pkgName) || isLegacyTheme) {
                    hasSystemUi = true;
                } else {
                    try {
                        Context themeContext = mContext.createPackageContext(pkgName, 0);
                        hasSystemUi = ThemePackageHelper.hasThemeComponent(themeContext,
                                ThemePackageHelper.sComponentToFolderName.get(
                                        ThemesColumns.MODIFIES_STATUS_BAR));
                    } catch (PackageManager.NameNotFoundException e) {
                        // default to false
                    }
                }
                if (hasSystemUi) {
                    db.execSQL(String.format("UPDATE %S SET %s='1', %s='1' WHERE %s='%s'",
                            ThemesTable.TABLE_NAME, ThemesColumns.MODIFIES_STATUS_BAR,
                            ThemesColumns.MODIFIES_NAVIGATION_BAR, ThemesColumns.PKG_NAME,
                            pkgName));
                    Intent intent = new Intent(mContext, PreviewGenerationService.class);
                    intent.setAction(PreviewGenerationService.ACTION_INSERT);
                    intent.putExtra(PreviewGenerationService.EXTRA_PKG_NAME, pkgName);
                    intent.putExtra(PreviewGenerationService.EXTRA_HAS_SYSTEMUI, true);
                    mContext.startService(intent);
                }
            }
            c.close();
        }
    }

    private void upgradeToVersion8(SQLiteDatabase db) {
        String addNavBar = String.format("ALTER TABLE %s ADD COLUMN %s BLOB",
                PreviewsTable.TABLE_NAME, PreviewColumns.NAVBAR_BACKGROUND);
        db.execSQL(addNavBar);

        // we need to update any existing themes with the new NAVBAR_BACKGROUND
        final String[] projection = { ThemesColumns.PKG_NAME, ThemesColumns.IS_LEGACY_THEME };
        final String selection = ThemesColumns.MODIFIES_OVERLAYS + "=? OR " +
                ThemesColumns.IS_LEGACY_THEME + "=?";
        final String[] selectionArgs = { "1", "1" };
        final Cursor c = db.query(ThemesTable.TABLE_NAME, projection, selection, selectionArgs,
                null, null, null);
        if (c != null) {
            while(c.moveToNext()) {
                final String pkgName = c.getString(0);
                final boolean isLegacyTheme = c.getInt(1) == 1;
                boolean hasSystemUi = false;
                if (SYSTEM_THEME_PKG_NAME.equals(pkgName) || isLegacyTheme) {
                    hasSystemUi = true;
                } else {
                    try {
                        Context themeContext = mContext.createPackageContext(pkgName, 0);
                        hasSystemUi = ThemePackageHelper.hasThemeComponent(themeContext,
                                ThemePackageHelper.sComponentToFolderName.get(
                                        ThemesColumns.MODIFIES_STATUS_BAR));
                    } catch (PackageManager.NameNotFoundException e) {
                        // default to false
                    }
                }
                if (hasSystemUi) {
                    Intent intent = new Intent(mContext, PreviewGenerationService.class);
                    intent.setAction(PreviewGenerationService.ACTION_INSERT);
                    intent.putExtra(PreviewGenerationService.EXTRA_PKG_NAME, pkgName);
                    intent.putExtra(PreviewGenerationService.EXTRA_HAS_SYSTEMUI, true);
                    mContext.startService(intent);
                }
            }
            c.close();
        }
    }

    private void upgradeToVersion9(SQLiteDatabase db) {
        String addNavBar = String.format("ALTER TABLE %s ADD COLUMN %s INTEGER DEFAULT 0",
                ThemesTable.TABLE_NAME, ThemesColumns.INSTALL_TIME);
        db.execSQL(addNavBar);

        // we need to update any existing themes with their install time
        final String[] projection = { ThemesColumns.PKG_NAME };
        final Cursor c = db.query(ThemesTable.TABLE_NAME, projection, null, null, null, null, null);
        if (c != null) {
            while(c.moveToNext()) {
                final String pkgName = c.getString(0);
                try {
                    PackageInfo pi = mContext.getPackageManager().getPackageInfo(pkgName, 0);
                    db.execSQL(String.format("UPDATE %s SET %s='%d' WHERE %s='%s'",
                            ThemesTable.TABLE_NAME, ThemesColumns.INSTALL_TIME, pi.firstInstallTime,
                            ThemesColumns.PKG_NAME, pkgName));
                } catch (PackageManager.NameNotFoundException e) {
                    Log.e(TAG, "Unable to update install time for " + pkgName, e);
                }
            }
            c.close();
        }
    }

    private void upgradeToVersion10(SQLiteDatabase db) {
        // add API entries
        String sql = String.format("ALTER TABLE %s ADD COLUMN %s TEXT",
                ThemesTable.TABLE_NAME, ThemesColumns.TARGET_API);
        db.execSQL(sql);

        // we need to update any existing themes with their install time
        final String[] projection = { ThemesColumns.PKG_NAME };
        final Cursor c = db.query(ThemesTable.TABLE_NAME, projection, null, null, null, null, null);
        if (c != null) {
            while(c.moveToNext()) {
                final String pkgName = c.getString(0);
                int targetSdk = -1;
                if (SYSTEM_THEME_PKG_NAME.equals(pkgName)) {
                    // 0 is a special value used for the system theme, not to be confused with the
                    // default theme which may not be the same as the system theme.
                    targetSdk = 0;
                } else {
                    try {
                        PackageInfo pi = mContext.getPackageManager().getPackageInfo(pkgName, 0);
                        targetSdk = pi.applicationInfo.targetSdkVersion;
                    } catch (PackageManager.NameNotFoundException e) {
                        Log.e(TAG, "Unable to update target sdk for " + pkgName, e);
                    }
                }
                if (targetSdk != -1) {
                    db.execSQL(String.format("UPDATE %s SET %s='%d' WHERE %s='%s'", ThemesTable
                                    .TABLE_NAME, ThemesColumns.TARGET_API,
                            targetSdk, ThemesColumns.PKG_NAME, pkgName));
                }
            }
            c.close();
        }
    }

    private void upgradeToVersion11(SQLiteDatabase db) {
        // Update holo theme to be called "system"
        final String NEW_THEME_TITLE = "System";
        final String PREV_SYSTEM_PKG_NAME = "holo";
        String holoToSystem = String.format("UPDATE TABLE %s " +
                        "SET title=%s, pkg_name=%s " +
                        "WHERE %s='%s'",
                ThemesTable.TABLE_NAME,
                NEW_THEME_TITLE,
                SYSTEM_THEME_PKG_NAME,
                ThemesColumns.PKG_NAME, PREV_SYSTEM_PKG_NAME);
        db.execSQL(holoToSystem);

    }

    private void upgradeToVersion12(SQLiteDatabase db) {
        // This upgrade performs an update to the ThemesColumns.PRESENT_AS_THEME since the
        // requirements for what is a presentable theme have changed.
        final String[] projection = { ThemesColumns.PKG_NAME, ThemesColumns.MODIFIES_LAUNCHER,
                ThemesColumns.MODIFIES_OVERLAYS};
        final Cursor c = db.query(ThemesTable.TABLE_NAME, projection, null, null, null, null, null);
        if (c != null) {
            while(c.moveToNext()) {
                final String pkgName = c.getString(0);
                boolean presentAsTheme =
                        c.getInt(c.getColumnIndex(ThemesColumns.MODIFIES_LAUNCHER)) == 1 &&
                                c.getInt(c.getColumnIndex(ThemesColumns.MODIFIES_OVERLAYS)) == 1;
                db.execSQL(String.format("UPDATE %s SET %s='%d' WHERE %s='%s'",
                        ThemesTable.TABLE_NAME, ThemesColumns.PRESENT_AS_THEME,
                            presentAsTheme ? 1 : 0, ThemesColumns.PKG_NAME, pkgName));
            }
            c.close();
        }
    }

    private void upgradeToVersion13(SQLiteDatabase db) {
        // add install_state column to themes db
        String sql = String.format("ALTER TABLE %s ADD COLUMN %s INTEGER DEFAULT %d",
                ThemesTable.TABLE_NAME, ThemesColumns.INSTALL_STATE,
                ThemesColumns.InstallState.UNKNOWN);
        db.execSQL(sql);

        // we need to update any existing themes with their install state
        db.execSQL(String.format("UPDATE %s SET %s='%d'", ThemesTable.TABLE_NAME,
                ThemesColumns.INSTALL_STATE, ThemesColumns.InstallState.INSTALLED));
    }

    private void upgradeToVersion14(SQLiteDatabase db) {
        // add previous_value column to mixnmatch db
        String sql = String.format("ALTER TABLE %s ADD COLUMN %s TEXT",
                MixnMatchTable.TABLE_NAME, MixnMatchColumns.COL_PREV_VALUE);
        db.execSQL(sql);

        // add update_time column to mixnmatch db
        sql = String.format("ALTER TABLE %s ADD COLUMN %s INTEGER DEFAULT 0",
                MixnMatchTable.TABLE_NAME, MixnMatchColumns.COL_UPDATE_TIME);
        db.execSQL(sql);
    }

    private void dropTables(SQLiteDatabase db) {
        db.execSQL("DROP TABLE IF EXISTS " + ThemesTable.TABLE_NAME);
        db.execSQL("DROP TABLE IF EXISTS " + MixnMatchTable.TABLE_NAME);
        db.execSQL("DROP TABLE IF EXISTS " + PreviewsTable.TABLE_NAME);
    }

    public static class ThemesTable {
        protected static final String TABLE_NAME = "themes";

        private static final String THEMES_TABLE_CREATE =
                "CREATE TABLE " + TABLE_NAME + " (" +
                        ThemesColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
                        ThemesColumns.TITLE + " TEXT," +
                        ThemesColumns.AUTHOR + " TEXT," +
                        ThemesColumns.PKG_NAME + " TEXT UNIQUE NOT NULL," +
                        ThemesColumns.DATE_CREATED + " INTEGER," +
                        ThemesColumns.HOMESCREEN_URI + " TEXT," +
                        ThemesColumns.LOCKSCREEN_URI + " TEXT," +
                        ThemesColumns.STYLE_URI + " TEXT," +
                        ThemesColumns.WALLPAPER_URI + " TEXT," +
                        ThemesColumns.BOOT_ANIM_URI + " TEXT," +
                        ThemesColumns.FONT_URI + " TEXT," +
                        ThemesColumns.STATUSBAR_URI + " TEXT," +
                        ThemesColumns.ICON_URI + " TEXT," +
                        ThemesColumns.PRIMARY_COLOR + " TEXT," +
                        ThemesColumns.SECONDARY_COLOR + " TEXT," +
                        ThemesColumns.MODIFIES_LAUNCHER + " INTEGER DEFAULT 0, " +
                        ThemesColumns.MODIFIES_LOCKSCREEN + " INTEGER DEFAULT 0, " +
                        ThemesColumns.MODIFIES_ICONS + " INTEGER DEFAULT 0, " +
                        ThemesColumns.MODIFIES_BOOT_ANIM + " INTEGER DEFAULT 0, " +
                        ThemesColumns.MODIFIES_FONTS + " INTEGER DEFAULT 0, " +
                        ThemesColumns.MODIFIES_RINGTONES + " INTEGER DEFAULT 0, " +
                        ThemesColumns.MODIFIES_NOTIFICATIONS + " INTEGER DEFAULT 0, " +
                        ThemesColumns.MODIFIES_ALARMS + " INTEGER DEFAULT 0, " +
                        ThemesColumns.MODIFIES_OVERLAYS + " INTEGER DEFAULT 0, " +
                        ThemesColumns.MODIFIES_STATUS_BAR + " INTEGER DEFAULT 0, " +
                        ThemesColumns.MODIFIES_NAVIGATION_BAR + " INTEGER DEFAULT 0, " +
                        ThemesColumns.PRESENT_AS_THEME + " INTEGER DEFAULT 0, " +
                        ThemesColumns.IS_LEGACY_THEME + " INTEGER DEFAULT 0, " +
                        ThemesColumns.IS_DEFAULT_THEME + " INTEGER DEFAULT 0, " +
                        ThemesColumns.IS_LEGACY_ICONPACK + " INTEGER DEFAULT 0, " +
                        ThemesColumns.LAST_UPDATE_TIME + " INTEGER DEFAULT 0, " +
                        ThemesColumns.INSTALL_TIME + " INTEGER DEFAULT 0, " +
                        ThemesColumns.TARGET_API + " INTEGER DEFAULT 0," +
                        ThemesColumns.INSTALL_STATE + " INTEGER DEFAULT " +
                        ThemesColumns.InstallState.UNKNOWN +
                        ")";

        public static void insertSystemDefaults(SQLiteDatabase db, Context context) {
            int isDefault = isSystemDefault(context) ? 1 : 0;
            ContentValues values = new ContentValues();
            values.put(ThemesColumns.TITLE, "System");
            values.put(ThemesColumns.PKG_NAME, SYSTEM_THEME_PKG_NAME);
            values.put(ThemesColumns.PRIMARY_COLOR, 0xff33b5e5);
            values.put(ThemesColumns.SECONDARY_COLOR, 0xff000000);
            values.put(ThemesColumns.AUTHOR, "Android");
            values.put(ThemesColumns.MODIFIES_ALARMS, 1);
            values.put(ThemesColumns.MODIFIES_BOOT_ANIM, 1);
            values.put(ThemesColumns.MODIFIES_FONTS, 1);
            values.put(ThemesColumns.MODIFIES_ICONS, 1);
            values.put(ThemesColumns.MODIFIES_LAUNCHER, 1);
            values.put(ThemesColumns.MODIFIES_LOCKSCREEN, 1);
            values.put(ThemesColumns.MODIFIES_NOTIFICATIONS, 1);
            values.put(ThemesColumns.MODIFIES_RINGTONES, 1);
            values.put(ThemesColumns.MODIFIES_STATUS_BAR, 1);
            values.put(ThemesColumns.MODIFIES_NAVIGATION_BAR, 1);
            values.put(ThemesColumns.PRESENT_AS_THEME, 1);
            values.put(ThemesColumns.IS_LEGACY_THEME, 0);
            values.put(ThemesColumns.IS_DEFAULT_THEME, isDefault);
            values.put(ThemesColumns.IS_LEGACY_ICONPACK, 0);
            values.put(ThemesColumns.MODIFIES_OVERLAYS, 1);
            values.put(ThemesColumns.TARGET_API, Build.VERSION.SDK_INT);
            values.put(ThemesColumns.INSTALL_STATE, ThemesColumns.InstallState.INSTALLED);
            db.insert(TABLE_NAME, null, values);
        }
    }

    public static class MixnMatchTable {
        public static final String TABLE_NAME = "mixnmatch";
        private static final String MIXNMATCH_TABLE_CREATE =
                "CREATE TABLE " + TABLE_NAME + " (" +
                        MixnMatchColumns.COL_KEY + " TEXT PRIMARY KEY," +
                        MixnMatchColumns.COL_VALUE + " TEXT," +
                        MixnMatchColumns.COL_PREV_VALUE + " TEXT," +
                        MixnMatchColumns.COL_UPDATE_TIME + " INTEGER DEFAULT 0" +
                        ")";

        public static void insertDefaults(SQLiteDatabase db) {
            ContentValues values = new ContentValues();
            long updateTime = System.currentTimeMillis();
            values.put(MixnMatchColumns.COL_VALUE, SYSTEM_THEME_PKG_NAME);
            values.put(MixnMatchColumns.COL_UPDATE_TIME, updateTime);
            for(String key : MixnMatchColumns.ROWS) {
                values.put(MixnMatchColumns.COL_KEY, key);
                db.insert(TABLE_NAME, null, values);
            }
        }
    }

    public static class PreviewsTable {
        protected static final String TABLE_NAME = "previews";
        private static final String PREVIEWS_TABLE_CREATE =
                "CREATE TABLE " + TABLE_NAME + " (" +
                        PreviewColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                        PreviewColumns.THEME_ID + " INTEGER, " +
                        PreviewColumns.STATUSBAR_BACKGROUND + " BLOB, " +
                        PreviewColumns.STATUSBAR_BLUETOOTH_ICON + " BLOB, " +
                        PreviewColumns.STATUSBAR_WIFI_ICON + " BLOB, " +
                        PreviewColumns.STATUSBAR_SIGNAL_ICON + " BLOB, " +
                        PreviewColumns.STATUSBAR_BATTERY_PORTRAIT + " BLOB, " +
                        PreviewColumns.STATUSBAR_BATTERY_LANDSCAPE + " BLOB, " +
                        PreviewColumns.STATUSBAR_BATTERY_CIRCLE + " BLOB, " +
                        PreviewColumns.STATUSBAR_CLOCK_TEXT_COLOR + " INTEGER, " +
                        PreviewColumns.STATUSBAR_WIFI_COMBO_MARGIN_END + " INTEGER, " +
                        PreviewColumns.NAVBAR_BACKGROUND + " BLOB, " +
                        PreviewColumns.NAVBAR_BACK_BUTTON + " BLOB, " +
                        PreviewColumns.NAVBAR_HOME_BUTTON + " BLOB, " +
                        PreviewColumns.NAVBAR_RECENT_BUTTON + " BLOB, " +
                        PreviewColumns.ICON_PREVIEW_1 + " BLOB, " +
                        PreviewColumns.ICON_PREVIEW_2 + " BLOB, " +
                        PreviewColumns.ICON_PREVIEW_3 + " BLOB, " +
                        PreviewColumns.ICON_PREVIEW_4 + " BLOB, " +
                        PreviewColumns.STYLE_PREVIEW + " BLOB, " +
                        PreviewColumns.STYLE_THUMBNAIL + " BLOB, " +
                        PreviewColumns.WALLPAPER_PREVIEW + " BLOB, " +
                        PreviewColumns.WALLPAPER_THUMBNAIL + " BLOB, " +
                        PreviewColumns.LOCK_WALLPAPER_PREVIEW + " BLOB, " +
                        PreviewColumns.LOCK_WALLPAPER_THUMBNAIL + " BLOB, " +
                        PreviewColumns.BOOTANIMATION_THUMBNAIL + " BLOB, " +
                        "FOREIGN KEY (" + PreviewColumns.THEME_ID + ") REFERENCES " +
                        ThemesTable.TABLE_NAME + "(" + ThemesColumns._ID + ")" +
                        ")";

        public static final String[] STATUS_BAR_PREVIEW_COLUMNS = {
                PreviewColumns.STATUSBAR_BACKGROUND,
                PreviewColumns.STATUSBAR_BLUETOOTH_ICON,
                PreviewColumns.STATUSBAR_WIFI_ICON,
                PreviewColumns.STATUSBAR_SIGNAL_ICON,
                PreviewColumns.STATUSBAR_BATTERY_PORTRAIT,
                PreviewColumns.STATUSBAR_BATTERY_LANDSCAPE,
                PreviewColumns.STATUSBAR_BATTERY_CIRCLE,
                PreviewColumns.STATUSBAR_WIFI_COMBO_MARGIN_END,
                PreviewColumns.STATUSBAR_CLOCK_TEXT_COLOR
        };
        public static final String[] NAVIGATION_BAR_PREVIEW_COLUMNS = {
                PreviewColumns.NAVBAR_BACK_BUTTON,
                PreviewColumns.NAVBAR_HOME_BUTTON,
                PreviewColumns.NAVBAR_RECENT_BUTTON,
                PreviewColumns.NAVBAR_BACKGROUND
        };
        public static final String[] ICON_PREVIEW_COLUMNS = {
                PreviewColumns.ICON_PREVIEW_1,
                PreviewColumns.ICON_PREVIEW_2,
                PreviewColumns.ICON_PREVIEW_3,
                PreviewColumns.ICON_PREVIEW_4
        };

        public static void insertDefaults(Context context) {
            Intent intent = new Intent(context, PreviewGenerationService.class);
            intent.setAction(PreviewGenerationService.ACTION_INSERT);
            intent.putExtra(PreviewGenerationService.EXTRA_PKG_NAME, SYSTEM_THEME_PKG_NAME);
            intent.putExtra(PreviewGenerationService.EXTRA_HAS_SYSTEMUI, true);
            intent.putExtra(PreviewGenerationService.EXTRA_HAS_ICONS, true);
            intent.putExtra(PreviewGenerationService.EXTRA_HAS_STYLES, true);
            intent.putExtra(PreviewGenerationService.EXTRA_HAS_WALLPAPER, true);
            intent.putExtra(PreviewGenerationService.EXTRA_HAS_BOOTANIMATION, true);
            context.startService(intent);
        }
    }

    private static boolean isSystemDefault(Context context) {
        // == is okay since we are checking if what is returned is the same constant string value
        return ThemeConfig.SYSTEM_DEFAULT == ThemeUtils.getDefaultThemePackageName(context);
    }
}