File indexing completed on 2024-12-22 04:41:41
0001 /* 0002 * SPDX-FileCopyrightText: 2019 Erik Duisters <e.duisters1@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.Plugins.SharePlugin; 0008 0009 import android.app.Notification; 0010 import android.app.NotificationManager; 0011 import android.app.PendingIntent; 0012 import android.content.Intent; 0013 import android.content.SharedPreferences; 0014 0015 import androidx.core.app.NotificationCompat; 0016 import androidx.core.content.ContextCompat; 0017 import androidx.preference.PreferenceManager; 0018 0019 import org.kde.kdeconnect.Device; 0020 import org.kde.kdeconnect.Helpers.NotificationHelper; 0021 import org.kde.kdeconnect_tp.R; 0022 0023 class UploadNotification { 0024 private final NotificationManager notificationManager; 0025 private NotificationCompat.Builder builder; 0026 private final int notificationId; 0027 private final Device device; 0028 private final long jobId; 0029 0030 UploadNotification(Device device, long jobId) { 0031 this.device = device; 0032 this.jobId = jobId; 0033 0034 notificationId = (int) System.currentTimeMillis(); 0035 notificationManager = ContextCompat.getSystemService(device.getContext(), NotificationManager.class); 0036 builder = new NotificationCompat.Builder(device.getContext(), NotificationHelper.Channels.FILETRANSFER_UPLOAD) 0037 .setSmallIcon(android.R.drawable.stat_sys_upload) 0038 .setAutoCancel(true) 0039 .setOngoing(true) 0040 .setProgress(100, 0, true); 0041 addCancelAction(); 0042 } 0043 0044 void addCancelAction() { 0045 Intent cancelIntent = new Intent(device.getContext(), ShareBroadcastReceiver.class); 0046 cancelIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); 0047 cancelIntent.setAction(SharePlugin.ACTION_CANCEL_SHARE); 0048 cancelIntent.putExtra(SharePlugin.CANCEL_SHARE_BACKGROUND_JOB_ID_EXTRA, jobId); 0049 cancelIntent.putExtra(SharePlugin.CANCEL_SHARE_DEVICE_ID_EXTRA, device.getDeviceId()); 0050 PendingIntent cancelPendingIntent = PendingIntent.getBroadcast(device.getContext(), 0, cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE); 0051 0052 builder.addAction(R.drawable.ic_reject_pairing_24dp, device.getContext().getString(R.string.cancel), cancelPendingIntent); 0053 } 0054 0055 public void setTitle(String title) { 0056 builder.setContentTitle(title); 0057 builder.setTicker(title); 0058 } 0059 0060 public void setProgress(int progress, String progressMessage) { 0061 builder.setProgress( 100, progress, false); 0062 builder.setContentText(progressMessage); 0063 builder.setStyle(new NotificationCompat.BigTextStyle().bigText(progressMessage)); 0064 } 0065 0066 public void setFinished(String message) { 0067 builder = new NotificationCompat.Builder(device.getContext(), NotificationHelper.Channels.FILETRANSFER_UPLOAD); 0068 builder.setContentTitle(message) 0069 .setTicker(message) 0070 .setSmallIcon(android.R.drawable.stat_sys_upload_done) 0071 .setAutoCancel(true) 0072 .setOngoing(false); 0073 0074 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(device.getContext()); 0075 if (prefs.getBoolean("share_notification_preference", true)) { 0076 builder.setDefaults(Notification.DEFAULT_ALL); 0077 } 0078 } 0079 0080 public void setFailed(String message) { 0081 setFinished(message); 0082 builder.setSmallIcon(android.R.drawable.stat_notify_error) 0083 .setChannelId(NotificationHelper.Channels.FILETRANSFER_ERROR); 0084 } 0085 0086 public void cancel() { 0087 notificationManager.cancel(notificationId); 0088 } 0089 0090 void show() { 0091 NotificationHelper.notifyCompat(notificationManager, notificationId, builder.build()); 0092 } 0093 } 0094