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