Merge: Fix warnings in the JSON, SAX and SAXophoNit libraries and in the `neo_doxygen...
[nit.git] / lib / android / notification / notification.nit
1 # This file is part of NIT (http://www.nitlanguage.org).
2 #
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 # Services to show notification in the Android status bar
18 #
19 # ~~~~nitish
20 # # Create and show a notification
21 # var notif = new Notification("My Title", "Some content")
22 # notif.ticker = "Ticker text"
23 # notif.show
24 #
25 # # Update the notification
26 # notif.text = "New content!"
27 # notif.ongoing = true # Make it un-dismissable
28 # notif.show
29 #
30 # # Hide the notification
31 # notif.cancel
32 # ~~~~
33 #
34 # For more information, see:
35 # http://developer.android.com/guide/topics/ui/notifiers/notifications.html
36 module notification
37
38 import standard
39 private import native_notification
40
41 # An Android notification, shown at the top of the screen
42 class Notification
43 # Title of this notification
44 var title: nullable Text is writable
45
46 # Text content of this notification
47 var text: nullable Text is writable
48
49 # Text to show in the bar as the notification appears
50 var ticker: nullable Text = null is writable
51
52 # Name of a resource found in the `res/drawable-*` folders to use for the small icon
53 #
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
57
58 # Number to display on the bottom right part of the notification
59 var number: nullable Int = null is writable
60
61 # Is this notification ongoing? Not user dismissable.
62 var ongoing: Bool = false is writable
63
64 private var id: nullable Int = null
65 private var tag = "app.nit notification"
66
67 # Show the notification
68 fun show
69 do
70 sys.jni_env.push_local_frame(8)
71
72 var context = app.native_activity
73 var builder = new NativeNotificationBuilder(context)
74
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
80
81 # Other options
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
86
87 var notif = builder.create
88 var manager = context.notification_manager
89
90 var id = self.id
91 if id == null then id = sys.next_notification_id
92 manager.notify(tag.to_java_string, id, notif)
93
94 self.id = id
95
96 sys.jni_env.pop_local_frame
97 end
98
99 # Was this notification shown with `show`?
100 #
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
104
105 # Cancel this notification and hide it if it is currently displayed
106 fun cancel
107 do
108 var id = self.id
109 if id != null then
110 sys.jni_env.push_local_frame(8)
111
112 var manager = app.native_activity.notification_manager
113 manager.cancel(tag.to_java_string, id)
114
115 self.id = null
116
117 sys.jni_env.pop_local_frame
118 end
119 end
120 end
121
122 redef class Sys
123 private var next_notification_id_cache = 0
124
125 # Returns a unique ID for new notifications
126 private fun next_notification_id: Int
127 do
128 var id = next_notification_id_cache
129 next_notification_id_cache = id + 1
130 return id
131 end
132 end