1 /*
2  * Copyright (C) 2019 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.testing;
17 
18 
19 import android.app.WallpaperManager;
20 import android.content.Context;
21 
22 import com.android.wallpaper.compat.BuildCompat;
23 import com.android.wallpaper.model.WallpaperMetadata;
24 import com.android.wallpaper.module.InjectorProvider;
25 import com.android.wallpaper.module.WallpaperPreferences;
26 import com.android.wallpaper.module.WallpaperRefresher;
27 
28 /**
29  * Test implementation of {@link WallpaperRefresher} which simply provides whatever metadata is
30  * saved in WallpaperPreferences and the image wallpaper set to {@link WallpaperManager}.
31  */
32 public class TestWallpaperRefresher implements WallpaperRefresher {
33 
34     private final Context mAppContext;
35 
36     /**
37      * @param context The application's context.
38      */
TestWallpaperRefresher(Context context)39     public TestWallpaperRefresher(Context context) {
40         mAppContext = context.getApplicationContext();
41     }
42 
43     @Override
refresh(RefreshListener listener)44     public void refresh(RefreshListener listener) {
45 
46         WallpaperPreferences prefs = InjectorProvider.getInjector().getPreferences(mAppContext);
47 
48         if (BuildCompat.isAtLeastN() && prefs.getLockWallpaperId() > 0) {
49             listener.onRefreshed(
50                     new WallpaperMetadata(
51                             prefs.getHomeWallpaperAttributions(),
52                             prefs.getHomeWallpaperActionUrl(),
53                             prefs.getHomeWallpaperActionLabelRes(),
54                             prefs.getHomeWallpaperActionIconRes(),
55                             prefs.getHomeWallpaperCollectionId(),
56                             prefs.getHomeWallpaperBackingFileName(),
57                             null /* wallpaperComponent */),
58                     new WallpaperMetadata(
59                             prefs.getLockWallpaperAttributions(),
60                             prefs.getLockWallpaperActionUrl(),
61                             prefs.getLockWallpaperActionLabelRes(),
62                             prefs.getLockWallpaperActionIconRes(),
63                             prefs.getLockWallpaperCollectionId(),
64                             prefs.getLockWallpaperBackingFileName(),
65                             null /* wallpaperComponent */),
66                     prefs.getWallpaperPresentationMode());
67         } else {
68             listener.onRefreshed(
69                     new WallpaperMetadata(
70                             prefs.getHomeWallpaperAttributions(),
71                             prefs.getHomeWallpaperActionUrl(),
72                             prefs.getHomeWallpaperActionLabelRes(),
73                             prefs.getHomeWallpaperActionIconRes(),
74                             prefs.getHomeWallpaperCollectionId(),
75                             prefs.getHomeWallpaperBackingFileName(),
76                             null /* wallpaperComponent */),
77                     null,
78                     prefs.getWallpaperPresentationMode());
79         }
80     }
81 }
82