18ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde/*
28ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde * Copyright (C) 2013 The Android Open Source Project
38ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde *
48ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde * Licensed under the Apache License, Version 2.0 (the "License");
58ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde * you may not use this file except in compliance with the License.
68ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde * You may obtain a copy of the License at
78ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde *
88ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde *      http://www.apache.org/licenses/LICENSE-2.0
98ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde *
108ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde * Unless required by applicable law or agreed to in writing, software
118ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde * distributed under the License is distributed on an "AS IS" BASIS,
128ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde * See the License for the specific language governing permissions and
148ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde * limitations under the License.
158ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde */
168ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde
178ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohdepackage com.android.camera.data;
188ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde
198ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohdeimport android.database.Cursor;
208ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohdeimport android.media.CamcorderProfile;
218ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohdeimport android.net.Uri;
228ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde
238ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohdeimport com.android.camera.debug.Log;
248ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohdeimport com.android.camera.util.Size;
258ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde
268ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohdeimport java.util.Date;
278ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde
288ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohdepublic class VideoDataFactory {
298ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde    private static final Log.Tag TAG = new Log.Tag("VideoDataFact");
308ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde
318ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde    // TODO: Consider replacing this with 0,0 and possibly a shared
328ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde    // ZERO size value.
338ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde    private static final Size UNKNOWN_SIZE = new Size(-2, -2);
348ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde
358ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde    public VideoItemData fromCursor(Cursor c) {
368ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde        long id = c.getLong(VideoDataQuery.COL_ID);
378ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde        String title = c.getString(VideoDataQuery.COL_TITLE);
388ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde        String mimeType = c.getString(VideoDataQuery.COL_MIME_TYPE);
398ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde        long creationDateInMilliSeconds = c.getLong(VideoDataQuery.COL_DATE_TAKEN);
408ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde        long lastModifiedDateInSeconds = c.getLong(VideoDataQuery.COL_DATE_MODIFIED);
418ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde        Date creationDate = new Date(creationDateInMilliSeconds);
428ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde        Date lastModifiedDate = new Date(lastModifiedDateInSeconds * 1000);
438ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde
448ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde        String filePath = c.getString(VideoDataQuery.COL_DATA);
458ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde        int width = c.getInt(VideoDataQuery.COL_WIDTH);
468ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde        int height = c.getInt(VideoDataQuery.COL_HEIGHT);
478ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde
488ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde        Size dimensions;
498ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde
508ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde        // If the media store doesn't contain a width and a height, use the width and height
518ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde        // of the default camera mode instead. When the metadata loader runs, it will set the
528ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde        // correct values.
538ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde        if (width == 0 || height == 0) {
548ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde            Log.w(TAG, "failed to retrieve width and height from the media store, defaulting " +
558ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde                  " to camera profile");
568ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde            CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
578ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde            if(profile != null) {
588ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde                dimensions = new Size(profile.videoFrameWidth, profile.videoFrameHeight);
598ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde            } else {
608ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde                Log.w(TAG, "Video profile was null, defaulting to unknown width and height.");
618ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde                dimensions = UNKNOWN_SIZE;
628ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde            }
638ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde        } else {
648ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde            dimensions = new Size(width, height);
658ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde        }
668ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde
678ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde        long sizeInBytes = c.getLong(VideoDataQuery.COL_SIZE);
688ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde        double latitude = c.getDouble(VideoDataQuery.COL_LATITUDE);
698ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde        double longitude = c.getDouble(VideoDataQuery.COL_LONGITUDE);
708ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde        long videoDurationMillis = c.getLong(VideoDataQuery.COL_DURATION);
718ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde        Location location = Location.from(latitude, longitude);
728ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde
738ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde        Uri uri = VideoDataQuery.CONTENT_URI.buildUpon().appendPath(String.valueOf(id)).build();
748ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde
758ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde        return new VideoItemData(
768ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde              id,
778ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde              title,
788ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde              mimeType,
798ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde              creationDate,
808ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde              lastModifiedDate,
818ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde              filePath,
828ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde              uri,
838ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde              dimensions,
848ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde              sizeInBytes,
858ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde              0 /* orientation */,
868ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde              location,
878ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde              videoDurationMillis);
888ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde    }
898ee16b8a323ffa20e6fb1270d498ec445f64defcPaul Rohde}