New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[TIMOB-24629] Android: Add Notification wakeLock #8990
Conversation
summary: Will wake up the device for the given time (in milliseconds) when the notification is shown. | ||
The application needs to also set the `android.permission.WAKE_LOCK` permission | ||
in the Android manifest section of the `tiapp.xml` file. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace with:
summary: |
Will wake up the device for the given time (in milliseconds) when the notification is shown.
The application needs to also set the `android.permission.WAKE_LOCK` permission
in the Android manifest section of the `tiapp.xml` file.
boolean isScreenOn = pm.isScreenOn(); | ||
if(isScreenOn==false) { | ||
WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "MyWakeLock"); | ||
wl.acquire(wakeTime); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indentation in 86 and space in ){
. Maybe rename MyWakeLock
to TiWakeLock
.
|
||
import ti.modules.titanium.android.AndroidModule; | ||
import android.app.Activity; | ||
import android.app.Notification; | ||
import android.app.NotificationManager; | ||
import android.os.PowerManager; | ||
import android.os.PowerManager.WakeLock; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also remove this extra line :)
if (wakeTime > 0){ | ||
PowerManager pm = (PowerManager)TiApplication.getInstance().getSystemService(TiApplication.getInstance().getApplicationContext().POWER_SERVICE); | ||
boolean isScreenOn = pm.isScreenOn(); | ||
if(isScreenOn==false) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here btw: Spaces spaces spaces 👾
Change the parameter to accept a HashMap like this: |
Titanium.Android.PARTIAL_WAKE_LOCK | Titanium.Android.FULL_WAKE_LOCK | | ||
Titanium.Android.SCREEN_DIM_WAKE_LOCK | Titanium.Android.SCREEN_BRIGHT_WAKE_LOCK | ||
Titanium.Android.ACQUIRE_CAUSES_WAKEUP | Titanium.Android.ON_AFTER_RELEASE | ||
default: Titanium.Android.FULL_WAKE_LOCK | Titanium.Android.ACQUIRE_CAUSES_WAKEUP | Titanium.Android.ON_AFTER_RELEASE |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We usually prefix the constants so it's easier to find and group them. For example:
- WAKE_LOCK_PARTIAL
- WAKE_LOCK_FULL
- WAKE_LOCK_SCREEN_DIM
Then you could also also write constants: [Titanium.Android_WAKE_LOCK_*]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, nice! I've made the changes.
- name: flags | ||
summary: Wake lock level and flag. See [Android Developers: PowerManager](https://developer.android.com/reference/android/os/PowerManager.html#newWakeLock(int,%20java.lang.String)) | ||
type: int | ||
constants: [Titanium.Android_WAKE_LOCK_*] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Titanium.Android_WAKE_LOCK_*
-> Titanium.Android.WAKE_LOCK_*
@Kroll.constant public static final int WAKE_LOCK_SCREEN_DIM = PowerManager.SCREEN_DIM_WAKE_LOCK; | ||
@Kroll.constant public static final int WAKE_LOCK_SCREEN_BRIGHT = PowerManager.SCREEN_BRIGHT_WAKE_LOCK; | ||
@Kroll.constant public static final int WAKE_LOCK_ACQUIRE_CAUSES_WAKEUP = PowerManager.ACQUIRE_CAUSES_WAKEUP; | ||
@Kroll.constant public static final int WAKE_LOCK_ON_AFTER_RELEASE = PowerManager.ON_AFTER_RELEASE; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing docs for the constants?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing file :-)
LGTM! @garymathews for an additional review. |
@m1ga You closed the PR? :( |
oh sorry. closed the wrong branch |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CR: PASS
Conflict needs fixing though
I think my latest resolve of the merge-conflicts screw up around here. Also, the backport #9307 has a different number of lines and removed the |
2390370
to
15e362b
Compare
3c311de
to
c13e2e1
Compare
@garymathews , @m1ga ,
|
Failing FR for now. |
ACQUIRE_CAUSES_WAKEUP and ON_AFTER_RELEASE are flags you can add to the WAKE_LOCK levels. See https://developer.android.com/reference/android/os/PowerManager.html |
Catch exception and show message
@m1ga , Thanks for the link I was using the flags in a wrong way. It works fine. But with flag |
@lokeshchdhry here's another test case var win = Ti.UI.createWindow({
title: 'TIMOB-24629',
backgroundColor: 'gray'
}),
btn = Ti.UI.createButton({
title: 'START',
width: '33%'
}),
ntn = Titanium.Android.createNotification({
contentTitle: 'WAKE-LOCK TEST',
contentText: 'Do nothing...',
wakeLock: {
time: 3000,
flags: Ti.Android.WAKE_LOCK_FULL | Ti.Android.WAKE_LOCK_ACQUIRE_CAUSES_WAKEUP | Ti.Android.WAKE_LOCK_ON_AFTER_RELEASE
}
});
btn.addEventListener('click', function() {
var i = 4,
interval = setInterval(function() {
btn.setTitle('COUNT ' + --i);
if (i <= 0) {
Ti.Android.NotificationManager.notify(1, ntn);
clearInterval(interval);
btn.setTitle('START');
btn.setEnabled(true);
}
}, 1000);
btn.setEnabled(false);
btn.setTitle('COUNT ' + --i);
});
win.add(btn);
win.open(); |
Thanks @garymathews , I was using the flags in a wrong way. But, as I have commented above with flag |
@lokeshchdhry I would say it's expected behaviour since the flags are being set correctly, that behaviour is dictated by Android. |
@garymathews , Ok sounds good. |
JIRA: https://jira.appcelerator.org/browse/TIMOB-24629
Example: