-
Notifications
You must be signed in to change notification settings - Fork 826
Expand file tree
/
Copy pathUtil.java
More file actions
131 lines (110 loc) · 3.79 KB
/
Util.java
File metadata and controls
131 lines (110 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package com.github.hiteshsondhi88.libffmpeg;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.os.AsyncTask;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
class Util {
static boolean isDebug(Context context) {
return (0 != (context.getApplicationContext().getApplicationInfo().flags &= ApplicationInfo.FLAG_DEBUGGABLE));
}
static void close(InputStream inputStream) {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
// Do nothing
}
}
}
static void close(OutputStream outputStream) {
if (outputStream != null) {
try {
outputStream.flush();
outputStream.close();
} catch (IOException e) {
// Do nothing
}
}
}
static String convertInputStreamToString(InputStream inputStream) {
try {
BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
String str;
StringBuilder sb = new StringBuilder();
while ((str = r.readLine()) != null) {
sb.append(str);
}
return sb.toString();
} catch (IOException e) {
Log.e("error converting input stream to string", e);
}
return null;
}
static void destroyProcess(Process process) {
if (process != null) {
try {
process.destroy();
} catch (Exception e) {
Log.e("process destroy error", e);
}
}
}
static boolean killAsync(AsyncTask asyncTask) {
return asyncTask != null && !asyncTask.isCancelled() && asyncTask.cancel(true);
}
static boolean isProcessCompleted(Process process) {
try {
if (process == null) return true;
process.exitValue();
return true;
} catch (IllegalThreadStateException e) {
// do nothing
}
return false;
}
public interface ObservePredicate {
Boolean isReadyToProceed();
}
public static FFmpegObserver observeOnce(final ObservePredicate predicate, final Runnable run, final int timeout) {
final android.os.Handler observer = new android.os.Handler();
// Enable this to detect neverending observers
// final Exception e = new RuntimeException("WTF");
final FFmpegObserver observeAction = new FFmpegObserver() {
private boolean canceled = false;
private int timeElapsed = 0;
@Override
public void run() {
if (timeElapsed + 40 > timeout) cancel();
timeElapsed += 40;
if (canceled) return;
Boolean readyToProceed = null;
try {
readyToProceed = predicate.isReadyToProceed();
} catch (Exception e) {
// Log.v("Observing " + e.getMessage());
observer.postDelayed(this, 40);
return;
}
if (readyToProceed != null && readyToProceed) {
// Log.v("Observed");
run.run();
} else {
// Enable this to detect neverending observers
// Log.v("Util", "Observing", e);
// Log.v("Observing");
observer.postDelayed(this, 40);
}
}
@Override
public void cancel() {
canceled = true;
}
};
observer.post(observeAction);
return observeAction;
}
}