Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 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.net.Uri; |
| 21 | import android.os.Parcel; |
| 22 | import android.os.Parcelable; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 23 | import android.util.Log; |
| 24 | |
Clément Julliard | 6b7589c | 2019-06-26 12:35:06 -0700 | [diff] [blame] | 25 | import androidx.exifinterface.media.ExifInterface; |
| 26 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 27 | import com.android.wallpaper.R; |
| 28 | import com.android.wallpaper.asset.Asset; |
| 29 | import com.android.wallpaper.asset.ContentUriAsset; |
| 30 | |
| 31 | import java.text.ParseException; |
| 32 | import java.text.SimpleDateFormat; |
| 33 | import java.util.ArrayList; |
| 34 | import java.util.Arrays; |
| 35 | import java.util.Date; |
| 36 | import java.util.List; |
| 37 | |
| 38 | /** |
| 39 | * Represents a wallpaper image from the system's image picker. |
| 40 | */ |
| 41 | public class ImageWallpaperInfo extends WallpaperInfo { |
| 42 | public static final Parcelable.Creator<ImageWallpaperInfo> CREATOR = |
| 43 | new Parcelable.Creator<ImageWallpaperInfo>() { |
| 44 | @Override |
| 45 | public ImageWallpaperInfo createFromParcel(Parcel in) { |
| 46 | return new ImageWallpaperInfo(in); |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public ImageWallpaperInfo[] newArray(int size) { |
| 51 | return new ImageWallpaperInfo[size]; |
| 52 | } |
| 53 | }; |
| 54 | private static final String TAG = "ImageWallpaperInfo"; |
| 55 | // Desired EXIF tags in descending order of priority. |
| 56 | private static final String[] EXIF_TAGS = { |
| 57 | ExifInterface.TAG_IMAGE_DESCRIPTION, |
| 58 | ExifInterface.TAG_ARTIST, |
| 59 | ExifInterface.TAG_DATETIME_ORIGINAL, |
| 60 | ExifInterface.TAG_MODEL, |
| 61 | }; |
| 62 | private Uri mUri; |
| 63 | private ContentUriAsset mAsset; |
Clément Julliard | 6b7589c | 2019-06-26 12:35:06 -0700 | [diff] [blame] | 64 | private boolean mIsAssetUncached; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 65 | |
| 66 | public ImageWallpaperInfo(Uri uri) { |
| 67 | mUri = uri; |
| 68 | } |
| 69 | |
Clément Julliard | 6b7589c | 2019-06-26 12:35:06 -0700 | [diff] [blame] | 70 | public ImageWallpaperInfo(Uri uri, boolean uncachedAsset) { |
| 71 | mUri = uri; |
| 72 | mIsAssetUncached = uncachedAsset; |
| 73 | } |
| 74 | |
Santiago Etchebehere | 085712c | 2019-06-13 13:49:18 -0700 | [diff] [blame] | 75 | protected ImageWallpaperInfo(Parcel in) { |
Santiago Etchebehere | a5736dc | 2021-06-14 14:32:32 -0700 | [diff] [blame] | 76 | super(in); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 77 | mUri = Uri.parse(in.readString()); |
| 78 | } |
| 79 | |
Clément Julliard | ea1638d | 2018-05-21 19:15:17 -0700 | [diff] [blame] | 80 | @Override |
| 81 | public Uri getUri() { |
| 82 | return mUri; |
| 83 | } |
| 84 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 85 | /** |
| 86 | * Formats a localized date string based on the provided datetime string in EXIF datetime format. |
| 87 | * |
| 88 | * @param exifDateTime Datetime string in EXIF datetime format. |
| 89 | * @return Localized date string, or the original datetime string if it could not be parsed. |
| 90 | */ |
| 91 | private static String formatDate(String exifDateTime) { |
| 92 | try { |
| 93 | Date parsedDate = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss").parse(exifDateTime); |
| 94 | return SimpleDateFormat.getDateInstance().format(parsedDate); |
| 95 | } catch (ParseException e) { |
| 96 | Log.w(TAG, "Unable to parse image datetime", e); |
| 97 | return exifDateTime; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | private static List<String> getGenericAttributions(Context context) { |
| 102 | return Arrays.asList( |
| 103 | context.getResources().getString(R.string.my_photos_generic_wallpaper_title)); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public String getTitle(Context context) { |
| 108 | return null; |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public List<String> getAttributions(Context context) { |
| 113 | ContentUriAsset asset = (ContentUriAsset) getAsset(context); |
| 114 | |
| 115 | if (!asset.isJpeg()) { |
| 116 | // Return generic attributions if image is not stored in the JPEG file format. |
| 117 | return getGenericAttributions(context); |
| 118 | } |
| 119 | |
| 120 | List<String> attributes = new ArrayList<>(); |
| 121 | |
| 122 | for (String tag : EXIF_TAGS) { |
| 123 | String attribute = asset.readExifTag(tag); |
| 124 | |
| 125 | if (attribute == null) { |
| 126 | continue; |
| 127 | } |
| 128 | |
| 129 | if (tag == ExifInterface.TAG_DATETIME_ORIGINAL) { |
| 130 | attribute = formatDate(attribute); |
| 131 | } |
| 132 | |
| 133 | attributes.add(attribute); |
| 134 | } |
| 135 | |
| 136 | if (!attributes.isEmpty()) { |
| 137 | return attributes; |
| 138 | } |
| 139 | |
| 140 | // Return generic attributions if image did not contain any desired EXIF tags. |
| 141 | return getGenericAttributions(context); |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public Asset getAsset(Context context) { |
Clément Julliard | 6b7589c | 2019-06-26 12:35:06 -0700 | [diff] [blame] | 146 | if (mIsAssetUncached) { |
| 147 | mAsset = new ContentUriAsset( |
| 148 | context, |
| 149 | mUri, |
| 150 | /* uncached */ true); |
| 151 | } else { |
| 152 | if (mAsset == null) { |
| 153 | mAsset = new ContentUriAsset(context, mUri); |
| 154 | } |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 155 | } |
Clément Julliard | 6b7589c | 2019-06-26 12:35:06 -0700 | [diff] [blame] | 156 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 157 | return mAsset; |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public Asset getThumbAsset(Context context) { |
| 162 | return getAsset(context); |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public String getCollectionId(Context context) { |
| 167 | return context.getString(R.string.image_wallpaper_collection_id); |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public void showPreview(Activity srcActivity, InlinePreviewIntentFactory factory, |
| 172 | int requestCode) { |
| 173 | srcActivity.startActivityForResult(factory.newIntent(srcActivity, this), requestCode); |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public void writeToParcel(Parcel parcel, int i) { |
Santiago Etchebehere | a5736dc | 2021-06-14 14:32:32 -0700 | [diff] [blame] | 178 | super.writeToParcel(parcel, i); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 179 | parcel.writeString(mUri.toString()); |
| 180 | } |
| 181 | } |