Skip to content
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

Merged
merged 6 commits into from Aug 23, 2017

Conversation

m1ga
Copy link
Contributor

@m1ga m1ga commented Apr 25, 2017

JIRA: https://jira.appcelerator.org/browse/TIMOB-24629

Example:

var win = Ti.UI.createWindow({});
var btn = Ti.UI.createButton({
	title: "click"
});
win.add(btn);

btn.addEventListener("click", function() {
	_.delay(function() {
		var intent = Ti.Android.createIntent({
			action: Ti.Android.ACTION_MAIN,
			className: 'com.appcelerator.notificationsample.NotificationsampleActivity',
			packageName: 'com.appcelerator.notificationsample'
		});
		intent.flags |= Ti.Android.FLAG_ACTIVITY_CLEAR_TOP | Ti.Android.FLAG_ACTIVITY_NEW_TASK;
		intent.addCategory(Ti.Android.CATEGORY_LAUNCHER);

		var pending = Titanium.Android.createPendingIntent({
			intent: intent,
			flags: Titanium.Android.FLAG_UPDATE_CURRENT
		});

		var notification = Titanium.Android.createNotification({
			contentTitle: 'Something Happened',
			contentText: 'Click to return to the application.',
			contentIntent: pending, 
			wakeLock: {
				time: 2000,
				flags: Ti.Android.WAKE_LOCK_PARTIAL | Ti.Android.WAKE_LOCK_ON_AFTER_RELEASE
			}
		});
		Titanium.Android.NotificationManager.notify(1, notification);
	}, 4000);
});

win.open();

@m1ga m1ga changed the title [AC-4926] Notification wake lock [AC-4926] Android: Notification wakeLock Apr 25, 2017
@hansemannn hansemannn changed the title [AC-4926] Android: Notification wakeLock [TIMOB-24629] Android: Add Notification wakeLock Apr 26, 2017
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.

Copy link
Collaborator

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);
Copy link
Collaborator

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;

Copy link
Contributor

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) {
Copy link
Collaborator

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 👾

@m1ga
Copy link
Contributor Author

m1ga commented Apr 26, 2017

Change the parameter to accept a HashMap like this: wakeLock:{time: 1000, flag: Ti.Android.PARTIAL_WAKE_LOCK} to be more flexible.

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
Copy link
Collaborator

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_*]

Copy link
Contributor Author

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_*]
Copy link
Collaborator

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;
Copy link
Collaborator

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

missing file :-)

@hansemannn
Copy link
Collaborator

hansemannn commented Apr 28, 2017

LGTM! @garymathews for an additional review.

@m1ga m1ga closed this May 20, 2017
@m1ga m1ga deleted the notificationWakeLock branch May 20, 2017 08:31
@hansemannn
Copy link
Collaborator

@m1ga You closed the PR? :(

@m1ga m1ga restored the notificationWakeLock branch May 20, 2017 14:34
@m1ga m1ga reopened this May 20, 2017
@m1ga
Copy link
Contributor Author

m1ga commented May 20, 2017

oh sorry. closed the wrong branch

Copy link
Contributor

@garymathews garymathews left a 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

@hansemannn
Copy link
Collaborator

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 dragstart event-key from the code, no idea why (it was only removed in an earlier commit but restored later). So we need to check those issue first before giving it to QE.

@hansemannn hansemannn removed this from the 6.2.0 milestone Aug 15, 2017
@lokeshchdhry
Copy link
Contributor

@garymathews , @m1ga ,
I am having issues when I try to test. I am checking with the above code.
It works fine with flags: Ti.Android.PARTIAL_WAKE_LOCK, but not with any other flag. The screen does not turn on at full brightness or in dim state when the notification arrives.
Also, for flag flags: Ti.Android.WAKE_LOCK_ACQUIRE_CAUSES_WAKEUP I am getting the below errors:

[WARN] :   W/System.err: java.lang.IllegalArgumentException: Must specify a valid wake lock level.
[WARN] :   W/System.err: 	at android.os.PowerManager.validateWakeLockParameters(PowerManager.java:517)
[WARN] :   W/System.err: 	at android.os.PowerManager.newWakeLock(PowerManager.java:501)
[WARN] :   W/System.err: 	at ti.modules.titanium.android.notificationmanager.NotificationManagerModule.notify(NotificationManagerModule.java:89)
[WARN] :   W/System.err: 	at org.appcelerator.kroll.runtime.v8.V8Function.nativeInvoke(Native Method)
[WARN] :   W/System.err: 	at org.appcelerator.kroll.runtime.v8.V8Function.callSync(V8Function.java:57)
[WARN] :   W/System.err: 	at org.appcelerator.kroll.runtime.v8.V8Function.call(V8Function.java:43)
[WARN] :   W/System.err: 	at ti.modules.titanium.TitaniumModule$Timer.run(TitaniumModule.java:147)
[WARN] :   W/System.err: 	at android.os.Handler.handleCallback(Handler.java:739)
[WARN] :   W/System.err: 	at android.os.Handler.dispatchMessage(Handler.java:95)
[WARN] :   W/System.err: 	at android.os.Looper.loop(Looper.java:148)
[WARN] :   W/System.err: 	at android.app.ActivityThread.main(ActivityThread.java:5417)
[WARN] :   W/System.err: 	at java.lang.reflect.Method.invoke(Native Method)
[WARN] :   W/System.err: 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
[WARN] :   W/System.err: 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
[ERROR] :  TiExceptionHandler: (main) [4352,4352] ----- Titanium Javascript Runtime Error -----
[ERROR] :  TiExceptionHandler: (main) [0,4352] - In /app.js:31,40
[ERROR] :  TiExceptionHandler: (main) [1,4353] - Message: Uncaught Must specify a valid wake lock level.
[ERROR] :  TiExceptionHandler: (main) [0,4353] - Source: 		Titanium.Android.NotificationManager.notify(1, notification);
[ERROR] :  V8Exception: Exception occurred at /app.js:31: Uncaught Must specify a valid wake lock level.
[ERROR] :  V8Exception: Must specify a valid wake lock level.

@lokeshchdhry
Copy link
Contributor

Failing FR for now.

@m1ga
Copy link
Contributor Author

m1ga commented Aug 23, 2017

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
I'll implement a check so it will show an error instead of the crash

Catch exception and show message
@lokeshchdhry
Copy link
Contributor

@m1ga , Thanks for the link I was using the flags in a wrong way. It works fine. But with flag WAKE_LOCK_SCREEN_BRIGHT & WAKE_LOCK_FULL the screen turns ON but not at the full brightness.

@garymathews
Copy link
Contributor

@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();

@lokeshchdhry
Copy link
Contributor

Thanks @garymathews , I was using the flags in a wrong way. But, as I have commented above with flag WAKE_LOCK_SCREEN_BRIGHT & WAKE_LOCK_FULL the screen turns ON but not at the full brightness.
Is this expected ?

@garymathews
Copy link
Contributor

@lokeshchdhry I would say it's expected behaviour since the flags are being set correctly, that behaviour is dictated by Android.

@lokeshchdhry
Copy link
Contributor

@garymathews , Ok sounds good.

@lokeshchdhry lokeshchdhry merged commit 41fda02 into tidev:master Aug 23, 2017
@m1ga m1ga deleted the notificationWakeLock branch September 9, 2017 12:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants