summaryrefslogtreecommitdiff
path: root/java/org/gnu/emacs/EmacsDesktopNotification.java
blob: 72569631a8c0889dfd1c6e6498d9c858870ca4f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/* Communication module for Android terminals.  -*- c-file-style: "GNU" -*-

Copyright (C) 2023-2024 Free Software Foundation, Inc.

This file is part of GNU Emacs.

GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */

package org.gnu.emacs;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.NotificationChannel;
import android.app.PendingIntent;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import android.net.Uri;

import android.os.Build;

import android.widget.RemoteViews;



/* Structure designating a single desktop notification.

   New versions of Android also organize notifications into individual
   ``channels'', which are used to implement groups.  Unlike on other
   systems, notification importance is set for each group, not for
   each individual notification.  */



public final class EmacsDesktopNotification
{
  /* Intent tag for notification action data.  */
  public static final String NOTIFICATION_ACTION = "emacs:notification_action";

  /* Intent tag for notification IDs.  */
  public static final String NOTIFICATION_TAG = "emacs:notification_tag";

  /* Action ID assigned to the broadcast receiver which should be
     notified of any notification's being dismissed.  */
  public static final String NOTIFICATION_DISMISSED = "org.gnu.emacs.DISMISSED";

  /* The content of this desktop notification.  */
  public final String content;

  /* The title of this desktop notification.  */
  public final String title;

  /* The notification group.  */
  public final String group;

  /* String identifying this notification for future replacement.
     Typically a string resembling ``XXXX.NNNN.YYYY'', where XXXX is
     the system boot time, NNNN is the PID of this Emacs instance, and
     YYYY is the counter value returned by the notifications display
     function.  */
  public final String tag;

  /* The identifier of this notification's icon.  */
  public final int icon;

  /* The importance of this notification's group.  */
  public final int importance;

  /* Array of actions and their user-facing text to be offered by this
     notification.  */
  public final String[] actions, titles;

  /* Delay in miliseconds after which this notification should be
     automatically dismissed.  */
  public final long delay;

  public
  EmacsDesktopNotification (String title, String content,
			    String group, String tag, int icon,
			    int importance,
			    String[] actions, String[] titles,
			    long delay)
  {
    this.content    = content;
    this.title	    = title;
    this.group	    = group;
    this.tag        = tag;
    this.icon       = icon;
    this.importance = importance;
    this.actions    = actions;
    this.titles     = titles;
    this.delay      = delay;
  }



  /* Functions for displaying desktop notifications.  */

  /* Insert each action in actions and titles into the notification
     builder BUILDER, with pending intents created with CONTEXT holding
     suitable metadata.  */

