1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-12-23 19:31:30 +00:00

Moved functions accessing syncthing API to new class RestApi.

This commit is contained in:
Felix Ableitner 2014-05-26 13:37:17 +02:00
parent 808ed14b27
commit b1574aaee1
4 changed files with 90 additions and 20 deletions

View file

@ -1,17 +1,22 @@
package com.nutomic.syncthingandroid; package com.nutomic.syncthingandroid;
import android.annotation.TargetApi; import android.annotation.TargetApi;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.ServiceConnection;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.IBinder;
import android.preference.Preference; import android.preference.Preference;
import android.preference.PreferenceActivity; import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen; import android.preference.PreferenceScreen;
import android.support.v4.app.NavUtils; import android.support.v4.app.NavUtils;
import android.view.MenuItem; import android.view.MenuItem;
import com.nutomic.syncthingandroid.syncthing.GetTask; import com.nutomic.syncthingandroid.syncthing.SyncthingService;
import com.nutomic.syncthingandroid.syncthing.SyncthingServiceBinder;
public class SettingsActivity extends PreferenceActivity { public class SettingsActivity extends PreferenceActivity {
@ -19,6 +24,23 @@ public class SettingsActivity extends PreferenceActivity {
private static final String SYNCTHING_VERSION_KEY = "syncthing_version"; private static final String SYNCTHING_VERSION_KEY = "syncthing_version";
private SyncthingService mSyncthingService;
private ServiceConnection mSyncthingServiceConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
SyncthingServiceBinder binder = (SyncthingServiceBinder) service;
mSyncthingService = binder.getService();
final PreferenceScreen screen = getPreferenceScreen();
final Preference version = screen.findPreference(SYNCTHING_VERSION_KEY);
version.setSummary(mSyncthingService.getApi().getVersion());
}
public void onServiceDisconnected(ComponentName className) {
mSyncthingService = null;
}
};
/** /**
* Loads layout, sets version from Rest API. * Loads layout, sets version from Rest API.
* *
@ -35,19 +57,17 @@ public class SettingsActivity extends PreferenceActivity {
getActionBar().setDisplayHomeAsUpEnabled(true); getActionBar().setDisplayHomeAsUpEnabled(true);
} }
bindService(new Intent(this, SyncthingService.class),
mSyncthingServiceConnection, Context.BIND_AUTO_CREATE);
addPreferencesFromResource(R.xml.settings); addPreferencesFromResource(R.xml.settings);
final PreferenceScreen screen = getPreferenceScreen();
final Preference version = screen.findPreference(SYNCTHING_VERSION_KEY);
new GetTask() {
@Override
protected void onPostExecute(String versionName) {
version.setSummary((versionName != null)
? versionName
: getString(R.string.syncthing_version_error));
}
}.execute(GetTask.URI_VERSION);
} }
@Override
public void onDestroy() {
super.onDestroy();
unbindService(mSyncthingServiceConnection);
}
/** /**
* Opens issue tracker when that preference is clicked. * Opens issue tracker when that preference is clicked.

View file

@ -89,7 +89,7 @@ public class WebGuiActivity extends Activity implements SyncthingService.OnWebGu
getApplicationContext().startService( getApplicationContext().startService(
new Intent(this, SyncthingService.class)); new Intent(this, SyncthingService.class));
getApplicationContext().bindService( bindService(
new Intent(this, SyncthingService.class), new Intent(this, SyncthingService.class),
mSyncthingServiceConnection, Context.BIND_AUTO_CREATE); mSyncthingServiceConnection, Context.BIND_AUTO_CREATE);
} }
@ -102,11 +102,10 @@ public class WebGuiActivity extends Activity implements SyncthingService.OnWebGu
mWebView.loadUrl(SyncthingService.SYNCTHING_URL); mWebView.loadUrl(SyncthingService.SYNCTHING_URL);
} }
@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
getApplicationContext().unbindService(mSyncthingServiceConnection); unbindService(mSyncthingServiceConnection);
} }
@Override @Override

View file

@ -0,0 +1,40 @@
package com.nutomic.syncthingandroid.syncthing;
import android.content.Context;
import com.nutomic.syncthingandroid.R;
/**
* Provides functions to interact with the syncthing REST API.
*/
public class RestApi implements SyncthingService.OnWebGuiAvailableListener {
private Context mContext;
private String mVersion;
public RestApi(Context context) {
mContext = context;
}
@Override
public void onWebGuiAvailable() {
new GetTask() {
@Override
protected void onPostExecute(String versionName) {
mVersion = (versionName != null)
? versionName
: mContext.getString(R.string.syncthing_version_error);
}
}.execute(GetTask.URI_VERSION);
}
public String getVersion() {
return mVersion;
}
public void shutdown() {
new PostTask().execute(PostTask.URI_SHUTDOWN);
}
}

View file

@ -57,11 +57,6 @@ public class SyncthingService extends Service {
*/ */
private static final String BINARY_NAME = "lib/libsyncthing.so"; private static final String BINARY_NAME = "lib/libsyncthing.so";
/**
* URL of the local syncthing web UI.
*/
public static final String SYNCTHING_URL = "http://127.0.0.1:8080";
/** /**
* Interval in ms, at which connections to the web gui are performed on first start * Interval in ms, at which connections to the web gui are performed on first start
* to find out if it's online. * to find out if it's online.
@ -73,6 +68,15 @@ public class SyncthingService extends Service {
*/ */
private static final String CONFIG_FILE = "config.xml"; private static final String CONFIG_FILE = "config.xml";
/**
* URL of the local syncthing web UI.
*
* TODO: read from config.
*/
public static final String SYNCTHING_URL = "http://127.0.0.1:8080";
private RestApi mApi;
private final SyncthingServiceBinder mBinder = new SyncthingServiceBinder(this); private final SyncthingServiceBinder mBinder = new SyncthingServiceBinder(this);
/** /**
@ -237,6 +241,9 @@ 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);
mApi = new RestApi(this);
registerOnWebGuiAvailableListener(mApi);
new Thread(new NativeSyncthingRunnable()).start(); new Thread(new NativeSyncthingRunnable()).start();
new PollWebGuiAvailableTask().execute(); new PollWebGuiAvailableTask().execute();
} }
@ -252,7 +259,7 @@ public class SyncthingService extends Service {
@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
new PostTask().execute(PostTask.URI_SHUTDOWN); mApi.shutdown();
} }
/** /**
@ -370,4 +377,8 @@ public class SyncthingService extends Service {
} }
} }
public RestApi getApi() {
return mApi;
}
} }