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 | |
| 25 | import com.android.wallpaper.R; |
| 26 | import com.android.wallpaper.asset.Asset; |
| 27 | import com.android.wallpaper.asset.ContentUriAsset; |
| 28 | |
| 29 | import java.text.ParseException; |
| 30 | import java.text.SimpleDateFormat; |
| 31 | import java.util.ArrayList; |
| 32 | import java.util.Arrays; |
| 33 | import java.util.Date; |
| 34 | import java.util.List; |
| 35 | |
Sunny Goyal | 8600a3f | 2018-08-15 12:48:01 -0700 | [diff] [blame^] | 36 | import androidx.exifinterface.media.ExifInterface; |
| 37 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 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; |
| 64 | |
| 65 | public ImageWallpaperInfo(Uri uri) { |
| 66 | mUri = uri; |
| 67 | } |
| 68 | |
| 69 | private ImageWallpaperInfo(Parcel in) { |
| 70 | mUri = Uri.parse(in.readString()); |
| 71 | } |
| 72 | |
Clément Julliard | ea1638d | 2018-05-21 19:15:17 -0700 | [diff] [blame] | 73 | @Override |
| 74 | public Uri getUri() { |
| 75 | return mUri; |
| 76 | } |
| 77 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 78 | /** |
| 79 | * Formats a localized date string based on the provided datetime string in EXIF datetime format. |
| 80 | * |
| 81 | * @param exifDateTime Datetime string in EXIF datetime format. |
| 82 | * @return Localized date string, or the original datetime string if it could not be parsed. |
| 83 | */ |
| 84 | private static String formatDate(String exifDateTime) { |
| 85 | try { |
| 86 | Date parsedDate = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss").parse(exifDateTime); |
| 87 | return SimpleDateFormat.getDateInstance().format(parsedDate); |
| 88 | } catch (ParseException e) { |
| 89 | Log.w(TAG, "Unable to parse image datetime", e); |
| 90 | return exifDateTime; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | private static List<String> getGenericAttributions(Context context) { |
| 95 | return Arrays.asList( |
| 96 | context.getResources().getString(R.string.my_photos_generic_wallpaper_title)); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public String getTitle(Context context) { |
| 101 | return null; |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public List<String> getAttributions(Context context) { |
| 106 | ContentUriAsset asset = (ContentUriAsset) getAsset(context); |
| 107 | |
| 108 | if (!asset.isJpeg()) { |
| 109 | // Return generic attributions if image is not stored in the JPEG file format. |
| 110 | return getGenericAttributions(context); |
| 111 | } |
| 112 | |
| 113 | List<String> attributes = new ArrayList<>(); |
| 114 | |
| 115 | for (String tag : EXIF_TAGS) { |
| 116 | String attribute = asset.readExifTag(tag); |
| 117 | |
| 118 | if (attribute == null) { |
| 119 | continue; |
| 120 | } |
| 121 | |
| 122 | if (tag == ExifInterface.TAG_DATETIME_ORIGINAL) { |
| 123 | attribute = formatDate(attribute); |
| 124 | } |
| 125 | |
| 126 | attributes.add(attribute); |
| 127 | } |
| 128 | |
| 129 | if (!attributes.isEmpty()) { |
| 130 | return attributes; |
| 131 | } |
| 132 | |
| 133 | // Return generic attributions if image did not contain any desired EXIF tags. |
| 134 | return getGenericAttributions(context); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public Asset getAsset(Context context) { |
| 139 | if (mAsset == null) { |
| 140 | mAsset = new ContentUriAsset(context, mUri); |
| 141 | } |
| 142 | return mAsset; |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public Asset getThumbAsset(Context context) { |
| 147 | return getAsset(context); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public String getCollectionId(Context context) { |
| 152 | return context.getString(R.string.image_wallpaper_collection_id); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public void showPreview(Activity srcActivity, InlinePreviewIntentFactory factory, |
| 157 | int requestCode) { |
| 158 | srcActivity.startActivityForResult(factory.newIntent(srcActivity, this), requestCode); |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public void writeToParcel(Parcel parcel, int i) { |
| 163 | parcel.writeString(mUri.toString()); |
| 164 | } |
| 165 | } |