blob: f4fb4eaeff93d16356bab5e83c6e193b2a6a2554 [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.module;
17
18import android.content.Context;
Jon Miranda16ea1b12017-12-12 14:52:48 -080019
20import com.android.wallpaper.compat.BuildCompat;
21import com.android.wallpaper.compat.WallpaperManagerCompat;
22import com.android.wallpaper.model.CurrentWallpaperInfoV16;
23import com.android.wallpaper.model.CurrentWallpaperInfoVN;
24import com.android.wallpaper.model.LiveWallpaperInfo;
25import com.android.wallpaper.model.WallpaperInfo;
26import com.android.wallpaper.model.WallpaperMetadata;
27import com.android.wallpaper.module.WallpaperPreferences.PresentationMode;
28import com.android.wallpaper.module.WallpaperRefresher.RefreshListener;
29
Sunny Goyal8600a3f2018-08-15 12:48:01 -070030import androidx.annotation.Nullable;
31
Jon Miranda16ea1b12017-12-12 14:52:48 -080032/**
33 * Default implementation of {@link CurrentWallpaperInfoFactory} which actually constructs
34 * {@link WallpaperInfo} instances representing the wallpapers currently set to the device.
35 */
36public class DefaultCurrentWallpaperInfoFactory implements CurrentWallpaperInfoFactory {
37
38 private Context mAppContext;
39 private WallpaperRefresher mWallpaperRefresher;
40 private LiveWallpaperStatusChecker mLiveWallpaperStatusChecker;
41
42 // Cached copies of the currently-set WallpaperInfo(s) and presentation mode.
43 private WallpaperInfo mHomeWallpaper;
44 @Nullable
45 private WallpaperInfo mLockWallpaper;
46 @PresentationMode
47 private int mPresentationMode;
48
49 public DefaultCurrentWallpaperInfoFactory(Context context) {
50 mAppContext = context.getApplicationContext();
51 mWallpaperRefresher = InjectorProvider.getInjector().getWallpaperRefresher(mAppContext);
52 mLiveWallpaperStatusChecker =
53 InjectorProvider.getInjector().getLiveWallpaperStatusChecker(mAppContext);
54 }
55
56 @Override
57 public synchronized void createCurrentWallpaperInfos(final WallpaperInfoCallback callback,
58 boolean forceRefresh) {
59 if (!forceRefresh && mHomeWallpaper != null) {
60 callback.onWallpaperInfoCreated(mHomeWallpaper, mLockWallpaper, mPresentationMode);
61 return;
62 }
63
64 // Clear cached copies if we are refreshing the currently-set WallpaperInfo(s) from the
65 // Refresher so that multiple calls to this method after a call with forceRefresh=true don't
66 // provide old cached copies.
67 if (forceRefresh) {
68 mHomeWallpaper = null;
69 mLockWallpaper = null;
70 }
71
72 mWallpaperRefresher.refresh(new RefreshListener() {
73 @Override
74 public void onRefreshed(WallpaperMetadata homeWallpaperMetadata,
75 @Nullable WallpaperMetadata lockWallpaperMetadata,
76 @PresentationMode int presentationMode) {
77
78 WallpaperInfo homeWallpaper;
79
80 if (homeWallpaperMetadata.getWallpaperComponent() == null
81 || mLiveWallpaperStatusChecker.isNoBackupImageWallpaperSet()) { // Image wallpaper
82 if (BuildCompat.isAtLeastN()) {
83 homeWallpaper = new CurrentWallpaperInfoVN(
84 homeWallpaperMetadata.getAttributions(),
85 homeWallpaperMetadata.getActionUrl(),
Santiago Etchebehered1bd5092018-04-18 16:03:30 -070086 homeWallpaperMetadata.getActionLabelRes(),
87 homeWallpaperMetadata.getActionIconRes(),
Jon Miranda16ea1b12017-12-12 14:52:48 -080088 homeWallpaperMetadata.getCollectionId(),
89 WallpaperManagerCompat.FLAG_SYSTEM);
90 } else {
91 homeWallpaper = new CurrentWallpaperInfoV16(
92 homeWallpaperMetadata.getAttributions(),
93 homeWallpaperMetadata.getActionUrl(),
Santiago Etchebehered1bd5092018-04-18 16:03:30 -070094 homeWallpaperMetadata.getActionLabelRes(),
95 homeWallpaperMetadata.getActionIconRes(),
Jon Miranda16ea1b12017-12-12 14:52:48 -080096 homeWallpaperMetadata.getCollectionId());
97 }
98 } else { // Live wallpaper
99 homeWallpaper = new LiveWallpaperInfo(homeWallpaperMetadata.getWallpaperComponent());
100 }
101
102 WallpaperInfo lockWallpaper = null;
103
104 if (lockWallpaperMetadata != null) {
105 lockWallpaper = new CurrentWallpaperInfoVN(
106 lockWallpaperMetadata.getAttributions(),
107 lockWallpaperMetadata.getActionUrl(),
Santiago Etchebehered1bd5092018-04-18 16:03:30 -0700108 lockWallpaperMetadata.getActionLabelRes(),
109 lockWallpaperMetadata.getActionIconRes(),
Jon Miranda16ea1b12017-12-12 14:52:48 -0800110 lockWallpaperMetadata.getCollectionId(),
111 WallpaperManagerCompat.FLAG_LOCK);
112 }
113
114 mHomeWallpaper = homeWallpaper;
115 mLockWallpaper = lockWallpaper;
116 mPresentationMode = presentationMode;
117
118 callback.onWallpaperInfoCreated(homeWallpaper, lockWallpaper, presentationMode);
119 }
120 });
121 }
122}