File indexing completed on 2024-12-22 04:41:40
0001 /* 0002 * SPDX-FileCopyrightText: 2014 Albert Vaca Cintora <albertvaka@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.PingPlugin; 0008 0009 import android.Manifest; 0010 import android.app.Activity; 0011 import android.app.Notification; 0012 import android.app.NotificationManager; 0013 import android.app.PendingIntent; 0014 import android.content.Intent; 0015 import android.content.pm.PackageManager; 0016 import android.os.Build; 0017 import android.os.Handler; 0018 import android.os.Looper; 0019 import android.util.Log; 0020 import android.widget.Toast; 0021 0022 import androidx.annotation.NonNull; 0023 import androidx.core.app.NotificationCompat; 0024 import androidx.core.content.ContextCompat; 0025 0026 import org.kde.kdeconnect.Helpers.NotificationHelper; 0027 import org.kde.kdeconnect.NetworkPacket; 0028 import org.kde.kdeconnect.Plugins.Plugin; 0029 import org.kde.kdeconnect.Plugins.PluginFactory; 0030 import org.kde.kdeconnect.UserInterface.MainActivity; 0031 import org.kde.kdeconnect_tp.R; 0032 0033 @PluginFactory.LoadablePlugin 0034 public class PingPlugin extends Plugin { 0035 0036 private final static String PACKET_TYPE_PING = "kdeconnect.ping"; 0037 0038 @Override 0039 public @NonNull String getDisplayName() { 0040 return context.getResources().getString(R.string.pref_plugin_ping); 0041 } 0042 0043 @Override 0044 public @NonNull String getDescription() { 0045 return context.getResources().getString(R.string.pref_plugin_ping_desc); 0046 } 0047 0048 @Override 0049 public boolean onPacketReceived(@NonNull NetworkPacket np) { 0050 0051 if (!np.getType().equals(PACKET_TYPE_PING)) { 0052 Log.e("PingPlugin", "Ping plugin should not receive packets other than pings!"); 0053 return false; 0054 } 0055 0056 //Log.e("PingPacketReceiver", "was a ping!"); 0057 0058 PendingIntent resultPendingIntent = PendingIntent.getActivity( 0059 context, 0060 0, 0061 new Intent(context, MainActivity.class), 0062 PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE 0063 ); 0064 0065 int id; 0066 String message; 0067 if (np.has("message")) { 0068 message = np.getString("message"); 0069 id = (int) System.currentTimeMillis(); 0070 } else { 0071 message = "Ping!"; 0072 id = 42; //A unique id to create only one notification 0073 } 0074 0075 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { 0076 int permissionResult = ContextCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS); 0077 if (permissionResult != PackageManager.PERMISSION_GRANTED) { 0078 // If notifications are not allowed, show a toast instead of a notification 0079 new Handler(Looper.getMainLooper()).post(() -> Toast.makeText(context, message, Toast.LENGTH_LONG).show()); 0080 return true; 0081 } 0082 } 0083 0084 NotificationManager notificationManager = ContextCompat.getSystemService(context, NotificationManager.class); 0085 0086 Notification noti = new NotificationCompat.Builder(context, NotificationHelper.Channels.DEFAULT) 0087 .setContentTitle(device.getName()) 0088 .setContentText(message) 0089 .setContentIntent(resultPendingIntent) 0090 .setTicker(message) 0091 .setSmallIcon(R.drawable.ic_notification) 0092 .setAutoCancel(true) 0093 .setDefaults(Notification.DEFAULT_ALL) 0094 .setStyle(new NotificationCompat.BigTextStyle().bigText(message)) 0095 .build(); 0096 0097 NotificationHelper.notifyCompat(notificationManager, id, noti); 0098 0099 return true; 0100 0101 } 0102 0103 @Override 0104 public @NonNull String getActionName() { 0105 return context.getString(R.string.send_ping); 0106 } 0107 0108 @Override 0109 public void startMainActivity(Activity activity) { 0110 if (device != null) { 0111 device.sendPacket(new NetworkPacket(PACKET_TYPE_PING)); 0112 } 0113 } 0114 0115 @Override 0116 public boolean displayInContextMenu() { 0117 return true; 0118 } 0119 0120 @Override 0121 public @NonNull String[] getSupportedPacketTypes() { 0122 return new String[]{PACKET_TYPE_PING}; 0123 } 0124 0125 @Override 0126 public @NonNull String[] getOutgoingPacketTypes() { 0127 return new String[]{PACKET_TYPE_PING}; 0128 } 0129 0130 }