DefaultWallpaperInfo.java revision 7d21299b0f731db1259f7ee22f36548d5b59fbf9
1package com.android.wallpaperpicker.tileinfo;
2
3import android.annotation.TargetApi;
4import android.app.Activity;
5import android.app.WallpaperManager;
6import android.content.Context;
7import android.content.DialogInterface;
8import android.content.res.Resources;
9import android.graphics.Bitmap;
10import android.graphics.BitmapFactory;
11import android.graphics.Canvas;
12import android.graphics.Point;
13import android.graphics.RectF;
14import android.graphics.drawable.BitmapDrawable;
15import android.graphics.drawable.Drawable;
16import android.os.Build;
17import android.util.Log;
18
19import com.android.wallpaperpicker.WallpaperCropActivity.CropViewScaleAndOffsetProvider;
20import com.android.wallpaperpicker.WallpaperFiles;
21import com.android.wallpaperpicker.WallpaperPickerActivity;
22import com.android.wallpaperpicker.common.DialogUtils;
23import com.android.wallpaperpicker.common.InputStreamProvider;
24import com.android.wallpaperpicker.common.Utilities;
25import com.android.wallpaperpicker.common.WallpaperManagerCompat;
26
27import java.io.File;
28import java.io.FileOutputStream;
29import java.io.IOException;
30
31public class DefaultWallpaperInfo extends DrawableThumbWallpaperInfo {
32
33    private static final String TAG = "DefaultWallpaperInfo";
34
35    public DefaultWallpaperInfo(Drawable thumb) {
36        super(thumb);
37    }
38
39    @Override
40    public void onClick(WallpaperPickerActivity a) {
41        a.setCropViewTileSource(null, false, false, new CropViewScaleAndOffsetProvider() {
42
43            @Override
44            public float getScale(Point wallpaperSize, RectF crop) {
45                return 1f;
46            }
47
48            @Override
49            public float getParallaxOffset() {
50                return 0.5f;
51            }
52        }, null);
53    }
54
55    @Override
56    public void onSave(final WallpaperPickerActivity a) {
57        if (Utilities.isAtLeastN()) {
58            DialogUtils.showWhichWallpaperHomeOrBothDialog(a, new DialogInterface.OnClickListener() {
59                @Override
60                public void onClick(DialogInterface dialog, int selectedItemIndex) {
61                    int whichWallpaper = WallpaperManagerCompat.FLAG_SET_SYSTEM;
62                    if (selectedItemIndex == 1 /* "home screen and lock screen" */) {
63                        whichWallpaper |= WallpaperManagerCompat.FLAG_SET_LOCK;
64                    }
65                    clearWallpaperAndFinish(a, whichWallpaper);
66                }
67            }, a.getOnDialogCancelListener());
68        } else {
69            clearWallpaperAndFinish(a, WallpaperManagerCompat.FLAG_SET_SYSTEM);
70        }
71    }
72
73    private void clearWallpaperAndFinish(WallpaperPickerActivity a, int whichWallpaper) {
74        try {
75            WallpaperManagerCompat.getInstance(a.getApplicationContext()).clear(whichWallpaper);
76            a.setResult(Activity.RESULT_OK);
77        } catch (IOException e) {
78            Log.w(TAG, "Setting wallpaper to default threw exception", e);
79        }
80        a.finish();
81    }
82
83    @Override
84    public boolean isSelectable() {
85        return true;
86    }
87
88    @Override
89    public boolean isNamelessWallpaper() {
90        return true;
91    }
92
93    /**
94     * @return the system default wallpaper tile or null
95     */
96    public static WallpaperTileInfo get(Context context) {
97        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
98                ? getDefaultWallpaper(context) : getPreKKDefaultWallpaperInfo(context);
99    }
100
101    @TargetApi(Build.VERSION_CODES.KITKAT)
102    private static DefaultWallpaperInfo getDefaultWallpaper(Context context) {
103        File defaultThumbFile = getDefaultThumbFile(context);
104        Bitmap thumb = null;
105        boolean defaultWallpaperExists = false;
106        Resources res = context.getResources();
107
108        if (defaultThumbFile.exists()) {
109            thumb = BitmapFactory.decodeFile(defaultThumbFile.getAbsolutePath());
110            defaultWallpaperExists = true;
111        } else {
112            Point defaultThumbSize = getDefaultThumbSize(res);
113            Drawable wallpaperDrawable = WallpaperManager.getInstance(context).getBuiltInDrawable(
114                    defaultThumbSize.x, defaultThumbSize.y, true, 0.5f, 0.5f);
115            if (wallpaperDrawable != null) {
116                thumb = Bitmap.createBitmap(
117                        defaultThumbSize.x, defaultThumbSize.y, Bitmap.Config.ARGB_8888);
118                Canvas c = new Canvas(thumb);
119                wallpaperDrawable.setBounds(0, 0, defaultThumbSize.x, defaultThumbSize.y);
120                wallpaperDrawable.draw(c);
121                c.setBitmap(null);
122            }
123            if (thumb != null) {
124                defaultWallpaperExists = saveDefaultWallpaperThumb(context, thumb);
125            }
126        }
127        if (defaultWallpaperExists) {
128            return new DefaultWallpaperInfo(new BitmapDrawable(res, thumb));
129        }
130        return null;
131    }
132
133    private static ResourceWallpaperInfo getPreKKDefaultWallpaperInfo(Context context) {
134        Resources sysRes = Resources.getSystem();
135        Resources res = context.getResources();
136
137        int resId = sysRes.getIdentifier("default_wallpaper", "drawable", "android");
138
139        File defaultThumbFile = getDefaultThumbFile(context);
140        Bitmap thumb = null;
141        boolean defaultWallpaperExists = false;
142        if (defaultThumbFile.exists()) {
143            thumb = BitmapFactory.decodeFile(defaultThumbFile.getAbsolutePath());
144            defaultWallpaperExists = true;
145        } else {
146            InputStreamProvider streamProvider = InputStreamProvider.fromResource(res, resId);
147            thumb = createThumbnail(
148                    streamProvider, context, streamProvider.getRotationFromExif(context), false);
149            if (thumb != null) {
150                defaultWallpaperExists = saveDefaultWallpaperThumb(context, thumb);
151            }
152        }
153        if (defaultWallpaperExists) {
154            return new ResourceWallpaperInfo(sysRes, resId, new BitmapDrawable(res, thumb));
155        }
156        return null;
157    }
158
159    private static File getDefaultThumbFile(Context context) {
160        return new File(context.getFilesDir(), Build.VERSION.SDK_INT
161                + "_" + WallpaperFiles.DEFAULT_WALLPAPER_THUMBNAIL);
162    }
163
164    private static boolean saveDefaultWallpaperThumb(Context c, Bitmap b) {
165        // Delete old thumbnails.
166        new File(c.getFilesDir(), WallpaperFiles.DEFAULT_WALLPAPER_THUMBNAIL_OLD).delete();
167        new File(c.getFilesDir(), WallpaperFiles.DEFAULT_WALLPAPER_THUMBNAIL).delete();
168
169        for (int i = Build.VERSION_CODES.JELLY_BEAN; i < Build.VERSION.SDK_INT; i++) {
170            new File(c.getFilesDir(), i + "_" + WallpaperFiles.DEFAULT_WALLPAPER_THUMBNAIL).delete();
171        }
172        File f = getDefaultThumbFile(c);
173        try {
174            f.createNewFile();
175            FileOutputStream thumbFileStream = c.openFileOutput(f.getName(), Context.MODE_PRIVATE);
176            b.compress(Bitmap.CompressFormat.JPEG, 95, thumbFileStream);
177            thumbFileStream.close();
178            return true;
179        } catch (IOException e) {
180            Log.e(TAG, "Error while writing bitmap to file " + e);
181            f.delete();
182            return false;
183        }
184    }
185}