Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 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; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 19 | |
| 20 | import com.android.wallpaper.compat.BuildCompat; |
| 21 | import com.android.wallpaper.compat.WallpaperManagerCompat; |
| 22 | import com.android.wallpaper.model.CurrentWallpaperInfoV16; |
| 23 | import com.android.wallpaper.model.CurrentWallpaperInfoVN; |
| 24 | import com.android.wallpaper.model.LiveWallpaperInfo; |
| 25 | import com.android.wallpaper.model.WallpaperInfo; |
| 26 | import com.android.wallpaper.model.WallpaperMetadata; |
| 27 | import com.android.wallpaper.module.WallpaperPreferences.PresentationMode; |
| 28 | import com.android.wallpaper.module.WallpaperRefresher.RefreshListener; |
| 29 | |
Sunny Goyal | 8600a3f | 2018-08-15 12:48:01 -0700 | [diff] [blame^] | 30 | import androidx.annotation.Nullable; |
| 31 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 32 | /** |
| 33 | * Default implementation of {@link CurrentWallpaperInfoFactory} which actually constructs |
| 34 | * {@link WallpaperInfo} instances representing the wallpapers currently set to the device. |
| 35 | */ |
| 36 | public 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 Etchebehere | d1bd509 | 2018-04-18 16:03:30 -0700 | [diff] [blame] | 86 | homeWallpaperMetadata.getActionLabelRes(), |
| 87 | homeWallpaperMetadata.getActionIconRes(), |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 88 | homeWallpaperMetadata.getCollectionId(), |
| 89 | WallpaperManagerCompat.FLAG_SYSTEM); |
| 90 | } else { |
| 91 | homeWallpaper = new CurrentWallpaperInfoV16( |
| 92 | homeWallpaperMetadata.getAttributions(), |
| 93 | homeWallpaperMetadata.getActionUrl(), |
Santiago Etchebehere | d1bd509 | 2018-04-18 16:03:30 -0700 | [diff] [blame] | 94 | homeWallpaperMetadata.getActionLabelRes(), |
| 95 | homeWallpaperMetadata.getActionIconRes(), |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 96 | 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 Etchebehere | d1bd509 | 2018-04-18 16:03:30 -0700 | [diff] [blame] | 108 | lockWallpaperMetadata.getActionLabelRes(), |
| 109 | lockWallpaperMetadata.getActionIconRes(), |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 110 | 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 | } |