|
| 1 | +// SPDX-License-Identifier: Unlicense OR MIT |
| 2 | + |
| 3 | +package org.gioui; |
| 4 | +import android.app.Notification; |
| 5 | +import android.app.Service; |
| 6 | +import android.app.Notification; |
| 7 | +import android.app.Notification.Builder; |
| 8 | +import android.app.NotificationChannel; |
| 9 | +import android.app.NotificationManager; |
| 10 | +import android.app.PendingIntent; |
| 11 | +import android.content.Context; |
| 12 | +import android.content.ComponentName; |
| 13 | +import android.content.Intent; |
| 14 | +import android.content.pm.PackageManager; |
| 15 | +import android.os.IBinder; |
| 16 | +import android.os.Bundle; |
| 17 | + |
| 18 | +// ForegroundService implements a Service required to use the FOREGROUND_SERVICE |
| 19 | +// permission on Android, in order to run an application in the background. |
| 20 | +// See https://developer.android.com/guide/components/foreground-services for |
| 21 | +// more details. To add this permission to your application, import |
| 22 | +// gioui.org/app/permission/foreground and use the Start method from that |
| 23 | +// package to control this service. |
| 24 | +public class ForegroundService extends Service { |
| 25 | + private String channelID; |
| 26 | + private String channelName; |
| 27 | + private String channelDesc; |
| 28 | + private int notificationID; |
| 29 | + |
| 30 | + @Override public int onStartCommand(Intent intent, int flags, int startId) { |
| 31 | + // Get the channel parameters from intent extras and package metadata. |
| 32 | + Bundle extras = intent.getExtras(); |
| 33 | + Context ctx = getApplicationContext(); |
| 34 | + try { |
| 35 | + ComponentName svc = new ComponentName(this, this.getClass()); |
| 36 | + Bundle metadata = getPackageManager().getServiceInfo(svc, PackageManager.GET_META_DATA).metaData; |
| 37 | + if (metadata != null) { |
| 38 | + channelID = metadata.getString("org.gioui.ForegroundChannelID"); |
| 39 | + notificationID = metadata.getInt("org.gioui.ForegroundNotificationID", 0x42424242); |
| 40 | + } |
| 41 | + } catch (Exception e) { |
| 42 | + throw new RuntimeException(e); |
| 43 | + } |
| 44 | + channelName = extras.getString("channelName"); |
| 45 | + channelDesc = extras.getString("channelDesc"); |
| 46 | + notificationID = extras.getInt("notificationID", notificationID); |
| 47 | + this.createNotificationChannel(); |
| 48 | + |
| 49 | + // Create the Intent that will bring GioActivity to foreground. |
| 50 | + Intent resultIntent = new Intent(ctx, GioActivity.class); |
| 51 | + PendingIntent pending = PendingIntent.getActivity(ctx, notificationID, resultIntent, Intent.FLAG_ACTIVITY_CLEAR_TASK); |
| 52 | + Notification.Builder builder = new Notification.Builder(ctx, channelID) |
| 53 | + .setContentTitle(channelName) |
| 54 | + .setSmallIcon(getResources().getIdentifier("@mipmap/ic_launcher_adaptive", "drawable", getPackageName())) |
| 55 | + .setContentText(channelDesc) |
| 56 | + .setContentIntent(pending) |
| 57 | + .setPriority(Notification.PRIORITY_MIN); |
| 58 | + startForeground(notificationID, builder.build()); |
| 59 | + return START_NOT_STICKY; |
| 60 | + } |
| 61 | + |
| 62 | + @Override public IBinder onBind(Intent intent) { |
| 63 | + return null; |
| 64 | + } |
| 65 | + |
| 66 | + @Override public void onCreate() { |
| 67 | + super.onCreate(); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void onTaskRemoved(Intent rootIntent) { |
| 72 | + super.onTaskRemoved(rootIntent); |
| 73 | + this.deleteNotificationChannel(); |
| 74 | + stopForeground(true); |
| 75 | + this.stopSelf(); |
| 76 | + } |
| 77 | + |
| 78 | + @Override public void onDestroy() { |
| 79 | + this.deleteNotificationChannel(); |
| 80 | + } |
| 81 | + |
| 82 | + private void deleteNotificationChannel() { |
| 83 | + NotificationManager notificationManager = getSystemService(NotificationManager.class); |
| 84 | + notificationManager.deleteNotificationChannel(channelName); |
| 85 | + } |
| 86 | + |
| 87 | + private void createNotificationChannel() { |
| 88 | + // https://developer.android.com/training/notify-user/build-notification#java |
| 89 | + NotificationChannel channel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_LOW); |
| 90 | + channel.setDescription(channelDesc); |
| 91 | + NotificationManager notificationManager = getSystemService(NotificationManager.class); |
| 92 | + notificationManager.createNotificationChannel(channel); |
| 93 | + } |
| 94 | +} |
0 commit comments