Fixed #6 Underlocking causing crashes on Android

I fixed this as per the solution in the issue, by surrounding the release
with a try / catch block
pull/1/merge
Gearóid Moroney 9 years ago
parent 2b6c92b0c4
commit 317a3ccbf3

@ -1,12 +1,12 @@
/* /*
* Copyright 2013-2014 Wolfgang Koller * Copyright 2013-2014 Wolfgang Koller
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -48,10 +48,10 @@ public class PowerManagement extends CordovaPlugin {
@Override @Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) { public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(cordova, webView); super.initialize(cordova, webView);
this.powerManager = (PowerManager) cordova.getActivity().getSystemService(Context.POWER_SERVICE); this.powerManager = (PowerManager) cordova.getActivity().getSystemService(Context.POWER_SERVICE);
} }
@Override @Override
public boolean execute(String action, JSONArray args, public boolean execute(String action, JSONArray args,
CallbackContext callbackContext) throws JSONException { CallbackContext callbackContext) throws JSONException {
@ -59,7 +59,7 @@ public class PowerManagement extends CordovaPlugin {
PluginResult result = null; PluginResult result = null;
Log.d("PowerManagementPlugin", "Plugin execute called - " + this.toString() ); Log.d("PowerManagementPlugin", "Plugin execute called - " + this.toString() );
Log.d("PowerManagementPlugin", "Action is " + action ); Log.d("PowerManagementPlugin", "Action is " + action );
try { try {
if( action.equals("acquire") ) { if( action.equals("acquire") ) {
if( args.length() > 0 && args.getBoolean(0) ) { if( args.length() > 0 && args.getBoolean(0) ) {
@ -77,11 +77,11 @@ public class PowerManagement extends CordovaPlugin {
catch( JSONException e ) { catch( JSONException e ) {
result = new PluginResult(Status.JSON_EXCEPTION, e.getMessage()); result = new PluginResult(Status.JSON_EXCEPTION, e.getMessage());
} }
callbackContext.sendPluginResult(result); callbackContext.sendPluginResult(result);
return true; return true;
} }
/** /**
* Acquire a wake-lock * Acquire a wake-lock
* @param p_flags Type of wake-lock to acquire * @param p_flags Type of wake-lock to acquire
@ -89,7 +89,7 @@ public class PowerManagement extends CordovaPlugin {
*/ */
private PluginResult acquire( int p_flags ) { private PluginResult acquire( int p_flags ) {
PluginResult result = null; PluginResult result = null;
if (this.wakeLock == null) { if (this.wakeLock == null) {
this.wakeLock = this.powerManager.newWakeLock(p_flags, "PowerManagementPlugin"); this.wakeLock = this.powerManager.newWakeLock(p_flags, "PowerManagementPlugin");
try { try {
@ -104,30 +104,35 @@ public class PowerManagement extends CordovaPlugin {
else { else {
result = new PluginResult(PluginResult.Status.ILLEGAL_ACCESS_EXCEPTION,"WakeLock already active - release first"); result = new PluginResult(PluginResult.Status.ILLEGAL_ACCESS_EXCEPTION,"WakeLock already active - release first");
} }
return result; return result;
} }
/** /**
* Release an active wake-lock * Release an active wake-lock
* @return PluginResult containing the status of the release process * @return PluginResult containing the status of the release process
*/ */
private PluginResult release() { private PluginResult release() {
PluginResult result = null; PluginResult result = null;
if( this.wakeLock != null ) { if( this.wakeLock != null ) {
this.wakeLock.release(); try {
this.wakeLock.release();
result = new PluginResult(PluginResult.Status.OK, "OK");
}
catch (Exception e) {
result = new PluginResult(PluginResult.Status.ILLEGAL_ACCESS_EXCEPTION, "WakeLock already released");
}
this.wakeLock = null; this.wakeLock = null;
result = new PluginResult(PluginResult.Status.OK, "OK");
} }
else { else {
result = new PluginResult(PluginResult.Status.ILLEGAL_ACCESS_EXCEPTION, "No WakeLock active - acquire first"); result = new PluginResult(PluginResult.Status.ILLEGAL_ACCESS_EXCEPTION, "No WakeLock active - acquire first");
} }
return result; return result;
} }
/** /**
* Make sure any wakelock is released if the app goes into pause * Make sure any wakelock is released if the app goes into pause
*/ */
@ -137,7 +142,7 @@ public class PowerManagement extends CordovaPlugin {
super.onPause(multitasking); super.onPause(multitasking);
} }
/** /**
* Make sure any wakelock is acquired again once we resume * Make sure any wakelock is acquired again once we resume
*/ */

Loading…
Cancel
Save