From a54607dd4f2d1eed16901a0ea0792ce00ba99ac7 Mon Sep 17 00:00:00 2001 From: Joachim Fritzsch Date: Sat, 18 May 2013 11:57:31 +0200 Subject: [PATCH] android: prevent wifi from turning off when boinc is running --- .../berkeley/boinc/client/ClientStatus.java | 37 +++++++++++++++++-- .../edu/berkeley/boinc/client/Monitor.java | 13 +++---- .../edu/berkeley/boinc/rpc/DeviceStatus.java | 2 +- 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/android/BOINC/src/edu/berkeley/boinc/client/ClientStatus.java b/android/BOINC/src/edu/berkeley/boinc/client/ClientStatus.java index 9c0944b5b6..e5794abac1 100644 --- a/android/BOINC/src/edu/berkeley/boinc/client/ClientStatus.java +++ b/android/BOINC/src/edu/berkeley/boinc/client/ClientStatus.java @@ -33,6 +33,7 @@ import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; +import android.net.wifi.WifiManager; import android.os.PowerManager; import android.os.PowerManager.WakeLock; import android.util.Log; @@ -53,8 +54,10 @@ public class ClientStatus { private final String TAG = "BOINC Client Status"; private Context ctx; // application context in order to fire broadcast events - //WakeLock - WakeLock wakeLock; + // CPU WakeLock + private WakeLock wakeLock; + // WiFi lock + private WifiManager.WifiLock wifiLock; //RPC wrapper private CcStatus status; @@ -93,16 +96,21 @@ public class ClientStatus { public ClientStatus(Context ctx) { this.ctx = ctx; - // set up Wake Lock + // set up CPU Wake Lock // see documentation at http://developer.android.com/reference/android/os/PowerManager.html PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE); wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); wakeLock.setReferenceCounted(false); // "one call to release() is sufficient to undo the effect of all previous calls to acquire()" + + // set up Wifi wake lock + WifiManager wm = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE); + wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL , "MyWifiLock"); + wifiLock.setReferenceCounted(false); } // call to acquire or release resources held by the WakeLock. // acquisition: every time the Monitor loop calls setClientStatus and computingStatus == COMPUTING_STATUS_COMPUTING - // release: every time computingStatus != COMPUTING_STATUS_COMPUTING , and in Monitor.onDestroy() + // release: every time acquisition criteria is not met , and in Monitor.onDestroy() public void setWakeLock(Boolean acquire) { try { if(wakeLock.isHeld() == acquire) return; // wakeLock already in desired state @@ -117,6 +125,23 @@ public class ClientStatus { } catch (Exception e) {Log.w(TAG, "Exception durign setWakeLock " + acquire, e);} } + // call to acquire or release resources held by the WifiLock. + // acquisition: every time the Monitor loop calls setClientStatus: computingStatus == COMPUTING_STATUS_COMPUTING || COMPUTING_STATUS_IDLE + // release: every time acquisition criteria is not met , and in Monitor.onDestroy() + public void setWifiLock(Boolean acquire) { + try { + if(wifiLock.isHeld() == acquire) return; // wifiLock already in desired state + + if(acquire) { // acquire wakeLock + wifiLock.acquire(); + Log.d(TAG, "wifiLock acquired"); + } else { // release wakeLock + wifiLock.release(); + Log.d(TAG, "wifiLock released"); + } + } catch (Exception e) {Log.w(TAG, "Exception durign setWifiLock " + acquire, e);} + } + /* * fires "clientstatuschange" broadcast, so registered Activities can update their model. */ @@ -349,6 +374,7 @@ public class ClientStatus { computingSuspendReason = status.task_suspend_reason; // = 4 - SUSPEND_REASON_USER_REQ???? computingParseError = false; setWakeLock(false); + setWifiLock(false); return; } if((status.task_mode == BOINCDefs.RUN_MODE_AUTO) && (status.task_suspend_reason != BOINCDefs.SUSPEND_NOT_SUSPENDED)) { @@ -356,6 +382,7 @@ public class ClientStatus { computingSuspendReason = status.task_suspend_reason; computingParseError = false; setWakeLock(false); + setWifiLock(false); return; } if((status.task_mode == BOINCDefs.RUN_MODE_AUTO) && (status.task_suspend_reason == BOINCDefs.SUSPEND_NOT_SUSPENDED)) { @@ -375,12 +402,14 @@ public class ClientStatus { computingSuspendReason = status.task_suspend_reason; // = 0 - SUSPEND_NOT_SUSPENDED computingParseError = false; setWakeLock(true); + setWifiLock(true); return; } else { // client "is able but idle" computingStatus = COMPUTING_STATUS_IDLE; computingSuspendReason = status.task_suspend_reason; // = 0 - SUSPEND_NOT_SUSPENDED computingParseError = false; setWakeLock(false); + setWifiLock(true); return; } } diff --git a/android/BOINC/src/edu/berkeley/boinc/client/Monitor.java b/android/BOINC/src/edu/berkeley/boinc/client/Monitor.java index 3aaf2ccf00..e19b116101 100644 --- a/android/BOINC/src/edu/berkeley/boinc/client/Monitor.java +++ b/android/BOINC/src/edu/berkeley/boinc/client/Monitor.java @@ -510,7 +510,9 @@ public class Monitor extends Service { monitorRunning = false; monitorThread.interrupt(); - clientStatus.setWakeLock(false); // release wakeLock, if held. + // release locks, if held. + clientStatus.setWakeLock(false); + clientStatus.setWifiLock(false); } @Override @@ -894,7 +896,7 @@ public class Monitor extends Service { return rpc.getMessages(seqNo); } - private final class ClientMonitorAsync extends AsyncTask { + private final class ClientMonitorAsync extends AsyncTask { private final String TAG = "BOINC ClientMonitorAsync"; private final Boolean showRpcCommands = false; @@ -913,7 +915,7 @@ public class Monitor extends Service { monitorThread = Thread.currentThread(); Boolean sleep = true; while(monitorRunning) { - publishProgress("doInBackground() monitor loop..."); + //Log.d(TAG,"doInBackground() monitor loop..."); if(!rpc.connectionAlive()) { //check whether connection is still alive // If connection is not working, either client has not been set up yet or client crashed. @@ -970,11 +972,6 @@ public class Monitor extends Service { return true; } - - @Override - protected void onProgressUpdate(String... arg0) { - Log.d(TAG, "onProgressUpdate() " + arg0[0]); - } @Override protected void onPostExecute(Boolean success) { diff --git a/android/BOINC/src/edu/berkeley/boinc/rpc/DeviceStatus.java b/android/BOINC/src/edu/berkeley/boinc/rpc/DeviceStatus.java index 1a2ef39942..e96b8308f7 100644 --- a/android/BOINC/src/edu/berkeley/boinc/rpc/DeviceStatus.java +++ b/android/BOINC/src/edu/berkeley/boinc/rpc/DeviceStatus.java @@ -106,7 +106,7 @@ public class DeviceStatus{ wifi_online = false; } - Log.d(TAG, "change: " + change + " - power supply: " + on_ac_power + " ; level: " + battery_charge_pct + " ; temperature: " + battery_temperature_celcius + " ; wifi: " + wifi_online); + //Log.d(TAG, "change: " + change + " - power supply: " + on_ac_power + " ; level: " + battery_charge_pct + " ; temperature: " + battery_temperature_celcius + " ; wifi: " + wifi_online); return change; }