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.model;
17 
18 import static android.app.WallpaperManager.SetWallpaperFlags;
19 
20 import android.app.Activity;
21 import android.app.WallpaperManager;
22 import android.content.Context;
23 import android.os.Parcel;
24 
25 import com.android.wallpaper.asset.Asset;
26 import com.android.wallpaper.asset.BuiltInWallpaperAsset;
27 import com.android.wallpaper.asset.CurrentWallpaperAsset;
28 import com.android.wallpaper.config.BaseFlags;
29 import com.android.wallpaper.module.InjectorProvider;
30 
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.List;
34 
35 /**
36  * Represents the currently set wallpaper on N+ devices. Should not be used to set a new wallpaper.
37  */
38 public class CurrentWallpaperInfo extends WallpaperInfo {
39 
40     public static final Creator<CurrentWallpaperInfo> CREATOR =
41             new Creator<CurrentWallpaperInfo>() {
42                 @Override
43                 public CurrentWallpaperInfo createFromParcel(Parcel source) {
44                     return new CurrentWallpaperInfo(source);
45                 }
46 
47                 @Override
48                 public CurrentWallpaperInfo[] newArray(int size) {
49                     return new CurrentWallpaperInfo[size];
50                 }
51             };
52     private static final String TAG = "CurrentWallpaperInfoVN";
53     private final List<String> mAttributions;
54     private Asset mAsset;
55     private final String mActionUrl;
56     private final String mCollectionId;
57     @SetWallpaperFlags
58     private final int mWallpaperManagerFlag;
59     public static final String UNKNOWN_CURRENT_WALLPAPER_ID = "unknown_current_wallpaper_id";
60 
61     /**
62      * Constructs a new instance of this class.
63      *
64      * @param wallpaperManagerFlag Either SYSTEM or LOCK--the source of image data which this object
65      *                             represents.
66      */
CurrentWallpaperInfo(List<String> attributions, String actionUrl, String collectionId, @SetWallpaperFlags int wallpaperManagerFlag)67     public CurrentWallpaperInfo(List<String> attributions, String actionUrl, String collectionId,
68             @SetWallpaperFlags int wallpaperManagerFlag) {
69         mAttributions = attributions;
70         mWallpaperManagerFlag = wallpaperManagerFlag;
71         mActionUrl = actionUrl;
72         mCollectionId = collectionId;
73     }
74 
CurrentWallpaperInfo(Parcel in)75     private CurrentWallpaperInfo(Parcel in) {
76         super(in);
77         mAttributions = new ArrayList<>();
78         in.readStringList(mAttributions);
79         //noinspection ResourceType
80         mWallpaperManagerFlag = in.readInt();
81         mActionUrl = in.readString();
82         mCollectionId = in.readString();
83         mCropHints.putAll(in.readSerializable(HashMap.class.getClassLoader(), HashMap.class));
84     }
85 
86     @Override
getWallpaperId()87     public String getWallpaperId() {
88         return UNKNOWN_CURRENT_WALLPAPER_ID + mWallpaperManagerFlag;
89     }
90 
91     @Override
getAttributions(Context context)92     public List<String> getAttributions(Context context) {
93         return mAttributions;
94     }
95 
96     @Override
getAsset(Context context)97     public Asset getAsset(Context context) {
98         if (mAsset == null) {
99             mAsset = createCurrentWallpaperAssetVN(context);
100         }
101         return mAsset;
102     }
103 
104     @Override
getThumbAsset(Context context)105     public Asset getThumbAsset(Context context) {
106         return getAsset(context);
107     }
108 
109     @Override
getActionUrl(Context unused)110     public String getActionUrl(Context unused) {
111         return mActionUrl;
112     }
113 
114     @Override
getCollectionId(Context unused)115     public String getCollectionId(Context unused) {
116         return mCollectionId;
117     }
118 
119     /**
120      * Constructs and returns an Asset instance representing the currently-set wallpaper asset.
121      */
createCurrentWallpaperAssetVN(Context context)122     private Asset createCurrentWallpaperAssetVN(Context context) {
123         // Whether the wallpaper this object represents is the default built-in wallpaper.
124         boolean isSystemBuiltIn = mWallpaperManagerFlag == WallpaperManager.FLAG_SYSTEM
125                 && !InjectorProvider.getInjector().getWallpaperStatusChecker(context)
126                 .isHomeStaticWallpaperSet();
127         BaseFlags flags = InjectorProvider.getInjector().getFlags();
128         // Only get the full wallpaper asset when previewing a multi-crop wallpaper, otherwise get
129         // the cropped asset.
130         boolean getFullAsset = flags.isMultiCropEnabled() && !mCropHints.isEmpty();
131 
132         return (isSystemBuiltIn)
133                 ? new BuiltInWallpaperAsset(context)
134                 : new CurrentWallpaperAsset(context, mWallpaperManagerFlag, /* getCropped= */
135                         !getFullAsset);
136     }
137 
138     @Override
writeToParcel(Parcel parcel, int flags)139     public void writeToParcel(Parcel parcel, int flags) {
140         super.writeToParcel(parcel, flags);
141         parcel.writeStringList(mAttributions);
142         parcel.writeInt(mWallpaperManagerFlag);
143         parcel.writeString(mActionUrl);
144         parcel.writeString(mCollectionId);
145         parcel.writeSerializable(mCropHints);
146     }
147 
148     @Override
showPreview(Activity srcActivity, InlinePreviewIntentFactory factory, int requestCode, boolean isAssetIdPresent)149     public void showPreview(Activity srcActivity, InlinePreviewIntentFactory factory,
150                             int requestCode, boolean isAssetIdPresent) {
151         srcActivity.startActivityForResult(factory.newIntent(srcActivity, this,
152                 isAssetIdPresent), requestCode);
153     }
154 
155     @Override
getStoredWallpaperId(Context context)156     public String getStoredWallpaperId(Context context) {
157         return null;
158     }
159 
getWallpaperManagerFlag()160     public int getWallpaperManagerFlag() {
161         return mWallpaperManagerFlag;
162     }
163 }
164