1 /*
2  * Copyright (C) 2014 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.graphics.Bitmap;
21 import android.graphics.Point;
22 import android.net.Uri;
23 import android.view.View;
24 import android.widget.ImageView;
25 
26 import com.android.camera.Storage;
27 import com.android.camera.debug.Log;
28 import com.android.camera.util.Size;
29 import com.android.camera2.R;
30 import com.bumptech.glide.Glide;
31 import com.google.common.base.Optional;
32 
33 import java.util.Date;
34 
35 import javax.annotation.Nonnull;
36 
37 /**
38  * This is used to represent a local data item that is in progress and not
39  * yet in the media store.
40  */
41 public class SessionItem implements FilmstripItem {
42     protected final Metadata mMetaData;
43     private FilmstripItemData mData;
44     private final FilmstripItemAttributes mAttributes;
45     private final Context mContext;
46     private final Uri mUri;
47 
48     /**
49      * Creates a new session from the given URI.
50      * @param context valid android application context.
51      * @param uri the URI of the session.
52      * @return If the session was found, a new SessionItem is returned.
53      */
create(Context context, Uri uri)54     public static Optional<SessionItem> create(Context context, Uri uri) {
55         if (!Storage.containsPlaceholderSize(uri)) {
56             return Optional.absent();
57         }
58         Size dimension = getSessionSize(uri);
59         if (dimension == null) {
60             return Optional.absent();
61         }
62         return Optional.of(new SessionItem(context, uri, dimension));
63     }
64 
SessionItem(Context context, Uri uri, Size dimension)65     protected SessionItem(Context context, Uri uri, Size dimension) {
66         mContext = context;
67         mUri = uri;
68 
69         mMetaData = new Metadata();
70         mMetaData.setLoaded(true);
71 
72         Date creationDate = new Date();
73         mData = new FilmstripItemData.Builder(uri)
74               .withCreationDate(creationDate)
75               .withLastModifiedDate(creationDate)
76               .withDimensions(dimension)
77               .build();
78 
79         mAttributes = new FilmstripItemAttributes.Builder()
80                 .with(FilmstripItemAttributes.Attributes.IS_RENDERING)
81                 .build();
82     }
83 
getSessionSize(Uri uri)84     private static Size getSessionSize(Uri uri) {
85         Point size = Storage.getSizeForSession(uri);
86         if (size == null) {
87             return null;
88         }
89         return new Size(size);
90     }
91 
92     @Override
getView(Optional<View> optionalView, LocalFilmstripDataAdapter adapter, boolean isInProgress, VideoClickedCallback videoClickedCallback)93     public View getView(Optional<View> optionalView, LocalFilmstripDataAdapter adapter,
94           boolean isInProgress, VideoClickedCallback videoClickedCallback) {
95         ImageView imageView;
96 
97         if (optionalView.isPresent()) {
98             imageView = (ImageView) optionalView.get();
99         } else {
100             imageView = new ImageView(mContext);
101             imageView.setTag(R.id.mediadata_tag_viewtype, getItemViewType().ordinal());
102         }
103 
104         Optional<Bitmap> placeholder = Storage.getPlaceholderForSession(mData.getUri());
105         if (placeholder.isPresent()) {
106             imageView.setImageBitmap(placeholder.get());
107         } else {
108             imageView.setImageResource(GlideFilmstripManager.DEFAULT_PLACEHOLDER_RESOURCE);
109         }
110         imageView.setContentDescription(mContext.getResources().getString(
111                 R.string.media_processing_content_description));
112         return imageView;
113     }
114 
115     @Override
getItemViewType()116     public FilmstripItemType getItemViewType() {
117         return FilmstripItemType.SESSION;
118     }
119 
120     @Override
setSuggestedSize(int widthPx, int heightPx)121     public void setSuggestedSize(int widthPx, int heightPx) { }
122 
123     @Override
renderTiny(@onnull View view)124     public void renderTiny(@Nonnull View view) { }
125 
126     @Override
renderThumbnail(@onnull View view)127     public void renderThumbnail(@Nonnull View view) { }
128 
129     @Override
renderFullRes(@onnull View view)130     public void renderFullRes(@Nonnull View view) { }
131 
132     @Override
delete()133     public boolean delete() {
134         return false;
135     }
136 
137     @Override
getMediaDetails()138     public Optional<MediaDetails> getMediaDetails() {
139         return Optional.absent();
140     }
141 
142     @Override
refresh()143     public FilmstripItem refresh() {
144         Size dimension = getSessionSize(mData.getUri());
145         if (dimension == null) {
146             Log.w(TAG, "Cannot refresh item, session does not exist.");
147             return this;
148         }
149 
150         mData = FilmstripItemData.Builder.from(mData)
151               .withDimensions(dimension)
152               .build();
153 
154         return this;
155     }
156 
157     @Override
getMetadata()158     public Metadata getMetadata() {
159         return mMetaData;
160     }
161 
162     @Override
getData()163     public FilmstripItemData getData() {
164         return mData;
165     }
166 
167     @Override
getAttributes()168     public FilmstripItemAttributes getAttributes() {
169         return mAttributes;
170     }
171 
172     @Override
generateThumbnail(int boundingWidthPx, int boundingHeightPx)173     public Optional<Bitmap> generateThumbnail(int boundingWidthPx, int boundingHeightPx) {
174         return Storage.getPlaceholderForSession(mUri);
175     }
176 
177     @Override
recycle(@onnull View view)178     public void recycle(@Nonnull View view) {
179         Glide.clear(view);
180     }
181 
182     @Override
getDimensions()183     public Size getDimensions() {
184         return mData.getDimensions();
185     }
186 
187     @Override
getOrientation()188     public int getOrientation() {
189         return mData.getOrientation();
190     }
191 }
192