mirror of
https://github.com/syncthing/syncthing-android.git
synced 2024-12-03 01:31:16 +00:00
Remove boilerplate shell code (#289)
* Util#runShellCommand: Add BufferedReader * Util: Correct class TAG * SyncthingRunnable: Use Util.runShellCommand for "chmod 500" * Util: Add notes * LogActivity: Replace ProcessBuilder with Util.runShellCommandGetOutput * SyncthingRunnable: Improve "error" message * LogActivity: Show Android log by default * SyncthingRunnable: Don't log if the chmod fail is expected on Android 5+ * LogActivity: Remove SyncthingNativeCode per line repetition if we are showing its own log
This commit is contained in:
parent
37c11836e1
commit
1bdc8fe6ec
3 changed files with 37 additions and 37 deletions
|
@ -13,6 +13,7 @@ import android.widget.ScrollView;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import com.nutomic.syncthingandroid.R;
|
import com.nutomic.syncthingandroid.R;
|
||||||
|
import com.nutomic.syncthingandroid.util.Util;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -26,8 +27,12 @@ public class LogActivity extends SyncthingActivity {
|
||||||
|
|
||||||
private final static String TAG = "LogActivity";
|
private final static String TAG = "LogActivity";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Show Android Log by default.
|
||||||
|
*/
|
||||||
|
private boolean mSyncthingLog = false;
|
||||||
|
|
||||||
private TextView mLog;
|
private TextView mLog;
|
||||||
private boolean mSyncthingLog = true;
|
|
||||||
private AsyncTask mFetchLogTask = null;
|
private AsyncTask mFetchLogTask = null;
|
||||||
private ScrollView mScrollView;
|
private ScrollView mScrollView;
|
||||||
private Intent mShareIntent;
|
private Intent mShareIntent;
|
||||||
|
@ -40,7 +45,7 @@ public class LogActivity extends SyncthingActivity {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
setContentView(R.layout.activity_log);
|
setContentView(R.layout.activity_log);
|
||||||
setTitle(R.string.syncthing_log_title);
|
setTitle(R.string.android_log_title);
|
||||||
|
|
||||||
if (savedInstanceState != null) {
|
if (savedInstanceState != null) {
|
||||||
mSyncthingLog = savedInstanceState.getBoolean("syncthingLog");
|
mSyncthingLog = savedInstanceState.getBoolean("syncthingLog");
|
||||||
|
@ -143,34 +148,12 @@ public class LogActivity extends SyncthingActivity {
|
||||||
* @param syncthingLog Filter on Syncthing's native messages.
|
* @param syncthingLog Filter on Syncthing's native messages.
|
||||||
*/
|
*/
|
||||||
private String getLog(final boolean syncthingLog) {
|
private String getLog(final boolean syncthingLog) {
|
||||||
Process process = null;
|
if (syncthingLog) {
|
||||||
try {
|
String output = Util.runShellCommandGetOutput("/system/bin/logcat -t 300 -v time -s SyncthingNativeCode", false);
|
||||||
ProcessBuilder pb;
|
return output.replaceAll("SyncthingNativeCode", "");
|
||||||
if (syncthingLog) {
|
} else {
|
||||||
pb = new ProcessBuilder("/system/bin/logcat", "-t", "300", "-v", "time", "-s", "SyncthingNativeCode");
|
return Util.runShellCommandGetOutput("/system/bin/logcat -t 300 -v time *:i ps:s art:s", false);
|
||||||
} else {
|
|
||||||
pb = new ProcessBuilder("/system/bin/logcat", "-t", "300", "-v", "time", "*:i ps:s art:s");
|
|
||||||
}
|
|
||||||
pb.redirectErrorStream(true);
|
|
||||||
process = pb.start();
|
|
||||||
BufferedReader bufferedReader = new BufferedReader(
|
|
||||||
new InputStreamReader(process.getInputStream(), "UTF-8"), 8192);
|
|
||||||
StringBuilder log = new StringBuilder();
|
|
||||||
String line;
|
|
||||||
String sep = System.getProperty("line.separator");
|
|
||||||
while ((line = bufferedReader.readLine()) != null) {
|
|
||||||
log.append(line);
|
|
||||||
log.append(sep);
|
|
||||||
}
|
|
||||||
return log.toString();
|
|
||||||
} catch (IOException e) {
|
|
||||||
Log.w(TAG, "Error reading Android log", e);
|
|
||||||
} finally {
|
|
||||||
if (process != null) {
|
|
||||||
process.destroy();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return "";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -85,6 +85,7 @@ public class SyncthingRunnable implements Runnable {
|
||||||
public SyncthingRunnable(Context context, Command command) {
|
public SyncthingRunnable(Context context, Command command) {
|
||||||
((SyncthingApp) context.getApplicationContext()).component().inject(this);
|
((SyncthingApp) context.getApplicationContext()).component().inject(this);
|
||||||
mContext = context;
|
mContext = context;
|
||||||
|
// Example: mSyncthingBinary="/data/app/com.github.catfriend1.syncthingandroid.debug-8HsN-IsVtZXc8GrE5-Hepw==/lib/x86/libsyncthing.so"
|
||||||
mSyncthingBinary = Constants.getSyncthingBinary(mContext);
|
mSyncthingBinary = Constants.getSyncthingBinary(mContext);
|
||||||
mLogFile = Constants.getLogFile(mContext);
|
mLogFile = Constants.getLogFile(mContext);
|
||||||
|
|
||||||
|
@ -131,12 +132,11 @@ public class SyncthingRunnable implements Runnable {
|
||||||
trimLogFile();
|
trimLogFile();
|
||||||
|
|
||||||
// Make sure Syncthing is executable
|
// Make sure Syncthing is executable
|
||||||
try {
|
exitCode = Util.runShellCommand("chmod 500 " + mSyncthingBinary.getPath(), false);
|
||||||
ProcessBuilder pb = new ProcessBuilder("chmod", "500", mSyncthingBinary.getPath());
|
if (exitCode == 1) {
|
||||||
Process p = pb.start();
|
LogV("chmod SyncthingNative exited with code 1 [permission denied]. This is expected on Android 5+.");
|
||||||
p.waitFor();
|
} else if (exitCode > 1) {
|
||||||
} catch (IOException | InterruptedException e) {
|
Log.w(TAG, "chmod SyncthingNative failed with exit code " + Integer.toString(exitCode));
|
||||||
Log.w(TAG, "Failed to chmod Syncthing", e);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -33,7 +33,7 @@ import eu.chainfire.libsuperuser.Shell;
|
||||||
|
|
||||||
public class Util {
|
public class Util {
|
||||||
|
|
||||||
private static final String TAG = "SyncthingUtil";
|
private static final String TAG = "Util";
|
||||||
|
|
||||||
private Util() {
|
private Util() {
|
||||||
}
|
}
|
||||||
|
@ -181,6 +181,7 @@ public class Util {
|
||||||
*/
|
*/
|
||||||
public static int runShellCommand(String cmd, Boolean useRoot) {
|
public static int runShellCommand(String cmd, Boolean useRoot) {
|
||||||
// Assume "failure" exit code if an error is caught.
|
// Assume "failure" exit code if an error is caught.
|
||||||
|
// Note: redirectErrorStream(true); System.getProperty("line.separator");
|
||||||
int exitCode = 255;
|
int exitCode = 255;
|
||||||
Process shellProc = null;
|
Process shellProc = null;
|
||||||
DataOutputStream shellOut = null;
|
DataOutputStream shellOut = null;
|
||||||
|
@ -193,6 +194,20 @@ public class Util {
|
||||||
bufferedWriter.flush();
|
bufferedWriter.flush();
|
||||||
shellOut.close();
|
shellOut.close();
|
||||||
shellOut = null;
|
shellOut = null;
|
||||||
|
BufferedReader bufferedReader = null;
|
||||||
|
try {
|
||||||
|
bufferedReader = new BufferedReader(new InputStreamReader(shellProc.getInputStream(), Charsets.UTF_8));
|
||||||
|
String line;
|
||||||
|
while ((line = bufferedReader.readLine()) != null) {
|
||||||
|
Log.v(TAG, "runShellCommand: " + line);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
Log.w(TAG, "runShellCommand: Failed to read output", e);
|
||||||
|
} finally {
|
||||||
|
if (bufferedReader != null) {
|
||||||
|
bufferedReader.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
exitCode = shellProc.waitFor();
|
exitCode = shellProc.waitFor();
|
||||||
} catch (IOException | InterruptedException e) {
|
} catch (IOException | InterruptedException e) {
|
||||||
Log.w(TAG, "runShellCommand: Exception", e);
|
Log.w(TAG, "runShellCommand: Exception", e);
|
||||||
|
@ -212,6 +227,7 @@ public class Util {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String runShellCommandGetOutput(String cmd, Boolean useRoot) {
|
public static String runShellCommandGetOutput(String cmd, Boolean useRoot) {
|
||||||
|
// Note: redirectErrorStream(true); System.getProperty("line.separator");
|
||||||
int exitCode = 255;
|
int exitCode = 255;
|
||||||
String capturedStdOut = "";
|
String capturedStdOut = "";
|
||||||
Process shellProc = null;
|
Process shellProc = null;
|
||||||
|
@ -236,8 +252,9 @@ public class Util {
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.w(TAG, "runShellCommandGetOutput: Failed to read output", e);
|
Log.w(TAG, "runShellCommandGetOutput: Failed to read output", e);
|
||||||
} finally {
|
} finally {
|
||||||
if (bufferedReader != null)
|
if (bufferedReader != null) {
|
||||||
bufferedReader.close();
|
bufferedReader.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
exitCode = shellProc.waitFor();
|
exitCode = shellProc.waitFor();
|
||||||
Log.i(TAG, "runShellCommandGetOutput: Exited with code " + exitCode);
|
Log.i(TAG, "runShellCommandGetOutput: Exited with code " + exitCode);
|
||||||
|
|
Loading…
Reference in a new issue