blob: 0393a48d5f6b6253490429d0a0ac841722b03f20 [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;
Santiago Etchebeherea2429992020-05-27 11:27:29 -070020import android.content.res.Resources;
21import android.util.AttributeSet;
22
23import androidx.annotation.IdRes;
Santiago Etchebehere39c31342021-05-03 15:35:06 -070024import androidx.annotation.Nullable;
Santiago Etchebeherea2429992020-05-27 11:27:29 -070025import androidx.annotation.StringRes;
Jon Miranda16ea1b12017-12-12 14:52:48 -080026
27import com.android.wallpaper.asset.Asset;
Santiago Etchebeherea2429992020-05-27 11:27:29 -070028import com.android.wallpaper.asset.ResourceAsset;
Jon Miranda16ea1b12017-12-12 14:52:48 -080029
30import java.util.ArrayList;
Santiago Etchebeherea2429992020-05-27 11:27:29 -070031import java.util.Collections;
Jon Miranda16ea1b12017-12-12 14:52:48 -080032import java.util.List;
33
34/**
35 * Default category for a collection of WallpaperInfo objects.
36 */
37public class WallpaperCategory extends Category {
38
Santiago Etchebeherea2429992020-05-27 11:27:29 -070039 public static final String TAG_NAME = "category";
40
Jon Miranda16ea1b12017-12-12 14:52:48 -080041 protected final Object mWallpapersLock;
42 private final List<WallpaperInfo> mWallpapers;
43 private Asset mThumbAsset;
44 private int mFeaturedThumbnailIndex;
45
46 public WallpaperCategory(String title, String collectionId, List<WallpaperInfo> wallpapers,
47 int priority) {
Santiago Etchebehere1ee76a22018-05-15 15:02:24 -070048 this(title, collectionId, 0, wallpapers, priority);
Jon Miranda16ea1b12017-12-12 14:52:48 -080049 }
50
51 public WallpaperCategory(String title, String collectionId, int featuredThumbnailIndex,
52 List<WallpaperInfo> wallpapers, int priority) {
53 super(title, collectionId, priority);
54 mWallpapers = wallpapers;
55 mWallpapersLock = new Object();
56 mFeaturedThumbnailIndex = featuredThumbnailIndex;
57 }
58
Santiago Etchebeherea2429992020-05-27 11:27:29 -070059 public WallpaperCategory(String title, String collectionId, Asset thumbAsset,
60 List<WallpaperInfo> wallpapers, int priority) {
61 super(title, collectionId, priority);
62 mWallpapers = wallpapers;
63 mWallpapersLock = new Object();
64 mThumbAsset = thumbAsset;
65 }
66
Jon Miranda16ea1b12017-12-12 14:52:48 -080067 /**
68 * Fetches wallpapers for this category and passes them to the receiver. Subclasses may use a
69 * context to fetch wallpaper info.
70 */
Santiago Etchebehere1ee76a22018-05-15 15:02:24 -070071 public void fetchWallpapers(Context unused, WallpaperReceiver receiver, boolean forceReload) {
Jon Miranda16ea1b12017-12-12 14:52:48 -080072 // Perform a shallow clone so as not to pass the reference to the list along to clients.
73 receiver.onWallpapersReceived(new ArrayList<>(mWallpapers));
74 }
75
76 @Override
77 public void show(Activity srcActivity, PickerIntentFactory factory, int requestCode) {
78 srcActivity.startActivityForResult(
79 factory.newIntent(srcActivity, getCollectionId()), requestCode);
80 }
81
82 @Override
83 public boolean isEnumerable() {
84 return true;
85 }
86
Santiago Etchebehere39c31342021-05-03 15:35:06 -070087 @Override
88 public boolean isSingleWallpaperCategory() {
89 return mWallpapers != null && mWallpapers.size() == 1;
90 }
91
92 @Nullable
93 @Override
94 public WallpaperInfo getSingleWallpaper() {
95 return isSingleWallpaperCategory() ? mWallpapers.get(0) : null;
96 }
97
Jon Miranda16ea1b12017-12-12 14:52:48 -080098 /**
99 * Returns the mutable list of wallpapers backed by this WallpaperCategory. All reads and writes
100 * on the returned list must be synchronized with {@code mWallpapersLock}.
101 */
102 protected List<WallpaperInfo> getMutableWallpapers() {
103 return mWallpapers;
104 }
105
Santiago Etchebeherea2429992020-05-27 11:27:29 -0700106 /**
107 * Returns an unmodifiable view the list of wallpapers in this WallpaperCategory.
108 */
109 public List<WallpaperInfo> getUnmodifiableWallpapers() {
110 return Collections.unmodifiableList(mWallpapers);
111 }
112
Jon Miranda16ea1b12017-12-12 14:52:48 -0800113 @Override
114 public Asset getThumbnail(Context context) {
115 synchronized (mWallpapersLock) {
116 if (mThumbAsset == null && mWallpapers.size() > 0) {
117 mThumbAsset = mWallpapers.get(mFeaturedThumbnailIndex).getThumbAsset(context);
118 }
119 }
120 return mThumbAsset;
121 }
Santiago Etchebehere1ee76a22018-05-15 15:02:24 -0700122
123 @Override
124 public boolean supportsThirdParty() {
125 return false;
126 }
127
128 @Override
129 public boolean containsThirdParty(String packageName) {
130 return false;
131 }
Santiago Etchebeherea2429992020-05-27 11:27:29 -0700132
133 /**
134 * Builder used to construct a {@link WallpaperCategory} object from an XML's
135 * {@link AttributeSet}.
136 */
137 public static class Builder {
138 private final List<WallpaperInfo> mWallpapers = new ArrayList<>();
139 private final Resources mPartnerRes;
140 private String mId;
141 private String mTitle;
142 private int mPriority;
143 private String mFeaturedId;
144 @IdRes private int mThumbResId;
145
146 public Builder(Resources partnerRes, AttributeSet attrs) {
147 mPartnerRes = partnerRes;
148 mId = attrs.getAttributeValue(null, "id");
149 @StringRes int titleResId = attrs.getAttributeResourceValue(null, "title", 0);
150 mTitle = titleResId != 0 ? mPartnerRes.getString(titleResId) : "";
151 mFeaturedId = attrs.getAttributeValue(null, "featured");
152 mPriority = attrs.getAttributeIntValue(null, "priority", -1);
153 mThumbResId = attrs.getAttributeResourceValue(null, "thumbnail", 0);
154 }
155
156 /**
157 * Add the given {@link WallpaperInfo} to this category
158 * @return this for chaining
159 */
160 public Builder addWallpaper(WallpaperInfo info) {
161 mWallpapers.add(info);
162 return this;
163 }
164
165 /**
166 * If no priority was parsed from the XML attributes for this category, set the priority to
167 * the given value.
168 * @return this for chaining
169 */
170 public Builder setPriorityIfEmpty(int priority) {
171 if (mPriority < 0) {
172 mPriority = priority;
173 }
174 return this;
175 }
176
177 /**
178 * Build a {@link WallpaperCategory} with this builder's information
179 */
180 public WallpaperCategory build() {
181 if (mThumbResId != 0) {
182 return new WallpaperCategory(mTitle, mId,
183 new ResourceAsset(mPartnerRes, mThumbResId), mWallpapers, mPriority);
184 } else {
185 int featuredIndex = 0;
186 for (int i = 0; i < mWallpapers.size(); i++) {
187 if (mWallpapers.get(i).getWallpaperId().equals(mFeaturedId)) {
188 featuredIndex = i;
189 break;
190 }
191 }
192 return new WallpaperCategory(mTitle, mId, featuredIndex, mWallpapers, mPriority);
193 }
194 }
195
196 /**
197 * Build a {@link PlaceholderCategory} with this builder's information.
198 */
199 public Category buildPlaceholder() {
200 return new PlaceholderCategory(mTitle, mId, mPriority);
201 }
202
203 public String getId() {
204 return mId;
205 }
206 }
Jon Miranda16ea1b12017-12-12 14:52:48 -0800207}