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