• 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         super(in);
87         mAttributions = new ArrayList<>();
88         in.readStringList(mAttributions);
89         //noinspection ResourceType
90         mWallpaperManagerFlag = in.readInt();
91         mActionUrl = in.readString();
92         mCollectionId = in.readString();
93         mActionLabelRes = in.readInt();
94         mActionIconRes = in.readInt();
95     }
96 
97     @Override
getAttributions(Context context)98     public List<String> getAttributions(Context context) {
99         return mAttributions;
100     }
101 
102     @Override
getAsset(Context context)103     public Asset getAsset(Context context) {
104         if (mAsset == null) {
105             mAsset = createCurrentWallpaperAssetVN(context);
106         }
107         return mAsset;
108     }
109 
110     @Override
getThumbAsset(Context context)111     public Asset getThumbAsset(Context context) {
112         return getAsset(context);
113     }
114 
115     @Override
getActionUrl(Context unused)116     public String getActionUrl(Context unused) {
117         return mActionUrl;
118     }
119 
120     @Override
getCollectionId(Context unused)121     public String getCollectionId(Context unused) {
122         return mCollectionId;
123     }
124 
125     @Override
getActionIconRes(Context unused)126     public int getActionIconRes(Context unused) {
127         return mActionIconRes != 0 ? mActionIconRes : WallpaperInfo.getDefaultActionIcon();
128     }
129 
130     @Override
getActionLabelRes(Context unused)131     public int getActionLabelRes(Context unused) {
132         return mActionLabelRes != 0 ? mActionLabelRes : WallpaperInfo.getDefaultActionLabel();
133     }
134 
getWallpaperManagerFlag()135     public int getWallpaperManagerFlag() {
136         return mWallpaperManagerFlag;
137     }
138 
139     /**
140      * Constructs and returns an Asset instance representing the currently-set wallpaper asset.
141      */
createCurrentWallpaperAssetVN(Context context)142     private Asset createCurrentWallpaperAssetVN(Context context) {
143         WallpaperManagerCompat wallpaperManagerCompat = InjectorProvider.getInjector()
144                 .getWallpaperManagerCompat(context);
145 
146         ParcelFileDescriptor systemWallpaperFile = wallpaperManagerCompat.getWallpaperFile(
147                 WallpaperManagerCompat.FLAG_SYSTEM);
148 
149         // Whether the wallpaper this object represents is the default built-in wallpaper.
150         boolean isSystemBuiltIn = mWallpaperManagerFlag == WallpaperManagerCompat.FLAG_SYSTEM
151                 && systemWallpaperFile == null;
152 
153         if (systemWallpaperFile != null) {
154             try {
155                 systemWallpaperFile.close();
156             } catch (IOException e) {
157                 Log.e(TAG, "Unable to close system wallpaper ParcelFileDescriptor", e);
158             }
159         }
160 
161         return (isSystemBuiltIn)
162                 ? new BuiltInWallpaperAsset(context)
163                 : new CurrentWallpaperAssetVN(context, mWallpaperManagerFlag);
164     }
165 
166     @Override
writeToParcel(Parcel parcel, int flags)167     public void writeToParcel(Parcel parcel, int flags) {
168         super.writeToParcel(parcel, flags);
169         parcel.writeStringList(mAttributions);
170         parcel.writeInt(mWallpaperManagerFlag);
171         parcel.writeString(mActionUrl);
172         parcel.writeString(mCollectionId);
173         parcel.writeInt(mActionLabelRes);
174         parcel.writeInt(mActionIconRes);
175     }
176 
177     @Override
showPreview(Activity srcActivity, InlinePreviewIntentFactory factory, int requestCode)178     public void showPreview(Activity srcActivity, InlinePreviewIntentFactory factory,
179                             int requestCode) {
180         srcActivity.startActivityForResult(factory.newIntent(srcActivity, this), requestCode);
181     }
182 }
183