FreeRDP
Loading...
Searching...
No Matches
PrintNotificationHelper.java
1/*
2 Print Notification Helper
3
4 Copyright 2026 Ibrahim Sevinc
5
6 This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
7 If a copy of the MPL was not distributed with this file, You can obtain one at
8 http://mozilla.org/MPL/2.0/.
9*/
10
11package com.freerdp.freerdpcore.presentation;
12
13import android.app.Notification;
14import android.app.NotificationChannel;
15import android.app.NotificationManager;
16import android.app.PendingIntent;
17import android.content.Context;
18import android.content.Intent;
19import android.net.Uri;
20import android.os.Build;
21
22import androidx.core.app.NotificationCompat;
23import androidx.core.content.FileProvider;
24
25import com.freerdp.freerdpcore.R;
26
27import java.io.File;
28import java.util.concurrent.atomic.AtomicInteger;
29
31{
32 private static final String CHANNEL_ID = "rdp_print";
33 private static final AtomicInteger notifId = new AtomicInteger(1000);
34
35 public static void ensureChannel(Context ctx)
36 {
37 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
38 return;
39 NotificationManager nm = ctx.getSystemService(NotificationManager.class);
40 if (nm.getNotificationChannel(CHANNEL_ID) != null)
41 return;
42 NotificationChannel ch =
43 new NotificationChannel(CHANNEL_ID, ctx.getString(R.string.print_notif_channel),
44 NotificationManager.IMPORTANCE_HIGH);
45 nm.createNotificationChannel(ch);
46 }
47
48 public static void notify(Context ctx, File pdfFile)
49 {
50 ensureChannel(ctx);
51
52 Uri uri = FileProvider.getUriForFile(ctx, ctx.getPackageName() + ".fileprovider", pdfFile);
53
54 PendingIntent openIntent = PendingIntent.getActivity(
55 ctx, notifId.get(),
56 new Intent(Intent.ACTION_VIEW)
57 .setDataAndType(uri, "application/pdf")
58 .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION),
59 PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
60
61 Intent printLaunch = new Intent(ctx, PrintProxyActivity.class);
62 printLaunch.setData(uri);
63 printLaunch.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
64 PendingIntent printIntent = PendingIntent.getActivity(
65 ctx, notifId.get() + 1, printLaunch,
66 PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
67
68 Notification notif = new NotificationCompat.Builder(ctx, CHANNEL_ID)
69 .setSmallIcon(android.R.drawable.ic_dialog_info)
70 .setContentTitle(ctx.getString(R.string.print_notif_title))
71 .setContentText(pdfFile.getName())
72 .setContentIntent(openIntent)
73 .addAction(android.R.drawable.ic_menu_view,
74 ctx.getString(R.string.print_notif_open), openIntent)
75 .addAction(android.R.drawable.ic_menu_send,
76 ctx.getString(R.string.print_notif_print), printIntent)
77 .setAutoCancel(true)
78 .build();
79
80 NotificationManager nm =
81 (NotificationManager)ctx.getSystemService(Context.NOTIFICATION_SERVICE);
82 nm.notify(notifId.getAndIncrement(), notif);
83 }
84}