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