Many improvements and bug fixes

Close #114
This commit is contained in:
topjohnwu
2017-02-21 03:30:37 +08:00
parent 8eba05ed4a
commit a3f0ef8e77
33 changed files with 304 additions and 212 deletions
@@ -10,8 +10,9 @@ public class CallbackEvent<Result> {
private Set<Listener<Result>> listeners;
public void register(Listener<Result> l) {
if (listeners == null)
if (listeners == null) {
listeners = new HashSet<>();
}
listeners.add(l);
}
@@ -20,8 +21,9 @@ public class CallbackEvent<Result> {
}
public void unRegister(Listener<Result> l) {
if (listeners != null)
if (listeners != null) {
listeners.remove(l);
}
}
public void trigger() {
@@ -32,8 +34,9 @@ public class CallbackEvent<Result> {
result = r;
isTriggered = true;
if (listeners != null) {
for (Listener<Result> listener : listeners)
for (Listener<Result> listener : listeners) {
listener.onTrigger(this);
}
}
}
@@ -120,11 +120,13 @@ public class Shell {
StreamGobbler STDOUT;
// Create the default shell if not init
if (!newShell && !isInit)
if (!newShell && !isInit) {
init();
}
if (!newShell && !rootAccess())
if (!newShell && !rootAccess()) {
return null;
}
if (newShell) {
res = Collections.synchronizedList(new ArrayList<String>());
@@ -114,8 +114,9 @@ public class Utils {
"echo \"${BOOTIMAGE##*/}\""
};
List<String> ret = Shell.su(commands);
if (isValidShellResponse(ret))
if (isValidShellResponse(ret)) {
return ret.get(0);
}
return null;
}
@@ -77,10 +77,11 @@ public class WebService {
StringBuilder result = new StringBuilder();
boolean first = true;
for (Map.Entry<String, String> entry : params.entrySet()) {
if (first)
if (first) {
first = false;
else
} else {
result.append("&");
}
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
@@ -112,14 +112,17 @@ public class ZipUtils {
// Remove the top directory from the path
path = entry.getName().substring(entry.getName().indexOf("/") + 1);
// If it's the top folder, ignore it
if (path.isEmpty())
if (path.isEmpty()) {
continue;
}
// Don't include placeholder
if (path.contains("system/placeholder"))
if (path.contains("system/placeholder")) {
continue;
}
dest.putNextEntry(new JarEntry(path));
while((size = source.read(buffer, 0, 2048)) != -1)
while((size = source.read(buffer, 0, 2048)) != -1) {
dest.write(buffer, 0, size);
}
}
source.close();
dest.close();
@@ -149,17 +152,20 @@ public class ZipUtils {
Enumeration<JarEntry> e = zipfile.entries();
while(e.hasMoreElements()) {
entry = e.nextElement();
if (!entry.getName().contains(path) || entry.isDirectory())
if (!entry.getName().contains(path) || entry.isDirectory()){
// Ignore directories, only create files
continue;
}
Logger.dev("ZipUtils: Extracting: " + entry);
is = zipfile.getInputStream(entry);
dest = new File(folder, entry.getName());
if (dest.getParentFile().mkdirs())
if (dest.getParentFile().mkdirs()) {
dest.createNewFile();
}
out = new FileOutputStream(dest);
while ((count = is.read(data, 0, 4096)) != -1)
while ((count = is.read(data, 0, 4096)) != -1) {
out.write(data, 0, count);
}
out.flush();
out.close();
is.close();
@@ -178,16 +184,19 @@ public class ZipUtils {
byte data[] = new byte[4096];
try (JarInputStream zipfile = new JarInputStream(file)) {
while((entry = zipfile.getNextJarEntry()) != null) {
if (!entry.getName().contains(path) || entry.isDirectory())
if (!entry.getName().contains(path) || entry.isDirectory()) {
// Ignore directories, only create files
continue;
}
Logger.dev("ZipUtils: Extracting: " + entry);
dest = new File(folder, entry.getName());
if (dest.getParentFile().mkdirs())
if (dest.getParentFile().mkdirs()) {
dest.createNewFile();
}
out = new FileOutputStream(dest);
while ((count = zipfile.read(data, 0, 4096)) != -1)
while ((count = zipfile.read(data, 0, 4096)) != -1) {
out.write(data, 0, count);
}
out.flush();
out.close();
}