Debian Codegraph @

plyer/platforms/android/notification.py

raw symbols SCIP
1 '''
2 Module of Android API for plyer.notification.
3
4 .. versionadded:: 1.0.0
5
6 .. versionchanged:: 1.4.0
7 Fixed notifications not displaying due to missing NotificationChannel
8 required by Android Oreo 8.0+ (API 26+).
9
10 .. versionchanged:: 1.4.0
11 Added simple toaster notification.
12
13 .. versionchanged:: 1.4.0
14 Fixed notifications not displaying big icons properly.
15 Added option for custom big icon via `icon`.
16 '''
17
18 from android import python_act
19 from android.runnable import run_on_ui_thread
20 from jnius import autoclass, cast
21
22 from plyer.facades import Notification
23 from plyer.platforms.android import activity, SDK_INT
24
25 AndroidString = autoclass('java.lang.String')
26 Context = autoclass('android.content.Context')
27 NotificationBuilder = autoclass('android.app.Notification$Builder')
28 NotificationManager = autoclass('android.app.NotificationManager')
29 PendingIntent = autoclass('android.app.PendingIntent')
30 Intent = autoclass('android.content.Intent')
31 Toast = autoclass('android.widget.Toast')
32 BitmapFactory = autoclass('android.graphics.BitmapFactory')
33
34
35 class AndroidNotification(Notification):
36 '''
37 Implementation of Android notification API.
38
39 .. versionadded:: 1.0.0
40 '''
41
42 def __init__(self):
43 package_name = activity.getPackageName()
44 self._ns = None
45 self._channel_id = package_name
46
47 pm = activity.getPackageManager()
48 info = pm.getActivityInfo(activity.getComponentName(), 0)
49 if info.icon == 0:
50 # Take the application icon instead.
51 info = pm.getApplicationInfo(package_name, 0)
52
53 self._app_icon = info.icon
54
55 def _get_notification_service(self):
56 if not self._ns:
57 self._ns = cast(NotificationManager, activity.getSystemService(
58 Context.NOTIFICATION_SERVICE
59 ))
60 return self._ns
61
62 def _build_notification_channel(self, name):
63 '''
64 Create a NotificationChannel using channel id of the application
65 package name (com.xyz, org.xyz, ...) and channel name same as the
66 provided notification title if the API is high enough, otherwise
67 do nothing.
68
69 .. versionadded:: 1.4.0
70 '''
71
72 if SDK_INT < 26:
73 return
74
75 channel = autoclass('android.app.NotificationChannel')
76
77 app_channel = channel(
78 self._channel_id, name, NotificationManager.IMPORTANCE_DEFAULT
79 )
80 self._get_notification_service().createNotificationChannel(
81 app_channel
82 )
83 return app_channel
84
85 @run_on_ui_thread
86 def _toast(self, message):
87 '''
88 Display a popup-like small notification at the bottom of the screen.
89
90 .. versionadded:: 1.4.0
91 '''
92 Toast.makeText(
93 activity,
94 cast('java.lang.CharSequence', AndroidString(message)),
95 Toast.LENGTH_LONG
96 ).show()
97
98 def _set_icons(self, notification, icon=None):
99 '''
100 Set the small application icon displayed at the top panel together with
101 WiFi, battery percentage and time and the big optional icon (preferably
102 PNG format with transparent parts) displayed directly in the
103 notification body.
104
105 .. versionadded:: 1.4.0
106 '''
107 app_icon = self._app_icon
108 notification.setSmallIcon(app_icon)
109
110 bitmap_icon = app_icon
111 if icon is not None:
112 bitmap_icon = BitmapFactory.decodeFile(icon)
113 notification.setLargeIcon(bitmap_icon)
114 elif icon == '':
115 # we don't want the big icon set,
116 # only the small one in the top panel
117 pass
118 else:
119 bitmap_icon = BitmapFactory.decodeResource(
120 python_act.getResources(), app_icon
121 )
122 notification.setLargeIcon(bitmap_icon)
123
124 def _build_notification(self, title):
125 '''
126 .. versionadded:: 1.4.0
127 '''
128 if SDK_INT < 26:
129 noti = NotificationBuilder(activity)
130 else:
131 self._channel = self._build_notification_channel(title)
132 noti = NotificationBuilder(activity, self._channel_id)
133 return noti
134
135 @staticmethod
136 def _set_open_behavior(notification):
137 '''
138 Open the source application when user opens the notification.
139
140 .. versionadded:: 1.4.0
141 '''
142
143 # create Intent that navigates back to the application
144 app_context = activity.getApplication().getApplicationContext()
145 notification_intent = Intent(app_context, python_act)
146
147 # set flags to run our application Activity
148 notification_intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
149 notification_intent.setAction(Intent.ACTION_MAIN)
150 notification_intent.addCategory(Intent.CATEGORY_LAUNCHER)
151
152 # get our application Activity
153 pending_intent = PendingIntent.getActivity(
154 app_context, 0, notification_intent, 0
155 )
156
157 notification.setContentIntent(pending_intent)
158 notification.setAutoCancel(True)
159
160 def _open_notification(self, notification):
161 if SDK_INT >= 16:
162 notification = notification.build()
163 else:
164 notification = notification.getNotification()
165
166 self._get_notification_service().notify(0, notification)
167
168 def _notify(self, **kwargs):
169 noti = None
170 message = kwargs.get('message').encode('utf-8')
171 ticker = kwargs.get('ticker').encode('utf-8')
172 title = AndroidString(
173 kwargs.get('title', '').encode('utf-8')
174 )
175 icon = kwargs.get('app_icon')
176
177 # decide whether toast only or proper notification
178 if kwargs.get('toast'):
179 self._toast(message)
180 return
181 else:
182 noti = self._build_notification(title)
183
184 # set basic properties for notification
185 noti.setContentTitle(title)
186 noti.setContentText(AndroidString(message))
187 noti.setTicker(AndroidString(ticker))
188
189 # set additional flags for notification
190 self._set_icons(noti, icon=icon)
191 self._set_open_behavior(noti)
192
193 # launch
194 self._open_notification(noti)
195
196
197 def instance():
198 '''
199 Instance for facade proxy.
200 '''
201 return AndroidNotification()