diff options
author | Teng-Hui Zhu <ztenghui@google.com> | 2012-11-16 09:53:16 -0800 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2012-11-16 09:53:16 -0800 |
commit | 99996e3b7d581a25d12cad8c00740221f2e1d922 (patch) | |
tree | 70e0d24a6c02756a432034b04edd91f51b27d864 /src/com/android/gallery3d/app | |
parent | b6dda0c74eee0994b70608ce1737ed01d168ee52 (diff) | |
parent | 46da45f76cae4b7cbcb9706cbd54b8b4b064f11d (diff) | |
download | android_packages_apps_Snap-99996e3b7d581a25d12cad8c00740221f2e1d922.tar.gz android_packages_apps_Snap-99996e3b7d581a25d12cad8c00740221f2e1d922.tar.bz2 android_packages_apps_Snap-99996e3b7d581a25d12cad8c00740221f2e1d922.zip |
Merge "Video: Add the mute functionality." into gb-ub-photos-bryce
Diffstat (limited to 'src/com/android/gallery3d/app')
-rw-r--r-- | src/com/android/gallery3d/app/MuteVideo.java | 105 | ||||
-rw-r--r-- | src/com/android/gallery3d/app/PhotoPage.java | 6 | ||||
-rw-r--r-- | src/com/android/gallery3d/app/TrimVideo.java | 119 | ||||
-rw-r--r-- | src/com/android/gallery3d/app/VideoUtils.java (renamed from src/com/android/gallery3d/app/TrimVideoUtils.java) | 58 |
4 files changed, 165 insertions, 123 deletions
diff --git a/src/com/android/gallery3d/app/MuteVideo.java b/src/com/android/gallery3d/app/MuteVideo.java new file mode 100644 index 000000000..fbb1804fb --- /dev/null +++ b/src/com/android/gallery3d/app/MuteVideo.java @@ -0,0 +1,105 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.gallery3d.app; + +import android.app.Activity; +import android.app.ProgressDialog; +import android.content.Intent; +import android.net.Uri; +import android.os.Handler; +import android.provider.MediaStore; +import android.widget.Toast; + +import com.android.gallery3d.R; +import com.android.gallery3d.data.MediaItem; +import com.android.gallery3d.util.SaveVideoFileInfo; +import com.android.gallery3d.util.SaveVideoFileUtils; + +import java.io.IOException; + +public class MuteVideo { + + private ProgressDialog mMuteProgress; + + private MediaItem mCurrentItem = null; + private Uri mUri = null; + private SaveVideoFileInfo mDstFileInfo = null; + private Activity mActivity = null; + private final Handler mHandler = new Handler(); + + final String TIME_STAMP_NAME = "'MUTE'_yyyyMMdd_HHmmss"; + + public MuteVideo(MediaItem current, Uri uri, Activity activity) { + mUri = uri; + mCurrentItem = current; + mActivity = activity; + } + + public void muteInBackground() { + mDstFileInfo = SaveVideoFileUtils.getDstMp4FileInfo(TIME_STAMP_NAME, + mActivity.getContentResolver(), mUri, + mActivity.getString(R.string.folder_download)); + + showProgressDialog(); + new Thread(new Runnable() { + @Override + public void run() { + try { + VideoUtils.startMute(mCurrentItem.getFilePath(), mDstFileInfo); + SaveVideoFileUtils.insertContent( + mDstFileInfo, mActivity.getContentResolver(), mUri); + } catch (IOException e) { + Toast.makeText(mActivity, mActivity.getString(R.string.video_mute_err), + Toast.LENGTH_SHORT).show(); + } + // After muting is done, trigger the UI changed. + mHandler.post(new Runnable() { + @Override + public void run() { + Toast.makeText(mActivity.getApplicationContext(), + mActivity.getString(R.string.save_into, + mDstFileInfo.mFolderName), + Toast.LENGTH_SHORT) + .show(); + + if (mMuteProgress != null) { + mMuteProgress.dismiss(); + mMuteProgress = null; + + // Show the result only when the activity not + // stopped. + Intent intent = new Intent(android.content.Intent.ACTION_VIEW); + intent.setDataAndTypeAndNormalize( + Uri.fromFile(mDstFileInfo.mFile), "video/*"); + intent.putExtra(MediaStore.EXTRA_FINISH_ON_COMPLETION, false); + mActivity.startActivity(intent); + } + } + }); + } + }).start(); + } + + private void showProgressDialog() { + mMuteProgress = new ProgressDialog(mActivity); + mMuteProgress.setTitle(mActivity.getString(R.string.muting)); + mMuteProgress.setMessage(mActivity.getString(R.string.please_wait)); + mMuteProgress.setCancelable(false); + mMuteProgress.setCanceledOnTouchOutside(false); + mMuteProgress.show(); + } +} diff --git a/src/com/android/gallery3d/app/PhotoPage.java b/src/com/android/gallery3d/app/PhotoPage.java index 492cdfb81..79bf0e0d7 100644 --- a/src/com/android/gallery3d/app/PhotoPage.java +++ b/src/com/android/gallery3d/app/PhotoPage.java @@ -1074,6 +1074,12 @@ public abstract class PhotoPage extends ActivityState implements mActivity.startActivityForResult(intent, REQUEST_TRIM); return true; } + case R.id.action_mute: { + MuteVideo muteVideo = new MuteVideo(current, + manager.getContentUri(path), mActivity); + muteVideo.muteInBackground(); + return true; + } case R.id.action_edit: { launchPhotoEditor(); return true; diff --git a/src/com/android/gallery3d/app/TrimVideo.java b/src/com/android/gallery3d/app/TrimVideo.java index f246ff6c7..9187ee80c 100644 --- a/src/com/android/gallery3d/app/TrimVideo.java +++ b/src/com/android/gallery3d/app/TrimVideo.java @@ -41,6 +41,8 @@ import android.widget.VideoView; import com.android.gallery3d.R; import com.android.gallery3d.util.BucketNames; +import com.android.gallery3d.util.SaveVideoFileInfo; +import com.android.gallery3d.util.SaveVideoFileUtils; import java.io.File; import java.io.IOException; @@ -71,13 +73,8 @@ public class TrimVideo extends Activity implements private boolean mHasPaused = false; private String mSrcVideoPath = null; - private String mSaveFileName = null; private static final String TIME_STAMP_NAME = "'TRIM'_yyyyMMdd_HHmmss"; - private File mSrcFile = null; - private File mDstFile = null; - private File mSaveDirectory = null; - // For showing the result. - private String saveFolderName = null; + private SaveVideoFileInfo mDstFileInfo = null; @Override public void onCreate(Bundle savedInstanceState) { @@ -223,41 +220,6 @@ public class TrimVideo extends Activity implements mController.showPaused(); } - // Copy from SaveCopyTask.java in terms of how to handle the destination - // path and filename : querySource() and getSaveDirectory(). - private interface ContentResolverQueryCallback { - void onCursorResult(Cursor cursor); - } - - private void querySource(String[] projection, ContentResolverQueryCallback callback) { - ContentResolver contentResolver = getContentResolver(); - Cursor cursor = null; - try { - cursor = contentResolver.query(mUri, projection, null, null, null); - if ((cursor != null) && cursor.moveToNext()) { - callback.onCursorResult(cursor); - } - } catch (Exception e) { - // Ignore error for lacking the data column from the source. - } finally { - if (cursor != null) { - cursor.close(); - } - } - } - - private File getSaveDirectory() { - final File[] dir = new File[1]; - querySource(new String[] { - VideoColumns.DATA }, new ContentResolverQueryCallback() { - - @Override - public void onCursorResult(Cursor cursor) { - dir[0] = new File(cursor.getString(0)).getParentFile(); - } - }); - return dir[0]; - } private boolean isModified() { int delta = mTrimEndTime - mTrimStartTime; @@ -270,22 +232,12 @@ public class TrimVideo extends Activity implements return true; } } + private void trimVideo() { - // Use the default save directory if the source directory cannot be - // saved. - mSaveDirectory = getSaveDirectory(); - if ((mSaveDirectory == null) || !mSaveDirectory.canWrite()) { - mSaveDirectory = new File(Environment.getExternalStorageDirectory(), - BucketNames.DOWNLOAD); - saveFolderName = getString(R.string.folder_download); - } else { - saveFolderName = mSaveDirectory.getName(); - } - mSaveFileName = new SimpleDateFormat(TIME_STAMP_NAME).format( - new Date(System.currentTimeMillis())); - mDstFile = new File(mSaveDirectory, mSaveFileName + ".mp4"); - mSrcFile = new File(mSrcVideoPath); + mDstFileInfo = SaveVideoFileUtils.getDstMp4FileInfo(TIME_STAMP_NAME, + getContentResolver(), mUri, getString(R.string.folder_download)); + final File mSrcFile = new File(mSrcVideoPath); showProgressDialog(); @@ -293,9 +245,11 @@ public class TrimVideo extends Activity implements @Override public void run() { try { - TrimVideoUtils.startTrim(mSrcFile, mDstFile, mTrimStartTime, mTrimEndTime); + VideoUtils.startTrim(mSrcFile, mDstFileInfo.mFile, + mTrimStartTime, mTrimEndTime, mVideoView.getDuration()); // Update the database for adding a new video file. - insertContent(mDstFile); + SaveVideoFileUtils.insertContent(mDstFileInfo, + getContentResolver(), mUri); } catch (IOException e) { e.printStackTrace(); } @@ -304,7 +258,7 @@ public class TrimVideo extends Activity implements @Override public void run() { Toast.makeText(getApplicationContext(), - getString(R.string.save_into) + " " + saveFolderName, + getString(R.string.save_into, mDstFileInfo.mFolderName), Toast.LENGTH_SHORT) .show(); // TODO: change trimming into a service to avoid @@ -314,7 +268,7 @@ public class TrimVideo extends Activity implements mProgress = null; // Show the result only when the activity not stopped. Intent intent = new Intent(android.content.Intent.ACTION_VIEW); - intent.setDataAndTypeAndNormalize(Uri.fromFile(mDstFile), "video/*"); + intent.setDataAndTypeAndNormalize(Uri.fromFile(mDstFileInfo.mFile), "video/*"); intent.putExtra(MediaStore.EXTRA_FINISH_ON_COMPLETION, false); startActivity(intent); finish(); @@ -337,53 +291,6 @@ public class TrimVideo extends Activity implements mProgress.show(); } - /** - * Insert the content (saved file) with proper video properties. - */ - private Uri insertContent(File file) { - long nowInMs = System.currentTimeMillis(); - long nowInSec = nowInMs / 1000; - final ContentValues values = new ContentValues(12); - values.put(Video.Media.TITLE, mSaveFileName); - values.put(Video.Media.DISPLAY_NAME, file.getName()); - values.put(Video.Media.MIME_TYPE, "video/mp4"); - values.put(Video.Media.DATE_TAKEN, nowInMs); - values.put(Video.Media.DATE_MODIFIED, nowInSec); - values.put(Video.Media.DATE_ADDED, nowInSec); - values.put(Video.Media.DATA, file.getAbsolutePath()); - values.put(Video.Media.SIZE, file.length()); - // Copy the data taken and location info from src. - String[] projection = new String[] { - VideoColumns.DATE_TAKEN, - VideoColumns.LATITUDE, - VideoColumns.LONGITUDE, - VideoColumns.RESOLUTION, - }; - - // Copy some info from the source file. - querySource(projection, new ContentResolverQueryCallback() { - @Override - public void onCursorResult(Cursor cursor) { - long timeTaken = cursor.getLong(0); - if (timeTaken > 0) { - values.put(Video.Media.DATE_TAKEN, timeTaken); - } - double latitude = cursor.getDouble(1); - double longitude = cursor.getDouble(2); - // TODO: Change || to && after the default location issue is - // fixed. - if ((latitude != 0f) || (longitude != 0f)) { - values.put(Video.Media.LATITUDE, latitude); - values.put(Video.Media.LONGITUDE, longitude); - } - values.put(Video.Media.RESOLUTION, cursor.getString(3)); - - } - }); - - return getContentResolver().insert(Video.Media.EXTERNAL_CONTENT_URI, values); - } - @Override public void onPlayPause() { if (mVideoView.isPlaying()) { diff --git a/src/com/android/gallery3d/app/TrimVideoUtils.java b/src/com/android/gallery3d/app/VideoUtils.java index ae9b1e9ce..8ffc3d5eb 100644 --- a/src/com/android/gallery3d/app/TrimVideoUtils.java +++ b/src/com/android/gallery3d/app/VideoUtils.java @@ -19,6 +19,7 @@ package com.android.gallery3d.app; +import com.android.gallery3d.util.SaveVideoFileInfo; import com.coremedia.iso.IsoFile; import com.coremedia.iso.boxes.TimeToSampleBox; import com.googlecode.mp4parser.authoring.Movie; @@ -36,12 +37,46 @@ import java.util.Arrays; import java.util.LinkedList; import java.util.List; -/** - * Shortens/Crops a track - */ -public class TrimVideoUtils { +public class VideoUtils { - public static void startTrim(File src, File dst, int startMs, int endMs) throws IOException { + public static void startMute(String filePath, SaveVideoFileInfo dstFileInfo) throws IOException { + File dst = dstFileInfo.mFile; + File src = new File(filePath); + RandomAccessFile randomAccessFile = new RandomAccessFile(src, "r"); + Movie movie = MovieCreator.build(randomAccessFile.getChannel()); + + // remove all tracks we will create new tracks from the old + List<Track> tracks = movie.getTracks(); + movie.setTracks(new LinkedList<Track>()); + + for (Track track : tracks) { + if (track.getHandler().equals("vide")) { + movie.addTrack(track); + } + } + writeMovieIntoFile(dst, movie); + randomAccessFile.close(); + } + + private static void writeMovieIntoFile(File dst, Movie movie) + throws IOException { + if (!dst.exists()) { + dst.createNewFile(); + } + + IsoFile out = new DefaultMp4Builder().build(movie); + FileOutputStream fos = new FileOutputStream(dst); + FileChannel fc = fos.getChannel(); + out.getBox(fc); // This one build up the memory. + + fc.close(); + fos.close(); + } + + /** + * Shortens/Crops a track + */ + public static void startTrim(File src, File dst, int startMs, int endMs, int totalMs) throws IOException { RandomAccessFile randomAccessFile = new RandomAccessFile(src, "r"); Movie movie = MovieCreator.build(randomAccessFile.getChannel()); @@ -100,18 +135,7 @@ public class TrimVideoUtils { } movie.addTrack(new CroppedTrack(track, startSample, endSample)); } - IsoFile out = new DefaultMp4Builder().build(movie); - - if (!dst.exists()) { - dst.createNewFile(); - } - - FileOutputStream fos = new FileOutputStream(dst); - FileChannel fc = fos.getChannel(); - out.getBox(fc); // This one build up the memory. - - fc.close(); - fos.close(); + writeMovieIntoFile(dst, movie); randomAccessFile.close(); } |