1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.components.browser_ui.photo_picker;
6 
7 import android.net.Uri;
8 
9 import androidx.annotation.IntDef;
10 import androidx.annotation.VisibleForTesting;
11 
12 import org.chromium.base.ApiCompatibilityUtils;
13 
14 import java.lang.annotation.Retention;
15 import java.lang.annotation.RetentionPolicy;
16 import java.text.DateFormat;
17 import java.util.Date;
18 
19 /**
20  * A class to keep track of the meta data associated with a an image in the photo picker.
21  */
22 public class PickerBitmap implements Comparable<PickerBitmap> {
23     // The possible types of tiles involved in the viewer. Note that the values for PICTURE and
24     // VIDEO matter, because they are used to prioritize still images over videos in the priority
25     // queue in PickerCategoryView.
26     @IntDef({TileTypes.PICTURE, TileTypes.CAMERA, TileTypes.GALLERY, TileTypes.VIDEO})
27     @Retention(RetentionPolicy.SOURCE)
28     public @interface TileTypes {
29         int PICTURE = 0;
30         int CAMERA = 1;
31         int GALLERY = 2;
32         int VIDEO = 3;
33     }
34 
35     // The URI of the bitmap to show.
36     private Uri mUri;
37 
38     // When the bitmap was last modified on disk.
39     private long mLastModified;
40 
41     // The type of tile involved.
42     @TileTypes
43     private int mType;
44 
45     /**
46      * The PickerBitmap constructor.
47      * @param uri The URI for the bitmap to show.
48      * @param lastModified When the bitmap was last modified on disk.
49      * @param type The type of tile involved.
50      */
PickerBitmap(Uri uri, long lastModified, @TileTypes int type)51     public PickerBitmap(Uri uri, long lastModified, @TileTypes int type) {
52         // PICTURE must have a lower value than VIDEO, in order for the priority queue in
53         // PickerCategoryView to prioritize still images ahead of video.
54         assert TileTypes.PICTURE < TileTypes.VIDEO;
55 
56         mUri = uri;
57         mLastModified = lastModified;
58         mType = type;
59     }
60 
61     /**
62      * Accessor for the URI.
63      * @return The URI for this PickerBitmap object.
64      */
65     public Uri getUri() {
66         return mUri;
67     }
68 
69     /**
70      * Accessor for the filename.
71      * @return The filename (without the extension and path).
72      */
73     public String getFilenameWithoutExtension() {
74         String filePath = mUri.getPath();
75         int index = filePath.lastIndexOf("/");
76         if (index == -1) return filePath;
77         return filePath.substring(index + 1, filePath.length());
78     }
79 
80     /**
81      * Accessor for the last modified date.
82      * @return The last modified date in string format.
83      */
84     public String getLastModifiedString() {
85         return DateFormat.getDateTimeInstance().format(new Date(mLastModified));
86     }
87 
88     /**
89      * Accessor for the tile type.
90      * @return The type of tile involved for this bitmap object.
91      */
92     @TileTypes
93     public int type() {
94         return mType;
95     }
96 
97     /**
98      * A comparison function for PickerBitmaps (results in a last-modified first sort).
99      * @param other The PickerBitmap to compare it to.
100      * @return 0, 1, or -1, depending on which is bigger.
101      */
102     @Override
103     public int compareTo(PickerBitmap other) {
104         return ApiCompatibilityUtils.compareLong(other.mLastModified, mLastModified);
105     }
106 
107     /**
108      * Accessor for the last modified date (for testing use only).
109      * @return The last modified date.
110      */
111     @VisibleForTesting
112     public long getLastModifiedForTesting() {
113         return mLastModified;
114     }
115 }
116