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 20 import androidx.annotation.Nullable; 21 22 import com.android.wallpaper.model.CurrentWallpaperInfo; 23 import com.android.wallpaper.model.LiveWallpaperMetadata; 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 WallpaperRefresher mWallpaperRefresher; 34 private final LiveWallpaperInfoFactory mLiveWallpaperInfoFactory; 35 36 // Cached copies of the currently-set WallpaperInfo(s) and presentation mode. 37 private WallpaperInfo mHomeWallpaper; 38 @Nullable 39 private WallpaperInfo mLockWallpaper; 40 @PresentationMode 41 private int mPresentationMode; 42 DefaultCurrentWallpaperInfoFactory(WallpaperRefresher wallpaperRefresher, LiveWallpaperInfoFactory liveWallpaperInfoFactory)43 public DefaultCurrentWallpaperInfoFactory(WallpaperRefresher wallpaperRefresher, 44 LiveWallpaperInfoFactory liveWallpaperInfoFactory) { 45 mWallpaperRefresher = wallpaperRefresher; 46 mLiveWallpaperInfoFactory = liveWallpaperInfoFactory; 47 } 48 49 @Override createCurrentWallpaperInfos(final WallpaperInfoCallback callback, boolean forceRefresh)50 public synchronized void createCurrentWallpaperInfos(final WallpaperInfoCallback callback, 51 boolean forceRefresh) { 52 if (!forceRefresh && mHomeWallpaper != null 53 && mPresentationMode != WallpaperPreferences.PRESENTATION_MODE_ROTATING) { 54 callback.onWallpaperInfoCreated(mHomeWallpaper, mLockWallpaper, mPresentationMode); 55 return; 56 } 57 58 // Clear cached copies if we are refreshing the currently-set WallpaperInfo(s) from the 59 // Refresher so that multiple calls to this method after a call with forceRefresh=true don't 60 // provide old cached copies. 61 if (forceRefresh) { 62 clearCurrentWallpaperInfos(); 63 } 64 65 mWallpaperRefresher.refresh( 66 (homeWallpaperMetadata, lockWallpaperMetadata, presentationMode) -> { 67 WallpaperInfo homeWallpaper; 68 if (homeWallpaperMetadata instanceof LiveWallpaperMetadata) { 69 homeWallpaper = mLiveWallpaperInfoFactory.getLiveWallpaperInfo( 70 homeWallpaperMetadata.getWallpaperComponent()); 71 } else { 72 homeWallpaper = new CurrentWallpaperInfo( 73 homeWallpaperMetadata.getAttributions(), 74 homeWallpaperMetadata.getActionUrl(), 75 homeWallpaperMetadata.getActionLabelRes(), 76 homeWallpaperMetadata.getActionIconRes(), 77 homeWallpaperMetadata.getCollectionId(), 78 WallpaperManager.FLAG_SYSTEM); 79 } 80 81 WallpaperInfo lockWallpaper = null; 82 83 if (lockWallpaperMetadata != null) { 84 85 if (lockWallpaperMetadata instanceof LiveWallpaperMetadata) { 86 lockWallpaper = mLiveWallpaperInfoFactory.getLiveWallpaperInfo( 87 lockWallpaperMetadata.getWallpaperComponent()); 88 } else { 89 lockWallpaper = new CurrentWallpaperInfo( 90 lockWallpaperMetadata.getAttributions(), 91 lockWallpaperMetadata.getActionUrl(), 92 lockWallpaperMetadata.getActionLabelRes(), 93 lockWallpaperMetadata.getActionIconRes(), 94 lockWallpaperMetadata.getCollectionId(), 95 WallpaperManager.FLAG_LOCK); 96 } 97 } 98 99 mHomeWallpaper = homeWallpaper; 100 mLockWallpaper = lockWallpaper; 101 mPresentationMode = presentationMode; 102 103 callback.onWallpaperInfoCreated(homeWallpaper, lockWallpaper, presentationMode); 104 }); 105 } 106 107 @Override clearCurrentWallpaperInfos()108 public void clearCurrentWallpaperInfos() { 109 mHomeWallpaper = null; 110 mLockWallpaper = null; 111 } 112 } 113