1/* 2 * Copyright (C) 2012 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.gallery3d.util; 18 19import android.content.ContentResolver; 20import android.content.ContentValues; 21import android.database.Cursor; 22import android.net.Uri; 23import android.os.Environment; 24import android.provider.MediaStore.Video; 25import android.provider.MediaStore.Video.VideoColumns; 26 27import java.io.File; 28import java.sql.Date; 29import java.text.SimpleDateFormat; 30 31public class SaveVideoFileUtils { 32 // Copy from SaveCopyTask.java in terms of how to handle the destination 33 // path and filename : querySource() and getSaveDirectory(). 34 public interface ContentResolverQueryCallback { 35 void onCursorResult(Cursor cursor); 36 } 37 38 // This function can decide which folder to save the video file, and generate 39 // the needed information for the video file including filename. 40 public static SaveVideoFileInfo getDstMp4FileInfo(String fileNameFormat, 41 ContentResolver contentResolver, Uri uri, String defaultFolderName) { 42 SaveVideoFileInfo dstFileInfo = new SaveVideoFileInfo(); 43 // Use the default save directory if the source directory cannot be 44 // saved. 45 dstFileInfo.mDirectory = getSaveDirectory(contentResolver, uri); 46 if ((dstFileInfo.mDirectory == null) || !dstFileInfo.mDirectory.canWrite()) { 47 dstFileInfo.mDirectory = new File(Environment.getExternalStorageDirectory(), 48 BucketNames.DOWNLOAD); 49 dstFileInfo.mFolderName = defaultFolderName; 50 } else { 51 dstFileInfo.mFolderName = dstFileInfo.mDirectory.getName(); 52 } 53 dstFileInfo.mFileName = new SimpleDateFormat(fileNameFormat).format( 54 new Date(System.currentTimeMillis())); 55 56 dstFileInfo.mFile = new File(dstFileInfo.mDirectory, dstFileInfo.mFileName + ".mp4"); 57 return dstFileInfo; 58 } 59 60 private static void querySource(ContentResolver contentResolver, Uri uri, 61 String[] projection, ContentResolverQueryCallback callback) { 62 Cursor cursor = null; 63 try { 64 cursor = contentResolver.query(uri, projection, null, null, null); 65 if ((cursor != null) && cursor.moveToNext()) { 66 callback.onCursorResult(cursor); 67 } 68 } catch (Exception e) { 69 // Ignore error for lacking the data column from the source. 70 } finally { 71 if (cursor != null) { 72 cursor.close(); 73 } 74 } 75 } 76 77 private static File getSaveDirectory(ContentResolver contentResolver, Uri uri) { 78 final File[] dir = new File[1]; 79 querySource(contentResolver, uri, 80 new String[] { VideoColumns.DATA }, 81 new ContentResolverQueryCallback() { 82 @Override 83 public void onCursorResult(Cursor cursor) { 84 dir[0] = new File(cursor.getString(0)).getParentFile(); 85 } 86 }); 87 return dir[0]; 88 } 89 90 91 /** 92 * Insert the content (saved file) with proper video properties. 93 */ 94 public static Uri insertContent(SaveVideoFileInfo mDstFileInfo, 95 ContentResolver contentResolver, Uri uri ) { 96 long nowInMs = System.currentTimeMillis(); 97 long nowInSec = nowInMs / 1000; 98 final ContentValues values = new ContentValues(12); 99 values.put(Video.Media.TITLE, mDstFileInfo.mFileName); 100 values.put(Video.Media.DISPLAY_NAME, mDstFileInfo.mFile.getName()); 101 values.put(Video.Media.MIME_TYPE, "video/mp4"); 102 values.put(Video.Media.DATE_TAKEN, nowInMs); 103 values.put(Video.Media.DATE_MODIFIED, nowInSec); 104 values.put(Video.Media.DATE_ADDED, nowInSec); 105 values.put(Video.Media.DATA, mDstFileInfo.mFile.getAbsolutePath()); 106 values.put(Video.Media.SIZE, mDstFileInfo.mFile.length()); 107 // Copy the data taken and location info from src. 108 String[] projection = new String[] { 109 VideoColumns.DATE_TAKEN, 110 VideoColumns.LATITUDE, 111 VideoColumns.LONGITUDE, 112 VideoColumns.RESOLUTION, 113 }; 114 115 // Copy some info from the source file. 116 querySource(contentResolver, uri, projection, 117 new ContentResolverQueryCallback() { 118 @Override 119 public void onCursorResult(Cursor cursor) { 120 long timeTaken = cursor.getLong(0); 121 if (timeTaken > 0) { 122 values.put(Video.Media.DATE_TAKEN, timeTaken); 123 } 124 double latitude = cursor.getDouble(1); 125 double longitude = cursor.getDouble(2); 126 // TODO: Change || to && after the default location 127 // issue is 128 // fixed. 129 if ((latitude != 0f) || (longitude != 0f)) { 130 values.put(Video.Media.LATITUDE, latitude); 131 values.put(Video.Media.LONGITUDE, longitude); 132 } 133 values.put(Video.Media.RESOLUTION, cursor.getString(3)); 134 135 } 136 }); 137 138 return contentResolver.insert(Video.Media.EXTERNAL_CONTENT_URI, values); 139 } 140 141} 142