handle rare case of power manager not available

Daniel Gultsch created

Change summary

src/main/java/eu/siacs/conversations/services/XmppConnectionService.java |  6 
src/main/java/eu/siacs/conversations/utils/WakeLockHelper.java           | 42 
2 files changed, 28 insertions(+), 20 deletions(-)

Detailed changes

src/main/java/eu/siacs/conversations/services/XmppConnectionService.java 🔗

@@ -1249,7 +1249,11 @@ public class XmppConnectionService extends Service {
         }
 
         final PowerManager powerManager = getSystemService(PowerManager.class);
-        this.wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Conversations:Service");
+        if (powerManager != null) {
+            this.wakeLock =
+                    powerManager.newWakeLock(
+                            PowerManager.PARTIAL_WAKE_LOCK, "Conversations:Service");
+        }
 
         toggleForegroundService();
         updateUnreadCountBadge();

src/main/java/eu/siacs/conversations/utils/WakeLockHelper.java 🔗

@@ -36,24 +36,28 @@ import eu.siacs.conversations.Config;
 
 public class WakeLockHelper {
 
-	public static void acquire(final PowerManager.WakeLock wakeLock) {
-		try {
-			wakeLock.acquire(2000);
-		} catch (final RuntimeException e) {
-			Log.d(Config.LOGTAG, "unable to acquire wake lock", e);
-		}
-	}
+    public static void acquire(final PowerManager.WakeLock wakeLock) {
+        if (wakeLock == null) {
+            Log.d(Config.LOGTAG, "could not acquire WakeLock. PowerManager was null");
+            return;
+        }
+        try {
+            wakeLock.acquire(2000);
+        } catch (final RuntimeException e) {
+            Log.d(Config.LOGTAG, "Could not acquire WakeLock", e);
+        }
+    }
 
-	public static void release(final PowerManager.WakeLock wakeLock) {
-		if (wakeLock == null) {
-			return;
-		}
-		try {
-			if (wakeLock.isHeld()) {
-				wakeLock.release();
-			}
-		} catch (final RuntimeException e) {
-			Log.d(Config.LOGTAG, "unable to release wake lock", e);
-		}
-	}
+    public static void release(final PowerManager.WakeLock wakeLock) {
+        if (wakeLock == null) {
+            return;
+        }
+        try {
+            if (wakeLock.isHeld()) {
+                wakeLock.release();
+            }
+        } catch (final RuntimeException e) {
+            Log.d(Config.LOGTAG, "unable to release wake lock", e);
+        }
+    }
 }