Skip to content

Commit 9f06d89

Browse files
author
Javier Manzano
committed
Array of strings on ShellCommand to allow paths with spaces
1 parent 1372638 commit 9f06d89

File tree

7 files changed

+36
-21
lines changed

7 files changed

+36
-21
lines changed

FFmpegAndroid/src/androidTest/java/com/github/hiteshsondhi88/libffmpeg/ShellCommandTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class ShellCommandTest extends CommonTestCase {
1414

1515
public void testRun() throws Exception {
1616
ShellCommand shellCommand = new ShellCommand();
17-
final Process process = shellCommand.run("logcat");
17+
final Process process = shellCommand.run(new String[] {"logcat"});
1818
assertNotNull(process);
1919
assertEquals(false, Util.isProcessCompleted(process));
2020

@@ -44,7 +44,7 @@ public void run() {
4444

4545
public void testRunWaitFor() throws Exception {
4646
ShellCommand shellCommand = new ShellCommand();
47-
CommandResult commandResult = shellCommand.runWaitFor("ls");
47+
CommandResult commandResult = shellCommand.runWaitFor(new String[] {"ls"});
4848
assertNotNull(commandResult);
4949
assertEquals(true, commandResult.success);
5050
assertThat(commandResult.output).isNotEmpty();

FFmpegAndroid/src/main/java/com/github/hiteshsondhi88/libffmpeg/FFmpeg.java

+20-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.content.Context;
44
import android.text.TextUtils;
55

6+
import java.lang.reflect.Array;
67
import java.util.Map;
78

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

6364
@Override
64-
public void execute(Map<String, String> environvenmentVars, String cmd, FFmpegExecuteResponseHandler ffmpegExecuteResponseHandler) throws FFmpegCommandAlreadyRunningException {
65+
public void execute(Map<String, String> environvenmentVars, String[] cmd, FFmpegExecuteResponseHandler ffmpegExecuteResponseHandler) throws FFmpegCommandAlreadyRunningException {
6566
if (ffmpegExecuteAsyncTask != null && !ffmpegExecuteAsyncTask.isProcessCompleted()) {
6667
throw new FFmpegCommandAlreadyRunningException("FFmpeg command is already running, you are only allowed to run single command at a time");
6768
}
68-
if (!TextUtils.isEmpty(cmd)) {
69-
String ffmpegCmd = FileUtils.getFFmpeg(context, environvenmentVars) + " "+ cmd;
70-
ffmpegExecuteAsyncTask = new FFmpegExecuteAsyncTask(ffmpegCmd, timeout, ffmpegExecuteResponseHandler);
69+
if (cmd.length != 0) {
70+
String[] ffmpegBinary = new String[] { FileUtils.getFFmpeg(context, environvenmentVars) };
71+
String[] command = concatenate(ffmpegBinary, cmd);
72+
ffmpegExecuteAsyncTask = new FFmpegExecuteAsyncTask(command , timeout, ffmpegExecuteResponseHandler);
7173
ffmpegExecuteAsyncTask.execute();
7274
} else {
7375
throw new IllegalArgumentException("shell command cannot be empty");
7476
}
7577
}
7678

79+
public <T> T[] concatenate (T[] a, T[] b) {
80+
int aLen = a.length;
81+
int bLen = b.length;
82+
83+
@SuppressWarnings("unchecked")
84+
T[] c = (T[]) Array.newInstance(a.getClass().getComponentType(), aLen + bLen);
85+
System.arraycopy(a, 0, c, 0, aLen);
86+
System.arraycopy(b, 0, c, aLen, bLen);
87+
88+
return c;
89+
}
90+
7791
@Override
78-
public void execute(String cmd, FFmpegExecuteResponseHandler ffmpegExecuteResponseHandler) throws FFmpegCommandAlreadyRunningException {
92+
public void execute(String[] cmd, FFmpegExecuteResponseHandler ffmpegExecuteResponseHandler) throws FFmpegCommandAlreadyRunningException {
7993
execute(null, cmd, ffmpegExecuteResponseHandler);
8094
}
8195

8296
@Override
8397
public String getDeviceFFmpegVersion() throws FFmpegCommandAlreadyRunningException {
8498
ShellCommand shellCommand = new ShellCommand();
85-
CommandResult commandResult = shellCommand.runWaitFor(FileUtils.getFFmpeg(context) + " -version");
99+
CommandResult commandResult = shellCommand.runWaitFor(new String[] { FileUtils.getFFmpeg(context), "-version" });
86100
if (commandResult.success) {
87101
return commandResult.output.split(" ")[2];
88102
}

FFmpegAndroid/src/main/java/com/github/hiteshsondhi88/libffmpeg/FFmpegExecuteAsyncTask.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99

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

12-
private final String cmd;
12+
private final String[] cmd;
1313
private final FFmpegExecuteResponseHandler ffmpegExecuteResponseHandler;
1414
private final ShellCommand shellCommand;
1515
private final long timeout;
1616
private long startTime;
1717
private Process process;
1818
private String output = "";
1919

20-
FFmpegExecuteAsyncTask(String cmd, long timeout, FFmpegExecuteResponseHandler ffmpegExecuteResponseHandler) {
20+
FFmpegExecuteAsyncTask(String[] cmd, long timeout, FFmpegExecuteResponseHandler ffmpegExecuteResponseHandler) {
2121
this.cmd = cmd;
2222
this.timeout = timeout;
2323
this.ffmpegExecuteResponseHandler = ffmpegExecuteResponseHandler;

FFmpegAndroid/src/main/java/com/github/hiteshsondhi88/libffmpeg/FFmpegInterface.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ interface FFmpegInterface {
2222
* @param ffmpegExecuteResponseHandler {@link FFmpegExecuteResponseHandler}
2323
* @throws FFmpegCommandAlreadyRunningException
2424
*/
25-
public void execute(Map<String, String> environvenmentVars, String cmd, FFmpegExecuteResponseHandler ffmpegExecuteResponseHandler) throws FFmpegCommandAlreadyRunningException;
25+
public void execute(Map<String, String> environvenmentVars, String[] cmd, FFmpegExecuteResponseHandler ffmpegExecuteResponseHandler) throws FFmpegCommandAlreadyRunningException;
2626

2727
/**
2828
* Executes a command
2929
* @param cmd command to execute
3030
* @param ffmpegExecuteResponseHandler {@link FFmpegExecuteResponseHandler}
3131
* @throws FFmpegCommandAlreadyRunningException
3232
*/
33-
public void execute(String cmd, FFmpegExecuteResponseHandler ffmpegExecuteResponseHandler) throws FFmpegCommandAlreadyRunningException;
33+
public void execute(String[] cmd, FFmpegExecuteResponseHandler ffmpegExecuteResponseHandler) throws FFmpegCommandAlreadyRunningException;
3434

3535
/**
3636
* Tells FFmpeg version currently on device

FFmpegAndroid/src/main/java/com/github/hiteshsondhi88/libffmpeg/ShellCommand.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class ShellCommand {
66

7-
Process run(String commandString) {
7+
Process run(String[] commandString) {
88
Process process = null;
99
try {
1010
process = Runtime.getRuntime().exec(commandString);
@@ -14,7 +14,7 @@ Process run(String commandString) {
1414
return process;
1515
}
1616

17-
CommandResult runWaitFor(String s) {
17+
CommandResult runWaitFor(String[] s) {
1818
Process process = run(s);
1919

2020
Integer exitValue = null;

app/src/main/java/com/github/hiteshsondhi88/sampleffmpeg/Home.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public void onFailure() {
7575
}
7676
}
7777

78-
private void execFFmpegBinary(final String command) {
78+
private void execFFmpegBinary(final String[] command) {
7979
try {
8080
ffmpeg.execute(command, new ExecuteBinaryResponseHandler() {
8181
@Override
@@ -142,8 +142,9 @@ public void onClick(DialogInterface dialog, int which) {
142142
public void onClick(View v) {
143143
switch (v.getId()) {
144144
case R.id.run_command:
145-
String command = commandEditText.getText().toString();
146-
if (!TextUtils.isEmpty(command)) {
145+
String cmd = commandEditText.getText().toString();
146+
String[] command = cmd.split(" ");
147+
if (command.length != 0) {
147148
execFFmpegBinary(command);
148149
} else {
149150
Toast.makeText(Home.this, getString(R.string.empty_command_toast), Toast.LENGTH_LONG).show();

build.gradle

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
jcenter()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:1.1.0'
8+
classpath 'com.android.tools.build:gradle:1.1.2'
99

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

2121
ext {
2222
compileSdkVersion = 22
23-
buildToolsVersion = '22'
23+
buildToolsVersion = '21.1.2'
2424
targetSdkVersion = 22
2525
minSdkVersion = 16
2626
versionCode = 25
27-
versionName = "0.2.5"
28-
}
27+
versionName = "0.2.6"
28+
}

0 commit comments

Comments
 (0)