Skip to content

Commit

Permalink
Array of strings on ShellCommand to allow paths with spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Javier Manzano committed Apr 13, 2015
1 parent 1372638 commit 9f06d89
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ShellCommandTest extends CommonTestCase {

public void testRun() throws Exception {
ShellCommand shellCommand = new ShellCommand();
final Process process = shellCommand.run("logcat");
final Process process = shellCommand.run(new String[] {"logcat"});
assertNotNull(process);
assertEquals(false, Util.isProcessCompleted(process));

Expand Down Expand Up @@ -44,7 +44,7 @@ public void run() {

public void testRunWaitFor() throws Exception {
ShellCommand shellCommand = new ShellCommand();
CommandResult commandResult = shellCommand.runWaitFor("ls");
CommandResult commandResult = shellCommand.runWaitFor(new String[] {"ls"});
assertNotNull(commandResult);
assertEquals(true, commandResult.success);
assertThat(commandResult.output).isNotEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.Context;
import android.text.TextUtils;

import java.lang.reflect.Array;
import java.util.Map;

import com.github.hiteshsondhi88.libffmpeg.exceptions.FFmpegCommandAlreadyRunningException;
Expand Down Expand Up @@ -61,28 +62,41 @@ public void loadBinary(FFmpegLoadBinaryResponseHandler ffmpegLoadBinaryResponseH
}

@Override
public void execute(Map<String, String> environvenmentVars, String cmd, FFmpegExecuteResponseHandler ffmpegExecuteResponseHandler) throws FFmpegCommandAlreadyRunningException {
public void execute(Map<String, String> environvenmentVars, String[] cmd, FFmpegExecuteResponseHandler ffmpegExecuteResponseHandler) throws FFmpegCommandAlreadyRunningException {
if (ffmpegExecuteAsyncTask != null && !ffmpegExecuteAsyncTask.isProcessCompleted()) {
throw new FFmpegCommandAlreadyRunningException("FFmpeg command is already running, you are only allowed to run single command at a time");
}
if (!TextUtils.isEmpty(cmd)) {
String ffmpegCmd = FileUtils.getFFmpeg(context, environvenmentVars) + " "+ cmd;
ffmpegExecuteAsyncTask = new FFmpegExecuteAsyncTask(ffmpegCmd, timeout, ffmpegExecuteResponseHandler);
if (cmd.length != 0) {
String[] ffmpegBinary = new String[] { FileUtils.getFFmpeg(context, environvenmentVars) };
String[] command = concatenate(ffmpegBinary, cmd);
ffmpegExecuteAsyncTask = new FFmpegExecuteAsyncTask(command , timeout, ffmpegExecuteResponseHandler);
ffmpegExecuteAsyncTask.execute();
} else {
throw new IllegalArgumentException("shell command cannot be empty");
}
}

public <T> T[] concatenate (T[] a, T[] b) {
int aLen = a.length;
int bLen = b.length;

@SuppressWarnings("unchecked")
T[] c = (T[]) Array.newInstance(a.getClass().getComponentType(), aLen + bLen);
System.arraycopy(a, 0, c, 0, aLen);
System.arraycopy(b, 0, c, aLen, bLen);

return c;
}

@Override
public void execute(String cmd, FFmpegExecuteResponseHandler ffmpegExecuteResponseHandler) throws FFmpegCommandAlreadyRunningException {
public void execute(String[] cmd, FFmpegExecuteResponseHandler ffmpegExecuteResponseHandler) throws FFmpegCommandAlreadyRunningException {
execute(null, cmd, ffmpegExecuteResponseHandler);
}

@Override
public String getDeviceFFmpegVersion() throws FFmpegCommandAlreadyRunningException {
ShellCommand shellCommand = new ShellCommand();
CommandResult commandResult = shellCommand.runWaitFor(FileUtils.getFFmpeg(context) + " -version");
CommandResult commandResult = shellCommand.runWaitFor(new String[] { FileUtils.getFFmpeg(context), "-version" });
if (commandResult.success) {
return commandResult.output.split(" ")[2];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@

class FFmpegExecuteAsyncTask extends AsyncTask<Void, String, CommandResult> {

private final String cmd;
private final String[] cmd;
private final FFmpegExecuteResponseHandler ffmpegExecuteResponseHandler;
private final ShellCommand shellCommand;
private final long timeout;
private long startTime;
private Process process;
private String output = "";

FFmpegExecuteAsyncTask(String cmd, long timeout, FFmpegExecuteResponseHandler ffmpegExecuteResponseHandler) {
FFmpegExecuteAsyncTask(String[] cmd, long timeout, FFmpegExecuteResponseHandler ffmpegExecuteResponseHandler) {
this.cmd = cmd;
this.timeout = timeout;
this.ffmpegExecuteResponseHandler = ffmpegExecuteResponseHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ interface FFmpegInterface {
* @param ffmpegExecuteResponseHandler {@link FFmpegExecuteResponseHandler}
* @throws FFmpegCommandAlreadyRunningException
*/
public void execute(Map<String, String> environvenmentVars, String cmd, FFmpegExecuteResponseHandler ffmpegExecuteResponseHandler) throws FFmpegCommandAlreadyRunningException;
public void execute(Map<String, String> environvenmentVars, String[] cmd, FFmpegExecuteResponseHandler ffmpegExecuteResponseHandler) throws FFmpegCommandAlreadyRunningException;

/**
* Executes a command
* @param cmd command to execute
* @param ffmpegExecuteResponseHandler {@link FFmpegExecuteResponseHandler}
* @throws FFmpegCommandAlreadyRunningException
*/
public void execute(String cmd, FFmpegExecuteResponseHandler ffmpegExecuteResponseHandler) throws FFmpegCommandAlreadyRunningException;
public void execute(String[] cmd, FFmpegExecuteResponseHandler ffmpegExecuteResponseHandler) throws FFmpegCommandAlreadyRunningException;

/**
* Tells FFmpeg version currently on device
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class ShellCommand {

Process run(String commandString) {
Process run(String[] commandString) {
Process process = null;
try {
process = Runtime.getRuntime().exec(commandString);
Expand All @@ -14,7 +14,7 @@ Process run(String commandString) {
return process;
}

CommandResult runWaitFor(String s) {
CommandResult runWaitFor(String[] s) {
Process process = run(s);

Integer exitValue = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void onFailure() {
}
}

private void execFFmpegBinary(final String command) {
private void execFFmpegBinary(final String[] command) {
try {
ffmpeg.execute(command, new ExecuteBinaryResponseHandler() {
@Override
Expand Down Expand Up @@ -142,8 +142,9 @@ public void onClick(DialogInterface dialog, int which) {
public void onClick(View v) {
switch (v.getId()) {
case R.id.run_command:
String command = commandEditText.getText().toString();
if (!TextUtils.isEmpty(command)) {
String cmd = commandEditText.getText().toString();
String[] command = cmd.split(" ");
if (command.length != 0) {
execFFmpegBinary(command);
} else {
Toast.makeText(Home.this, getString(R.string.empty_command_toast), Toast.LENGTH_LONG).show();
Expand Down
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'
classpath 'com.android.tools.build:gradle:1.1.2'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand All @@ -20,9 +20,9 @@ allprojects {

ext {
compileSdkVersion = 22
buildToolsVersion = '22'
buildToolsVersion = '21.1.2'
targetSdkVersion = 22
minSdkVersion = 16
versionCode = 25
versionName = "0.2.5"
}
versionName = "0.2.6"
}

0 comments on commit 9f06d89

Please sign in to comment.