summaryrefslogtreecommitdiffstats
path: root/src/org/cyanogenmod/themes/provider/PreviewGenerationService.java
blob: e732c451f3d4c35e1ed4219cbeb48fe9ca44f9b3 (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
/*
 * 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.app.IntentService;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.ThemeConfig;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.os.Bundle;
import android.os.FileUtils;
import android.provider.ThemesContract.ThemesColumns;
import android.provider.ThemesContract.PreviewColumns;
import android.util.Log;
import org.cyanogenmod.themes.provider.util.BootAnimationPreviewGenerator;
import org.cyanogenmod.themes.provider.util.IconPreviewGenerator;
import org.cyanogenmod.themes.provider.util.IconPreviewGenerator.IconItems;
import org.cyanogenmod.themes.provider.util.StylePreviewGenerator;
import org.cyanogenmod.themes.provider.util.StylePreviewGenerator.StyleItems;
import org.cyanogenmod.themes.provider.util.SystemUiPreviewGenerator;
import org.cyanogenmod.themes.provider.util.SystemUiPreviewGenerator.SystemUiItems;
import org.cyanogenmod.themes.provider.util.WallpaperPreviewGenerator;
import org.cyanogenmod.themes.provider.util.WallpaperPreviewGenerator.WallpaperItems;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

/*
 * Copies images from the theme APK to the local provider's cache
 */
public class PreviewGenerationService extends IntentService {
    public static final String ACTION_INSERT = "org.cyanogenmod.themes.provider.action.insert";
    public static final String ACTION_UPDATE = "org.cyanogenmod.themes.provider.action.update";
    public static final String EXTRA_PKG_NAME = "extra_pkg_name";

    public static final String PREVIEWS_DIR = "previews";

    private static final String TAG = PreviewGenerationService.class.getName();

    public PreviewGenerationService() {
        super(PreviewGenerationService.class.getName());
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent.getExtras() == null || intent.getExtras().getString(EXTRA_PKG_NAME) == null) {
            Log.e(TAG, "No package name or extras provided");
            return;
        }

        final Bundle extras = intent.getExtras();
        String pkgName = extras.getString(EXTRA_PKG_NAME);
        boolean hasSystemUi = false;
        boolean hasIcons = false;
        boolean hasWallpaper = false;
        boolean hasStyles = false;
        boolean hasBootanimation = false;
        boolean isSystemTheme = ThemeConfig.SYSTEM_DEFAULT.equals(pkgName);
        Cursor c = queryTheme(this, pkgName);
        if (c != null) {
            if (c.moveToFirst()) {
                // mods_status_bar was added in version 7 of the database so we need to make sure
                // it exists when trying to get the int value from the row.
                final int sysUiIndex = c.getColumnIndex(ThemesColumns.MODIFIES_STATUS_BAR);
                hasSystemUi = sysUiIndex >= 0 && c.getInt(sysUiIndex) == 1;
                hasIcons = c.getInt(c.getColumnIndex(ThemesColumns.MODIFIES_ICONS)) == 1;
                hasWallpaper = c.getInt(c.getColumnIndex(ThemesColumns.MODIFIES_LAUNCHER)) == 1 ||
                        c.getInt(c.getColumnIndex(ThemesColumns.MODIFIES_LOCKSCREEN)) == 1;
                hasStyles = c.getInt(c.getColumnIndex(ThemesColumns.MODIFIES_OVERLAYS)) == 1;
                hasBootanimation =
                        c.getInt(c.getColumnIndex(ThemesColumns.MODIFIES_BOOT_ANIM)) == 1;
            }
            c.close();
        }
        final String action = intent.getAction();
        if (ACTION_INSERT.equals(action) || ACTION_UPDATE.equals(action)) {
            PackageInfo info = null;
            try {
                if (!isSystemTheme ) {
                    info = getPackageManager().getPackageInfo(pkgName, 0);
                }
            } catch (NameNotFoundException e) {
                Log.e(TAG, "Unable to get package info for " + pkgName, e);
            }
            if (isSystemTheme || info != null) {
                SystemUiItems items = null;
                try {
                    items = !hasSystemUi ? null :
                            new SystemUiPreviewGenerator(this).generateSystemUiItems(pkgName);
                } catch (Exception e) {
                    Log.e(TAG, "Unable to create statusbar previews for " + pkgName, e);
                }

                IconItems iconItems = null;
                if (hasIcons) {
                    try {
                        iconItems = new IconPreviewGenerator(this).generateIconItems(pkgName);
                    } catch (Exception e) {
                        Log.e(TAG, "Unable to create icon previews for " + pkgName, e);
                    }
                }

                WallpaperItems wallpaperItems = null;
                if (hasWallpaper) {
                    try {
                        wallpaperItems = new WallpaperPreviewGenerator(this)
                                .generateWallpaperPreviews(info);
                    } catch (Exception e) {
                        Log.e(TAG, "Unable to create wallpaper previews for " + pkgName, e);
                    }
                }

                StyleItems styleItems = null;
                if (hasStyles) {
                    try {
                        styleItems = new StylePreviewGenerator(this).generateStylePreviews(pkgName);
                    } catch (Exception e) {
                        Log.e(TAG, "Unable to create style previews for " + pkgName, e);
                    }
                }

                Bitmap bootAnim = null;
                if (hasBootanimation) {
                    try {
                        bootAnim = new BootAnimationPreviewGenerator(this)
                                .generateBootAnimationPreview(pkgName);
                    } catch (Exception e) {
                        Log.e(TAG, "Unable to create boot animation preview for " + pkgName, e);
                    }
                }
                insertPreviewItemsIntoDb(pkgName, items, iconItems, wallpaperItems, styleItems,
                        bootAnim);
            }
        }
    }

    private void insertPreviewItemsIntoDb(String pkgName, SystemUiItems items, IconItems icons,
                                          WallpaperItems wallpaperItems, StyleItems styleItems,
                                          Bitmap bootAnim) {
        String[] projection = {ThemesColumns._ID};
        String selection = ThemesColumns.PKG_NAME + "=?";
        String[] selectionArgs = { pkgName };

        final ContentResolver resolver = getContentResolver();
        Cursor cursor = resolver.query(ThemesColumns.CONTENT_URI, projection, selection,
                selectionArgs, null);

        if (cursor != null) {
            cursor.moveToFirst();
            int id = cursor.getInt(cursor.getColumnIndexOrThrow(ThemesColumns._ID));
            cursor.close();

            List<ContentValues> themeValues = new ArrayList<ContentValues>();
            ContentValues values = null;
            String filesDir = this.getFilesDir().getAbsolutePath();
            String themePreviewsDir =
                    filesDir + File.separator + PREVIEWS_DIR + File.separator + pkgName;
            String path = null;
            clearThemePreviewsDir(themePreviewsDir);

            if (items != null) {
                path = compressAndSavePng(items.statusbarBackground, filesDir, pkgName,
                        PreviewColumns.STATUSBAR_BACKGROUND);
                values = createPreviewEntryString(id, PreviewColumns.STATUSBAR_BACKGROUND,
                        path);
                themeValues.add(values);

                path = compressAndSavePng(items.bluetoothIcon, filesDir, pkgName,
                        PreviewColumns.STATUSBAR_BLUETOOTH_ICON);
                values = createPreviewEntryString(id, PreviewColumns.STATUSBAR_BLUETOOTH_ICON,
                        path);
                themeValues.add(values);

                path = compressAndSavePng(items.wifiIcon, filesDir, pkgName,
                        PreviewColumns.STATUSBAR_WIFI_ICON);
                values = createPreviewEntryString(id, PreviewColumns.STATUSBAR_WIFI_ICON, path);
                themeValues.add(values);

                path = compressAndSavePng(items.signalIcon, filesDir, pkgName,
                        PreviewColumns.STATUSBAR_SIGNAL_ICON);
                values = createPreviewEntryString(id, PreviewColumns.STATUSBAR_SIGNAL_ICON,
                        path);
                themeValues.add(values);

                path = compressAndSavePng(items.batteryPortrait, filesDir, pkgName,
                        PreviewColumns.STATUSBAR_BATTERY_PORTRAIT);
                values = createPreviewEntryString(id, PreviewColumns.STATUSBAR_BATTERY_PORTRAIT,
                        path);
                themeValues.add(values);

                path = compressAndSavePng(items.batteryLandscape, filesDir, pkgName,
                        PreviewColumns.STATUSBAR_BATTERY_LANDSCAPE);
                values = createPreviewEntryString(id,
                        PreviewColumns.STATUSBAR_BATTERY_LANDSCAPE, path);
                themeValues.add(values);

                path = compressAndSavePng(items.batteryCircle, filesDir, pkgName,
                        PreviewColumns.STATUSBAR_BATTERY_CIRCLE);
                values = createPreviewEntryString(id, PreviewColumns.STATUSBAR_BATTERY_CIRCLE,
                        path);
                themeValues.add(values);

                values = createPreviewEntryInt(id, PreviewColumns.STATUSBAR_CLOCK_TEXT_COLOR,
                        items.clockColor);
                themeValues.add(values);

                values = createPreviewEntryInt(id,
                        PreviewColumns.STATUSBAR_WIFI_COMBO_MARGIN_END, items.wifiMarginEnd);
                themeValues.add(values);

                path = compressAndSavePng(items.navbarBackground, filesDir, pkgName,
                        PreviewColumns.NAVBAR_BACKGROUND);
                values = createPreviewEntryString(id, PreviewColumns.NAVBAR_BACKGROUND, path);
                themeValues.add(values);

                path = compressAndSavePng(items.navbarBack, filesDir, pkgName,
                        PreviewColumns.NAVBAR_BACK_BUTTON);
                values = createPreviewEntryString(id, PreviewColumns.NAVBAR_BACK_BUTTON, path);
                themeValues.add(values);

                path = compressAndSavePng(items.navbarHome, filesDir, pkgName,
                        PreviewColumns.NAVBAR_HOME_BUTTON);
                values = createPreviewEntryString(id, PreviewColumns.NAVBAR_HOME_BUTTON, path);
                themeValues.add(values);

                path = compressAndSavePng(items.navbarRecent, filesDir, pkgName,
                        PreviewColumns.NAVBAR_RECENT_BUTTON);
                values = createPreviewEntryString(id, PreviewColumns.NAVBAR_RECENT_BUTTON,
                        path);
                themeValues.add(values);
            }
            if (icons != null) {
                path = compressAndSavePng(icons.icon1, filesDir, pkgName,
                        PreviewColumns.ICON_PREVIEW_1);
                values = createPreviewEntryString(id, PreviewColumns.ICON_PREVIEW_1, path);
                themeValues.add(values);

                path = compressAndSavePng(icons.icon2, filesDir, pkgName,
                        PreviewColumns.ICON_PREVIEW_2);
                values = createPreviewEntryString(id, PreviewColumns.ICON_PREVIEW_2, path);
                themeValues.add(values);

                path = compressAndSavePng(icons.icon3, filesDir, pkgName,
                        PreviewColumns.ICON_PREVIEW_3);
                values = createPreviewEntryString(id, PreviewColumns.ICON_PREVIEW_3, path);
                themeValues.add(values);
            }
            if (wallpaperItems != null) {
                path = compressAndSaveJpg(wallpaperItems.wpPreview, filesDir, pkgName,
                        PreviewColumns.WALLPAPER_PREVIEW);
                values = createPreviewEntryString(id, PreviewColumns.WALLPAPER_PREVIEW, path);
                themeValues.add(values);

                path = compressAndSavePng(wallpaperItems.wpThumbnail, filesDir, pkgName,
                        PreviewColumns.WALLPAPER_THUMBNAIL);
                values = createPreviewEntryString(id, PreviewColumns.WALLPAPER_THUMBNAIL, path);
                themeValues.add(values);

                path = compressAndSaveJpg(wallpaperItems.lsPreview, filesDir, pkgName,
                        PreviewColumns.LOCK_WALLPAPER_PREVIEW);
                values = createPreviewEntryString(id, PreviewColumns.LOCK_WALLPAPER_PREVIEW,
                        path);
                themeValues.add(values);

                path = compressAndSavePng(wallpaperItems.lsThumbnail, filesDir, pkgName,
                        PreviewColumns.LOCK_WALLPAPER_THUMBNAIL);
                values = createPreviewEntryString(id, PreviewColumns.LOCK_WALLPAPER_THUMBNAIL,
                        path);
                themeValues.add(values);
            }
            if (styleItems != null) {
                path = compressAndSavePng(styleItems.thumbnail, filesDir, pkgName,
                        PreviewColumns.STYLE_THUMBNAIL);
                values = createPreviewEntryString(id, PreviewColumns.STYLE_THUMBNAIL, path);
                themeValues.add(values);

                path = compressAndSavePng(styleItems.preview, filesDir, pkgName,
                        PreviewColumns.STYLE_PREVIEW);
                values = createPreviewEntryString(id, PreviewColumns.STYLE_PREVIEW, path);
                themeValues.add(values);
            }
            if (bootAnim != null) {
                path = compressAndSavePng(bootAnim, filesDir, pkgName,
                        PreviewColumns.BOOTANIMATION_THUMBNAIL);
                values = createPreviewEntryString(id, PreviewColumns.BOOTANIMATION_THUMBNAIL,
                        path);
                themeValues.add(values);
            }

            if (!themeValues.isEmpty()) {
                selection = PreviewColumns.THEME_ID + "=? AND " + PreviewColumns.COL_KEY + "=?";
                for (ContentValues contentValues : themeValues) {
                    selectionArgs = new String[]{String.valueOf(id),
                            contentValues.getAsString(PreviewColumns.COL_KEY)};
                    // Try an update first, if that returns 0 then we need to insert these values
                    if (resolver.update(PreviewColumns.CONTENT_URI, contentValues, selection,
                            selectionArgs) == 0) {
                        resolver.insert(PreviewColumns.CONTENT_URI, contentValues);
                    }
                }
            }
        }
    }

    private static ContentValues createPreviewEntryInt(int id, String key, int value) {
        ContentValues values = new ContentValues();
        values.put(PreviewColumns.THEME_ID, id);
        values.put(PreviewColumns.COL_KEY, key);
        values.put(PreviewColumns.COL_VALUE, value);

        return values;
    }

    private static ContentValues createPreviewEntryString(int id, String key, String value) {
        ContentValues values = new ContentValues();
        values.put(PreviewColumns.THEME_ID, id);
        values.put(PreviewColumns.COL_KEY, key);
        values.put(PreviewColumns.COL_VALUE, value);

        return values;
    }

    private static String compressAndSavePng(Bitmap bmp, String baseDir, String pkgName,
                                               String fileName) {
        byte[] image = getBitmapBlobPng(bmp);
        return saveCompressedImage(image, baseDir, pkgName, fileName);
    }

    private static String compressAndSaveJpg(Bitmap bmp, String baseDir, String pkgName,
                                             String fileName) {
        byte[] image = getBitmapBlobJpg(bmp);
        return saveCompressedImage(image, baseDir, pkgName, fileName);
    }

    private static byte[] getBitmapBlobPng(Bitmap bmp) {
        return getBitmapBlob(bmp, CompressFormat.PNG, 100);
    }

    private static byte[] getBitmapBlobJpg(Bitmap bmp) {
        return getBitmapBlob(bmp, CompressFormat.JPEG, 80);
    }

    private static byte[] getBitmapBlob(Bitmap bmp, CompressFormat format, int quality) {
        if (bmp == null) return null;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        bmp.compress(format, quality, out);
        return out.toByteArray();
    }

    private static String saveCompressedImage(byte[] image, String baseDir, String pkgName,
                                              String fileName) {
        // Create relevant directories
        String previewsDir = baseDir + File.separator + PREVIEWS_DIR;
        String pkgDir = previewsDir + File.separator + pkgName;
        String filePath = pkgDir + File.separator + fileName;
        createDirIfNotExists(previewsDir);
        createDirIfNotExists(pkgDir);

        // Save blob
        FileOutputStream outputStream;
        final File pkgPreviewDir = new File(pkgDir);
        try {
            File outFile = new File(pkgPreviewDir, fileName);
            outputStream = new FileOutputStream(outFile);
            outputStream.write(image);
            outputStream.close();
            FileUtils.setPermissions(outFile,
                    FileUtils.S_IRWXU | FileUtils.S_IRWXG | FileUtils.S_IROTH,
                    -1, -1);
        } catch (Exception e) {
            Log.w(TAG, "Unable to save preview " + pkgName + File.separator + fileName, e);
            filePath = null;
        }

        return filePath;
    }

    public static void clearThemePreviewsDir(String path) {
        File directory = new File(path);
        FileUtils.deleteContents(directory);
        directory.delete();
    }

    private static Cursor queryTheme(Context context, String pkgName) {
        String selection = ThemesColumns.PKG_NAME + "=?";
        String[] selectionArgs = { pkgName };
        return context.getContentResolver().query(ThemesColumns.CONTENT_URI, null,
                selection, selectionArgs, null);
    }

    private static boolean dirExists(String dirPath) {
        final File dir = new File(dirPath);
        return dir.exists() && dir.isDirectory();
    }

    private static void createDirIfNotExists(String dirPath) {
        if (!dirExists(dirPath)) {
            File dir = new File(dirPath);
            if (dir.mkdir()) {
                FileUtils.setPermissions(dir, FileUtils.S_IRWXU |
                        FileUtils.S_IRWXG | FileUtils.S_IROTH | FileUtils.S_IXOTH, -1, -1);
            }
        }
    }
}