blob: 8449edf680c630930097273cf95b44b37d1a4787 [file] [log] [blame]
Jon Miranda16ea1b12017-12-12 14:52:48 -08001/*
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 */
16package com.android.wallpaper.model;
17
18import android.app.Activity;
19import android.content.Context;
20import android.net.Uri;
21import android.os.Parcel;
22import android.os.Parcelable;
Jon Miranda16ea1b12017-12-12 14:52:48 -080023import android.util.Log;
24
Clément Julliard6b7589c2019-06-26 12:35:06 -070025import androidx.exifinterface.media.ExifInterface;
26
Jon Miranda16ea1b12017-12-12 14:52:48 -080027import com.android.wallpaper.R;
28import com.android.wallpaper.asset.Asset;
29import com.android.wallpaper.asset.ContentUriAsset;
30
31import java.text.ParseException;
32import java.text.SimpleDateFormat;
33import java.util.ArrayList;
34import java.util.Arrays;
35import java.util.Date;
36import java.util.List;
37
38/**
39 * Represents a wallpaper image from the system's image picker.
40 */
41public 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 Julliard6b7589c2019-06-26 12:35:06 -070064 private boolean mIsAssetUncached;
Jon Miranda16ea1b12017-12-12 14:52:48 -080065
66 public ImageWallpaperInfo(Uri uri) {
67 mUri = uri;
68 }
69
Clément Julliard6b7589c2019-06-26 12:35:06 -070070 public ImageWallpaperInfo(Uri uri, boolean uncachedAsset) {
71 mUri = uri;
72 mIsAssetUncached = uncachedAsset;
73 }
74
Santiago Etchebehere085712c2019-06-13 13:49:18 -070075 protected ImageWallpaperInfo(Parcel in) {
Santiago Etchebeherea5736dc2021-06-14 14:32:32 -070076 super(in);
Jon Miranda16ea1b12017-12-12 14:52:48 -080077 mUri = Uri.parse(in.readString());
78 }
79
Clément Julliardea1638d2018-05-21 19:15:17 -070080 @Override
81 public Uri getUri() {
82 return mUri;
83 }
84
Jon Miranda16ea1b12017-12-12 14:52:48 -080085 /**
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 Julliard6b7589c2019-06-26 12:35:06 -0700146 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 Miranda16ea1b12017-12-12 14:52:48 -0800155 }
Clément Julliard6b7589c2019-06-26 12:35:06 -0700156
Jon Miranda16ea1b12017-12-12 14:52:48 -0800157 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 Etchebeherea5736dc2021-06-14 14:32:32 -0700178 super.writeToParcel(parcel, i);
Jon Miranda16ea1b12017-12-12 14:52:48 -0800179 parcel.writeString(mUri.toString());
180 }
181}