1 # This file is part of NIT (http://www.nitlanguage.org).
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Services to show notification in the Android status bar
20 # # Create and show a notification
21 # var notif = new Notification("My Title", "Some content")
22 # notif.ticker = "Ticker text"
25 # # Update the notification
26 # notif.text = "New content!"
27 # notif.ongoing = true # Make it un-dismissable
30 # # Hide the notification
34 # For more information, see:
35 # http://developer.android.com/guide/topics/ui/notifiers/notifications.html
39 private import native_notification
41 # An Android notification, shown at the top of the screen
43 # Title of this notification
44 var title
: nullable Text is writable
46 # Text content of this notification
47 var text
: nullable Text is writable
49 # Text to show in the bar as the notification appears
50 var ticker
: nullable Text = null is writable
52 # Name of a resource found in the `res/drawable-*` folders to use for the small icon
54 # By default, we use the app's icon, named "icon". A valid icon must be used
55 # to display notifications.
56 var small_icon
: nullable Text = null is writable
58 # Number to display on the bottom right part of the notification
59 var number
: nullable Int = null is writable
61 # Is this notification ongoing? Not user dismissable.
62 var ongoing
: Bool = false is writable
64 private var id
: nullable Int = null
65 private var tag
= "app.nit notification"
67 # Show the notification
70 sys
.jni_env
.push_local_frame
(8)
72 var context
= app
.native_activity
73 var builder
= new NativeNotificationBuilder(context
)
75 # If no custom icon is specified, use app's
76 var small_icon
= self.small_icon
77 if small_icon
== null then small_icon
= "icon"
78 var small_icon_id
= app
.resource_manager
.other_id
(small_icon
.to_s
, "drawable")
79 builder
.small_icon
= small_icon_id
82 if title
!= null then builder
.title
= title
.to_java_string
83 if text
!= null then builder
.text
= text
.to_java_string
84 if ticker
!= null then builder
.ticker
= ticker
.to_java_string
85 builder
.ongoing
= ongoing
87 var notif
= builder
.create
88 var manager
= context
.notification_manager
91 if id
== null then id
= sys
.next_notification_id
92 manager
.notify
(tag
.to_java_string
, id
, notif
)
96 sys
.jni_env
.pop_local_frame
99 # Was this notification shown with `show`?
101 # This does not indicates whether is has been dismissed or not. Only that
102 # it was shown at least once.
103 private fun was_shown
: Bool do return id
!= null
105 # Cancel this notification and hide it if it is currently displayed
110 sys
.jni_env
.push_local_frame
(8)
112 var manager
= app
.native_activity
.notification_manager
113 manager
.cancel
(tag
.to_java_string
, id
)
117 sys
.jni_env
.pop_local_frame
123 private var next_notification_id_cache
= 0
125 # Returns a unique ID for new notifications
126 private fun next_notification_id
: Int
128 var id
= next_notification_id_cache
129 next_notification_id_cache
= id
+ 1