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] (6_2_X) Android: Add Notification wakeLock #9307

Merged
merged 3 commits into from
Aug 28, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import android.content.pm.ActivityInfo;
import android.media.AudioManager;
import android.view.MenuItem;
import android.os.PowerManager;

@SuppressWarnings("deprecation")
@Kroll.module
Expand Down Expand Up @@ -274,6 +275,13 @@ public class AndroidModule extends KrollModule
@Kroll.constant public static final int NAVIGATION_MODE_STANDARD = ActionBar.NAVIGATION_MODE_STANDARD;
@Kroll.constant public static final int NAVIGATION_MODE_TABS = ActionBar.NAVIGATION_MODE_TABS;

@Kroll.constant public static final int WAKE_LOCK_PARTIAL = PowerManager.PARTIAL_WAKE_LOCK;
@Kroll.constant public static final int WAKE_LOCK_FULL = PowerManager.FULL_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;

protected RProxy r;
private static final int REQUEST_CODE = 99;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2016 by Appcelerator, Inc. All Rights Reserved.
* Copyright (c) 2009-2017 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
Expand All @@ -9,11 +9,19 @@
import org.appcelerator.kroll.KrollModule;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.kroll.KrollDict;

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;

import java.util.HashMap;

@Kroll.module(parentModule=AndroidModule.class)
public class NotificationManagerModule extends KrollModule
Expand Down Expand Up @@ -71,6 +79,17 @@ public void cancelAll()
public void notify(int id, NotificationProxy notificationProxy)
{
getManager().notify(id, notificationProxy.buildNotification());

HashMap wakeParams = notificationProxy.getWakeParams();
if (wakeParams != null) {
int wakeTime = TiConvert.toInt(wakeParams.get("time"), 3000);
int wakeFlags = TiConvert.toInt(wakeParams.get("flags"), (PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE));
PowerManager pm = (PowerManager) TiApplication.getInstance().getSystemService(TiApplication.getInstance().getApplicationContext().POWER_SERVICE);
if (pm != null && !pm.isScreenOn()) {
WakeLock wl = pm.newWakeLock(wakeFlags, "TiWakeLock");
wl.acquire(wakeTime);
}
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@
import ti.modules.titanium.android.AndroidModule;
import ti.modules.titanium.android.PendingIntentProxy;
import ti.modules.titanium.android.RemoteViewsProxy;

import android.app.Notification;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;

import java.util.HashMap;

@SuppressWarnings("deprecation")
@Kroll.proxy(creatableInModule=AndroidModule.class, propertyAccessors = {
TiC.PROPERTY_CONTENT_TEXT,
Expand All @@ -40,6 +43,7 @@ public class NotificationProxy extends KrollProxy
private int flags, ledARGB, ledOnMS, ledOffMS;
private Uri sound;
private int audioStreamType;
private HashMap wakeParams;

public NotificationProxy()
{
Expand All @@ -51,6 +55,7 @@ public NotificationProxy()
//set up default values
flags = Notification.FLAG_AUTO_CANCEL;
audioStreamType = Notification.STREAM_DEFAULT;
wakeParams = new HashMap();
}

@Override
Expand Down Expand Up @@ -120,6 +125,20 @@ public void handleCreationDict(KrollDict d)
if (d.containsKey(TiC.PROPERTY_PRIORITY)) {
setPriority(TiConvert.toInt(d, TiC.PROPERTY_PRIORITY));
}
if (d.containsKey(TiC.PROPERTY_GROUP_KEY)) {
setGroupKey(TiConvert.toString(d, TiC.PROPERTY_GROUP_KEY));
}
/*
if (d.containsKey(TiC.PROPERTY_GROUP_ALERT_BEHAVIOR)) {
setGroupAlertBehavior(TiConvert.toInt(d, TiC.PROPERTY_GROUP_ALERT_BEHAVIOR));
}
*/
if (d.containsKey(TiC.PROPERTY_GROUP_SUMMARY)) {
setGroupSummary(TiConvert.toBoolean(d, TiC.PROPERTY_GROUP_SUMMARY));
}
if (d.containsKey(TiC.PROPERTY_WAKE_LOCK)) {
setWakeLock((HashMap) d.get(TiC.PROPERTY_WAKE_LOCK));
}
checkLatestEventInfoProperties(d);
}

Expand Down Expand Up @@ -181,6 +200,15 @@ public void setPriority(int priority)
setProperty(TiC.PROPERTY_PRIORITY, priority);
}

@Kroll.method @Kroll.setProperty
public void setWakeLock(HashMap d)
{
if (d == null) {
return;
}
wakeParams = d;
}

@Kroll.method @Kroll.setProperty
public void setTickerText(String tickerText)
{
Expand Down Expand Up @@ -346,6 +374,10 @@ public Notification buildNotification()
return notification;
}

public HashMap getWakeParams() {
return wakeParams;
}

@Override
public String getApiName()
{
Expand Down
6 changes: 6 additions & 0 deletions android/titanium/src/java/org/appcelerator/titanium/TiC.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ public class TiC
* @module.api
*/
public static final String EVENT_DRAGSTART = "dragstart";

/**
* @module.api
*/
Expand Down Expand Up @@ -2898,6 +2899,11 @@ public class TiC
*/
public static final String PROPERTY_VOLUME = "volume";

/**
* @module.api
*/
public static final String PROPERTY_WAKE_LOCK = "wakeLock";

/**
* @module.api
*/
Expand Down
59 changes: 59 additions & 0 deletions apidoc/Titanium/Android/Android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1986,6 +1986,65 @@ properties:
since: "3.6.0"
osver: {android: {min: "5.0"}}

- name: WAKE_LOCK_PARTIAL
summary: Ensures that the CPU is running; the screen and keyboard backlight will be allowed to go off.
description: |
Use with the <Titanium.Android.Notification.wakeLock> property.

See also: [PowerManager.PARTIAL_WAKE_LOCK in the Android API Reference](https://developer.android.com/reference/android/os/PowerManager.html#PARTIAL_WAKE_LOCK).
type: Number
permission: read-only
since: "6.2.0"

- name: WAKE_LOCK_FULL
summary: Ensures that the screen and keyboard backlight are on at full brightness.
description: |
Use with the <Titanium.Android.Notification.wakeLock> property.

See also: [PowerManager.FULL_WAKE_LOCK in the Android API Reference](https://developer.android.com/reference/android/os/PowerManager.html#FULL_WAKE_LOCK).
type: Number
permission: read-only
since: "6.2.0"

- name: WAKE_LOCK_SCREEN_DIM
summary: Ensures that the screen is on (but may be dimmed); the keyboard backlight will be allowed to go off.
description: |
Use with the <Titanium.Android.Notification.wakeLock> property.

See also: [PowerManager.SCREEN_DIM_WAKE_LOCK in the Android API Reference](https://developer.android.com/reference/android/os/PowerManager.html#SCREEN_DIM_WAKE_LOCK).
type: Number
permission: read-only
since: "6.2.0"

- name: WAKE_LOCK_SCREEN_BRIGHT
summary: Ensures that the screen is on at full brightness; the keyboard backlight will be allowed to go off.
description: |
Use with the <Titanium.Android.Notification.wakeLock> property.

See also: [PowerManager.SCREEN_BRIGHT_WAKE_LOCK in the Android API Reference](https://developer.android.com/reference/android/os/PowerManager.html#SCREEN_BRIGHT_WAKE_LOCK).
type: Number
permission: read-only
since: "6.2.0"

- name: WAKE_LOCK_ACQUIRE_CAUSES_WAKEUP
summary: Turn the screen on when the wake lock is acquired.
description: |
Use with the <Titanium.Android.Notification.wakeLock> property.

See also: [PowerManager.ACQUIRE_CAUSES_WAKEUP in the Android API Reference](https://developer.android.com/reference/android/os/PowerManager.html#ACQUIRE_CAUSES_WAKEUP).
type: Number
permission: read-only
since: "6.2.0"

- name: WAKE_LOCK_ON_AFTER_RELEASE
summary: When this wake lock is released, poke the user activity timer so the screen stays on for a little longer.
description: |
Use with the <Titanium.Android.Notification.wakeLock> property.

See also: [PowerManager.ON_AFTER_RELEASE in the Android API Reference](https://developer.android.com/reference/android/os/PowerManager.html#ON_AFTER_RELEASE).
type: Number
permission: read-only
since: "6.2.0"

# Constants that should probably be removed

Expand Down
34 changes: 33 additions & 1 deletion apidoc/Titanium/Android/Notification.yml
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,41 @@ properties:
since: "3.6.0"
osver: {android: {min: "5.0"}}

- name: wakeLock
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.

<ti:app>
<android>
<manifest>
<uses-permission android:name="android.permission.WAKE_LOCK" />
</manifest>
</android>
</ti:app>
type: wakeLockOptions
default: 0
since: "6.2.0"

- name: when
summary: The timestamp for the notification (defaults to the current time).
description: |
If the timestamp is set to 0, no time is displayed in the notification
window for this notification.
type: [Date,Number]
type: [Date, Number]

---
name: wakeLockOptions
summary: Parameter for wakeLock
since: 6.2.0
properties:
- name: time
summary: Minimum time the device will be switched on (plus device switch-off time)
type: Number
default: 3000
- name: flags
summary: Wake lock level and flag. See [PowerManager.newWakeLock in the Android API Reference](https://developer.android.com/reference/android/os/PowerManager.html#newWakeLock(int,%20java.lang.String))
type: Number
constants: [Titanium.Android.WAKE_LOCK_*]
default: Titanium.Android.WAKE_LOCK_FULL | Titanium.Android.WAKE_LOCK_ACQUIRE_CAUSES_WAKEUP | Titanium.Android.WAKE_LOCK_ON_AFTER_RELEASE