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         super(in);
69         mPackageName = in.readString();
70         mThumbRes = in.readInt();
71         mFullRes = in.readInt();
72     }
73 
74     /**
75      * Returns a list of wallpapers bundled with the app described by the given appInfo.
76      *
77      * @param context
78      * @param appInfo   The ApplicationInfo for the app hosting the wallpapers.
79      * @param listResId The ID of the list resource for the list of wallpapers.
80      * @return The wallpapers.
81      */
getAll(Context context, ApplicationInfo appInfo, int listResId)82     public static List<WallpaperInfo> getAll(Context context, ApplicationInfo appInfo,
83                                              int listResId) {
84         ArrayList<WallpaperInfo> wallpapers = new ArrayList<>();
85 
86         try {
87             Resources resources = context.getPackageManager().getResourcesForApplication(appInfo);
88 
89             final String[] wallpaperResNames = resources.getStringArray(listResId);
90             for (String name : wallpaperResNames) {
91                 final int fullRes = resources.getIdentifier(name, DRAWABLE_DEF_TYPE, appInfo.packageName);
92                 final int thumbRes = resources.getIdentifier(
93                         name + "_small", DRAWABLE_DEF_TYPE, appInfo.packageName);
94                 if (fullRes != 0 && thumbRes != 0) {
95                     WallpaperInfo wallpaperInfo = new AppResourceWallpaperInfo(
96                             appInfo.packageName, thumbRes, fullRes);
97                     wallpapers.add(wallpaperInfo);
98                 }
99             }
100 
101         } catch (PackageManager.NameNotFoundException e) {
102             Log.e(TAG, "Hosting app package not found");
103         }
104 
105         return wallpapers;
106     }
107 
getPackageResources(Context ctx)108     private Resources getPackageResources(Context ctx) {
109         if (mResources != null) {
110             return mResources;
111         }
112 
113         try {
114             mResources = ctx.getPackageManager().getResourcesForApplication(mPackageName);
115         } catch (PackageManager.NameNotFoundException e) {
116             Log.e(TAG, "Could not get app resources");
117         }
118         return mResources;
119     }
120 
121     @Override
getAsset(Context context)122     public Asset getAsset(Context context) {
123         if (mAsset == null) {
124             Resources res = getPackageResources(context);
125             mAsset = new ResourceAsset(res, mFullRes);
126         }
127         return mAsset;
128     }
129 
130     @Override
getThumbAsset(Context context)131     public Asset getThumbAsset(Context context) {
132         if (mThumbAsset == null) {
133             Resources res = getPackageResources(context);
134             mThumbAsset = new ResourceAsset(res, mThumbRes);
135         }
136         return mThumbAsset;
137     }
138 
139     @Override
getAttributions(Context context)140     public List<String> getAttributions(Context context) {
141         return Arrays.asList(context.getResources().getString(R.string.on_device_wallpaper_title));
142     }
143 
144     @Override
getCollectionId(Context context)145     public String getCollectionId(Context context) {
146         return context.getString(R.string.on_device_wallpaper_collection_id);
147     }
148 
149     @Override
showPreview(Activity srcActivity, InlinePreviewIntentFactory factory, int requestCode, boolean isAssetIdPresent)150     public void showPreview(Activity srcActivity, InlinePreviewIntentFactory factory,
151                             int requestCode, boolean isAssetIdPresent) {
152         srcActivity.startActivityForResult(factory.newIntent(srcActivity, this,
153                 isAssetIdPresent), requestCode);
154     }
155 
156     @Override
157     @BackupPermission
getBackupPermission()158     public int getBackupPermission() {
159         return BACKUP_NOT_ALLOWED;
160     }
161 
162     @Override
equals(Object object)163     public boolean equals(Object object) {
164         if (object == this) {
165             return true;
166         }
167         if (object instanceof AppResourceWallpaperInfo) {
168             return mPackageName.equals(((AppResourceWallpaperInfo) object).mPackageName)
169                     && mThumbRes == ((AppResourceWallpaperInfo) object).mThumbRes
170                     && mFullRes == ((AppResourceWallpaperInfo) object).mFullRes;
171         }
172         return false;
173     }
174 
175     @Override
hashCode()176     public int hashCode() {
177         int result = 17;
178         result = 31 * result + mPackageName.hashCode();
179         result = 31 * result + mThumbRes;
180         result = 31 * result + mFullRes;
181         return result;
182     }
183 
184     @Override
writeToParcel(Parcel parcel, int i)185     public void writeToParcel(Parcel parcel, int i) {
186         super.writeToParcel(parcel, i);
187         parcel.writeString(mPackageName);
188         parcel.writeInt(mThumbRes);
189         parcel.writeInt(mFullRes);
190     }
191 }
192