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.graphics.Bitmap; 21 import android.graphics.BitmapFactory; 22 import android.graphics.drawable.BitmapDrawable; 23 import android.graphics.drawable.Drawable; 24 import android.os.Parcel; 25 26 import com.android.wallpaper.R; 27 import com.android.wallpaper.asset.Asset; 28 import com.android.wallpaper.asset.FileAsset; 29 import com.android.wallpaper.module.InjectorProvider; 30 import com.android.wallpaper.module.PartnerProvider; 31 32 import java.io.File; 33 import java.util.ArrayList; 34 import java.util.Arrays; 35 import java.util.List; 36 37 /** 38 * Represents a wallpaper from the "partner customization" APK installed on the system. 39 */ 40 public class LegacyPartnerWallpaperInfo extends WallpaperInfo { 41 public static final Creator<LegacyPartnerWallpaperInfo> CREATOR = 42 new Creator<LegacyPartnerWallpaperInfo>() { 43 @Override 44 public LegacyPartnerWallpaperInfo createFromParcel(Parcel in) { 45 return new LegacyPartnerWallpaperInfo(in); 46 } 47 48 @Override 49 public LegacyPartnerWallpaperInfo[] newArray(int size) { 50 return new LegacyPartnerWallpaperInfo[size]; 51 } 52 }; 53 private String mThumbName; 54 private String mFullName; 55 private File mSystemLegacyDir; 56 private boolean mFetchedSystemLegacyDir; 57 private FileAsset mAsset; 58 private FileAsset mThumbAsset; 59 LegacyPartnerWallpaperInfo(String thumbName, String fullName)60 public LegacyPartnerWallpaperInfo(String thumbName, String fullName) { 61 mThumbName = thumbName; 62 mFullName = fullName; 63 } 64 LegacyPartnerWallpaperInfo(Parcel in)65 private LegacyPartnerWallpaperInfo(Parcel in) { 66 super(in); 67 mThumbName = in.readString(); 68 mFullName = in.readString(); 69 } 70 71 /** 72 * @param ctx 73 * @return All legacy partner wallpapers found on the device. 74 */ getAll(Context ctx)75 public static List<WallpaperInfo> getAll(Context ctx) { 76 PartnerProvider partnerProvider = InjectorProvider.getInjector().getPartnerProvider(ctx); 77 78 List<WallpaperInfo> wallpaperInfos = new ArrayList<>(); 79 80 // Add system wallpapers from the legacy wallpaper directory, if present. 81 File systemDir = partnerProvider.getLegacyWallpaperDirectory(); 82 83 // None found, so return empty list. 84 if (systemDir == null || !systemDir.isDirectory()) { 85 return wallpaperInfos; 86 } 87 88 for (File file : systemDir.listFiles()) { 89 if (!file.isFile()) { 90 continue; 91 } 92 String fullName = file.getName(); 93 String name = file.getName(); 94 int dotPos = name.lastIndexOf('.'); 95 String extension = ""; 96 if (dotPos > -1) { 97 extension = name.substring(dotPos); 98 name = name.substring(0, dotPos); 99 } 100 101 if (name.endsWith("_small")) { 102 // Skip thumbnails as they are handled when we iterate over the full size counterpart. 103 continue; 104 } 105 106 String thumbName = name + "_small" + extension; 107 wallpaperInfos.add(new LegacyPartnerWallpaperInfo(thumbName, fullName)); 108 } 109 110 return wallpaperInfos; 111 } 112 113 /** 114 * Gets (and caches) the system legacy directory. May return null if no such directory is found 115 * (which is the case for newer devices). 116 */ getSystemLegacyDir(Context context)117 private File getSystemLegacyDir(Context context) { 118 if (!mFetchedSystemLegacyDir) { 119 PartnerProvider partnerProvider = InjectorProvider.getInjector().getPartnerProvider(context); 120 mSystemLegacyDir = partnerProvider.getLegacyWallpaperDirectory(); 121 mFetchedSystemLegacyDir = true; 122 } 123 124 return mSystemLegacyDir; 125 } 126 getThumbnail(Context context)127 public Drawable getThumbnail(Context context) { 128 final File systemDir = getSystemLegacyDir(context); 129 if (systemDir == null) { 130 return null; 131 } 132 133 File thumbnail = new File(systemDir, mThumbName); 134 Bitmap thumbBitmap = BitmapFactory.decodeFile(thumbnail.getAbsolutePath()); 135 return new BitmapDrawable(context.getResources(), thumbBitmap); 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 getAsset(Context context)144 public Asset getAsset(Context context) { 145 if (mAsset == null) { 146 final File systemDir = getSystemLegacyDir(context); 147 File fullSizeImage = (systemDir == null) ? null : new File(systemDir, mFullName); 148 mAsset = new FileAsset(fullSizeImage); 149 } 150 return mAsset; 151 } 152 153 @Override getThumbAsset(Context context)154 public Asset getThumbAsset(Context context) { 155 if (mThumbAsset == null) { 156 final File systemDir = getSystemLegacyDir(context); 157 File thumbnail = (systemDir == null) ? null : new File(systemDir, mThumbName); 158 mThumbAsset = new FileAsset(thumbnail); 159 } 160 return mThumbAsset; 161 } 162 163 @Override getCollectionId(Context context)164 public String getCollectionId(Context context) { 165 return context.getString(R.string.on_device_wallpaper_collection_id); 166 } 167 168 @Override showPreview(Activity srcActivity, InlinePreviewIntentFactory factory, int requestCode)169 public void showPreview(Activity srcActivity, InlinePreviewIntentFactory factory, 170 int requestCode) { 171 srcActivity.startActivityForResult(factory.newIntent(srcActivity, this), requestCode); 172 } 173 174 @Override 175 @BackupPermission getBackupPermission()176 public int getBackupPermission() { 177 return BACKUP_NOT_ALLOWED; 178 } 179 180 @Override writeToParcel(Parcel parcel, int i)181 public void writeToParcel(Parcel parcel, int i) { 182 super.writeToParcel(parcel, i); 183 parcel.writeString(mThumbName); 184 parcel.writeString(mFullName); 185 } 186 187 } 188