blob: bf54bfdf898a8adc06197b4371a19cb3a65b83c1 [file] [log] [blame]
Jon Miranda16ea1b12017-12-12 14:52:48 -08001/*
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 */
16package com.android.wallpaper.model;
17
18import android.app.Activity;
19import android.content.Context;
20import android.os.Parcel;
21import android.os.ParcelFileDescriptor;
22import android.util.Log;
23
Santiago Etchebeheree6d69b52019-06-14 18:42:46 -070024import androidx.annotation.DrawableRes;
25import androidx.annotation.StringRes;
26
Jon Miranda16ea1b12017-12-12 14:52:48 -080027import com.android.wallpaper.asset.Asset;
28import com.android.wallpaper.asset.BuiltInWallpaperAsset;
29import com.android.wallpaper.asset.CurrentWallpaperAssetVN;
Jon Miranda16ea1b12017-12-12 14:52:48 -080030import com.android.wallpaper.compat.WallpaperManagerCompat;
31import com.android.wallpaper.compat.WallpaperManagerCompat.WallpaperLocation;
32import com.android.wallpaper.module.InjectorProvider;
Jon Miranda16ea1b12017-12-12 14:52:48 -080033
Jon Miranda16ea1b12017-12-12 14:52:48 -080034import java.io.IOException;
35import java.util.ArrayList;
36import java.util.List;
37
38/**
39 * Represents the currently set wallpaper on N+ devices. Should not be used to set a new wallpaper.
40 */
41public 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;
Santiago Etchebehered1bd5092018-04-18 16:03:30 -070059 @StringRes
60 private int mActionLabelRes;
61 @DrawableRes
62 private int mActionIconRes;
Jon Miranda16ea1b12017-12-12 14:52:48 -080063 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 */
Santiago Etchebehered1bd5092018-04-18 16:03:30 -070073 public CurrentWallpaperInfoVN(List<String> attributions, String actionUrl,
74 @StringRes int actionLabelRes, @DrawableRes int actionIconRes,
75 String collectionId,
Jon Miranda16ea1b12017-12-12 14:52:48 -080076 @WallpaperLocation int wallpaperManagerFlag) {
77 mAttributions = attributions;
78 mWallpaperManagerFlag = wallpaperManagerFlag;
79 mActionUrl = actionUrl;
Santiago Etchebehered1bd5092018-04-18 16:03:30 -070080 mActionLabelRes = actionLabelRes;
81 mActionIconRes = actionIconRes;
Jon Miranda16ea1b12017-12-12 14:52:48 -080082 mCollectionId = collectionId;
83 }
84
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();
Santiago Etchebehered1bd5092018-04-18 16:03:30 -070092 mActionLabelRes = in.readInt();
93 mActionIconRes = in.readInt();
Jon Miranda16ea1b12017-12-12 14:52:48 -080094 }
95
96 @Override
97 public List<String> getAttributions(Context context) {
98 return mAttributions;
99 }
100
101 @Override
102 public Asset getAsset(Context context) {
103 if (mAsset == null) {
104 mAsset = createCurrentWallpaperAssetVN(context);
105 }
106 return mAsset;
107 }
108
109 @Override
110 public Asset getThumbAsset(Context context) {
111 return getAsset(context);
112 }
113
114 @Override
115 public String getActionUrl(Context unused) {
116 return mActionUrl;
117 }
118
119 @Override
120 public String getCollectionId(Context unused) {
121 return mCollectionId;
122 }
123
Santiago Etchebehered1bd5092018-04-18 16:03:30 -0700124 @Override
Santiago Etchebeheree0810d02018-05-10 17:39:40 -0700125 public int getActionIconRes(Context unused) {
Santiago Etchebehered1bd5092018-04-18 16:03:30 -0700126 return mActionIconRes != 0 ? mActionIconRes : WallpaperInfo.getDefaultActionIcon();
127 }
128
129 @Override
Santiago Etchebeheree0810d02018-05-10 17:39:40 -0700130 public int getActionLabelRes(Context unused) {
Santiago Etchebehered1bd5092018-04-18 16:03:30 -0700131 return mActionLabelRes != 0 ? mActionLabelRes : WallpaperInfo.getDefaultActionLabel();
132 }
133
Santiago Etchebeheree6d69b52019-06-14 18:42:46 -0700134 public int getWallpaperManagerFlag() {
135 return mWallpaperManagerFlag;
136 }
137
Jon Miranda16ea1b12017-12-12 14:52:48 -0800138 /**
139 * Constructs and returns an Asset instance representing the currently-set wallpaper asset.
140 */
141 private Asset createCurrentWallpaperAssetVN(Context context) {
Jon Miranda16ea1b12017-12-12 14:52:48 -0800142 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
166 public void writeToParcel(Parcel parcel, int flags) {
167 parcel.writeStringList(mAttributions);
168 parcel.writeInt(mWallpaperManagerFlag);
169 parcel.writeString(mActionUrl);
170 parcel.writeString(mCollectionId);
Santiago Etchebehered1bd5092018-04-18 16:03:30 -0700171 parcel.writeInt(mActionLabelRes);
172 parcel.writeInt(mActionIconRes);
Jon Miranda16ea1b12017-12-12 14:52:48 -0800173 }
174
175 @Override
176 public void showPreview(Activity srcActivity, InlinePreviewIntentFactory factory,
177 int requestCode) {
178 srcActivity.startActivityForResult(factory.newIntent(srcActivity, this), requestCode);
179 }
180}