|
1 | 1 | package com.github.hiteshsondhi88.sampleffmpeg; |
2 | 2 |
|
3 | 3 | import android.app.Activity; |
| 4 | +import android.app.AlertDialog; |
| 5 | +import android.app.ProgressDialog; |
| 6 | +import android.content.DialogInterface; |
4 | 7 | import android.os.Bundle; |
| 8 | +import android.text.TextUtils; |
5 | 9 | import android.util.Log; |
| 10 | +import android.view.View; |
| 11 | +import android.widget.Button; |
| 12 | +import android.widget.EditText; |
| 13 | +import android.widget.LinearLayout; |
| 14 | +import android.widget.TextView; |
| 15 | +import android.widget.Toast; |
6 | 16 |
|
7 | 17 | import javax.inject.Inject; |
8 | 18 |
|
| 19 | +import butterknife.ButterKnife; |
| 20 | +import butterknife.InjectView; |
9 | 21 | import dagger.ObjectGraph; |
| 22 | + |
| 23 | +import com.github.hiteshsondhi88.libffmpeg.ExecuteBinaryResponseHandler; |
10 | 24 | import com.github.hiteshsondhi88.libffmpeg.FFmpeg; |
11 | | -import com.github.hiteshsondhi88.libffmpeg.FFmpegLoadBinaryResponseHandler; |
| 25 | +import com.github.hiteshsondhi88.libffmpeg.LoadBinaryResponseHandler; |
| 26 | +import com.github.hiteshsondhi88.libffmpeg.exceptions.FFmpegCommandAlreadyRunningException; |
12 | 27 | import com.github.hiteshsondhi88.libffmpeg.exceptions.FFmpegNotSupportedException; |
13 | 28 |
|
14 | | -public class Home extends Activity { |
| 29 | +public class Home extends Activity implements View.OnClickListener { |
15 | 30 |
|
16 | 31 | private static final String TAG = Home.class.getSimpleName(); |
17 | 32 |
|
18 | 33 | @Inject |
19 | 34 | FFmpeg ffmpeg; |
20 | 35 |
|
| 36 | + @InjectView(R.id.command) |
| 37 | + EditText commandEditText; |
| 38 | + |
| 39 | + @InjectView(R.id.command_output) |
| 40 | + LinearLayout outputLayout; |
| 41 | + |
| 42 | + @InjectView(R.id.run_command) |
| 43 | + Button runButton; |
| 44 | + |
| 45 | + private ProgressDialog progressDialog; |
| 46 | + |
21 | 47 | @Override |
22 | 48 | protected void onCreate(Bundle savedInstanceState) { |
23 | 49 | super.onCreate(savedInstanceState); |
24 | 50 | setContentView(R.layout.activity_home); |
25 | | - |
| 51 | + ButterKnife.inject(this); |
26 | 52 | ObjectGraph.create(new DaggerDependencyModule(this)).inject(this); |
27 | 53 |
|
| 54 | + loadFFMpegBinary(); |
| 55 | + initUI(); |
| 56 | + } |
| 57 | + |
| 58 | + private void initUI() { |
| 59 | + runButton.setOnClickListener(this); |
| 60 | + |
| 61 | + progressDialog = new ProgressDialog(this); |
| 62 | + progressDialog.setTitle(null); |
| 63 | + } |
| 64 | + |
| 65 | + private void loadFFMpegBinary() { |
28 | 66 | try { |
29 | | - ffmpeg.loadBinary(new FFmpegLoadBinaryResponseHandler() { |
| 67 | + ffmpeg.loadBinary(new LoadBinaryResponseHandler() { |
| 68 | + @Override |
| 69 | + public void onFailure() { |
| 70 | + showUnsupportedExceptionDialog(); |
| 71 | + } |
| 72 | + }); |
| 73 | + } catch (FFmpegNotSupportedException e) { |
| 74 | + showUnsupportedExceptionDialog(); |
| 75 | + } |
| 76 | + } |
30 | 77 |
|
| 78 | + private void execFFmpegBinary(final String command) { |
| 79 | + try { |
| 80 | + ffmpeg.execute(command, new ExecuteBinaryResponseHandler() { |
31 | 81 | @Override |
32 | | - public void onStart() { |
33 | | - // Do Nothing |
| 82 | + public void onFailure(String s) { |
| 83 | + addTextViewToLayout("FAILED with output : "+s); |
34 | 84 | } |
35 | 85 |
|
36 | 86 | @Override |
37 | | - public void onFailure() { |
38 | | - Log.e(TAG, "FFmpeg loading failed"); |
| 87 | + public void onSuccess(String s) { |
| 88 | + addTextViewToLayout("SUCCESS with output : "+s); |
39 | 89 | } |
40 | 90 |
|
41 | 91 | @Override |
42 | | - public void onSuccess() { |
43 | | - Log.e(TAG, "FFmpeg loading success"); |
| 92 | + public void onProgress(String s) { |
| 93 | + Log.d(TAG, "Started command : ffmpeg "+command); |
| 94 | + addTextViewToLayout("progress : "+s); |
| 95 | + progressDialog.setMessage("Processing\n"+s); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void onStart() { |
| 100 | + outputLayout.removeAllViews(); |
| 101 | + |
| 102 | + Log.d(TAG, "Started command : ffmpeg " + command); |
| 103 | + progressDialog.setMessage("Processing..."); |
| 104 | + progressDialog.show(); |
44 | 105 | } |
45 | 106 |
|
46 | 107 | @Override |
47 | 108 | public void onFinish() { |
48 | | - Log.e(TAG, "FFmpeg loading finished"); |
| 109 | + Log.d(TAG, "Finished command : ffmpeg "+command); |
| 110 | + progressDialog.dismiss(); |
49 | 111 | } |
50 | 112 | }); |
51 | | - } catch (FFmpegNotSupportedException e) { |
52 | | - Log.e(TAG, "FFmpeg not supported", e); |
| 113 | + } catch (FFmpegCommandAlreadyRunningException e) { |
| 114 | + // do nothing for now |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + private void addTextViewToLayout(String text) { |
| 119 | + TextView textView = new TextView(Home.this); |
| 120 | + textView.setText(text); |
| 121 | + outputLayout.addView(textView); |
| 122 | + } |
| 123 | + |
| 124 | + private void showUnsupportedExceptionDialog() { |
| 125 | + new AlertDialog.Builder(Home.this) |
| 126 | + .setIcon(android.R.drawable.ic_dialog_alert) |
| 127 | + .setTitle(getString(R.string.device_not_supported)) |
| 128 | + .setMessage(getString(R.string.device_not_supported_message)) |
| 129 | + .setCancelable(false) |
| 130 | + .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
| 131 | + @Override |
| 132 | + public void onClick(DialogInterface dialog, int which) { |
| 133 | + Home.this.finish(); |
| 134 | + } |
| 135 | + }) |
| 136 | + .create() |
| 137 | + .show(); |
| 138 | + |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public void onClick(View v) { |
| 143 | + switch (v.getId()) { |
| 144 | + case R.id.run_command: |
| 145 | + String command = commandEditText.getText().toString(); |
| 146 | + if (!TextUtils.isEmpty(command)) { |
| 147 | + execFFmpegBinary(command); |
| 148 | + } else { |
| 149 | + Toast.makeText(Home.this, getString(R.string.empty_command_toast), Toast.LENGTH_LONG).show(); |
| 150 | + } |
| 151 | + break; |
53 | 152 | } |
54 | 153 | } |
55 | 154 | } |
0 commit comments