• 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.content.res.Resources;
21 import android.util.AttributeSet;
22 
23 import androidx.annotation.IdRes;
24 import androidx.annotation.Nullable;
25 import androidx.annotation.StringRes;
26 
27 import com.android.wallpaper.asset.Asset;
28 import com.android.wallpaper.asset.ResourceAsset;
29 
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.List;
33 
34 /**
35  * Default category for a collection of WallpaperInfo objects.
36  */
37 public class WallpaperCategory extends Category {
38 
39     public static final String TAG_NAME = "category";
40 
41     protected final Object mWallpapersLock;
42     private final List<WallpaperInfo> mWallpapers;
43     private Asset mThumbAsset;
44     private int mFeaturedThumbnailIndex;
45 
WallpaperCategory(String title, String collectionId, List<WallpaperInfo> wallpapers, int priority, boolean isDownloadable, String downloadComponent)46     public WallpaperCategory(String title, String collectionId, List<WallpaperInfo> wallpapers,
47                              int priority, boolean isDownloadable, String downloadComponent) {
48         this(title, collectionId, 0, wallpapers, priority, isDownloadable, downloadComponent);
49     }
50 
WallpaperCategory(String title, String collectionId, List<WallpaperInfo> wallpapers, int priority)51     public WallpaperCategory(String title, String collectionId, List<WallpaperInfo> wallpapers,
52             int priority) {
53         this(title, collectionId, 0, wallpapers, priority, false, null);
54     }
55 
WallpaperCategory(String title, String collectionId, int featuredThumbnailIndex, List<WallpaperInfo> wallpapers, int priority)56     public WallpaperCategory(String title, String collectionId, int featuredThumbnailIndex,
57                              List<WallpaperInfo> wallpapers, int priority) {
58         this(title, collectionId, featuredThumbnailIndex, wallpapers, priority, false, null);
59     }
60 
WallpaperCategory(String title, String collectionId, int featuredThumbnailIndex, List<WallpaperInfo> wallpapers, int priority, boolean isDownloadable, String downloadComponent)61     public WallpaperCategory(String title, String collectionId, int featuredThumbnailIndex,
62             List<WallpaperInfo> wallpapers, int priority, boolean isDownloadable,
63             String downloadComponent) {
64         super(title, collectionId, priority, isDownloadable, downloadComponent);
65         mWallpapers = wallpapers;
66         mWallpapersLock = new Object();
67         mFeaturedThumbnailIndex = featuredThumbnailIndex;
68     }
69 
WallpaperCategory(String title, String collectionId, Asset thumbAsset, List<WallpaperInfo> wallpapers, int priority)70     public WallpaperCategory(String title, String collectionId, Asset thumbAsset,
71             List<WallpaperInfo> wallpapers, int priority) {
72         super(title, collectionId, priority, false, null);
73         mWallpapers = wallpapers;
74         mWallpapersLock = new Object();
75         mThumbAsset = thumbAsset;
76     }
77 
78     /**
79      * Fetches wallpapers for this category and passes them to the receiver. Subclasses may use a
80      * context to fetch wallpaper info.
81      */
fetchWallpapers(Context unused, WallpaperReceiver receiver, boolean forceReload)82     public void fetchWallpapers(Context unused, WallpaperReceiver receiver, boolean forceReload) {
83         // Perform a shallow clone so as not to pass the reference to the list along to clients.
84         receiver.onWallpapersReceived(new ArrayList<>(mWallpapers));
85     }
86 
87     @Override
show(Activity srcActivity, int requestCode)88     public void show(Activity srcActivity, int requestCode) {
89         // No op
90     }
91 
getWallpapers()92     public List<WallpaperInfo> getWallpapers() {
93         return mWallpapers;
94     }
95 
getThumbAsset()96     public Asset getThumbAsset() {
97         return mThumbAsset;
98     }
99 
getFeaturedThumbnailIndex()100     public int getFeaturedThumbnailIndex() {
101         return mFeaturedThumbnailIndex;
102     }
103     @Override
isEnumerable()104     public boolean isEnumerable() {
105         return true;
106     }
107 
108     @Override
isSingleWallpaperCategory()109     public boolean isSingleWallpaperCategory() {
110         return mWallpapers != null && mWallpapers.size() == 1;
111     }
112 
113     @Nullable
114     @Override
getSingleWallpaper()115     public WallpaperInfo getSingleWallpaper() {
116         return isSingleWallpaperCategory() ? mWallpapers.get(0) : null;
117     }
118 
119     /**
120      * Returns the mutable list of wallpapers backed by this WallpaperCategory. All reads and writes
121      * on the returned list must be synchronized with {@code mWallpapersLock}.
122      */
getMutableWallpapers()123     protected List<WallpaperInfo> getMutableWallpapers() {
124         return mWallpapers;
125     }
126 
127     /**
128      * Returns an unmodifiable view the list of wallpapers in this WallpaperCategory.
129      */
getUnmodifiableWallpapers()130     public List<WallpaperInfo> getUnmodifiableWallpapers() {
131         return Collections.unmodifiableList(mWallpapers);
132     }
133 
134     @Override
getThumbnail(Context context)135     public Asset getThumbnail(Context context) {
136         synchronized (mWallpapersLock) {
137             if (mThumbAsset == null && mWallpapers.size() > 0) {
138                 mThumbAsset = mWallpapers.get(mFeaturedThumbnailIndex).getThumbAsset(context);
139             }
140         }
141         return mThumbAsset;
142     }
143 
144     @Override
supportsThirdParty()145     public boolean supportsThirdParty() {
146         return false;
147     }
148 
149     @Override
containsThirdParty(String packageName)150     public boolean containsThirdParty(String packageName) {
151         return false;
152     }
153 
154     /**
155      * Builder used to construct a {@link WallpaperCategory} object from an XML's
156      * {@link AttributeSet}.
157      */
158     public static class Builder {
159         private final List<WallpaperInfo> mWallpapers = new ArrayList<>();
160         private final Resources mPartnerRes;
161         private String mId;
162         private String mTitle;
163         private int mPriority;
164         private String mFeaturedId;
165         @IdRes private int mThumbResId;
166 
Builder(Resources partnerRes, AttributeSet attrs)167         public Builder(Resources partnerRes, AttributeSet attrs) {
168             mPartnerRes = partnerRes;
169             mId = attrs.getAttributeValue(null, "id");
170             @StringRes int titleResId = attrs.getAttributeResourceValue(null, "title", 0);
171             mTitle = titleResId != 0 ? mPartnerRes.getString(titleResId) : "";
172             mFeaturedId = attrs.getAttributeValue(null, "featured");
173             mPriority = attrs.getAttributeIntValue(null, "priority", -1);
174             mThumbResId = attrs.getAttributeResourceValue(null, "thumbnail", 0);
175         }
176 
177         /**
178          * Add the given {@link WallpaperInfo} to this category
179          * @return this for chaining
180          */
addWallpaper(WallpaperInfo info)181         public Builder addWallpaper(WallpaperInfo info) {
182             mWallpapers.add(info);
183             return this;
184         }
185 
186         /**
187          * Adds the given list of {@link WallpaperInfo} to this category
188          * @return this for chaining
189          */
addWallpapers(List<WallpaperInfo> wallpapers)190         public Builder addWallpapers(List<WallpaperInfo> wallpapers) {
191             mWallpapers.addAll(wallpapers);
192             return this;
193         }
194 
195         /**
196          * If no priority was parsed from the XML attributes for this category, set the priority to
197          * the given value.
198          * @return this for chaining
199          */
setPriorityIfEmpty(int priority)200         public Builder setPriorityIfEmpty(int priority) {
201             if (mPriority < 0) {
202                 mPriority = priority;
203             }
204             return this;
205         }
206 
207         /**
208          * Build a {@link WallpaperCategory} with this builder's information
209          */
build()210         public WallpaperCategory build() {
211             if (mThumbResId != 0) {
212                 return new WallpaperCategory(mTitle, mId,
213                         new ResourceAsset(mPartnerRes, mThumbResId, true), mWallpapers, mPriority);
214             } else {
215                 int featuredIndex = 0;
216                 for (int i = 0; i < mWallpapers.size(); i++) {
217                     if (mWallpapers.get(i).getWallpaperId().equals(mFeaturedId)) {
218                         featuredIndex = i;
219                         break;
220                     }
221                 }
222                 return new WallpaperCategory(mTitle, mId, featuredIndex, mWallpapers, mPriority);
223             }
224         }
225 
226         /**
227          * Build a {@link PlaceholderCategory} with this builder's information.
228          */
buildPlaceholder()229         public Category buildPlaceholder() {
230             return new PlaceholderCategory(mTitle, mId, mPriority);
231         }
232 
getId()233         public String getId() {
234             return mId;
235         }
236     }
237 }
238