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.database.Cursor;
20import android.media.CamcorderProfile;
21import android.net.Uri;
22
23import com.android.camera.debug.Log;
24import com.android.camera.util.Size;
25
26import java.util.Date;
27
28public class VideoDataFactory {
29    private static final Log.Tag TAG = new Log.Tag("VideoDataFact");
30
31    // TODO: Consider replacing this with 0,0 and possibly a shared
32    // ZERO size value.
33    private static final Size UNKNOWN_SIZE = new Size(-2, -2);
34
35    public VideoItemData fromCursor(Cursor c) {
36        long id = c.getLong(VideoDataQuery.COL_ID);
37        String title = c.getString(VideoDataQuery.COL_TITLE);
38        String mimeType = c.getString(VideoDataQuery.COL_MIME_TYPE);
39        long creationDateInMilliSeconds = c.getLong(VideoDataQuery.COL_DATE_TAKEN);
40        long lastModifiedDateInSeconds = c.getLong(VideoDataQuery.COL_DATE_MODIFIED);
41        Date creationDate = new Date(creationDateInMilliSeconds);
42        Date lastModifiedDate = new Date(lastModifiedDateInSeconds * 1000);
43
44        String filePath = c.getString(VideoDataQuery.COL_DATA);
45        int width = c.getInt(VideoDataQuery.COL_WIDTH);
46        int height = c.getInt(VideoDataQuery.COL_HEIGHT);
47
48        Size dimensions;
49
50        // If the media store doesn't contain a width and a height, use the width and height
51        // of the default camera mode instead. When the metadata loader runs, it will set the
52        // correct values.
53        if (width == 0 || height == 0) {
54            Log.w(TAG, "failed to retrieve width and height from the media store, defaulting " +
55                  " to camera profile");
56            CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
57            if(profile != null) {
58                dimensions = new Size(profile.videoFrameWidth, profile.videoFrameHeight);
59            } else {
60                Log.w(TAG, "Video profile was null, defaulting to unknown width and height.");
61                dimensions = UNKNOWN_SIZE;
62            }
63        } else {
64            dimensions = new Size(width, height);
65        }
66
67        long sizeInBytes = c.getLong(VideoDataQuery.COL_SIZE);
68        double latitude = c.getDouble(VideoDataQuery.COL_LATITUDE);
69        double longitude = c.getDouble(VideoDataQuery.COL_LONGITUDE);
70        long videoDurationMillis = c.getLong(VideoDataQuery.COL_DURATION);
71        Location location = Location.from(latitude, longitude);
72
73        Uri uri = VideoDataQuery.CONTENT_URI.buildUpon().appendPath(String.valueOf(id)).build();
74
75        return new VideoItemData(
76              id,
77              title,
78              mimeType,
79              creationDate,
80              lastModifiedDate,
81              filePath,
82              uri,
83              dimensions,
84              sizeInBytes,
85              0 /* orientation */,
86              location,
87              videoDurationMillis);
88    }
89}