summaryrefslogtreecommitdiffstats
path: root/src/org/cyanogenmod/themes/provider/util/WallpaperPreviewGenerator.java
blob: f10a2f7f5d796a79c531fb9d84660c89e96405d3 (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
/*
 * 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.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;
        if (themeInfo == null) {
            Resources res = mContext.getPackageManager().getThemedResourcesForApplication("android",
                    ThemeConfig.SYSTEM_DEFAULT);
            item = new WallpaperItem();
            item.preview = BitmapUtils.decodeResource(res,
                    com.android.internal.R.drawable.default_wallpaper, mPreviewSize, mPreviewSize);
            item.thumbnail = Bitmap.createScaledBitmap(item.preview, mThumbnailSize, mThumbnailSize,
                    true);
            if (item != null) {
                items.wallpapers.add(item);
                items.lockscreen = item;
            }
        } else {
            final Context themeContext = mContext.createPackageContext(themeInfo.packageName, 0);
            final AssetManager assets = themeContext.getAssets();
            // Get all wallpapers
            List<String> paths = ThemeUtils.getWallpaperPathList(assets);
            for (String path : paths) {
                if (!TextUtils.isEmpty(path)) {
                    preview = BitmapUtils.getBitmapFromAsset(themeContext, path,
                            mPreviewSize, mPreviewSize);
                    item = createWallpaperItems(path, preview);
                    if (item != null) {
                        items.wallpapers.add(item);
                    }
                }
            }
            // Get the lockscreen
            String path = ThemeUtils.getLockscreenWallpaperPath(assets);
            if (!TextUtils.isEmpty(path)) {
                preview = BitmapUtils.getBitmapFromAsset(themeContext, path,
                        mPreviewSize, mPreviewSize);
                items.lockscreen = createWallpaperItems(path, preview);
            }
        }
        return items;
    }

    private WallpaperItem createWallpaperItems(String path, Bitmap preview) {
        if (TextUtils.isEmpty(path) || preview == null) {
            return null;
        }
        WallpaperItem item = new WallpaperItem();
        item.assetPath = path;
        item.preview = preview;
        item.thumbnail = Bitmap.createScaledBitmap(item.preview, mThumbnailSize, mThumbnailSize,
                true);
        return item;
    }

    public class WallpaperItem {
        public String assetPath;
        public Bitmap preview;
        public Bitmap thumbnail;
    }

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

        // Lockscreen wallpaper item
        public WallpaperItem lockscreen;

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