blob: e6b5f38826647f98cf8504701e4f7c3d92dc9b97 [file] [log] [blame]
Jon Miranda16ea1b12017-12-12 14:52:48 -08001/*
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 */
16package com.android.wallpaper.asset;
17
18import android.annotation.TargetApi;
19import android.app.WallpaperManager;
20import android.content.Context;
21import android.graphics.drawable.Drawable;
22import android.os.Build;
23import android.support.annotation.IntDef;
24import android.util.Log;
25
26import com.bumptech.glide.load.Key;
27import com.bumptech.glide.signature.ObjectKey;
28
29/**
30 * Glide model representing wallpaper image data retrieved from {@link WallpaperManager}.
31 * <p>
32 * Instances of this class can be used to load wallpaper images normally retrieved directly from the
33 * {@link WallpaperManager} by passing to Glide's {@link com.bumptech.glide.RequestBuilder#load} and
34 * registering {@link WallpaperModelLoader} on Glide's {@link com.bumptech.glide.Registry} in a
35 * custom {@link com.bumptech.glide.module.GlideModule}.
36 */
37public class WallpaperModel {
38 public static final int SOURCE_BUILT_IN = 0;
39 private static final String TAG = "WallpaperModel";
40 private static final boolean SCALE_TO_FIT = true;
41 private static final float HORIZONTAL_CENTER_ALIGNED = 0.5f;
42 private static final float VERTICAL_CENTER_ALIGNED = 0.5f;
43 @Source
44 private int mWallpaperSource;
45 private WallpaperManager mWallpaperManager;
Jon Miranda15a92642018-01-05 16:17:13 -080046
Jon Miranda16ea1b12017-12-12 14:52:48 -080047 public WallpaperModel(Context context, @Source int wallpaperSource) {
48 mWallpaperSource = wallpaperSource;
49 mWallpaperManager = WallpaperManager.getInstance(context);
50 }
51
52 /**
53 * Returns the {@link Drawable} for the wallpaper image represented by this object.
54 */
55 @TargetApi(Build.VERSION_CODES.KITKAT)
56 public Drawable getDrawable(int width, int height) {
57 if (mWallpaperSource != SOURCE_BUILT_IN) {
58 Log.e(TAG, "Invalid wallpaper data source: " + mWallpaperSource);
59 return null;
60 }
61
62 return mWallpaperManager.getBuiltInDrawable(
63 width,
64 height,
65 SCALE_TO_FIT,
66 HORIZONTAL_CENTER_ALIGNED,
67 VERTICAL_CENTER_ALIGNED);
68 }
69
70 /**
71 * Returns the Key used to cache the data loaded by this model.
72 */
73 public Key getKey() {
74 if (mWallpaperSource != SOURCE_BUILT_IN) {
75 Log.e(TAG, "Invalid wallpaper data source: " + mWallpaperSource);
76 }
77
78 // The built-in wallpaper image can only change via an OTA, so it cannot change over the
79 // lifetime of this object so just use the object's signature as a cache key.
80 return new ObjectKey(this);
81 }
82
83 /**
84 * Possible sources of wallpaper image data from {@link android.app.WallpaperManager}.
85 */
86 @IntDef({
87 SOURCE_BUILT_IN})
88 public @interface Source {
89 }
90}