Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ Install the plugin using the cordova command line utility:

`$ cordova plugin add https://github.com/boltex/cordova-plugin-powermanagement.git`

OR
---

`npm i cordova-plugin-powermanagement2`

Usage
-----

Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{
"name": "cordova-plugin-powermanagement-orig",
"name": "cordova-plugin-powermanagement2",
"version": "1.1.0",
"description": "Plugin for managing the power state (i.e. idle switching) for cordova",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/boltex/cordova-plugin-powermanagement.git"
"url": "git+https://github.com/sitronik/cordova-plugin-powermanagement2.git"
},
"author": "Viras-",
"author": "Sitronik",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/boltex/cordova-plugin-powermanagement/issues"
"url": "https://github.com/sitronik/cordova-plugin-powermanagement2/issues"
},
"homepage": "https://github.com/boltex/cordova-plugin-powermanagement#readme",
"homepage": "https://github.com/sitronik/cordova-plugin-powermanagement2#readme",
"keywords": [
"ecosystem:cordova",
"cordova-android",
Expand Down
70 changes: 37 additions & 33 deletions src/android/PowerManagement.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,33 +55,33 @@ public class PowerManagement extends CordovaPlugin {
private CordovaWebView webView;

private final Runnable heartbeat = new Runnable() {
public void run() {
try {
//Log.d("PowerManagementPlugin", "About to declare ourselves VISIBLE");
webView.getEngine().getView().dispatchWindowVisibilityChanged(View.VISIBLE);
public void run() {
try {
//Log.d("PowerManagementPlugin", "About to declare ourselves VISIBLE");
webView.getView().dispatchWindowVisibilityChanged(View.VISIBLE);

// if sdk is 23 (android 6) or greater
// if sdk is 23 (android 6) or greater
if(android.os.Build.VERSION.SDK_INT > 22){

if (wakeLock != null && powerManager != null && powerManager.isDeviceIdleMode()) {
//Log.d("PowerManagementPlugin", "Poking location service");
try {
wakeupIntent.send();
} catch (SecurityException e) {
Log.d("PowerManagementPlugin", "SecurityException : Heartbeat location manager keep-alive failed");
} catch (PendingIntent.CanceledException e) {
Log.d("PowerManagementPlugin", "PendingIntent.CanceledException : Heartbeat location manager keep-alive failed");
}
}

}

} finally {
if (handler != null) {
handler.postDelayed(this, 10000);
}
}
}
if (wakeLock != null && powerManager != null && powerManager.isDeviceIdleMode()) {
//Log.d("PowerManagementPlugin", "Poking location service");
try {
wakeupIntent.send();
} catch (SecurityException e) {
Log.d("PowerManagementPlugin", "SecurityException : Heartbeat location manager keep-alive failed");
} catch (PendingIntent.CanceledException e) {
Log.d("PowerManagementPlugin", "PendingIntent.CanceledException : Heartbeat location manager keep-alive failed");
}
}

}

} finally {
if (handler != null) {
handler.postDelayed(this, 10000);
}
}
}
};

/**
Expand All @@ -99,14 +99,21 @@ public void initialize(CordovaInterface cordova, CordovaWebView webViewPara) {
this.powerManager = (PowerManager) cordova.getActivity().getSystemService(Context.POWER_SERVICE);

handler = new Handler();
wakeupIntent = PendingIntent.getBroadcast( context , 0,
new Intent("com.android.internal.location.ALARM_WAKEUP"), 0);
wakeupIntent = PendingIntent.getBroadcast( context , 0,
new Intent("com.android.internal.location.ALARM_WAKEUP"), 0);

}

public PluginResult partialWakeLock() {
Log.d("PowerManagementPlugin", "Partial wake lock" );
PluginResult result = this.acquire( PowerManager.PARTIAL_WAKE_LOCK );
handler.postDelayed(heartbeat, 10000);
return result;
}

@Override
public boolean execute(String action, JSONArray args,
CallbackContext callbackContext) throws JSONException {
CallbackContext callbackContext) throws JSONException {

PluginResult result = null;
Log.d("PowerManagementPlugin", "Plugin execute called - " + this.toString() );
Expand All @@ -115,12 +122,9 @@ public boolean execute(String action, JSONArray args,
try {
if( action.equals("acquire") ) {
if( args.length() > 0 && args.getBoolean(0) ) {
Log.d("PowerManagementPlugin", "Partial wake lock" );
result = this.acquire( PowerManager.PARTIAL_WAKE_LOCK );
handler.postDelayed(heartbeat, 10000);
}
else {
result = this.acquire( PowerManager.FULL_WAKE_LOCK );
result = partialWakeLock();
Copy link
Owner

@boltex boltex Aug 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so no more full wake lock if no arguments? - also this if/then doesnt change anything if true/false :

result = partialWakeLock();
    } else {
result = partialWakeLock();

both are the same??

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, since when starting in an ionic project, we always get into the “else” construction where there was a complete wake lock. Even if there are arguments or not.
I'm not good at Java, but when removing “if” and “else” in «if (action.equals ("acquire"))» it didn't compile because catch won't work, I decided to leave it that way, because everything works in my case

} else {
result = partialWakeLock();
}
} else if( action.equals("release") ) {
result = this.release();
Expand Down