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