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.app.WallpaperManager; 19 import android.content.Context; 20 21 import androidx.annotation.Nullable; 22 23 import com.android.wallpaper.compat.WallpaperManagerCompat; 24 import com.android.wallpaper.model.CurrentWallpaperInfoVN; 25 import com.android.wallpaper.model.LiveWallpaperMetadata; 26 import com.android.wallpaper.model.WallpaperInfo; 27 import com.android.wallpaper.module.WallpaperPreferences.PresentationMode; 28 29 /** 30 * Default implementation of {@link CurrentWallpaperInfoFactory} which actually constructs 31 * {@link WallpaperInfo} instances representing the wallpapers currently set to the device. 32 */ 33 public class DefaultCurrentWallpaperInfoFactory implements CurrentWallpaperInfoFactory { 34 35 private final Context mAppContext; 36 private final WallpaperRefresher mWallpaperRefresher; 37 private final LiveWallpaperInfoFactory mLiveWallpaperInfoFactory; 38 private final WallpaperManager mWallpaperManager; 39 40 // Cached copies of the currently-set WallpaperInfo(s) and presentation mode. 41 private WallpaperInfo mHomeWallpaper; 42 @Nullable 43 private WallpaperInfo mLockWallpaper; 44 @PresentationMode 45 private int mPresentationMode; 46 DefaultCurrentWallpaperInfoFactory(Context context)47 public DefaultCurrentWallpaperInfoFactory(Context context) { 48 mAppContext = context.getApplicationContext(); 49 Injector injector = InjectorProvider.getInjector(); 50 mWallpaperRefresher = injector.getWallpaperRefresher(mAppContext); 51 mLiveWallpaperInfoFactory = injector.getLiveWallpaperInfoFactory(mAppContext); 52 mWallpaperManager = WallpaperManager.getInstance(context); 53 } 54 55 @Override createCurrentWallpaperInfos(final WallpaperInfoCallback callback, boolean forceRefresh)56 public synchronized void createCurrentWallpaperInfos(final WallpaperInfoCallback callback, 57 boolean forceRefresh) { 58 if (!forceRefresh && mHomeWallpaper != null 59 && mPresentationMode != WallpaperPreferences.PRESENTATION_MODE_ROTATING) { 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( 73 (homeWallpaperMetadata, lockWallpaperMetadata, presentationMode) -> { 74 WallpaperInfo homeWallpaper; 75 if (homeWallpaperMetadata instanceof LiveWallpaperMetadata) { 76 homeWallpaper = mLiveWallpaperInfoFactory.getLiveWallpaperInfo( 77 homeWallpaperMetadata.getWallpaperComponent()); 78 } else { 79 homeWallpaper = new CurrentWallpaperInfoVN( 80 homeWallpaperMetadata.getAttributions(), 81 homeWallpaperMetadata.getActionUrl(), 82 homeWallpaperMetadata.getActionLabelRes(), 83 homeWallpaperMetadata.getActionIconRes(), 84 homeWallpaperMetadata.getCollectionId(), 85 WallpaperManagerCompat.FLAG_SYSTEM); 86 } 87 88 WallpaperInfo lockWallpaper = null; 89 90 if (lockWallpaperMetadata != null) { 91 92 if (lockWallpaperMetadata instanceof LiveWallpaperMetadata) { 93 lockWallpaper = mLiveWallpaperInfoFactory.getLiveWallpaperInfo( 94 lockWallpaperMetadata.getWallpaperComponent()); 95 } else { 96 lockWallpaper = new CurrentWallpaperInfoVN( 97 lockWallpaperMetadata.getAttributions(), 98 lockWallpaperMetadata.getActionUrl(), 99 lockWallpaperMetadata.getActionLabelRes(), 100 lockWallpaperMetadata.getActionIconRes(), 101 lockWallpaperMetadata.getCollectionId(), 102 WallpaperManagerCompat.FLAG_LOCK); 103 } 104 } 105 106 mHomeWallpaper = homeWallpaper; 107 mLockWallpaper = lockWallpaper; 108 mPresentationMode = presentationMode; 109 110 callback.onWallpaperInfoCreated(homeWallpaper, lockWallpaper, presentationMode); 111 }); 112 } 113 } 114