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.pm.ApplicationInfo;
21 import android.content.pm.PackageManager;
22 import android.content.res.Resources;
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 import android.util.Log;
26 
27 import com.android.wallpaper.R;
28 import com.android.wallpaper.asset.Asset;
29 import com.android.wallpaper.asset.ResourceAsset;
30 
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.List;
34 
35 /**
36  * Represents a wallpaper coming from the resources of the host app for the wallpaper picker.
37  */
38 public class AppResourceWallpaperInfo extends WallpaperInfo {
39     public static final Parcelable.Creator<AppResourceWallpaperInfo> CREATOR =
40             new Parcelable.Creator<AppResourceWallpaperInfo>() {
41                 @Override
42                 public AppResourceWallpaperInfo createFromParcel(Parcel in) {
43                     return new AppResourceWallpaperInfo(in);
44                 }
45 
46                 @Override
47                 public AppResourceWallpaperInfo[] newArray(int size) {
48                     return new AppResourceWallpaperInfo[size];
49                 }
50             };
51     private static final String TAG = "AppResource";
52     private static final String DRAWABLE_DEF_TYPE = "drawable";
53     private int mThumbRes;
54     private int mFullRes;
55     private String mPackageName;
56     private Resources mResources;
57     private ResourceAsset mAsset;
58     private ResourceAsset mThumbAsset;
59 
AppResourceWallpaperInfo(String packageName, int thumbRes, int fullRes)60     public AppResourceWallpaperInfo(String packageName, int thumbRes, int fullRes) {
61         super();
62         mPackageName = packageName;
63         mThumbRes = thumbRes;
64         mFullRes = fullRes;
65     }
66 
AppResourceWallpaperInfo(Parcel in)67     private AppResourceWallpaperInfo(Parcel in) {
68         mPackageName = in.readString();
69         mThumbRes = in.readInt();
70         mFullRes = in.readInt();
71     }
72 
73     /**
74      * Returns a list of wallpapers bundled with the app described by the given appInfo.
75      *
76      * @param context
77      * @param appInfo   The ApplicationInfo for the app hosting the wallpapers.
78      * @param listResId The ID of the list resource for the list of wallpapers.
79      * @return The wallpapers.
80      */
getAll(Context context, ApplicationInfo appInfo, int listResId)81     public static List<WallpaperInfo> getAll(Context context, ApplicationInfo appInfo,
82                                              int listResId) {
83         ArrayList<WallpaperInfo> wallpapers = new ArrayList<>();
84 
85         try {
86             Resources resources = context.getPackageManager().getResourcesForApplication(appInfo);
87 
88             final String[] wallpaperResNames = resources.getStringArray(listResId);
89             for (String name : wallpaperResNames) {
90                 final int fullRes = resources.getIdentifier(name, DRAWABLE_DEF_TYPE, appInfo.packageName);
91                 final int thumbRes = resources.getIdentifier(
92                         name + "_small", DRAWABLE_DEF_TYPE, appInfo.packageName);
93                 if (fullRes != 0 && thumbRes != 0) {
94                     WallpaperInfo wallpaperInfo = new AppResourceWallpaperInfo(
95                             appInfo.packageName, thumbRes, fullRes);
96                     wallpapers.add(wallpaperInfo);
97                 }
98             }
99 
100         } catch (PackageManager.NameNotFoundException e) {
101             Log.e(TAG, "Hosting app package not found");
102         }
103 
104         return wallpapers;
105     }
106 
getPackageResources(Context ctx)107     private Resources getPackageResources(Context ctx) {
108         if (mResources != null) {
109             return mResources;
110         }
111 
112         try {
113             mResources = ctx.getPackageManager().getResourcesForApplication(mPackageName);
114         } catch (PackageManager.NameNotFoundException e) {
115             Log.e(TAG, "Could not get app resources");
116         }
117         return mResources;
118     }
119 
120     @Override
getAsset(Context context)121     public Asset getAsset(Context context) {
122         if (mAsset == null) {
123             Resources res = getPackageResources(context);
124             mAsset = new ResourceAsset(res, mFullRes);
125         }
126         return mAsset;
127     }
128 
129     @Override
getThumbAsset(Context context)130     public Asset getThumbAsset(Context context) {
131         if (mThumbAsset == null) {
132             Resources res = getPackageResources(context);
133             mThumbAsset = new ResourceAsset(res, mThumbRes);
134         }
135         return mThumbAsset;
136     }
137 
138     @Override
getAttributions(Context context)139     public List<String> getAttributions(Context context) {
140         return Arrays.asList(context.getResources().getString(R.string.on_device_wallpaper_title));
141     }
142 
143     @Override
getCollectionId(Context context)144     public String getCollectionId(Context context) {
145         return context.getString(R.string.on_device_wallpaper_collection_id);
146     }
147 
148     @Override
showPreview(Activity srcActivity, InlinePreviewIntentFactory factory, int requestCode)149     public void showPreview(Activity srcActivity, InlinePreviewIntentFactory factory,
150                             int requestCode) {
151         srcActivity.startActivityForResult(factory.newIntent(srcActivity, this), requestCode);
152     }
153 
154     @Override
155     @BackupPermission
getBackupPermission()156     public int getBackupPermission() {
157         return BACKUP_NOT_ALLOWED;
158     }
159 
160     @Override
equals(Object object)161     public boolean equals(Object object) {
162         if (object == this) {
163             return true;
164         }
165         if (object instanceof AppResourceWallpaperInfo) {
166             return mPackageName.equals(((AppResourceWallpaperInfo) object).mPackageName)
167                     && mThumbRes == ((AppResourceWallpaperInfo) object).mThumbRes
168                     && mFullRes == ((AppResourceWallpaperInfo) object).mFullRes;
169         }
170         return false;
171     }
172 
173     @Override
hashCode()174     public int hashCode() {
175         int result = 17;
176         result = 31 * result + mPackageName.hashCode();
177         result = 31 * result + mThumbRes;
178         result = 31 * result + mFullRes;
179         return result;
180     }
181 
182     @Override
writeToParcel(Parcel parcel, int i)183     public void writeToParcel(Parcel parcel, int i) {
184         parcel.writeString(mPackageName);
185         parcel.writeInt(mThumbRes);
186         parcel.writeInt(mFullRes);
187     }
188 }
189