mirror of
https://github.com/syncthing/syncthing-android.git
synced 2025-01-10 20:15:54 +00:00
Cleaner solution for Gingerbread problems: use AsyncTask instead of Runnable
ref #18
This commit is contained in:
parent
5ba80e5bfa
commit
7b2ef0d6a0
1 changed files with 89 additions and 73 deletions
|
@ -6,10 +6,12 @@ import android.app.Service;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
|
import android.os.Handler;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import android.os.Looper;
|
import android.os.Looper;
|
||||||
import android.support.v4.app.NotificationCompat;
|
import android.support.v4.app.NotificationCompat;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
import android.util.Pair;
|
||||||
|
|
||||||
import com.nutomic.syncthingandroid.R;
|
import com.nutomic.syncthingandroid.R;
|
||||||
import com.nutomic.syncthingandroid.WebGuiActivity;
|
import com.nutomic.syncthingandroid.WebGuiActivity;
|
||||||
|
@ -129,39 +131,42 @@ public class SyncthingService extends Service {
|
||||||
/**
|
/**
|
||||||
* Runs the syncthing binary from command line, and prints its output to logcat (on exit).
|
* Runs the syncthing binary from command line, and prints its output to logcat (on exit).
|
||||||
*/
|
*/
|
||||||
private void runNative() {
|
private class SyncthingRunnable implements Runnable {
|
||||||
DataOutputStream dos = null;
|
@Override
|
||||||
int ret = 1;
|
public void run() {
|
||||||
Process process = null;
|
DataOutputStream dos = null;
|
||||||
try {
|
int ret = 1;
|
||||||
process = Runtime.getRuntime().exec("sh");
|
Process process = null;
|
||||||
dos = new DataOutputStream(process.getOutputStream());
|
try {
|
||||||
// Set home directory to data folder for syncthing to use.
|
process = Runtime.getRuntime().exec("sh");
|
||||||
dos.writeBytes("HOME=" + getFilesDir() + " ");
|
dos = new DataOutputStream(process.getOutputStream());
|
||||||
// Call syncthing with -home (as it would otherwise use "~/.config/syncthing/".
|
// Set home directory to data folder for syncthing to use.
|
||||||
dos.writeBytes(getApplicationInfo().dataDir + "/" + BINARY_NAME + " " +
|
dos.writeBytes("HOME=" + getFilesDir() + " ");
|
||||||
"-home " + getFilesDir() + "\n");
|
// Call syncthing with -home (as it would otherwise use "~/.config/syncthing/".
|
||||||
dos.writeBytes("exit\n");
|
dos.writeBytes(getApplicationInfo().dataDir + "/" + BINARY_NAME + " " +
|
||||||
dos.flush();
|
"-home " + getFilesDir() + "\n");
|
||||||
|
dos.writeBytes("exit\n");
|
||||||
|
dos.flush();
|
||||||
|
|
||||||
log(process.getInputStream(), Log.INFO);
|
log(process.getInputStream(), Log.INFO);
|
||||||
log(process.getErrorStream(), Log.WARN);
|
log(process.getErrorStream(), Log.WARN);
|
||||||
|
|
||||||
ret = process.waitFor();
|
ret = process.waitFor();
|
||||||
}
|
}
|
||||||
catch(IOException e) {
|
catch(IOException e) {
|
||||||
Log.e(TAG, "Failed to execute syncthing binary or read output", e);
|
Log.e(TAG, "Failed to execute syncthing binary or read output", e);
|
||||||
}
|
}
|
||||||
catch(InterruptedException e) {
|
catch(InterruptedException e) {
|
||||||
Log.e(TAG, "Failed to execute syncthing binary or read output", e);
|
Log.e(TAG, "Failed to execute syncthing binary or read output", e);
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
process.destroy();
|
process.destroy();
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
stopSelf();
|
stopSelf();
|
||||||
// Include the log for Play Store crash reports.
|
// Include the log for Play Store crash reports.
|
||||||
throw new NativeExecutionException("Syncthing binary returned error code " +
|
throw new NativeExecutionException("Syncthing binary returned error code " +
|
||||||
Integer.toString(ret), mNativeLog);
|
Integer.toString(ret), mNativeLog);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -292,50 +297,61 @@ 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);
|
||||||
|
|
||||||
new Thread(new Runnable() {
|
new StartupTask().execute();
|
||||||
@Override
|
}
|
||||||
public void run() {
|
|
||||||
Looper.prepare();
|
|
||||||
if (isFirstStart(SyncthingService.this)) {
|
|
||||||
Log.i(TAG, "App started for the first time. " +
|
|
||||||
"Copying default config, keys will be generated automatically");
|
|
||||||
copyDefaultConfig();
|
|
||||||
}
|
|
||||||
moveConfigFiles();
|
|
||||||
updateConfig();
|
|
||||||
|
|
||||||
String syncthingUrl = null;
|
/**
|
||||||
String apiKey = null;
|
* Sets up the initial configuration, updates the config when coming from an old
|
||||||
try {
|
* version, and reads syncthing URL and API key (these are passed internally as
|
||||||
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
* {@code Pair<String, String>}.
|
||||||
Document d = db.parse(getConfigFile());
|
*/
|
||||||
Element gui = (Element) d.getDocumentElement()
|
private class StartupTask extends AsyncTask<Void, Void, Pair<String, String>> {
|
||||||
.getElementsByTagName("gui").item(0);
|
@Override
|
||||||
syncthingUrl = gui.getElementsByTagName("address").item(0).getTextContent();
|
protected Pair<String, String> doInBackground(Void... voids) {Looper.prepare();
|
||||||
apiKey = gui.getElementsByTagName("apikey").item(0).getTextContent();
|
if (isFirstStart(SyncthingService.this)) {
|
||||||
}
|
Log.i(TAG, "App started for the first time. " +
|
||||||
catch (SAXException e) {
|
"Copying default config, keys will be generated automatically");
|
||||||
throw new RuntimeException("Failed to read gui url, aborting", e);
|
copyDefaultConfig();
|
||||||
}
|
|
||||||
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("http://" + syncthingUrl, apiKey);
|
|
||||||
Log.i(TAG, "Web GUI will be available at " + mApi.getUrl());
|
|
||||||
// HACK: Make sure there is no syncthing binary left running from an improper
|
|
||||||
// shutdown (eg Play Store update).
|
|
||||||
// NOTE: This will log an exception if syncthing is not actually running.
|
|
||||||
new PostTask().execute(mApi.getUrl(), PostTask.URI_SHUTDOWN, apiKey);
|
|
||||||
registerOnWebGuiAvailableListener(mApi);
|
|
||||||
}
|
|
||||||
new PollWebGuiAvailableTask().execute();
|
|
||||||
runNative();
|
|
||||||
}
|
}
|
||||||
}).start();
|
moveConfigFiles();
|
||||||
|
updateConfig();
|
||||||
|
|
||||||
|
String syncthingUrl = null;
|
||||||
|
String apiKey = null;
|
||||||
|
try {
|
||||||
|
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
||||||
|
Document d = db.parse(getConfigFile());
|
||||||
|
Element gui = (Element) d.getDocumentElement()
|
||||||
|
.getElementsByTagName("gui").item(0);
|
||||||
|
syncthingUrl = gui.getElementsByTagName("address").item(0).getTextContent();
|
||||||
|
apiKey = gui.getElementsByTagName("apikey").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 {
|
||||||
|
return new Pair<String, String>("http://" + syncthingUrl, apiKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPostExecute(Pair<String, String> urlAndKey) {
|
||||||
|
mApi = new RestApi(urlAndKey.first, urlAndKey.second);
|
||||||
|
Log.i(TAG, "Web GUI will be available at " + mApi.getUrl());
|
||||||
|
// HACK: Make sure there is no syncthing binary left running from an improper
|
||||||
|
// shutdown (eg Play Store update).
|
||||||
|
// NOTE: This will log an exception if syncthing is not actually running.
|
||||||
|
new PostTask().execute(mApi.getUrl(), PostTask.URI_SHUTDOWN, urlAndKey.second);
|
||||||
|
registerOnWebGuiAvailableListener(mApi);
|
||||||
|
new PollWebGuiAvailableTask().execute();
|
||||||
|
new Thread(new SyncthingRunnable()).start();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in a new issue