Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.android.wallpaper.asset; |
| 17 | |
| 18 | import android.annotation.TargetApi; |
| 19 | import android.app.WallpaperManager; |
| 20 | import android.content.Context; |
| 21 | import android.graphics.drawable.Drawable; |
| 22 | import android.os.Build; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 23 | import android.util.Log; |
| 24 | |
| 25 | import com.bumptech.glide.load.Key; |
| 26 | import com.bumptech.glide.signature.ObjectKey; |
| 27 | |
Sunny Goyal | 8600a3f | 2018-08-15 12:48:01 -0700 | [diff] [blame^] | 28 | import androidx.annotation.IntDef; |
| 29 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 30 | /** |
| 31 | * Glide model representing wallpaper image data retrieved from {@link WallpaperManager}. |
| 32 | * <p> |
| 33 | * Instances of this class can be used to load wallpaper images normally retrieved directly from the |
| 34 | * {@link WallpaperManager} by passing to Glide's {@link com.bumptech.glide.RequestBuilder#load} and |
| 35 | * registering {@link WallpaperModelLoader} on Glide's {@link com.bumptech.glide.Registry} in a |
| 36 | * custom {@link com.bumptech.glide.module.GlideModule}. |
| 37 | */ |
| 38 | public class WallpaperModel { |
| 39 | public static final int SOURCE_BUILT_IN = 0; |
| 40 | private static final String TAG = "WallpaperModel"; |
| 41 | private static final boolean SCALE_TO_FIT = true; |
| 42 | private static final float HORIZONTAL_CENTER_ALIGNED = 0.5f; |
| 43 | private static final float VERTICAL_CENTER_ALIGNED = 0.5f; |
| 44 | @Source |
| 45 | private int mWallpaperSource; |
| 46 | private WallpaperManager mWallpaperManager; |
Jon Miranda | 15a9264 | 2018-01-05 16:17:13 -0800 | [diff] [blame] | 47 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 48 | public WallpaperModel(Context context, @Source int wallpaperSource) { |
| 49 | mWallpaperSource = wallpaperSource; |
| 50 | mWallpaperManager = WallpaperManager.getInstance(context); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Returns the {@link Drawable} for the wallpaper image represented by this object. |
| 55 | */ |
| 56 | @TargetApi(Build.VERSION_CODES.KITKAT) |
| 57 | public Drawable getDrawable(int width, int height) { |
| 58 | if (mWallpaperSource != SOURCE_BUILT_IN) { |
| 59 | Log.e(TAG, "Invalid wallpaper data source: " + mWallpaperSource); |
| 60 | return null; |
| 61 | } |
| 62 | |
| 63 | return mWallpaperManager.getBuiltInDrawable( |
| 64 | width, |
| 65 | height, |
| 66 | SCALE_TO_FIT, |
| 67 | HORIZONTAL_CENTER_ALIGNED, |
| 68 | VERTICAL_CENTER_ALIGNED); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Returns the Key used to cache the data loaded by this model. |
| 73 | */ |
| 74 | public Key getKey() { |
| 75 | if (mWallpaperSource != SOURCE_BUILT_IN) { |
| 76 | Log.e(TAG, "Invalid wallpaper data source: " + mWallpaperSource); |
| 77 | } |
| 78 | |
| 79 | // The built-in wallpaper image can only change via an OTA, so it cannot change over the |
| 80 | // lifetime of this object so just use the object's signature as a cache key. |
| 81 | return new ObjectKey(this); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Possible sources of wallpaper image data from {@link android.app.WallpaperManager}. |
| 86 | */ |
| 87 | @IntDef({ |
| 88 | SOURCE_BUILT_IN}) |
| 89 | public @interface Source { |
| 90 | } |
| 91 | } |