File indexing completed on 2025-02-02 04:47:49

0001 /*
0002  * SPDX-FileCopyrightText: 2020 Vincent Blücher <vincent.bluecher@gmail.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 package org.kde.kdeconnect.Helpers;
0008 
0009 import android.app.Notification;
0010 import android.app.PendingIntent;
0011 import android.content.Context;
0012 import android.content.Intent;
0013 import android.os.Build;
0014 
0015 import androidx.core.app.NotificationCompat;
0016 import androidx.core.app.NotificationManagerCompat;
0017 
0018 import org.kde.kdeconnect_tp.R;
0019 
0020 public class IntentHelper {
0021 
0022     /**
0023      * On API 29+: post a high priority notification which starts the given Intent when clicked
0024      * On API <=28: launch a given Intent directly since no background restrictions apply on these platforms.
0025      * @param context the Context from which the Intent is started
0026      * @param intent the Intent to be started
0027      * @param title a title which is shown in the notification on Android 10+
0028      */
0029     public static void startActivityFromBackgroundOrCreateNotification(Context context, Intent intent, String title) {
0030         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && !LifecycleHelper.isInForeground()) {
0031             PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_MUTABLE);
0032             Notification notification = new NotificationCompat
0033                     .Builder(context, NotificationHelper.Channels.HIGHPRIORITY)
0034                     .setContentIntent(pendingIntent)
0035                     .setFullScreenIntent(pendingIntent, true)
0036                     .setContentTitle(title)
0037                     .setContentText(context.getString(R.string.tap_to_open))
0038                     .setSmallIcon(R.drawable.ic_notification)
0039                     .setPriority(NotificationCompat.PRIORITY_HIGH)
0040                     .setAutoCancel(true)
0041                     .build();
0042             int id = (int) System.currentTimeMillis();
0043             NotificationManagerCompat.from(context).notify(id, notification);
0044         } else {
0045             context.startActivity(intent);
0046         }
0047     }
0048 }