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.os.Parcel;
22 import android.util.Log;
23 
24 import com.android.wallpaper.R;
25 import com.android.wallpaper.asset.Asset;
26 import com.android.wallpaper.asset.ResourceAsset;
27 import com.android.wallpaper.module.InjectorProvider;
28 import com.android.wallpaper.module.PartnerProvider;
29 
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.List;
33 
34 /**
35  * Represents a wallpaper from the "partner customization" APK installed on the system.
36  */
37 public class PartnerWallpaperInfo extends DefaultWallpaperInfo {
38     public static final Creator<PartnerWallpaperInfo> CREATOR =
39             new Creator<PartnerWallpaperInfo>() {
40                 @Override
41                 public PartnerWallpaperInfo createFromParcel(Parcel in) {
42                     return new PartnerWallpaperInfo(in);
43                 }
44 
45                 @Override
46                 public PartnerWallpaperInfo[] newArray(int size) {
47                     return new PartnerWallpaperInfo[size];
48                 }
49             };
50     private int mThumbRes;
51     private int mFullRes;
52     private ResourceAsset mAsset;
53     private ResourceAsset mThumbAsset;
54     private Resources mPartnerResources;
55     private boolean mFetchedPartnerResources;
56 
PartnerWallpaperInfo(int thumbRes, int fullRes)57     public PartnerWallpaperInfo(int thumbRes, int fullRes) {
58         mThumbRes = thumbRes;
59         mFullRes = fullRes;
60     }
61 
PartnerWallpaperInfo(Parcel in)62     private PartnerWallpaperInfo(Parcel in) {
63         super(in);
64         mThumbRes = in.readInt();
65         mFullRes = in.readInt();
66     }
67 
68     /**
69      * @param ctx
70      * @return All partner wallpapers found on the device.
71      */
getAll(Context ctx)72     public static List<WallpaperInfo> getAll(Context ctx) {
73         PartnerProvider partnerProvider = InjectorProvider.getInjector().getPartnerProvider(ctx);
74 
75         List<WallpaperInfo> wallpaperInfos = new ArrayList<>();
76 
77         final Resources partnerRes = partnerProvider.getResources();
78         final String packageName = partnerProvider.getPackageName();
79         if (partnerRes == null) {
80             return wallpaperInfos;
81         }
82 
83         final int resId = partnerRes.getIdentifier(PartnerProvider.LEGACY_WALLPAPER_RES_ID, "array",
84                 packageName);
85         // Certain partner configurations don't have wallpapers provided, so need to check; return
86         // early if they are missing.
87         if (resId == 0) {
88             return wallpaperInfos;
89         }
90 
91         final String[] extras = partnerRes.getStringArray(resId);
92         for (String extra : extras) {
93             int wpResId = partnerRes.getIdentifier(extra, "drawable", packageName);
94             if (wpResId != 0) {
95                 final int thumbRes = partnerRes.getIdentifier(extra + "_small", "drawable", packageName);
96 
97                 if (thumbRes != 0) {
98                     final int fullRes = partnerRes.getIdentifier(extra, "drawable", packageName);
99                     WallpaperInfo wallpaperInfo = new PartnerWallpaperInfo(thumbRes, fullRes);
100                     wallpaperInfos.add(wallpaperInfo);
101                 }
102             } else {
103                 Log.e("PartnerWallpaperInfo", "Couldn't find wallpaper " + extra);
104             }
105         }
106 
107         return wallpaperInfos;
108     }
109 
getPartnerResources(Context context)110     private Resources getPartnerResources(Context context) {
111         if (!mFetchedPartnerResources) {
112             PartnerProvider partnerProvider = InjectorProvider.getInjector().getPartnerProvider(context);
113             mPartnerResources = partnerProvider.getResources();
114             mFetchedPartnerResources = true;
115         }
116 
117         return mPartnerResources;
118     }
119 
120     @Override
getAttributions(Context context)121     public List<String> getAttributions(Context context) {
122         return Arrays.asList(context.getResources().getString(R.string.on_device_wallpaper_title));
123     }
124 
125     @Override
getAsset(Context context)126     public Asset getAsset(Context context) {
127         if (mAsset == null) {
128             Resources partnerRes = getPartnerResources(context);
129             mAsset = new ResourceAsset(partnerRes, mFullRes);
130         }
131         return mAsset;
132     }
133 
134     @Override
getThumbAsset(Context context)135     public Asset getThumbAsset(Context context) {
136         if (mThumbAsset == null) {
137             Resources partnerRes = getPartnerResources(context);
138             mThumbAsset = new ResourceAsset(partnerRes, mThumbRes);
139         }
140         return mThumbAsset;
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
getWallpaperId()149     public String getWallpaperId() {
150         return "" + mFullRes;
151     }
152 
153     @Override
showPreview(Activity srcActivity, InlinePreviewIntentFactory factory, int requestCode, boolean isAssetIdPresent)154     public void showPreview(Activity srcActivity, InlinePreviewIntentFactory factory,
155                             int requestCode, boolean isAssetIdPresent) {
156         srcActivity.startActivityForResult(factory.newIntent(srcActivity, this,
157                 isAssetIdPresent), requestCode);
158     }
159 
160     @Override
161     @BackupPermission
getBackupPermission()162     public int getBackupPermission() {
163         return BACKUP_NOT_ALLOWED;
164     }
165 
166     @Override
writeToParcel(Parcel parcel, int i)167     public void writeToParcel(Parcel parcel, int i) {
168         super.writeToParcel(parcel, i);
169         parcel.writeInt(mThumbRes);
170         parcel.writeInt(mFullRes);
171     }
172 
173 }
174