1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-25 22:01:16 +00:00

Additional info level logging.

This commit is contained in:
Felix Ableitner 2014-05-27 01:24:37 +02:00
parent a8b84a21e5
commit fa749da4a6
4 changed files with 35 additions and 23 deletions

View file

@ -15,30 +15,27 @@ import java.io.InputStream;
import java.io.InputStreamReader;
/**
* Performs a GET request with no parameters to the URL in uri[0] and
* Performs a GET request with no parameters to the URL in uri[0] with the path in uri[1] and
* returns the result as a String.
*/
public class GetTask extends AsyncTask<String, Void, String> {
private static final String TAG = "GetTask";
/**
* URI to call to get version name.
*/
public static final String URI_VERSION = "/rest/version";
@Override
protected String doInBackground(String... uri) {
String fullUri = uri[0] + uri[1];
Log.i(TAG, "Sending GET request to " + fullUri);
HttpClient httpclient = new DefaultHttpClient();
HttpGet get = new HttpGet(uri[0] + uri[1]);
HttpGet get = new HttpGet(fullUri);
String responseString = null;
try {
HttpResponse response = httpclient.execute(get);
HttpEntity entity = response.getEntity();
if (entity != null) {
// A Simple JSON Response Read
InputStream is = entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
@ -52,7 +49,7 @@ public class GetTask extends AsyncTask<String, Void, String> {
}
}
catch (IOException e) {
Log.w(TAG, "Failed to call Rest API at " + uri[0], e);
Log.w(TAG, "Failed to call Rest API at " + fullUri, e);
}
return null;
}

View file

@ -11,27 +11,26 @@ import org.apache.http.impl.client.DefaultHttpClient;
import java.io.IOException;
/**
* Performs a POST request with no parameters to the URL in uri[0].
* Performs a POST request with no parameters to the URL in uri[0] with the path in uri[1].
*/
public class PostTask extends AsyncTask<String, Void, Void> {
private static final String TAG = "PostTask";
/**
* URI to call for shutdown.
*/
public static final String URI_SHUTDOWN = "/rest/shutdown";
@Override
protected Void doInBackground(String... uri) {
String fullUri = uri[0] + uri[1];
Log.i(TAG, "Sending POST request to " + fullUri);
HttpClient httpclient = new DefaultHttpClient();
HttpPost post = new HttpPost(uri[0] + uri[1]);
HttpPost post = new HttpPost(fullUri);
String responseString = null;
try {
HttpResponse response = httpclient.execute(post);
}
catch (IOException e) {
Log.w(TAG, "Failed to call Rest API at " + uri[0], e);
Log.w(TAG, "Failed to call Rest API at " + fullUri, e);
}
return null;
}

View file

@ -1,6 +1,7 @@
package com.nutomic.syncthingandroid.syncthing;
import android.content.Context;
import android.util.Log;
import com.nutomic.syncthingandroid.R;
@ -9,6 +10,8 @@ import com.nutomic.syncthingandroid.R;
*/
public class RestApi implements SyncthingService.OnWebGuiAvailableListener {
private static final String TAG = "RestApi";
private Context mContext;
private String mVersion;
@ -31,10 +34,9 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener {
public void onWebGuiAvailable() {
new GetTask() {
@Override
protected void onPostExecute(String versionName) {
mVersion = (versionName != null)
? versionName
: mContext.getString(R.string.syncthing_version_error);
protected void onPostExecute(String version) {
mVersion = version;
Log.i(TAG, "Syncthing version is " + mVersion);
}
}.execute(mUrl, GetTask.URI_VERSION);
}
@ -43,7 +45,9 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener {
* Returns the version name, or a (text) error message on failure.
*/
public String getVersion() {
return mVersion;
return (mVersion != null)
? mVersion
: mContext.getString(R.string.syncthing_version_error);
}
/**

View file

@ -201,6 +201,7 @@ public class SyncthingService extends Service {
@Override
protected void onPostExecute(Void aVoid) {
Log.i(TAG, "Web GUI has come online at " + mApi.getUrl());
mIsWebGuiAvailable = true;
for (OnWebGuiAvailableListener listener : mOnWebGuiAvailableListeners) {
listener.onWebGuiAvailable();
@ -229,6 +230,8 @@ public class SyncthingService extends Service {
@Override
public void run() {
if (isFirstStart(SyncthingService.this)) {
Log.i(TAG, "App started for the first time. " +
"Copying default config, keys will be generated automatically");
copyDefaultConfig();
}
updateConfig();
@ -240,14 +243,19 @@ public class SyncthingService extends Service {
Element options = (Element)
d.getDocumentElement().getElementsByTagName("gui").item(0);
syncthingUrl = options.getElementsByTagName("address").item(0).getTextContent();
} catch (SAXException e) {
}
catch (SAXException e) {
throw new RuntimeException("Failed to read gui url, aborting", e);
} catch (ParserConfigurationException e) {
}
catch (ParserConfigurationException e) {
throw new RuntimeException("Failed to read gui url, aborting", e);
} catch (IOException e) {
}
catch (IOException e) {
throw new RuntimeException("Failed to read gui url, aborting", e);
} finally {
}
finally {
mApi = new RestApi(SyncthingService.this, "http://" + syncthingUrl);
Log.i(TAG, "Web GUI will be available at " + mApi.getUrl());
registerOnWebGuiAvailableListener(mApi);
}
new PollWebGuiAvailableTask().execute();
@ -267,6 +275,7 @@ public class SyncthingService extends Service {
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "Shutting down service");
mApi.shutdown();
}
@ -299,6 +308,7 @@ public class SyncthingService extends Service {
*/
private void updateConfig() {
try {
Log.i(TAG, "Checking for needed config updates");
boolean changed = false;
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document d = db.parse(getConfigFile());
@ -321,6 +331,7 @@ public class SyncthingService extends Service {
Element r = (Element) repos.item(i);
if (!r.hasAttribute("ignorePerms") ||
!Boolean.parseBoolean(r.getAttribute("ignorePerms"))) {
Log.i(TAG, "Set 'ignorePerms' on repository " + r.getAttribute("id"));
r.setAttribute("ignorePerms", Boolean.toString(true));
changed = true;
}
@ -328,6 +339,7 @@ public class SyncthingService extends Service {
// Write the changes back to file.
if (changed) {
Log.i(TAG, "Writing updated config back to file");
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource domSource = new DOMSource(d);