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.module;
17 
18 import android.content.Context;
19 
20 import androidx.annotation.Nullable;
21 
22 import com.android.wallpaper.compat.WallpaperManagerCompat;
23 import com.android.wallpaper.model.CurrentWallpaperInfoVN;
24 import com.android.wallpaper.model.WallpaperInfo;
25 import com.android.wallpaper.module.WallpaperPreferences.PresentationMode;
26 
27 /**
28  * Default implementation of {@link CurrentWallpaperInfoFactory} which actually constructs
29  * {@link WallpaperInfo} instances representing the wallpapers currently set to the device.
30  */
31 public class DefaultCurrentWallpaperInfoFactory implements CurrentWallpaperInfoFactory {
32 
33     private final Context mAppContext;
34     private final WallpaperRefresher mWallpaperRefresher;
35     private final LiveWallpaperInfoFactory mLiveWallpaperInfoFactory;
36 
37     // Cached copies of the currently-set WallpaperInfo(s) and presentation mode.
38     private WallpaperInfo mHomeWallpaper;
39     @Nullable
40     private WallpaperInfo mLockWallpaper;
41     @PresentationMode
42     private int mPresentationMode;
43 
DefaultCurrentWallpaperInfoFactory(Context context)44     public DefaultCurrentWallpaperInfoFactory(Context context) {
45         mAppContext = context.getApplicationContext();
46         Injector injector = InjectorProvider.getInjector();
47         mWallpaperRefresher = injector.getWallpaperRefresher(mAppContext);
48         mLiveWallpaperInfoFactory = injector.getLiveWallpaperInfoFactory(mAppContext);
49     }
50 
51     @Override
createCurrentWallpaperInfos(final WallpaperInfoCallback callback, boolean forceRefresh)52     public synchronized void createCurrentWallpaperInfos(final WallpaperInfoCallback callback,
53                                                          boolean forceRefresh) {
54         if (!forceRefresh && mHomeWallpaper != null
55                 && mPresentationMode != WallpaperPreferences.PRESENTATION_MODE_ROTATING) {
56             callback.onWallpaperInfoCreated(mHomeWallpaper, mLockWallpaper, mPresentationMode);
57             return;
58         }
59 
60         // Clear cached copies if we are refreshing the currently-set WallpaperInfo(s) from the
61         // Refresher so that multiple calls to this method after a call with forceRefresh=true don't
62         // provide old cached copies.
63         if (forceRefresh) {
64             mHomeWallpaper = null;
65             mLockWallpaper = null;
66         }
67 
68         mWallpaperRefresher.refresh(
69                 (homeWallpaperMetadata, lockWallpaperMetadata, presentationMode) -> {
70                     WallpaperInfo homeWallpaper;
71                     if (homeWallpaperMetadata.getWallpaperComponent() == null) {
72                         homeWallpaper = new CurrentWallpaperInfoVN(
73                                 homeWallpaperMetadata.getAttributions(),
74                                 homeWallpaperMetadata.getActionUrl(),
75                                 homeWallpaperMetadata.getActionLabelRes(),
76                                 homeWallpaperMetadata.getActionIconRes(),
77                                 homeWallpaperMetadata.getCollectionId(),
78                                 WallpaperManagerCompat.FLAG_SYSTEM);
79                     } else {
80                         homeWallpaper = mLiveWallpaperInfoFactory.getLiveWallpaperInfo(
81                                 homeWallpaperMetadata.getWallpaperComponent());
82                     }
83 
84                     WallpaperInfo lockWallpaper = null;
85 
86                     if (lockWallpaperMetadata != null) {
87                         lockWallpaper = new CurrentWallpaperInfoVN(
88                                 lockWallpaperMetadata.getAttributions(),
89                                 lockWallpaperMetadata.getActionUrl(),
90                                 lockWallpaperMetadata.getActionLabelRes(),
91                                 lockWallpaperMetadata.getActionIconRes(),
92                                 lockWallpaperMetadata.getCollectionId(),
93                                 WallpaperManagerCompat.FLAG_LOCK);
94                     }
95 
96                     mHomeWallpaper = homeWallpaper;
97                     mLockWallpaper = lockWallpaper;
98                     mPresentationMode = presentationMode;
99 
100                     callback.onWallpaperInfoCreated(homeWallpaper, lockWallpaper, presentationMode);
101                 });
102     }
103 }
104