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 android.app.Activity;
19 import android.content.Context;
20 import android.os.Parcel;
21 
22 import androidx.annotation.DrawableRes;
23 import androidx.annotation.StringRes;
24 
25 import com.android.wallpaper.asset.Asset;
26 import com.android.wallpaper.asset.BuiltInWallpaperAsset;
27 import com.android.wallpaper.asset.CurrentWallpaperAssetVN;
28 import com.android.wallpaper.compat.WallpaperManagerCompat;
29 import com.android.wallpaper.compat.WallpaperManagerCompat.WallpaperLocation;
30 import com.android.wallpaper.module.InjectorProvider;
31 
32 import java.util.ArrayList;
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 CurrentWallpaperInfoVN extends WallpaperInfo {
39 
40     public static final Creator<CurrentWallpaperInfoVN> CREATOR =
41             new Creator<CurrentWallpaperInfoVN>() {
42                 @Override
43                 public CurrentWallpaperInfoVN createFromParcel(Parcel source) {
44                     return new CurrentWallpaperInfoVN(source);
45                 }
46 
47                 @Override
48                 public CurrentWallpaperInfoVN[] newArray(int size) {
49                     return new CurrentWallpaperInfoVN[size];
50                 }
51             };
52     private static final String TAG = "CurrentWallpaperInfoVN";
53     private List<String> mAttributions;
54     private Asset mAsset;
55     private String mActionUrl;
56     @StringRes
57     private int mActionLabelRes;
58     @DrawableRes
59     private int mActionIconRes;
60     private String mCollectionId;
61     @WallpaperLocation
62     private int mWallpaperManagerFlag;
63     public static final String UNKNOWN_CURRENT_WALLPAPER_ID = "unknown_current_wallpaper_id";
64 
65     /**
66      * Constructs a new instance of this class.
67      *
68      * @param wallpaperManagerFlag Either SYSTEM or LOCK--the source of image data which this object
69      *                             represents.
70      */
CurrentWallpaperInfoVN(List<String> attributions, String actionUrl, @StringRes int actionLabelRes, @DrawableRes int actionIconRes, String collectionId, @WallpaperLocation int wallpaperManagerFlag)71     public CurrentWallpaperInfoVN(List<String> attributions, String actionUrl,
72                                   @StringRes int actionLabelRes, @DrawableRes int actionIconRes,
73                                   String collectionId,
74                                   @WallpaperLocation int wallpaperManagerFlag) {
75         mAttributions = attributions;
76         mWallpaperManagerFlag = wallpaperManagerFlag;
77         mActionUrl = actionUrl;
78         mActionLabelRes = actionLabelRes;
79         mActionIconRes = actionIconRes;
80         mCollectionId = collectionId;
81     }
82 
CurrentWallpaperInfoVN(Parcel in)83     private CurrentWallpaperInfoVN(Parcel in) {
84         super(in);
85         mAttributions = new ArrayList<>();
86         in.readStringList(mAttributions);
87         //noinspection ResourceType
88         mWallpaperManagerFlag = in.readInt();
89         mActionUrl = in.readString();
90         mCollectionId = in.readString();
91         mActionLabelRes = in.readInt();
92         mActionIconRes = in.readInt();
93     }
94 
95     @Override
getWallpaperId()96     public String getWallpaperId() {
97         return UNKNOWN_CURRENT_WALLPAPER_ID;
98     }
99 
100     @Override
getAttributions(Context context)101     public List<String> getAttributions(Context context) {
102         return mAttributions;
103     }
104 
105     @Override
getAsset(Context context)106     public Asset getAsset(Context context) {
107         if (mAsset == null) {
108             mAsset = createCurrentWallpaperAssetVN(context);
109         }
110         return mAsset;
111     }
112 
113     @Override
getThumbAsset(Context context)114     public Asset getThumbAsset(Context context) {
115         return getAsset(context);
116     }
117 
118     @Override
getActionUrl(Context unused)119     public String getActionUrl(Context unused) {
120         return mActionUrl;
121     }
122 
123     @Override
getCollectionId(Context unused)124     public String getCollectionId(Context unused) {
125         return mCollectionId;
126     }
127 
128     @Override
getActionIconRes(Context unused)129     public int getActionIconRes(Context unused) {
130         return mActionIconRes != 0 ? mActionIconRes : WallpaperInfo.getDefaultActionIcon();
131     }
132 
133     @Override
getActionLabelRes(Context unused)134     public int getActionLabelRes(Context unused) {
135         return mActionLabelRes != 0 ? mActionLabelRes : WallpaperInfo.getDefaultActionLabel();
136     }
137 
getWallpaperManagerFlag()138     public int getWallpaperManagerFlag() {
139         return mWallpaperManagerFlag;
140     }
141 
142     /**
143      * Constructs and returns an Asset instance representing the currently-set wallpaper asset.
144      */
createCurrentWallpaperAssetVN(Context context)145     private Asset createCurrentWallpaperAssetVN(Context context) {
146         // Whether the wallpaper this object represents is the default built-in wallpaper.
147         boolean isSystemBuiltIn = mWallpaperManagerFlag == WallpaperManagerCompat.FLAG_SYSTEM
148                 && !InjectorProvider.getInjector().getWallpaperStatusChecker()
149                 .isHomeStaticWallpaperSet(context);
150 
151         return (isSystemBuiltIn)
152                 ? new BuiltInWallpaperAsset(context)
153                 : new CurrentWallpaperAssetVN(context, mWallpaperManagerFlag);
154     }
155 
156     @Override
writeToParcel(Parcel parcel, int flags)157     public void writeToParcel(Parcel parcel, int flags) {
158         super.writeToParcel(parcel, flags);
159         parcel.writeStringList(mAttributions);
160         parcel.writeInt(mWallpaperManagerFlag);
161         parcel.writeString(mActionUrl);
162         parcel.writeString(mCollectionId);
163         parcel.writeInt(mActionLabelRes);
164         parcel.writeInt(mActionIconRes);
165     }
166 
167     @Override
showPreview(Activity srcActivity, InlinePreviewIntentFactory factory, int requestCode)168     public void showPreview(Activity srcActivity, InlinePreviewIntentFactory factory,
169                             int requestCode) {
170         srcActivity.startActivityForResult(factory.newIntent(srcActivity, this), requestCode);
171     }
172 }
173