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

import java.io.File;
import java.io.IOException;

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();
        if (themeInfo == null) {
            Resources res = mContext.getPackageManager().getThemedResourcesForApplication("android",
                    ThemeConfig.SYSTEM_DEFAULT);
            items.wpPreview = items.lsPreview = BitmapUtils.decodeResource(res,
                    com.android.internal.R.drawable.default_wallpaper, mPreviewSize, mPreviewSize);
        } else {
            final Context themeContext = mContext.createPackageContext(themeInfo.packageName, 0);
            final AssetManager assets = themeContext.getAssets();
            String path = ThemeUtils.getWallpaperPath(assets);
            if (path != null) {
                items.wpPreview = BitmapUtils.getBitmapFromAsset(themeContext, path,
                        mPreviewSize, mPreviewSize);
            }
            path = ThemeUtils.getLockscreenWallpaperPath(assets);
            if (path != null) {
                items.lsPreview = BitmapUtils.getBitmapFromAsset(themeContext, path,
                        mPreviewSize, mPreviewSize);
            }
        }
        if (items.wpPreview != null) {
            items.wpThumbnail = Bitmap.createScaledBitmap(items.wpPreview, mThumbnailSize,
                    mThumbnailSize, true);
        }
        if (items.lsPreview != null) {
            items.lsThumbnail = Bitmap.createScaledBitmap(items.lsPreview, mThumbnailSize,
                    mThumbnailSize, true);
        }
        return items;
    }

    public class WallpaperItems {
        // Wallpaper items
        public Bitmap wpThumbnail;
        public Bitmap wpPreview;

        // Lockscreen wallpaper items
        public Bitmap lsThumbnail;
        public Bitmap lsPreview;
    }
}