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