android: interface for project added

This commit is contained in:
Joachim Fritzsch 2013-01-13 19:24:08 +01:00 committed by Oliver Bock
parent 6d9f42590e
commit 028fb5827c
5 changed files with 83 additions and 49 deletions

View File

@ -36,7 +36,6 @@
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
@ -52,6 +51,9 @@
</intent-filter>
</activity>
<service android:name=".client.Monitor"></service>
<service android:name="edu.berkeley.boinc.client.ClientRemoteService"
android:enabled="true"
android:exported="true"></service>
<receiver android:name=".receiver.LogReceiver">
<intent-filter android:priority="999">
<action android:name="edu.berkeley.boinc.log" />

View File

@ -1,8 +1,10 @@
package edu.berkeley.boinc.client;
import edu.berkeley.boinc.rpc.AccountOut;
import edu.berkeley.boinc.rpc.ProjectAttachReply;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
@ -14,7 +16,7 @@ public class ClientRemoteService extends Service {
private final String TAG = "ClientRemoteService";
private Monitor monitor;
private Boolean mIsBound = false;
private Boolean mIsMonitorBound = false;
@Override
public void onCreate() {
@ -35,17 +37,29 @@ public class ClientRemoteService extends Service {
private final IClientRemoteService.Stub mBinder = new IClientRemoteService.Stub() {
@Override
public boolean isReady() throws RemoteException {
return mIsMonitorBound;
}
@Override
public boolean attachProject(String packageName, String url, String id, String pwd) throws RemoteException {
// TODO store packageName in AppPreferences
if(mIsBound) {
if(mIsMonitorBound) {
return monitor.attachProject(url, id, pwd);
} else {Log.e(TAG, "could not attach project, service not bound!"); return false;}
}
@Override
public boolean checkProjectAttached(String url) throws RemoteException {
if(mIsMonitorBound) {
return monitor.checkProjectAttached(url);
} else {Log.e(TAG, "could not attach project, service not bound!"); return false;}
}
@Override
public AccountOut verifyCredentials(String url, String id, String pwd) throws RemoteException {
if(mIsBound) {
if(mIsMonitorBound) {
return monitor.lookupCredentials(url, id, pwd);
} else {Log.e(TAG, "could not verify credentials, service not bound!"); return null;}
}
@ -53,21 +67,21 @@ public class ClientRemoteService extends Service {
@Override
public boolean detachProject(String packageName, String url) throws RemoteException {
// TODO remove packageName in AppPreferences
if(mIsBound) {
if(mIsMonitorBound) {
return monitor.detachProject(url);
} else {Log.e(TAG, "could not detach project, service not bound!"); return false;}
}
@Override
public AccountOut createAccount(String url, String email, String userName, String pwd, String teamName) throws RemoteException {
if(mIsBound) {
if(mIsMonitorBound) {
return monitor.createAccount(url, email, userName, pwd, teamName);
} else {Log.e(TAG, "could not create account, service not bound!"); return null;}
}
@Override
public String getRpcAuthToken() throws RemoteException {
if(mIsBound) {
if(mIsMonitorBound) {
return monitor.readAuthToken();
} else {Log.e(TAG, "could not read auth token, service not bound!"); return null;}
}
@ -81,25 +95,26 @@ public class ClientRemoteService extends Service {
public void onServiceConnected(ComponentName className, IBinder service) {
Log.d(TAG,"onServiceConnected - local Monitor service bound.");
monitor = ((Monitor.LocalBinder)service).getService();
mIsBound = true;
mIsMonitorBound = true;
}
public void onServiceDisconnected(ComponentName className) {
monitor = null;
mIsBound = false;
mIsMonitorBound = false;
}
};
private void doBindService() {
if(!mIsBound) {
getApplicationContext().bindService(new Intent(this, Monitor.class), mConnection, 0);
if(!mIsMonitorBound) {
Log.d(TAG, "binding local Monitor service...");
getApplicationContext().bindService(new Intent(this, Monitor.class), mConnection, Context.BIND_AUTO_CREATE);
}
}
private void doUnbindService() {
if (mIsBound) {
if(mIsMonitorBound) {
getApplicationContext().unbindService(mConnection);
mIsBound = false;
mIsMonitorBound = false;
}
}

View File

@ -30,6 +30,10 @@ import edu.berkeley.boinc.rpc.AccountOut;
interface IClientRemoteService {
/* Checks whether interface recipient is ready to serve commands.
* returns success*/
boolean isReady();
//== project management ==
/* Attach project to BOINC application.
* packageName: package name of Android application causing this attach
@ -39,6 +43,11 @@ interface IClientRemoteService {
* returns success*/
boolean attachProject(in String packageName, in String url, in String id, in String authenticator);
/* Check whether given project URL is attached to BOINC
* url: project URL
* returns success*/
boolean checkProjectAttached(in String url);
/* Verifies given credentials to project account
* url: project URL
* id: project login identifier, i.e. email address

View File

@ -108,12 +108,28 @@ public class Monitor extends Service{
return mBinder;
}
//onCreate is life-cycle method of service. regardless of bound or started service, this method gets called once upon first creation.
@Override
public void onCreate() {
Log.d(TAG,"onCreate()");
//populate attributes with XML resource values
clientName = getString(R.string.client_name);
authFileName = getString(R.string.auth_file_name);
clientPath = getString(R.string.client_path);
// initialize singleton helper classes and provide application context
getClientStatus().setCtx(this);
getAppPrefs().readPrefs(this);
if(!started) {
started = true;
(new ClientMonitorAsync()).execute(new Integer[0]); //start monitor in new thread
Log.d(TAG, "asynchronous monitor started!");
}
else {
Log.d(TAG, "asynchronous monitor NOT started!");
}
}
@Override
@ -123,37 +139,12 @@ public class Monitor extends Service{
Boolean autostart = false;
try {
autostart = intent.getBooleanExtra("autostart", false); //if true, received intent is for autostart and got fired by the BootReceiver on start up.
}
catch (NullPointerException e) { // occurs, when onStartCommand is called with a null intent. Occurs on re-start, if START_STICKY is used.
Log.d(TAG,"NullPointerException, intent flags: " + flags);
}
getAppPrefs().readPrefs(this); //create singleton AppPreferences prefs with current application context
/*
* start monitor if either
* the user's preference autostart is enabled and the intent carries the autostart flag (intent from BootReceiver)
* or it is not an autostart-intent (not from BootReceiver) and the service hasnt been started yet
*/
Log.d(TAG, "values: intent-autostart " + autostart + " - prefs-autostart " + appPrefs.getAutostart() + " - started " + started);
if((!autostart && !started) || (autostart && appPrefs.getAutostart())) {
started = true;
Log.d(TAG, "starting service sticky & setup start of monitor...");
getClientStatus().setCtx(this);
if(autostart) {
// show notification about started service in notification panel
showNotification();
}
(new ClientMonitorAsync()).execute(new Integer[0]); //start monitor in new thread
Log.d(TAG, "asynchronous monitor started!");
}
else {
Log.d(TAG, "asynchronous monitor NOT started!");
autostart = intent.getBooleanExtra("autostart", false);
} catch (Exception e) {}
if(autostart) {
// show notification about started service in notification panel
// only necessary
showNotification();
}
/*
* START_NOT_STICKY is now used and replaced START_STICKY in previous implementations.
@ -290,6 +281,11 @@ public class Monitor extends Service{
return success;
}
public Boolean checkProjectAttached(String url) {
//TODO
return false;
}
public AccountOut lookupCredentials(String url, String id, String pwd) {
Integer retval = -1;
AccountOut auth = null;

View File

@ -18,20 +18,32 @@
******************************************************************************/
package edu.berkeley.boinc.receiver;
import edu.berkeley.boinc.AppPreferences;
import edu.berkeley.boinc.client.Monitor;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class BootReceiver extends BroadcastReceiver {
private final String TAG = "BootReceiver";
@Override
public void onReceive(Context context, Intent intent) {
// check whether preference "autostart" is enabled is done within the service
Intent startServiceIntent = new Intent(context, Monitor.class);
startServiceIntent.putExtra("autostart", true);
context.startService(startServiceIntent);
//TODO untested!
AppPreferences prefs = new AppPreferences();
prefs.readPrefs(context);
if(prefs.getAutostart()) {
Log.d(TAG,"autostart enabled, start Monitor...");
Intent startServiceIntent = new Intent(context, Monitor.class);
startServiceIntent.putExtra("autostart", true);
context.startService(startServiceIntent);
} else {
// do nothing
Log.d(TAG,"autostart disabeld");
}
}
}