1 /* 2 * Copyright (C) 2019 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.customization.model.theme; 17 18 import android.app.Activity; 19 import android.content.ActivityNotFoundException; 20 import android.content.Context; 21 import android.content.pm.PackageManager; 22 import android.content.res.Resources; 23 import android.os.Parcel; 24 import android.util.Log; 25 26 import androidx.annotation.DrawableRes; 27 import androidx.annotation.StringRes; 28 29 import com.android.wallpaper.asset.Asset; 30 import com.android.wallpaper.asset.ResourceAsset; 31 import com.android.wallpaper.model.InlinePreviewIntentFactory; 32 import com.android.wallpaper.model.WallpaperInfo; 33 34 import java.util.ArrayList; 35 import java.util.List; 36 37 /** 38 * Represents a wallpaper coming from the resources of the theme bundle container APK. 39 */ 40 public class ThemeBundledWallpaperInfo extends WallpaperInfo { 41 public static final Creator<ThemeBundledWallpaperInfo> CREATOR = 42 new Creator<ThemeBundledWallpaperInfo>() { 43 @Override 44 public ThemeBundledWallpaperInfo createFromParcel(Parcel in) { 45 return new ThemeBundledWallpaperInfo(in); 46 } 47 48 @Override 49 public ThemeBundledWallpaperInfo[] newArray(int size) { 50 return new ThemeBundledWallpaperInfo[size]; 51 } 52 }; 53 54 private static final String TAG = "ThemeBundledWallpaperInfo"; 55 56 private final String mPackageName; 57 private final String mResName; 58 private final String mCollectionId; 59 @DrawableRes private final int mDrawableResId; 60 @StringRes private final int mTitleResId; 61 @StringRes private final int mAttributionResId; 62 @StringRes private final int mActionUrlResId; 63 private List<String> mAttributions; 64 private String mActionUrl; 65 private Resources mResources; 66 private Asset mAsset; 67 68 /** 69 * Constructs a new theme-bundled static wallpaper model object. 70 * 71 * @param drawableResId Resource ID of the raw wallpaper image. 72 * @param resName The unique name of the wallpaper resource, e.g. "z_wp001". 73 * @param themeName Unique name of the collection this wallpaper belongs in; used for logging. 74 * @param titleResId Resource ID of the string for the title attribution. 75 * @param attributionResId Resource ID of the string for the first subtitle attribution. 76 */ ThemeBundledWallpaperInfo(String packageName, String resName, String themeName, int drawableResId, int titleResId, int attributionResId, int actionUrlResId)77 public ThemeBundledWallpaperInfo(String packageName, String resName, String themeName, 78 int drawableResId, int titleResId, int attributionResId, int actionUrlResId) { 79 mPackageName = packageName; 80 mResName = resName; 81 mCollectionId = themeName; 82 mDrawableResId = drawableResId; 83 mTitleResId = titleResId; 84 mAttributionResId = attributionResId; 85 mActionUrlResId = actionUrlResId; 86 } 87 ThemeBundledWallpaperInfo(Parcel in)88 private ThemeBundledWallpaperInfo(Parcel in) { 89 super(in); 90 mPackageName = in.readString(); 91 mResName = in.readString(); 92 mCollectionId = in.readString(); 93 mDrawableResId = in.readInt(); 94 mTitleResId = in.readInt(); 95 mAttributionResId = in.readInt(); 96 mActionUrlResId = in.readInt(); 97 } 98 99 @Override getAttributions(Context context)100 public List<String> getAttributions(Context context) { 101 if (mAttributions == null) { 102 Resources res = getPackageResources(context); 103 mAttributions = new ArrayList<>(); 104 if (mTitleResId != 0) { 105 mAttributions.add(res.getString(mTitleResId)); 106 } 107 if (mAttributionResId != 0) { 108 mAttributions.add(res.getString(mAttributionResId)); 109 } 110 } 111 112 return mAttributions; 113 } 114 115 @Override getActionUrl(Context context)116 public String getActionUrl(Context context) { 117 if (mActionUrl == null && mActionUrlResId != 0) { 118 mActionUrl = getPackageResources(context).getString(mActionUrlResId); 119 } 120 return mActionUrl; 121 } 122 123 @Override getAsset(Context context)124 public Asset getAsset(Context context) { 125 if (mAsset == null) { 126 Resources res = getPackageResources(context); 127 mAsset = new ResourceAsset(res, mDrawableResId); 128 } 129 130 return mAsset; 131 } 132 133 @Override getThumbAsset(Context context)134 public Asset getThumbAsset(Context context) { 135 return getAsset(context); 136 } 137 138 @Override showPreview(Activity srcActivity, InlinePreviewIntentFactory factory, int requestCode)139 public void showPreview(Activity srcActivity, InlinePreviewIntentFactory factory, 140 int requestCode) { 141 try { 142 srcActivity.startActivityForResult(factory.newIntent(srcActivity, this), requestCode); 143 } catch (ActivityNotFoundException |SecurityException e) { 144 Log.e(TAG, "App isn't installed or ThemePicker doesn't have permission to launch", e); 145 } 146 147 } 148 149 @Override getCollectionId(Context unused)150 public String getCollectionId(Context unused) { 151 return mCollectionId; 152 } 153 154 @Override getWallpaperId()155 public String getWallpaperId() { 156 return mResName; 157 } 158 getResName()159 public String getResName() { 160 return mResName; 161 } 162 163 /** 164 * Returns the {@link Resources} instance for the theme bundles stub APK. 165 */ getPackageResources(Context context)166 private Resources getPackageResources(Context context) { 167 if (mResources != null) { 168 return mResources; 169 } 170 171 try { 172 mResources = context.getPackageManager().getResourcesForApplication(mPackageName); 173 } catch (PackageManager.NameNotFoundException e) { 174 Log.e(TAG, "Could not get app resources for " + mPackageName); 175 } 176 return mResources; 177 } 178 179 @Override writeToParcel(Parcel dest, int flags)180 public void writeToParcel(Parcel dest, int flags) { 181 super.writeToParcel(dest, flags); 182 dest.writeString(mPackageName); 183 dest.writeString(mResName); 184 dest.writeString(mCollectionId); 185 dest.writeInt(mDrawableResId); 186 dest.writeInt(mTitleResId); 187 dest.writeInt(mAttributionResId); 188 dest.writeInt(mActionUrlResId); 189 } 190 } 191