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;
20
21import androidx.core.app.NotificationCompat;
22import androidx.core.content.FileProvider;
23
24import com.freerdp.freerdpcore.R;
25
26import java.io.File;
27import java.util.concurrent.atomic.AtomicInteger;
28
30{
31 private static final String CHANNEL_ID = "rdp_print";
32 private static final String AUTHORITY = "com.freerdp.afreerdp.fileprovider";
33 private static final AtomicInteger notifId = new AtomicInteger(1000);
34
35 public static void ensureChannel(Context ctx)
36 {
37 NotificationManager nm = ctx.getSystemService(NotificationManager.class);
38 if (nm.getNotificationChannel(CHANNEL_ID) != null)
39 return;
40 NotificationChannel ch =
41 new NotificationChannel(CHANNEL_ID, ctx.getString(R.string.print_notif_channel),
42 NotificationManager.IMPORTANCE_HIGH);
43 nm.createNotificationChannel(ch);
44 }
45
46 public static void notify(Context ctx, File pdfFile)
47 {
48 ensureChannel(ctx);
49
50 Uri uri = FileProvider.getUriForFile(ctx, AUTHORITY, pdfFile);
51
52 PendingIntent openIntent = PendingIntent.getActivity(
53 ctx, notifId.get(),
54 new Intent(Intent.ACTION_VIEW)
55 .setDataAndType(uri, "application/pdf")
56 .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION),
57 PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
58
59 Intent printLaunch = new Intent(ctx, PrintProxyActivity.class);
60 printLaunch.setData(uri);
61 printLaunch.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
62 PendingIntent printIntent = PendingIntent.getActivity(
63 ctx, notifId.get() + 1, printLaunch,
64 PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
65
66 Notification notif = new NotificationCompat.Builder(ctx, CHANNEL_ID)
67 .setSmallIcon(android.R.drawable.ic_dialog_info)
68 .setContentTitle(ctx.getString(R.string.print_notif_title))
69 .setContentText(pdfFile.getName())
70 .setContentIntent(openIntent)
71 .addAction(android.R.drawable.ic_menu_view,
72 ctx.getString(R.string.print_notif_open), openIntent)
73 .addAction(android.R.drawable.ic_menu_send,
74 ctx.getString(R.string.print_notif_print), printIntent)
75 .setAutoCancel(true)
76 .build();
77
78 NotificationManager nm =
79 (NotificationManager)ctx.getSystemService(Context.NOTIFICATION_SERVICE);
80 nm.notify(notifId.getAndIncrement(), notif);
81 }
82}