1/*
2 * Copyright (C) 2013 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
17package com.android.camera.data;
18
19import android.content.Context;
20import android.os.Bundle;
21import android.view.View;
22import com.android.camera.debug.Log;
23import com.android.camera.filmstrip.ImageData;
24
25import java.util.Comparator;
26
27/**
28 * An abstract interface that represents the local media data. Also implements
29 * Comparable interface so we can sort in DataAdapter.
30 * Note that all the sub-class of LocalData are designed to be immutable, i.e:
31 * all the members need to be final, and there is no setter. In this way, we
32 * can guarantee thread safety for LocalData.
33 */
34public interface LocalData extends ImageData {
35    static final Log.Tag TAG = new Log.Tag("LocalData");
36
37    public static final String MIME_TYPE_JPEG = "image/jpeg";
38
39    // Data actions.
40    public static final int DATA_ACTION_NONE = 0;
41    public static final int DATA_ACTION_PLAY = 1;
42    public static final int DATA_ACTION_DELETE = (1 << 1);
43    public static final int DATA_ACTION_EDIT = (1 << 2);
44    public static final int DATA_ACTION_SHARE = (1 << 3);
45
46    // Local data types. Returned by getLocalDataType().
47    /**
48     * Constant for denoting a camera preview.
49     */
50    public static final int LOCAL_CAMERA_PREVIEW   = 1;
51    /**
52     * Constant for denoting an arbitrary view.
53     */
54    public static final int LOCAL_VIEW             = 2;
55    /**
56     * Constant for denoting a still image.
57     */
58    public static final int LOCAL_IMAGE            = 3;
59    /**
60     * Constant for denoting a video.
61     */
62    public static final int LOCAL_VIDEO            = 4;
63    /**
64     * Constant for denoting an in-progress item which should not be touched
65     * before the related task is done. Data of this type should not support
66     * any actions like sharing, editing, etc.
67     */
68    public static final int LOCAL_IN_PROGRESS_DATA = 5;
69
70    // TODO: Re-think how the in-progress logic works. We shouldn't need to pass
71    // in the information about whether this session is in progress.
72
73    /**
74     * Creates View to represent media.
75     *
76     * @param context The {@link android.content.Context} to create the view.
77     * @param thumbWidth Width in pixels of the suggested zoomed out view/image size.
78     * @param thumbHeight Height in pixels of the suggested zoomed out view/image size.
79     * @param adapter Data adapter for this data item.
80     */
81    View getView(Context context, View recycled, int thumbWidth, int thumbHeight,
82        int placeHolderResourceId, LocalDataAdapter adapter, boolean isInProgress);
83
84    /** Returns a unique identifier for the view created by this data so that the view
85     * can be reused.
86     *
87     * @see android.widget.BaseAdapter#getItemViewType(int)
88     */
89    LocalDataViewType getItemViewType();
90
91   /**
92     * Request resize of View created by getView().
93     *
94     * @param context The {@link android.content.Context} to create the view.
95     * @param thumbWidth Width in pixels of the suggested zoomed out view/image size.
96     * @param thumbHeight Height in pixels of the suggested zoomed out view/image size.
97     * @param view View created by getView();
98     * @param adapter Data adapter for this data item.
99     */
100    public void loadFullImage(Context context, int thumbWidth, int thumbHeight, View view,
101        LocalDataAdapter adapter);
102
103    /**
104     * Gets the date when this data is created. The returned date is also used
105     * for sorting data. Value is epoch milliseconds.
106     *
107     * @return The date when this data is created.
108     * @see {@link NewestFirstComparator}
109     */
110    long getDateTaken();
111
112    /**
113     * Gets the date when this data is modified. The returned date is also used
114     * for sorting data. Value is epoch seconds.
115     *
116     * @return The date when this data is modified.
117     * @see {@link NewestFirstComparator}
118     */
119    long getDateModified();
120
121    /** Gets the title of this data */
122    String getTitle();
123
124    /**
125     * Checks if the data actions (delete/play ...) can be applied on this data.
126     *
127     * @param actions The actions to check.
128     * @return Whether all the actions are supported.
129     */
130    boolean isDataActionSupported(int actions);
131
132    /** Removes the data from the storage if possible. */
133    boolean delete(Context c);
134
135    void onFullScreen(boolean fullScreen);
136
137    /** Returns {@code true} if it allows swipe to filmstrip in full screen. */
138    boolean canSwipeInFullScreen();
139
140    /**
141     * Returns the path to the data on the storage.
142     *
143     * @return Empty path if there's none.
144     */
145    String getPath();
146
147    /**
148     * @return The mimetype of this data item, or null, if this item has no
149     *         mimetype associated with it.
150     */
151    String getMimeType();
152
153    /**
154     * @return The media details (such as EXIF) for the data. {@code null} if
155     * not available for the data.
156     */
157    MediaDetails getMediaDetails(Context context);
158
159    /**
160     * Returns the type of the local data defined by {@link LocalData}.
161     *
162     * @return The local data type. Could be one of the following:
163     * {@code LOCAL_CAMERA_PREVIEW}, {@code LOCAL_VIEW}, {@code LOCAL_IMAGE},
164     * {@code LOCAL_VIDEO}, {@code LOCAL_PHOTO_SPHERE},
165     * {@code LOCAL_360_PHOTO_SPHERE}, and {@code LOCAL_RGBZ}
166     */
167    int getLocalDataType();
168
169    /**
170     * @return The size of the data in bytes
171     */
172    long getSizeInBytes();
173
174    /**
175     * Refresh the data content.
176     *
177     * @param context The Android {@link android.content.Context}.
178     * @return A new LocalData object if success, null otherwise.
179     */
180    LocalData refresh(Context context);
181
182    /**
183     * @return the {@link android.content.ContentResolver} Id of the data.
184     */
185    long getContentId();
186
187    /**
188     * @return the metadata. Should never be {@code null}.
189     */
190    Bundle getMetadata();
191
192    /**
193     * Any media store attribute that can potentially change the local data
194     * should be included in this signature, primarily oriented at detecting
195     * edits.
196     *
197     * @return A string identifying the set of changeable attributes.
198     */
199    String getSignature();
200
201    /**
202     * @return whether the metadata is updated.
203     */
204    public boolean isMetadataUpdated();
205
206    static class NewestFirstComparator implements Comparator<LocalData> {
207
208        /** Compare taken/modified date of LocalData in descent order to make
209         newer data in the front.
210         The negative numbers here are always considered "bigger" than
211         positive ones. Thus, if any one of the numbers is negative, the logic
212         is reversed. */
213        private static int compareDate(long v1, long v2) {
214            if (v1 >= 0 && v2 >= 0) {
215                return ((v1 < v2) ? 1 : ((v1 > v2) ? -1 : 0));
216            }
217            return ((v2 < v1) ? 1 : ((v2 > v1) ? -1 : 0));
218        }
219
220        @Override
221        public int compare(LocalData d1, LocalData d2) {
222            int cmp = compareDate(d1.getDateTaken(), d2.getDateTaken());
223            if (cmp == 0) {
224                cmp = compareDate(d1.getDateModified(), d2.getDateModified());
225            }
226            if (cmp == 0) {
227                cmp = d1.getTitle().compareTo(d2.getTitle());
228            }
229            return cmp;
230        }
231    }
232}
233