  @SuppressWarnings ("deprecation")
  private void
  insertActions (Context context, Notification.Builder builder)
  {
    int i;
    PendingIntent pending;
    Intent intent;
    Notification.Action.Builder action;

    if (actions == null)
      return;

    for (i = 0; i < actions.length; ++i)
      {
	/* Actions named default should not be displayed.  */
	if (actions[i].equals ("default"))
	  continue;

	intent = new Intent (context, EmacsActivity.class);
	intent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK);

	/* Pending intents are specific to combinations of class, action
	   and data, but not information provided as extras.  In order
	   that its target may be invoked with the action and tag set
	   below, generate a URL from those two elements and specify it
	   as the intent data, which ensures that the intent allocated
	   fully reflects the duo.  */

	intent.setData (new Uri.Builder ().scheme ("action")
			.appendPath (tag).appendPath (actions[i])
			.build ());
	intent.putExtra (NOTIFICATION_ACTION, actions[i]);
	intent.putExtra (NOTIFICATION_TAG, tag);

	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
	  pending = PendingIntent.getActivity (context, 0, intent,
					       PendingIntent.FLAG_IMMUTABLE);
	else
	  pending = PendingIntent.getActivity (context, 0, intent, 0);

	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
	  {
	    action = new Notification.Action.Builder (0, titles[i], pending);
	    builder.addAction (action.build ());
	  }
	else
	  builder.addAction (0, titles[i], pending);
      }
  }

  /* Internal helper for `display' executed on the main thread.  */

  @SuppressWarnings ("deprecation") /* Notification.Builder (Context).  */
  private void
  display1 (Context context)
  {
    NotificationManager manager;
    NotificationChannel channel;
    Notification notification;
    Object tem;
    RemoteViews contentView;
    Intent intent;
    PendingIntent pending;
    int priority;
    Notification.Builder builder;

    tem = context.getSystemService (Context.NOTIFICATION_SERVICE);
    manager = (NotificationManager) tem;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
      {
	/* Create the notification channel for this group.  If a group
	   already exists with the same name, its linked attributes
	   (such as its importance) will be overridden.  */
        channel = new NotificationChannel (group, group, importance);
	manager.createNotificationChannel (channel);
	builder = new Notification.Builder (context, group);

	/* Create and configure a notification object and display
	   it.  */

	builder.setContentTitle (title);
	builder.setContentText (content);
	builder.setSmallIcon (icon);
	builder.setTimeoutAfter (delay);

	insertActions (context, builder);
	notification = builder.build ();
      }
    else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
      {
	/* Android 7.1 and earlier don't segregate notifications into
	   distinct categories, but permit an importance to be
	   assigned to each individual notification.  */

	builder = new Notification.Builder (context);
	builder.setContentTitle (title);
	builder.setContentText (content);
	builder.setSmallIcon (icon);

	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
	  {
	    switch (importance)
	      {
	      case 2: /* IMPORTANCE_LOW */
	      default:
		priority = Notification.PRIORITY_LOW;
		break;

	      case 3: /* IMPORTANCE_DEFAULT */
		priority = Notification.PRIORITY_DEFAULT;
		break;

	      case 4: /* IMPORTANCE_HIGH */
		priority = Notification.PRIORITY_HIGH;
		break;
	      }

	    builder.setPriority (priority);
	    insertActions (context, builder);
	    notification = builder.build ();
	  }
	else
	  notification = builder.getNotification ();
      }
    else
      {
	notification = new Notification ();
	notification.icon = icon;

	/* This remote widget tree is defined in
	   java/res/layout/sdk8_notifications_view.xml.  */
	notification.contentView
	  = contentView
	  = new RemoteViews ("org.gnu.emacs",
			     R.layout.sdk8_notifications_view);
	contentView.setTextViewText (R.id.sdk8_notifications_title,
				     title);
	contentView.setTextViewText (R.id.sdk8_notifications_content,
				     content);
      }

    /* Provide a content intent which starts Emacs when the
       notification is clicked.  */

    intent = new Intent (context, EmacsActivity.class);
    intent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setData (new Uri.Builder ()
		    .scheme ("action")
		    .appendPath (tag)
		    .build ());
    intent.putExtra (NOTIFICATION_ACTION, "default");
    intent.putExtra (NOTIFICATION_TAG, tag);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
      pending = PendingIntent.getActivity (context, 0, intent,
					   PendingIntent.FLAG_IMMUTABLE);
    else
      pending = PendingIntent.getActivity (context, 0, intent, 0);

    notification.contentIntent = pending;

    /* Provide a cancellation intent to respond to notification
       dismissals.  */

    intent = new Intent (context, CancellationReceiver.class);
    intent.setAction (NOTIFICATION_DISMISSED);
    intent.setPackage ("org.gnu.emacs");
    intent.setData (new Uri.Builder ()
		    .scheme ("action")
		    .appendPath (tag)
		    .build ());
    intent.putExtra (NOTIFICATION_TAG, tag);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
      pending = PendingIntent.getBroadcast (context, 0, intent,
					    PendingIntent.FLAG_IMMUTABLE);
    else
      pending = PendingIntent.getBroadcast (context, 0, intent, 0);

    notification.deleteIntent = pending;
    manager.notify (tag, 2, notification);
  }

  /* Display this desktop notification.

     Create a notification channel named GROUP or update its
     importance if such a channel is already defined.  */

  public void
  display ()
  {
    EmacsService.SERVICE.runOnUiThread (new Runnable () {
	@Override
	public void
	run ()
	{
	  display1 (EmacsService.SERVICE);
	}
      });
  }



  /* Broadcast receiver.  This is something of a system-wide callback
     arranged to be invoked whenever a notification posted by Emacs is
     dismissed, in order to relay news of its dismissal to
     androidselect.c and run or remove callbacks as appropriate.  */

  public static class CancellationReceiver extends BroadcastReceiver
  {
    @Override
    public void
    onReceive (Context context, Intent intent)
    {
      String tag, action;

      if (intent == null || EmacsService.SERVICE == null)
	return;

      tag = intent.getStringExtra (NOTIFICATION_TAG);

      if (tag == null)
	return;

      EmacsNative.sendNotificationDeleted (tag);
    }
  };
};