1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-26 06:11:19 +00:00

Fixed crash on first start after install.

This commit is contained in:
Felix Ableitner 2014-05-27 00:55:42 +02:00
parent d6025a5e6b
commit ce7773bac7

View file

@ -106,70 +106,63 @@ public class SyncthingService extends Service {
return super.getMessage() + "\n" + mLog; return super.getMessage() + "\n" + mLog;
} }
} }
/** /**
* Runs the syncthing binary from command line, and prints its output to logcat. * Runs the syncthing binary from command line, and prints its output to logcat (on exit).
*/ */
private class NativeSyncthingRunnable implements Runnable { private void runNative() {
@Override DataOutputStream dos = null;
public void run() throws NativeExecutionException { InputStreamReader isr = null;
if (isFirstStart(SyncthingService.this)) { int ret = 1;
copyDefaultConfig(); String log = "";
} try {
updateConfig(); Process p = Runtime.getRuntime().exec("sh");
dos = new DataOutputStream(p.getOutputStream());
// Set home directory to data folder for syncthing to use.
dos.writeBytes("HOME=" + getApplicationInfo().dataDir + "\n");
// Call syncthing with -home (as it would otherwise use "~/.config/syncthing/".
dos.writeBytes(getApplicationInfo().dataDir + "/" + BINARY_NAME + " " +
"-home " + getApplicationInfo().dataDir + "\n");
dos.writeBytes("exit\n");
dos.flush();
DataOutputStream dos = null; ret = p.waitFor();
InputStreamReader isr = null;
int ret = 1;
String log = "";
try {
Process p = Runtime.getRuntime().exec("sh");
dos = new DataOutputStream(p.getOutputStream());
// Set home directory to data folder for syncthing to use.
dos.writeBytes("HOME=" + getApplicationInfo().dataDir + "\n");
// Call syncthing with -home (as it would otherwise use "~/.config/syncthing/".
dos.writeBytes(getApplicationInfo().dataDir + "/" + BINARY_NAME + " " +
"-home " + getApplicationInfo().dataDir + "\n");
dos.writeBytes("exit\n");
dos.flush();
ret = p.waitFor(); // Write syncthing binary output to log.
// NOTE: This is only done on shutdown, not live.
// Write syncthing binary output to log. isr = new InputStreamReader(p.getInputStream());
// NOTE: This is only done on shutdown, not live. BufferedReader stdout = new BufferedReader(isr);
isr = new InputStreamReader(p.getInputStream()); String line;
BufferedReader stdout = new BufferedReader(isr); while((line = stdout.readLine()) != null) {
String line; log += "stderr: " + line + "\n";
while((line = stdout.readLine()) != null) { Log.w(TAG, "stderr: " + line);
log += "stderr: " + line + "\n";
Log.w(TAG, "stderr: " + line);
}
isr = new InputStreamReader(p.getErrorStream());
BufferedReader stderr = new BufferedReader(isr);
while((line = stderr.readLine()) != null) {
log += "stdout: " + line + "\n";
Log.i(TAG, "stdout: " + line);
}
} }
catch(IOException e) { isr = new InputStreamReader(p.getErrorStream());
Log.e(TAG, "Failed to execute syncthing binary or read output", e); BufferedReader stderr = new BufferedReader(isr);
while((line = stderr.readLine()) != null) {
log += "stdout: " + line + "\n";
Log.i(TAG, "stdout: " + line);
} }
catch(InterruptedException e) { }
Log.e(TAG, "Failed to execute syncthing binary or read output", e); catch(IOException e) {
Log.e(TAG, "Failed to execute syncthing binary or read output", e);
}
catch(InterruptedException e) {
Log.e(TAG, "Failed to execute syncthing binary or read output", e);
}
finally {
if (ret != 0) {
stopSelf();
// Include the log for Play Store crash reports.
throw new NativeExecutionException("Syncthing binary returned error code " +
Integer.toString(ret), log);
} }
finally { try {
if (ret != 0) { dos.close();
stopSelf(); isr.close();
// Include the log for Play Store crash reports. }
throw new NativeExecutionException("Syncthing binary returned error code " + catch (IOException e) {
Integer.toString(ret), log); Log.w(TAG, "Failed to close stream", e);
}
try {
dos.close();
isr.close();
}
catch (IOException e) {
Log.w(TAG, "Failed to close stream", e);
}
} }
} }
} }
@ -232,30 +225,35 @@ public class SyncthingService extends Service {
n.flags |= Notification.FLAG_ONGOING_EVENT; n.flags |= Notification.FLAG_ONGOING_EVENT;
startForeground(NOTIFICATION_ID, n); startForeground(NOTIFICATION_ID, n);
String syncthingUrl = null; new Thread(new Runnable() {
try { @Override
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); public void run() {
Document d = db.parse(getConfigFile()); if (isFirstStart(SyncthingService.this)) {
Element options = (Element) copyDefaultConfig();
d.getDocumentElement().getElementsByTagName("gui").item(0); }
syncthingUrl = options.getElementsByTagName("address").item(0).getTextContent(); updateConfig();
}
catch (SAXException e) {
throw new RuntimeException("Failed to read gui url, aborting", e);
}
catch (ParserConfigurationException e) {
throw new RuntimeException("Failed to read gui url, aborting", e);
}
catch (IOException e) {
throw new RuntimeException("Failed to read gui url, aborting", e);
}
finally {
mApi = new RestApi(this, "http://" + syncthingUrl);
registerOnWebGuiAvailableListener(mApi);
}
new Thread(new NativeSyncthingRunnable()).start(); String syncthingUrl = null;
new PollWebGuiAvailableTask().execute(); try {
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document d = db.parse(getConfigFile());
Element options = (Element)
d.getDocumentElement().getElementsByTagName("gui").item(0);
syncthingUrl = options.getElementsByTagName("address").item(0).getTextContent();
} catch (SAXException e) {
throw new RuntimeException("Failed to read gui url, aborting", e);
} catch (ParserConfigurationException e) {
throw new RuntimeException("Failed to read gui url, aborting", e);
} catch (IOException e) {
throw new RuntimeException("Failed to read gui url, aborting", e);
} finally {
mApi = new RestApi(SyncthingService.this, "http://" + syncthingUrl);
registerOnWebGuiAvailableListener(mApi);
}
new PollWebGuiAvailableTask().execute();
runNative();
}
}).start();
} }
@Override @Override
@ -347,7 +345,7 @@ public class SyncthingService extends Service {
Log.w(TAG, "Failed to parse config", e); Log.w(TAG, "Failed to parse config", e);
} }
catch (TransformerException e) { catch (TransformerException e) {
Log.d(TAG, "Failed to save updated config", e); Log.w(TAG, "Failed to save updated config", e);
} }
} }