1 /*
2  * Copyright (C) 2016 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 
17 package com.android.dialer.callcomposer;
18 
19 import android.database.Cursor;
20 import android.net.Uri;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 import android.provider.MediaStore.Images.Media;
24 import android.support.annotation.Nullable;
25 import android.text.TextUtils;
26 import com.android.dialer.common.Assert;
27 import java.io.File;
28 import java.util.Objects;
29 
30 /** Provides data for GalleryGridItemView */
31 public final class GalleryGridItemData implements Parcelable {
32   public static final String[] IMAGE_PROJECTION =
33       new String[] {Media._ID, Media.DATA, Media.MIME_TYPE, Media.DATE_MODIFIED};
34 
35   private static final int INDEX_DATA_PATH = 1;
36   private static final int INDEX_MIME_TYPE = 2;
37   private static final int INDEX_DATE_MODIFIED = 3;
38 
39   private String filePath;
40   private String mimeType;
41   private long dateModifiedSeconds;
42 
GalleryGridItemData()43   public GalleryGridItemData() {}
44 
GalleryGridItemData(GalleryGridItemData copyData)45   public GalleryGridItemData(GalleryGridItemData copyData) {
46     filePath = Assert.isNotNull(copyData.getFilePath());
47     mimeType = Assert.isNotNull(copyData.getMimeType());
48     dateModifiedSeconds = Assert.isNotNull(copyData.getDateModifiedSeconds());
49   }
50 
GalleryGridItemData(Cursor cursor)51   public GalleryGridItemData(Cursor cursor) {
52     bind(cursor);
53   }
54 
bind(Cursor cursor)55   public void bind(Cursor cursor) {
56     mimeType = Assert.isNotNull(cursor.getString(INDEX_MIME_TYPE));
57     String dateModified = Assert.isNotNull(cursor.getString(INDEX_DATE_MODIFIED));
58     dateModifiedSeconds = !TextUtils.isEmpty(dateModified) ? Long.parseLong(dateModified) : -1;
59     filePath = Assert.isNotNull(cursor.getString(INDEX_DATA_PATH));
60   }
61 
62   @Nullable
getFilePath()63   public String getFilePath() {
64     return filePath;
65   }
66 
67   @Nullable
getFileUri()68   public Uri getFileUri() {
69     return TextUtils.isEmpty(filePath) ? null : Uri.fromFile(new File(filePath));
70   }
71 
72   /** @return The date in seconds. This can be negative if we could not retrieve date info */
getDateModifiedSeconds()73   public long getDateModifiedSeconds() {
74     return dateModifiedSeconds;
75   }
76 
getMimeType()77   public String getMimeType() {
78     return mimeType;
79   }
80 
81   @Override
equals(Object obj)82   public boolean equals(Object obj) {
83     return obj instanceof GalleryGridItemData
84         && Objects.equals(mimeType, ((GalleryGridItemData) obj).mimeType)
85         && Objects.equals(filePath, ((GalleryGridItemData) obj).filePath)
86         && ((GalleryGridItemData) obj).dateModifiedSeconds == dateModifiedSeconds;
87   }
88 
89   @Override
hashCode()90   public int hashCode() {
91     return Objects.hash(filePath, mimeType, dateModifiedSeconds);
92   }
93 
94   @Override
describeContents()95   public int describeContents() {
96     return 0;
97   }
98 
99   @Override
writeToParcel(Parcel dest, int flags)100   public void writeToParcel(Parcel dest, int flags) {
101     dest.writeString(filePath);
102     dest.writeString(mimeType);
103     dest.writeLong(dateModifiedSeconds);
104   }
105 
106   public static final Creator<GalleryGridItemData> CREATOR =
107       new Creator<GalleryGridItemData>() {
108         @Override
109         public GalleryGridItemData createFromParcel(Parcel in) {
110           return new GalleryGridItemData(in);
111         }
112 
113         @Override
114         public GalleryGridItemData[] newArray(int size) {
115           return new GalleryGridItemData[size];
116         }
117       };
118 
GalleryGridItemData(Parcel in)119   private GalleryGridItemData(Parcel in) {
120     filePath = in.readString();
121     mimeType = in.readString();
122     dateModifiedSeconds = in.readLong();
123   }
124 }
125