blob: fcc12766337d2e7b9425d7039d1f7bb2dc7377ff [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
25import com.android.wallpaper.R;
26import com.android.wallpaper.asset.Asset;
27import com.android.wallpaper.asset.ContentUriAsset;
28
29import java.text.ParseException;
30import java.text.SimpleDateFormat;
31import java.util.ArrayList;
32import java.util.Arrays;
33import java.util.Date;
34import java.util.List;
35
Sunny Goyal8600a3f2018-08-15 12:48:01 -070036import androidx.exifinterface.media.ExifInterface;
37
Jon Miranda16ea1b12017-12-12 14:52:48 -080038/**
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;
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 Julliardea1638d2018-05-21 19:15:17 -070073 @Override
74 public Uri getUri() {
75 return mUri;
76 }
77
Jon Miranda16ea1b12017-12-12 14:52:48 -080078 /**
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}