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
17package com.android.camera.data;
18
19import android.content.Context;
20import android.graphics.Bitmap;
21import android.graphics.Point;
22import android.net.Uri;
23import android.view.View;
24import android.widget.ImageView;
25
26import com.android.camera.Storage;
27import com.android.camera.debug.Log;
28import com.android.camera.util.Size;
29import com.android.camera2.R;
30import com.bumptech.glide.Glide;
31import com.google.common.base.Optional;
32
33import java.util.Date;
34
35import 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 */
41public 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     */
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
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
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
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
116    public FilmstripItemType getItemViewType() {
117        return FilmstripItemType.SESSION;
118    }
119
120    @Override
121    public void setSuggestedSize(int widthPx, int heightPx) { }
122
123    @Override
124    public void renderTiny(@Nonnull View view) { }
125
126    @Override
127    public void renderThumbnail(@Nonnull View view) { }
128
129    @Override
130    public void renderFullRes(@Nonnull View view) { }
131
132    @Override
133    public boolean delete() {
134        return false;
135    }
136
137    @Override
138    public Optional<MediaDetails> getMediaDetails() {
139        return Optional.absent();
140    }
141
142    @Override
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
158    public Metadata getMetadata() {
159        return mMetaData;
160    }
161
162    @Override
163    public FilmstripItemData getData() {
164        return mData;
165    }
166
167    @Override
168    public FilmstripItemAttributes getAttributes() {
169        return mAttributes;
170    }
171
172    @Override
173    public Optional<Bitmap> generateThumbnail(int boundingWidthPx, int boundingHeightPx) {
174        return Storage.getPlaceholderForSession(mUri);
175    }
176
177    @Override
178    public void recycle(@Nonnull View view) {
179        Glide.clear(view);
180    }
181
182    @Override
183    public Size getDimensions() {
184        return mData.getDimensions();
185    }
186
187    @Override
188    public int getOrientation() {
189        return mData.getOrientation();
190    }
191}
192