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.app;
18
19import android.app.Activity;
20import android.app.ProgressDialog;
21import android.content.Intent;
22import android.net.Uri;
23import android.os.Handler;
24import android.provider.MediaStore;
25import android.widget.Toast;
26
27import com.android.gallery3d.R;
28import com.android.gallery3d.data.MediaItem;
29import com.android.gallery3d.util.SaveVideoFileInfo;
30import com.android.gallery3d.util.SaveVideoFileUtils;
31
32import java.io.IOException;
33
34public class MuteVideo {
35
36    private ProgressDialog mMuteProgress;
37
38    private String mFilePath = null;
39    private Uri mUri = null;
40    private SaveVideoFileInfo mDstFileInfo = null;
41    private Activity mActivity = null;
42    private final Handler mHandler = new Handler();
43
44    final String TIME_STAMP_NAME = "'MUTE'_yyyyMMdd_HHmmss";
45
46    public MuteVideo(String filePath, Uri uri, Activity activity) {
47        mUri = uri;
48        mFilePath = filePath;
49        mActivity = activity;
50    }
51
52    public void muteInBackground() {
53        mDstFileInfo = SaveVideoFileUtils.getDstMp4FileInfo(TIME_STAMP_NAME,
54                mActivity.getContentResolver(), mUri,
55                mActivity.getString(R.string.folder_download));
56
57        showProgressDialog();
58        new Thread(new Runnable() {
59                @Override
60            public void run() {
61                try {
62                    VideoUtils.startMute(mFilePath, mDstFileInfo);
63                    SaveVideoFileUtils.insertContent(
64                            mDstFileInfo, mActivity.getContentResolver(), mUri);
65                } catch (IOException e) {
66                    Toast.makeText(mActivity, mActivity.getString(R.string.video_mute_err),
67                            Toast.LENGTH_SHORT).show();
68                }
69                // After muting is done, trigger the UI changed.
70                mHandler.post(new Runnable() {
71                        @Override
72                    public void run() {
73                        Toast.makeText(mActivity.getApplicationContext(),
74                                mActivity.getString(R.string.save_into,
75                                        mDstFileInfo.mFolderName),
76                                Toast.LENGTH_SHORT)
77                                .show();
78
79                        if (mMuteProgress != null) {
80                            mMuteProgress.dismiss();
81                            mMuteProgress = null;
82
83                            // Show the result only when the activity not
84                            // stopped.
85                            Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
86                            intent.setDataAndType(Uri.fromFile(mDstFileInfo.mFile), "video/*");
87                            intent.putExtra(MediaStore.EXTRA_FINISH_ON_COMPLETION, false);
88                            mActivity.startActivity(intent);
89                        }
90                    }
91                });
92            }
93        }).start();
94    }
95
96    private void showProgressDialog() {
97        mMuteProgress = new ProgressDialog(mActivity);
98        mMuteProgress.setTitle(mActivity.getString(R.string.muting));
99        mMuteProgress.setMessage(mActivity.getString(R.string.please_wait));
100        mMuteProgress.setCancelable(false);
101        mMuteProgress.setCanceledOnTouchOutside(false);
102        mMuteProgress.show();
103    }
104}
105