/**
   Copyright (c) 2014 Rory Hool
   
   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.roryhool.videomanipulation;

import java.nio.ByteBuffer;
import java.util.Locale;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaCodec;
import android.media.MediaCodec.BufferInfo;
import android.media.MediaExtractor;
import android.media.MediaMuxer;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

@TargetApi( Build.VERSION_CODES.JELLY_BEAN_MR2 )
public class ResampleActivity extends Activity {

   Uri mInputUri = Uri.parse( "/mnt/sdcard/test2.mp4" );
   Uri mOutputUri;

   MediaCodec mDecoder;
   MediaCodec mEncoder;

   MediaExtractor mExtractor;

   MediaMuxer mMuxer;

   ByteBuffer[] mInputBuffers;
   ByteBuffer[] mOutputBuffers;

   BufferInfo mDecoderBufferInfo;
   BufferInfo mEncoderBufferInfo;

   int mEncoderWidth = 640;
   int mEncoderHeight = 480;

   int mBitRate = 2000000;

   @Override
   public void onCreate( Bundle savedInstanceState ) {
      super.onCreate( savedInstanceState );

      setContentView( R.layout.activity_resample );

      Uri data = getIntent().getData();

      if ( data != null ) {
         mInputUri = data;
      }

      String pathWithoutExtension = mInputUri.toString().replace( ".mp4", "" );

      String resampledFileName = String.format( Locale.US, "%s_resampled.mp4", pathWithoutExtension );

      mOutputUri = Uri.parse( resampledFileName );

   }

   public void onResampleClicked( View view ) {
      new ResampleTask().execute( (Void) null );
   }

   VideoResampler mTest;

   class ResampleTask extends AsyncTask<Void, Void, Void> {

      @Override
      protected Void doInBackground( Void... params ) {

         mTest = new VideoResampler();
         mTest.setInput( mInputUri );
         mTest.setOutput( mOutputUri );
         mTest.setOutputResolution( VideoResampler.WIDTH_720P, VideoResampler.HEIGHT_720P );
         mTest.setOutputBitRate( VideoResampler.BITRATE_720P );

         try {
            mTest.start();
         } catch ( Throwable e ) {
            e.printStackTrace();
         }

         return null;
      }

      @Override
      protected void onPostExecute( Void v ) {
         Intent sendIntent = new Intent();
         sendIntent.setAction( Intent.ACTION_VIEW );
         sendIntent.setDataAndType( mOutputUri, "video/mp4" );
         startActivity( sendIntent );
      }
   }
}

@@ -2164,53 +2164,37 @@
 te( 
-(Void) null );%0A   %7D%0A%0A   VideoResampler mTest;
+mInputUri, mOutputUri );%0A   %7D
 %0A%0A  
@@ -2235,20 +2235,19 @@
 ask%3C
-Void
+Uri
 , Void, 
 Void
@@ -2242,20 +2242,19 @@
 , Void, 
-Void
+Uri
 %3E %7B%0A%0A   
@@ -2282,20 +2282,19 @@
 otected 
-Void
+Uri
  doInBac
@@ -2306,21 +2306,18 @@
 nd( 
-Void... param
+Uri... uri
 s ) 
@@ -2328,21 +2328,179 @@
         
-mTest
+if ( uris.length %3C 2 ) %7B%0A            return null;%0A         %7D%0A%0A         Uri inputUri = uris%5B0%5D;%0A         Uri outputUri = uris%5B1%5D;%0A%0A         VideoResampler resampler
  = new V
@@ -2521,29 +2521,33 @@
 );%0A         
-mTest
+resampler
 .setInput( m
@@ -2545,18 +2545,17 @@
 tInput( 
-mI
+i
 nputUri 
@@ -2558,37 +2558,41 @@
 Uri );%0A         
-mTest
+resampler
 .setOutput( mOut
@@ -2587,18 +2587,17 @@
 Output( 
-mO
+o
 utputUri
@@ -2601,37 +2601,41 @@
 Uri );%0A         
-mTest
+resampler
 .setOutputResolu
@@ -2706,21 +2706,25 @@
         
-mTest
+resampler
 .setOutp
@@ -2797,13 +2797,17 @@
     
-mTest
+resampler
 .sta
@@ -2900,36 +2900,41 @@
          return 
-null
+outputUri
 ;%0A      %7D%0A%0A     
@@ -2984,14 +2984,21 @@
 te( 
-Void v
+Uri outputUri
  ) %7B
@@ -3130,18 +3130,17 @@
 ndType( 
-mO
+o
 utputUri

Use AsyncTask a bit more correctly?