From 14fa4dc7085adc6b10a45fac92d01a719d49a869 Mon Sep 17 00:00:00 2001 From: Joachim Fritzsch Date: Mon, 3 Jun 2013 16:24:23 +0200 Subject: [PATCH] android: polling all_projects_list moved into client startup routing to avoid network on UI thread --- .../boinc/AttachProjectListActivity.java | 57 +++---------------- .../edu/berkeley/boinc/ProjectsActivity.java | 7 +++ .../berkeley/boinc/client/ClientStatus.java | 10 +++- .../edu/berkeley/boinc/client/Monitor.java | 37 +++++++----- 4 files changed, 46 insertions(+), 65 deletions(-) diff --git a/android/BOINC/src/edu/berkeley/boinc/AttachProjectListActivity.java b/android/BOINC/src/edu/berkeley/boinc/AttachProjectListActivity.java index f53a78a32c..df39af3f0a 100644 --- a/android/BOINC/src/edu/berkeley/boinc/AttachProjectListActivity.java +++ b/android/BOINC/src/edu/berkeley/boinc/AttachProjectListActivity.java @@ -25,15 +25,11 @@ import edu.berkeley.boinc.client.Monitor; import edu.berkeley.boinc.rpc.ProjectInfo; import android.app.Activity; import android.app.Dialog; -import android.app.Service; -import android.content.ComponentName; import android.content.Context; import android.content.Intent; -import android.content.ServiceConnection; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Bundle; -import android.os.IBinder; import android.util.Log; import android.view.View; import android.view.Window; @@ -46,29 +42,11 @@ import android.widget.Toast; public class AttachProjectListActivity extends Activity implements android.view.View.OnClickListener{ private final String TAG = "BOINC AttachProjectListActivity"; - - private Monitor monitor; - private Boolean mIsBound; private ListView lv; private AttachProjectListAdapter listAdapter; private Dialog manualUrlInputDialog; - private ServiceConnection mConnection = new ServiceConnection() { - public void onServiceConnected(ComponentName className, IBinder service) { - // This is called when the connection with the service has been established, getService returns the Monitor object that is needed to call functions. - monitor = ((Monitor.LocalBinder)service).getService(); - mIsBound = true; - - populateView(); - } - - public void onServiceDisconnected(ComponentName className) { // This should not happen - monitor = null; - mIsBound = false; - } - }; - @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -76,33 +54,8 @@ public class AttachProjectListActivity extends Activity implements android.view. Log.d(TAG, "onCreate"); - //bind monitor service - doBindService(); - } - - @Override - protected void onDestroy() { - Log.d(TAG, "onDestroy"); - doUnbindService(); - super.onDestroy(); - } - - private void doBindService() { - // Establish a connection with the service, onServiceConnected gets called when - bindService(new Intent(this, Monitor.class), mConnection, Service.BIND_AUTO_CREATE); - } - - private void doUnbindService() { - if (mIsBound) { - // Detach existing connection. - unbindService(mConnection); - mIsBound = false; - } - } - - private void populateView(){ - //retrieve projects from monitor - ArrayList data = monitor.getAndroidProjectsList(); + //get supported projects + ArrayList data = Monitor.getClientStatus().supportedProjects; Log.d(TAG,"monitor.getAndroidProjectsList returned with " + data.size() + " elements"); // setup layout @@ -113,6 +66,12 @@ public class AttachProjectListActivity extends Activity implements android.view. // set title bar getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar); + } + + @Override + protected void onDestroy() { + Log.d(TAG, "onDestroy"); + super.onDestroy(); } // check whether device is online before starting connection attempt diff --git a/android/BOINC/src/edu/berkeley/boinc/ProjectsActivity.java b/android/BOINC/src/edu/berkeley/boinc/ProjectsActivity.java index b1c9261264..32f20ef61c 100644 --- a/android/BOINC/src/edu/berkeley/boinc/ProjectsActivity.java +++ b/android/BOINC/src/edu/berkeley/boinc/ProjectsActivity.java @@ -363,8 +363,15 @@ public class ProjectsActivity extends FragmentActivity { private final String TAG = "ProjectOperationAsync"; + @Override + protected void onPreExecute() { + Log.d(TAG,"onPreExecute"); + super.onPreExecute(); + } + @Override protected Boolean doInBackground(String... params) { + Log.d(TAG,"doInBackground"); try{ String url = params[0]; Integer operation = Integer.parseInt(params[1]); diff --git a/android/BOINC/src/edu/berkeley/boinc/client/ClientStatus.java b/android/BOINC/src/edu/berkeley/boinc/client/ClientStatus.java index 074ceb37a5..29dd9b4661 100644 --- a/android/BOINC/src/edu/berkeley/boinc/client/ClientStatus.java +++ b/android/BOINC/src/edu/berkeley/boinc/client/ClientStatus.java @@ -41,6 +41,7 @@ import edu.berkeley.boinc.R; import edu.berkeley.boinc.rpc.CcStatus; import edu.berkeley.boinc.rpc.GlobalPreferences; import edu.berkeley.boinc.rpc.Project; +import edu.berkeley.boinc.rpc.ProjectInfo; import edu.berkeley.boinc.rpc.Result; import edu.berkeley.boinc.rpc.Transfer; import edu.berkeley.boinc.utils.BOINCDefs; @@ -85,7 +86,7 @@ public class ClientStatus { public Integer computingSuspendReason = 0; //reason why computing got suspended, only if COMPUTING_STATUS_SUSPENDED private Boolean computingParseError = false; //indicates that status could not be parsed and is therefore invalid - //network status + // network status public Integer networkStatus = 2; public static final int NETWORK_STATUS_NEVER = 0; public static final int NETWORK_STATUS_SUSPENDED = 1; @@ -93,6 +94,9 @@ public class ClientStatus { public Integer networkSuspendReason = 0; //reason why network activity got suspended, only if NETWORK_STATUS_SUSPENDED private Boolean networkParseError = false; //indicates that status could not be parsed and is therefore invalid + // supported projects + public ArrayList supportedProjects; + public ClientStatus(Context ctx) { this.ctx = ctx; @@ -190,6 +194,10 @@ public class ClientStatus { this.prefs = prefs; } + public synchronized ArrayList getSupprtedProjects () { + return supportedProjects; + } + public synchronized CcStatus getClientStatus() { if(results == null) { //check in case monitor is not set up yet (e.g. while logging in) Log.d(TAG, "state is null"); diff --git a/android/BOINC/src/edu/berkeley/boinc/client/Monitor.java b/android/BOINC/src/edu/berkeley/boinc/client/Monitor.java index 7e2c0f1b03..7d51873fe3 100644 --- a/android/BOINC/src/edu/berkeley/boinc/client/Monitor.java +++ b/android/BOINC/src/edu/berkeley/boinc/client/Monitor.java @@ -165,6 +165,8 @@ public class Monitor extends Service { // read preferences for GUI to be able to display data GlobalPreferences clientPrefs = rpc.getGlobalPrefsWorkingStruct(); Monitor.getClientStatus().setPrefs(clientPrefs); + // read supported projects + readAndroidProjectsList(); } if(connected) { @@ -451,6 +453,26 @@ public class Monitor extends Service { } } + // reads all_project_list.xml from Client and filters + // projects not supporting Android. List does not change + // during run-time. Called once during setup. + // Stored in ClientStatus. + private void readAndroidProjectsList() { + ArrayList allProjects = rpc.getAllProjectsList(); + ArrayList androidProjects = new ArrayList(); + + //filter projects that do not support Android + for (ProjectInfo project: allProjects) { + if(project.platforms.contains(getString(R.string.boinc_platform_name))) { + Log.d(TAG, project.name + " supports " + getString(R.string.boinc_platform_name)); + androidProjects.add(project); + } + } + + // set list in ClientStatus + getClientStatus().supportedProjects = androidProjects; + } + public static ClientStatus getClientStatus() { //singleton pattern if (clientStatus == null) { Log.d(TAG,"WARNING: clientStatus not yet initialized"); @@ -465,7 +487,6 @@ public class Monitor extends Service { return appPrefs; } - /* * returns this class, allows clients to access this service's functions and attributes. */ @@ -661,20 +682,6 @@ public class Monitor extends Service { return authKey; } - public ArrayList getAndroidProjectsList() { - ArrayList allProjects = rpc.getAllProjectsList(); - ArrayList androidProjects = new ArrayList(); - - //filter projects that do not support Android - for (ProjectInfo project: allProjects) { - if(project.platforms.contains(getString(R.string.boinc_platform_name))) { - Log.d(TAG, project.name + " supports " + getString(R.string.boinc_platform_name)); - androidProjects.add(project); - } - } - return androidProjects; - } - public ProjectConfig getProjectConfig(String url) { ProjectConfig config = null;