Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 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; |
Santiago Etchebehere | a242999 | 2020-05-27 11:27:29 -0700 | [diff] [blame] | 20 | import android.content.res.Resources; |
| 21 | import android.util.AttributeSet; |
| 22 | |
| 23 | import androidx.annotation.IdRes; |
Santiago Etchebehere | 39c3134 | 2021-05-03 15:35:06 -0700 | [diff] [blame] | 24 | import androidx.annotation.Nullable; |
Santiago Etchebehere | a242999 | 2020-05-27 11:27:29 -0700 | [diff] [blame] | 25 | import androidx.annotation.StringRes; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 26 | |
| 27 | import com.android.wallpaper.asset.Asset; |
Santiago Etchebehere | a242999 | 2020-05-27 11:27:29 -0700 | [diff] [blame] | 28 | import com.android.wallpaper.asset.ResourceAsset; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 29 | |
| 30 | import java.util.ArrayList; |
Santiago Etchebehere | a242999 | 2020-05-27 11:27:29 -0700 | [diff] [blame] | 31 | import java.util.Collections; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 32 | import java.util.List; |
| 33 | |
| 34 | /** |
| 35 | * Default category for a collection of WallpaperInfo objects. |
| 36 | */ |
| 37 | public class WallpaperCategory extends Category { |
| 38 | |
Santiago Etchebehere | a242999 | 2020-05-27 11:27:29 -0700 | [diff] [blame] | 39 | public static final String TAG_NAME = "category"; |
| 40 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 41 | 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 Etchebehere | 1ee76a2 | 2018-05-15 15:02:24 -0700 | [diff] [blame] | 48 | this(title, collectionId, 0, wallpapers, priority); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 49 | } |
| 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 Etchebehere | a242999 | 2020-05-27 11:27:29 -0700 | [diff] [blame] | 59 | 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 Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 67 | /** |
| 68 | * Fetches wallpapers for this category and passes them to the receiver. Subclasses may use a |
| 69 | * context to fetch wallpaper info. |
| 70 | */ |
Santiago Etchebehere | 1ee76a2 | 2018-05-15 15:02:24 -0700 | [diff] [blame] | 71 | public void fetchWallpapers(Context unused, WallpaperReceiver receiver, boolean forceReload) { |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 72 | // 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 Etchebehere | 39c3134 | 2021-05-03 15:35:06 -0700 | [diff] [blame] | 87 | @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 Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 98 | /** |
| 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 Etchebehere | a242999 | 2020-05-27 11:27:29 -0700 | [diff] [blame] | 106 | /** |
| 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 Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 113 | @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 Etchebehere | 1ee76a2 | 2018-05-15 15:02:24 -0700 | [diff] [blame] | 122 | |
| 123 | @Override |
| 124 | public boolean supportsThirdParty() { |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public boolean containsThirdParty(String packageName) { |
| 130 | return false; |
| 131 | } |
Santiago Etchebehere | a242999 | 2020-05-27 11:27:29 -0700 | [diff] [blame] | 132 | |
| 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 Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 207 | } |