summaryrefslogtreecommitdiffstats
path: root/src/org/cyanogenmod/themes/provider/util/WallpaperPreviewGenerator.java
blob: e2046409c7d587367cfdc9eb534d674ca16312dd (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
/*
 * 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.util;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ThemeUtils;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.content.res.ThemeConfig;
import android.graphics.Bitmap;
import android.provider.ThemesContract.PreviewColumns;
import android.text.TextUtils;

import org.cyanogenmod.themes.provider.R;

import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

public class WallpaperPreviewGenerator {
    private static final String WALLPAPER_ASSET_PATH = "wallpapers";
    private static final String LOCKSCREEN_ASSET_PATH = "lockscreen";
    private Context mContext;
    private int mPreviewSize;
    private int mThumbnailSize;

    public WallpaperPreviewGenerator(Context context) {
        mContext = context;
        final Resources res = context.getResources();
        mPreviewSize = res.getDimensionPixelSize(R.dimen.wallpaper_preview_size);
        mThumbnailSize = res.getDimensionPixelSize(R.dimen.wallpaper_thumbnail_size);
    }

    public WallpaperItems generateWallpaperPreviews(PackageInfo themeInfo)
            throws NameNotFoundException, IOException {
        WallpaperItems items = new WallpaperItems();
        WallpaperItem item = null;
        Bitmap preview = null;
        String baseDir = mContext.getFilesDir().getAbsolutePath();
        String pkgName;
        if (themeInfo == null) {
            pkgName = ThemeConfig.SYSTEM_DEFAULT;
            Resources res = mContext.getPackageManager().getThemedResourcesForApplication("android",
                    ThemeConfig.SYSTEM_DEFAULT);
            if (preview != null) {
                preview.recycle();
            }
            preview = BitmapUtils.decodeResource(res,
                    com.android.internal.R.drawable.default_wallpaper, mPreviewSize, mPreviewSize);
            item = createWallpaperItems(0, baseDir, null, pkgName, preview, false);
            if (item != null) {
                items.wallpapers.add(item);
                items.lockscreen = item;
            }
        } else {
            final Context themeContext = mContext.createPackageContext(themeInfo.packageName, 0);
            final AssetManager assets = themeContext.getAssets();
            pkgName = themeInfo.packageName;
            // Get all wallpapers
            List<String> paths = ThemeUtils.getWallpaperPathList(assets);
            int id = 0;
            for (String path : paths) {
                if (!TextUtils.isEmpty(path)) {
                    if (preview != null) {
                        preview.recycle();
                    }
                    preview = BitmapUtils.getBitmapFromAsset(themeContext, path,
                            mPreviewSize, mPreviewSize);
                    item = createWallpaperItems(id, baseDir, path, pkgName, preview, false);
                    if (item != null) {
                        items.wallpapers.add(item);
                        id++;
                    }
                }
            }
            // Get the lockscreen
            String path = ThemeUtils.getLockscreenWallpaperPath(assets);
            if (!TextUtils.isEmpty(path)) {
                if (preview != null) {
                    preview.recycle();
                }
                preview = BitmapUtils.getBitmapFromAsset(themeContext, path,
                        mPreviewSize, mPreviewSize);
                items.lockscreen = createWallpaperItems(0, baseDir, path, pkgName, preview, true);
            }
        }
        return items;
    }

    private WallpaperItem createWallpaperItems(int id, String baseDir, String assetPath,
            String pkgName, Bitmap preview, boolean isLockWallpaper) {
        if (TextUtils.isEmpty(assetPath) && preview == null) {
            return null;
        }

        String componentID =  String.format("%03d", id);
        WallpaperItem item = new WallpaperItem();

        item.assetPath = assetPath;
        if (preview != null) {
            String filePrefix = isLockWallpaper ? PreviewColumns.LOCK_WALLPAPER_PREVIEW :
                    PreviewColumns.WALLPAPER_PREVIEW;
            String fileName = filePrefix + componentID;
            item.previewPath = PreviewUtils.compressAndSaveJpg(preview, baseDir, pkgName, fileName);
            Bitmap thumbnail = Bitmap.createScaledBitmap(preview, mThumbnailSize, mThumbnailSize,
                    true);
            filePrefix = isLockWallpaper ? PreviewColumns.LOCK_WALLPAPER_THUMBNAIL :
                    PreviewColumns.LOCK_WALLPAPER_THUMBNAIL;
            fileName = filePrefix + componentID;
            item.thumbnailPath = PreviewUtils.compressAndSavePng(thumbnail, baseDir, pkgName,
                    fileName);
        }
        return item;
    }

    public class WallpaperItem {
        public String assetPath;
        public String previewPath;
        public String thumbnailPath;
    }

    public class WallpaperItems {
        // Wallpaper items
        public List<WallpaperItem> wallpapers;

        // Lockscreen wallpaper item
        public WallpaperItem lockscreen;

        public WallpaperItems() {
            wallpapers = new LinkedList<WallpaperItem>();
            lockscreen = null;
        }
    }
}