2014-11-16 23:14:06 -08:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Albert Vaca Cintora <albertvaka@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of
|
|
|
|
* the License or (at your option) version 3 or any later version
|
|
|
|
* accepted by the membership of KDE e.V. (or its successor approved
|
|
|
|
* by the membership of KDE e.V.), which shall act as a proxy
|
|
|
|
* defined in Section 14 of version 3 of the license.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2013-09-05 01:37:59 +02:00
|
|
|
package org.kde.kdeconnect.Plugins.NotificationsPlugin;
|
2013-08-19 19:57:29 +02:00
|
|
|
|
2015-09-02 12:15:38 -07:00
|
|
|
import android.annotation.TargetApi;
|
2013-09-03 17:58:59 +02:00
|
|
|
import android.app.Activity;
|
2013-08-19 19:57:29 +02:00
|
|
|
import android.app.AlertDialog;
|
|
|
|
import android.app.Notification;
|
2017-05-31 00:08:38 +02:00
|
|
|
import android.app.PendingIntent;
|
|
|
|
import android.app.RemoteInput;
|
Use small notification icons when available
Summary:
The notification plugin now sends small notification icons, if the large
icon is not available.
Before patch, with icon:
{F5698607}
{F5698608}
Before patch, without icon:
{F5698612}
{F5698613}
After patch, without icon:
{F5698614}
{F5698616}
Test Plan: I tested notifications with small icons and those with large icons, and it works as expected.
Reviewers: #kde_connect, #vdg, fabianr, albertvaka
Reviewed By: #kde_connect, #vdg, fabianr, albertvaka
Subscribers: albertvaka, mmustac, fabianr, apol, nicolasfella
Differential Revision: https://phabricator.kde.org/D10165
2017-11-23 21:11:56 +01:00
|
|
|
import android.content.Context;
|
2013-08-19 19:57:29 +02:00
|
|
|
import android.content.Intent;
|
Use small notification icons when available
Summary:
The notification plugin now sends small notification icons, if the large
icon is not available.
Before patch, with icon:
{F5698607}
{F5698608}
Before patch, without icon:
{F5698612}
{F5698613}
After patch, without icon:
{F5698614}
{F5698616}
Test Plan: I tested notifications with small icons and those with large icons, and it works as expected.
Reviewers: #kde_connect, #vdg, fabianr, albertvaka
Reviewed By: #kde_connect, #vdg, fabianr, albertvaka
Subscribers: albertvaka, mmustac, fabianr, apol, nicolasfella
Differential Revision: https://phabricator.kde.org/D10165
2017-11-23 21:11:56 +01:00
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
import android.content.res.Resources;
|
2017-04-13 21:26:26 +02:00
|
|
|
import android.graphics.Bitmap;
|
Use small notification icons when available
Summary:
The notification plugin now sends small notification icons, if the large
icon is not available.
Before patch, with icon:
{F5698607}
{F5698608}
Before patch, without icon:
{F5698612}
{F5698613}
After patch, without icon:
{F5698614}
{F5698616}
Test Plan: I tested notifications with small icons and those with large icons, and it works as expected.
Reviewers: #kde_connect, #vdg, fabianr, albertvaka
Reviewed By: #kde_connect, #vdg, fabianr, albertvaka
Subscribers: albertvaka, mmustac, fabianr, apol, nicolasfella
Differential Revision: https://phabricator.kde.org/D10165
2017-11-23 21:11:56 +01:00
|
|
|
import android.graphics.Canvas;
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.graphics.drawable.Icon;
|
2013-08-19 19:57:29 +02:00
|
|
|
import android.os.Build;
|
2014-03-27 23:17:43 +01:00
|
|
|
import android.os.Bundle;
|
2013-08-19 19:57:29 +02:00
|
|
|
import android.provider.Settings;
|
|
|
|
import android.service.notification.StatusBarNotification;
|
2018-01-03 20:52:35 +01:00
|
|
|
import android.support.annotation.RequiresApi;
|
2018-03-15 23:47:35 +01:00
|
|
|
import android.support.v4.app.NotificationCompat;
|
2013-08-19 19:57:29 +02:00
|
|
|
import android.util.Log;
|
|
|
|
|
2013-09-05 01:37:59 +02:00
|
|
|
import org.kde.kdeconnect.Helpers.AppsHelper;
|
2018-03-04 11:31:37 +01:00
|
|
|
import org.kde.kdeconnect.NetworkPacket;
|
2013-09-05 01:37:59 +02:00
|
|
|
import org.kde.kdeconnect.Plugins.Plugin;
|
2018-10-16 14:43:00 +02:00
|
|
|
import org.kde.kdeconnect.UserInterface.DeviceSettingsActivity;
|
2018-03-03 17:21:16 +01:00
|
|
|
import org.kde.kdeconnect.UserInterface.MainActivity;
|
2013-09-05 01:35:12 +02:00
|
|
|
import org.kde.kdeconnect_tp.R;
|
2013-08-19 19:57:29 +02:00
|
|
|
|
2017-04-13 21:26:26 +02:00
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.security.MessageDigest;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
2017-05-31 00:08:38 +02:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2017-04-13 21:26:26 +02:00
|
|
|
|
2015-09-02 12:15:38 -07:00
|
|
|
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
|
2013-08-19 19:57:29 +02:00
|
|
|
public class NotificationsPlugin extends Plugin implements NotificationReceiver.NotificationListener {
|
2016-05-31 17:19:39 +02:00
|
|
|
|
2018-03-04 11:31:37 +01:00
|
|
|
private final static String PACKET_TYPE_NOTIFICATION = "kdeconnect.notification";
|
|
|
|
private final static String PACKET_TYPE_NOTIFICATION_REQUEST = "kdeconnect.notification.request";
|
|
|
|
private final static String PACKET_TYPE_NOTIFICATION_REPLY = "kdeconnect.notification.reply";
|
2016-05-31 17:19:39 +02:00
|
|
|
|
2018-03-15 20:26:55 +01:00
|
|
|
private AppDatabase appDatabase;
|
|
|
|
|
2017-05-31 00:08:38 +02:00
|
|
|
private Map<String, RepliableNotification> pendingIntents;
|
2018-01-06 16:06:49 +01:00
|
|
|
private boolean serviceReady;
|
2016-05-31 17:19:39 +02:00
|
|
|
|
2013-08-19 19:57:29 +02:00
|
|
|
@Override
|
|
|
|
public String getDisplayName() {
|
|
|
|
return context.getResources().getString(R.string.pref_plugin_notifications);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getDescription() {
|
|
|
|
return context.getResources().getString(R.string.pref_plugin_notifications_desc);
|
|
|
|
}
|
|
|
|
|
2014-09-16 15:45:31 +02:00
|
|
|
@Override
|
|
|
|
public boolean hasSettings() {
|
2015-01-10 00:22:53 -08:00
|
|
|
return true;
|
2014-09-16 15:45:31 +02:00
|
|
|
}
|
|
|
|
|
2015-01-10 00:31:07 -08:00
|
|
|
@Override
|
2018-10-16 14:43:00 +02:00
|
|
|
public void startPreferencesActivity(final DeviceSettingsActivity parentActivity) {
|
2015-01-10 00:31:07 -08:00
|
|
|
if (hasPermission()) {
|
|
|
|
Intent intent = new Intent(parentActivity, NotificationFilterActivity.class);
|
|
|
|
parentActivity.startActivity(intent);
|
|
|
|
} else {
|
2015-02-01 12:31:29 -08:00
|
|
|
getErrorDialog(parentActivity).show();
|
2015-01-10 00:31:07 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean hasPermission() {
|
|
|
|
String notificationListenerList = Settings.Secure.getString(context.getContentResolver(), "enabled_notification_listeners");
|
|
|
|
return (notificationListenerList != null && notificationListenerList.contains(context.getPackageName()));
|
|
|
|
}
|
2013-08-20 13:41:13 +02:00
|
|
|
|
2013-08-19 19:57:29 +02:00
|
|
|
@Override
|
|
|
|
public boolean onCreate() {
|
2018-01-06 16:06:49 +01:00
|
|
|
|
|
|
|
if (!hasPermission()) return false;
|
|
|
|
|
2018-01-03 20:54:00 +01:00
|
|
|
pendingIntents = new HashMap<>();
|
2018-01-03 20:20:14 +01:00
|
|
|
|
2018-03-15 20:26:55 +01:00
|
|
|
appDatabase = new AppDatabase(context, true);
|
|
|
|
|
2018-05-09 14:02:56 +02:00
|
|
|
NotificationReceiver.RunCommand(context, service -> {
|
2018-01-06 16:06:49 +01:00
|
|
|
|
2018-05-09 14:02:56 +02:00
|
|
|
service.addListener(NotificationsPlugin.this);
|
2018-01-06 16:06:49 +01:00
|
|
|
|
2018-05-09 14:02:56 +02:00
|
|
|
serviceReady = service.isConnected();
|
2018-01-06 16:06:49 +01:00
|
|
|
|
2018-05-09 14:02:56 +02:00
|
|
|
if (serviceReady) {
|
|
|
|
sendCurrentNotifications(service);
|
2018-01-06 16:06:49 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-12-07 21:35:31 +01:00
|
|
|
return true;
|
2013-08-19 19:57:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
2014-03-15 17:31:44 +01:00
|
|
|
|
2018-05-09 14:02:56 +02:00
|
|
|
NotificationReceiver.RunCommand(context, service -> service.removeListener(NotificationsPlugin.this));
|
2013-08-19 19:57:29 +02:00
|
|
|
}
|
|
|
|
|
2018-01-06 16:06:49 +01:00
|
|
|
@Override
|
|
|
|
public void onListenerConnected(NotificationReceiver service) {
|
|
|
|
serviceReady = true;
|
|
|
|
sendCurrentNotifications(service);
|
|
|
|
}
|
2013-08-19 19:57:29 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onNotificationRemoved(StatusBarNotification statusBarNotification) {
|
2017-05-24 00:23:57 +02:00
|
|
|
if (statusBarNotification == null) {
|
|
|
|
Log.w("onNotificationRemoved", "notification is null");
|
|
|
|
return;
|
|
|
|
}
|
2015-09-02 12:15:38 -07:00
|
|
|
String id = getNotificationKeyCompat(statusBarNotification);
|
2018-03-04 11:31:37 +01:00
|
|
|
NetworkPacket np = new NetworkPacket(PACKET_TYPE_NOTIFICATION);
|
2015-08-10 00:28:16 -07:00
|
|
|
np.set("id", id);
|
2013-08-20 13:41:13 +02:00
|
|
|
np.set("isCancel", true);
|
2018-03-04 11:31:37 +01:00
|
|
|
device.sendPacket(np);
|
2013-08-19 19:57:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onNotificationPosted(StatusBarNotification statusBarNotification) {
|
2018-01-06 16:06:49 +01:00
|
|
|
sendNotification(statusBarNotification);
|
2013-08-22 04:38:04 +02:00
|
|
|
}
|
|
|
|
|
2018-01-06 16:06:49 +01:00
|
|
|
private void sendNotification(StatusBarNotification statusBarNotification) {
|
2013-08-19 19:57:29 +02:00
|
|
|
|
|
|
|
Notification notification = statusBarNotification.getNotification();
|
2014-01-10 18:00:26 +01:00
|
|
|
|
2015-09-02 12:15:38 -07:00
|
|
|
if ((notification.flags & Notification.FLAG_FOREGROUND_SERVICE) != 0
|
|
|
|
|| (notification.flags & Notification.FLAG_ONGOING_EVENT) != 0
|
2018-03-15 23:47:35 +01:00
|
|
|
|| (notification.flags & Notification.FLAG_LOCAL_ONLY) != 0
|
|
|
|
|| (notification.flags & NotificationCompat.FLAG_GROUP_SUMMARY) != 0) //The notification that groups other notifications
|
|
|
|
{
|
2014-12-13 21:45:41 -08:00
|
|
|
//This is not a notification we want!
|
2014-01-10 18:00:26 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-03 20:20:14 +01:00
|
|
|
if (!appDatabase.isEnabled(statusBarNotification.getPackageName())) {
|
2015-01-10 00:22:53 -08:00
|
|
|
return;
|
|
|
|
// we dont want notification from this app
|
|
|
|
}
|
|
|
|
|
2015-09-02 12:15:38 -07:00
|
|
|
String key = getNotificationKeyCompat(statusBarNotification);
|
2013-08-19 19:57:29 +02:00
|
|
|
String packageName = statusBarNotification.getPackageName();
|
|
|
|
String appName = AppsHelper.appNameLookup(context, packageName);
|
|
|
|
|
2017-04-13 21:26:26 +02:00
|
|
|
|
2015-09-02 12:15:38 -07:00
|
|
|
if ("com.facebook.orca".equals(packageName) &&
|
|
|
|
(statusBarNotification.getId() == 10012) &&
|
|
|
|
"Messenger".equals(appName) &&
|
|
|
|
notification.tickerText == null) {
|
2015-01-20 23:10:36 -08:00
|
|
|
//HACK: Hide weird Facebook empty "Messenger" notification that is actually not shown in the phone
|
|
|
|
return;
|
2016-03-07 15:46:43 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ("com.android.systemui".equals(packageName) &&
|
2018-01-03 20:20:14 +01:00
|
|
|
"low_battery".equals(statusBarNotification.getTag())) {
|
2016-03-07 15:46:43 -08:00
|
|
|
//HACK: Android low battery notification are posted again every few seconds. Ignore them, as we already have a battery indicator.
|
|
|
|
return;
|
2015-01-20 23:10:36 -08:00
|
|
|
}
|
|
|
|
|
2018-03-04 11:31:37 +01:00
|
|
|
NetworkPacket np = new NetworkPacket(PACKET_TYPE_NOTIFICATION);
|
2015-01-20 21:31:53 -08:00
|
|
|
|
2018-01-03 20:20:14 +01:00
|
|
|
if (packageName.equals("org.kde.kdeconnect_tp")) {
|
2015-01-20 23:10:53 -08:00
|
|
|
//Make our own notifications silent :)
|
|
|
|
np.set("silent", true);
|
|
|
|
np.set("requestAnswer", true); //For compatibility with old desktop versions of KDE Connect that don't support "silent"
|
|
|
|
}
|
2017-04-13 21:26:26 +02:00
|
|
|
|
2018-01-03 20:54:00 +01:00
|
|
|
try {
|
Use small notification icons when available
Summary:
The notification plugin now sends small notification icons, if the large
icon is not available.
Before patch, with icon:
{F5698607}
{F5698608}
Before patch, without icon:
{F5698612}
{F5698613}
After patch, without icon:
{F5698614}
{F5698616}
Test Plan: I tested notifications with small icons and those with large icons, and it works as expected.
Reviewers: #kde_connect, #vdg, fabianr, albertvaka
Reviewed By: #kde_connect, #vdg, fabianr, albertvaka
Subscribers: albertvaka, mmustac, fabianr, apol, nicolasfella
Differential Revision: https://phabricator.kde.org/D10165
2017-11-23 21:11:56 +01:00
|
|
|
Bitmap appIcon = null;
|
|
|
|
Context foreignContext = context.createPackageContext(statusBarNotification.getPackageName(), 0);
|
|
|
|
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
|
|
|
|
appIcon = iconToBitmap(foreignContext, notification.getLargeIcon());
|
|
|
|
} else {
|
|
|
|
appIcon = notification.largeIcon;
|
|
|
|
}
|
|
|
|
//appIcon = drawableToBitmap(context.getResources().getDrawable(R.drawable.icon));
|
|
|
|
if (appIcon == null) {
|
|
|
|
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
|
|
|
|
appIcon = iconToBitmap(foreignContext, notification.getSmallIcon());
|
|
|
|
} else {
|
|
|
|
PackageManager pm = context.getPackageManager();
|
|
|
|
Resources foreignResources = pm.getResourcesForApplication(statusBarNotification.getPackageName());
|
|
|
|
Drawable foreignIcon = foreignResources.getDrawable(notification.icon);
|
|
|
|
appIcon = drawableToBitmap(foreignIcon);
|
|
|
|
}
|
|
|
|
}
|
2017-04-13 21:26:26 +02:00
|
|
|
|
2018-01-03 20:54:00 +01:00
|
|
|
if (appIcon != null) {
|
|
|
|
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
|
|
|
|
appIcon.compress(Bitmap.CompressFormat.PNG, 90, outStream);
|
|
|
|
byte[] bitmapData = outStream.toByteArray();
|
2017-04-13 21:26:26 +02:00
|
|
|
|
Use small notification icons when available
Summary:
The notification plugin now sends small notification icons, if the large
icon is not available.
Before patch, with icon:
{F5698607}
{F5698608}
Before patch, without icon:
{F5698612}
{F5698613}
After patch, without icon:
{F5698614}
{F5698616}
Test Plan: I tested notifications with small icons and those with large icons, and it works as expected.
Reviewers: #kde_connect, #vdg, fabianr, albertvaka
Reviewed By: #kde_connect, #vdg, fabianr, albertvaka
Subscribers: albertvaka, mmustac, fabianr, apol, nicolasfella
Differential Revision: https://phabricator.kde.org/D10165
2017-11-23 21:11:56 +01:00
|
|
|
|
2018-01-03 20:54:00 +01:00
|
|
|
np.setPayload(bitmapData);
|
2017-04-13 21:26:26 +02:00
|
|
|
|
2018-01-03 20:54:00 +01:00
|
|
|
np.set("payloadHash", getChecksum(bitmapData));
|
2015-01-20 21:31:53 -08:00
|
|
|
}
|
2018-01-03 20:54:00 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
Log.e("NotificationsPlugin", "Error retrieving icon");
|
2013-08-19 19:57:29 +02:00
|
|
|
}
|
2018-01-03 20:20:14 +01:00
|
|
|
|
2017-05-31 00:08:38 +02:00
|
|
|
RepliableNotification rn = extractRepliableNotification(statusBarNotification);
|
2018-01-03 20:20:14 +01:00
|
|
|
if (rn.pendingIntent != null) {
|
2017-05-31 00:08:38 +02:00
|
|
|
np.set("requestReplyId", rn.id);
|
|
|
|
pendingIntents.put(rn.id, rn);
|
|
|
|
}
|
2017-04-13 21:26:26 +02:00
|
|
|
|
2015-09-02 12:15:38 -07:00
|
|
|
np.set("id", key);
|
2018-01-03 20:20:14 +01:00
|
|
|
np.set("appName", appName == null ? packageName : appName);
|
2013-08-19 19:57:29 +02:00
|
|
|
np.set("isClearable", statusBarNotification.isClearable());
|
2014-03-27 23:17:43 +01:00
|
|
|
np.set("ticker", getTickerText(notification));
|
2017-04-13 21:26:26 +02:00
|
|
|
np.set("title", getNotificationTitle(notification));
|
|
|
|
np.set("text", getNotificationText(notification));
|
2013-09-05 10:37:42 +02:00
|
|
|
np.set("time", Long.toString(statusBarNotification.getPostTime()));
|
2013-08-19 19:57:29 +02:00
|
|
|
|
2018-03-04 11:31:37 +01:00
|
|
|
device.sendPacket(np);
|
2013-08-19 19:57:29 +02:00
|
|
|
}
|
|
|
|
|
Use small notification icons when available
Summary:
The notification plugin now sends small notification icons, if the large
icon is not available.
Before patch, with icon:
{F5698607}
{F5698608}
Before patch, without icon:
{F5698612}
{F5698613}
After patch, without icon:
{F5698614}
{F5698616}
Test Plan: I tested notifications with small icons and those with large icons, and it works as expected.
Reviewers: #kde_connect, #vdg, fabianr, albertvaka
Reviewed By: #kde_connect, #vdg, fabianr, albertvaka
Subscribers: albertvaka, mmustac, fabianr, apol, nicolasfella
Differential Revision: https://phabricator.kde.org/D10165
2017-11-23 21:11:56 +01:00
|
|
|
private Bitmap drawableToBitmap(Drawable drawable) {
|
|
|
|
if (drawable == null) return null;
|
|
|
|
|
|
|
|
Bitmap res;
|
|
|
|
if (drawable.getIntrinsicWidth() > 128 || drawable.getIntrinsicHeight() > 128) {
|
|
|
|
res = Bitmap.createBitmap(96, 96, Bitmap.Config.ARGB_8888);
|
|
|
|
} else if (drawable.getIntrinsicWidth() <= 64 || drawable.getIntrinsicHeight() <= 64) {
|
|
|
|
res = Bitmap.createBitmap(96, 96, Bitmap.Config.ARGB_8888);
|
|
|
|
} else {
|
|
|
|
res = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
|
|
|
|
}
|
|
|
|
|
|
|
|
Canvas canvas = new Canvas(res);
|
|
|
|
drawable.setBounds(0, 0, res.getWidth(), res.getHeight());
|
|
|
|
drawable.draw(canvas);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
@RequiresApi(Build.VERSION_CODES.M)
|
|
|
|
private Bitmap iconToBitmap(Context foreignContext, Icon icon) {
|
|
|
|
if (icon == null) return null;
|
|
|
|
|
|
|
|
return drawableToBitmap(icon.loadDrawable(foreignContext));
|
|
|
|
}
|
|
|
|
|
2018-01-03 20:52:35 +01:00
|
|
|
@RequiresApi(api = Build.VERSION_CODES.KITKAT_WATCH)
|
2018-01-03 21:09:04 +01:00
|
|
|
private void replyToNotification(String id, String message) {
|
2018-01-03 20:20:14 +01:00
|
|
|
if (pendingIntents.isEmpty() || !pendingIntents.containsKey(id)) {
|
2017-05-31 00:08:38 +02:00
|
|
|
Log.e("NotificationsPlugin", "No such notification");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RepliableNotification repliableNotification = pendingIntents.get(id);
|
2018-01-03 20:20:14 +01:00
|
|
|
if (repliableNotification == null) {
|
2017-05-31 00:08:38 +02:00
|
|
|
Log.e("NotificationsPlugin", "No such notification");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
RemoteInput[] remoteInputs = new RemoteInput[repliableNotification.remoteInputs.size()];
|
|
|
|
|
|
|
|
Intent localIntent = new Intent();
|
|
|
|
localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
Bundle localBundle = new Bundle();
|
|
|
|
int i = 0;
|
2018-01-03 20:20:14 +01:00
|
|
|
for (RemoteInput remoteIn : repliableNotification.remoteInputs) {
|
2017-05-31 00:08:38 +02:00
|
|
|
getDetailsOfNotification(remoteIn);
|
|
|
|
remoteInputs[i] = remoteIn;
|
|
|
|
localBundle.putCharSequence(remoteInputs[i].getResultKey(), message);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
RemoteInput.addResultsToIntent(remoteInputs, localIntent, localBundle);
|
2018-01-03 20:20:14 +01:00
|
|
|
|
2017-05-31 00:08:38 +02:00
|
|
|
try {
|
|
|
|
repliableNotification.pendingIntent.send(context, 0, localIntent);
|
|
|
|
} catch (PendingIntent.CanceledException e) {
|
|
|
|
Log.e("NotificationPlugin", "replyToNotification error: " + e.getMessage());
|
|
|
|
}
|
|
|
|
pendingIntents.remove(id);
|
|
|
|
}
|
|
|
|
|
2018-01-03 20:52:35 +01:00
|
|
|
@RequiresApi(api = Build.VERSION_CODES.KITKAT_WATCH)
|
2017-05-31 00:08:38 +02:00
|
|
|
private void getDetailsOfNotification(RemoteInput remoteInput) {
|
|
|
|
//Some more details of RemoteInput... no idea what for but maybe it will be useful at some point
|
|
|
|
String resultKey = remoteInput.getResultKey();
|
|
|
|
String label = remoteInput.getLabel().toString();
|
|
|
|
Boolean canFreeForm = remoteInput.getAllowFreeFormInput();
|
2018-01-03 20:20:14 +01:00
|
|
|
if (remoteInput.getChoices() != null && remoteInput.getChoices().length > 0) {
|
2017-05-31 00:08:38 +02:00
|
|
|
String[] possibleChoices = new String[remoteInput.getChoices().length];
|
2018-01-03 20:20:14 +01:00
|
|
|
for (int i = 0; i < remoteInput.getChoices().length; i++) {
|
2017-05-31 00:08:38 +02:00
|
|
|
possibleChoices[i] = remoteInput.getChoices()[i].toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-03 20:20:14 +01:00
|
|
|
|
2017-04-13 21:26:26 +02:00
|
|
|
private String getNotificationTitle(Notification notification) {
|
|
|
|
final String TITLE_KEY = "android.title";
|
|
|
|
final String TEXT_KEY = "android.text";
|
|
|
|
String title = "";
|
|
|
|
|
2018-01-03 20:20:14 +01:00
|
|
|
if (notification != null) {
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
2017-04-13 21:26:26 +02:00
|
|
|
try {
|
|
|
|
Bundle extras = notification.extras;
|
2018-01-06 16:05:39 +01:00
|
|
|
title = extras.getString(TITLE_KEY);
|
2018-01-03 20:20:14 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
Log.w("NotificationPlugin", "problem parsing notification extras for " + notification.tickerText);
|
2017-04-13 21:26:26 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO Add compat for under Kitkat devices
|
|
|
|
|
|
|
|
return title;
|
|
|
|
}
|
2018-01-03 20:20:14 +01:00
|
|
|
|
2017-05-31 00:08:38 +02:00
|
|
|
private RepliableNotification extractRepliableNotification(StatusBarNotification statusBarNotification) {
|
|
|
|
RepliableNotification repliableNotification = new RepliableNotification();
|
2018-01-03 20:20:14 +01:00
|
|
|
|
|
|
|
if (statusBarNotification != null) {
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
2017-05-31 00:08:38 +02:00
|
|
|
try {
|
|
|
|
Boolean reply = false;
|
2018-01-03 20:20:14 +01:00
|
|
|
|
2017-05-31 00:08:38 +02:00
|
|
|
//works for WhatsApp, but not for Telegram
|
2018-01-03 20:20:14 +01:00
|
|
|
if (statusBarNotification.getNotification().actions != null) {
|
2017-07-17 15:21:04 +02:00
|
|
|
for (Notification.Action act : statusBarNotification.getNotification().actions) {
|
|
|
|
if (act != null && act.getRemoteInputs() != null) {
|
|
|
|
repliableNotification.remoteInputs.addAll(Arrays.asList(act.getRemoteInputs()));
|
|
|
|
repliableNotification.pendingIntent = act.actionIntent;
|
|
|
|
reply = true;
|
|
|
|
break;
|
|
|
|
}
|
2017-05-31 00:08:38 +02:00
|
|
|
}
|
2017-07-17 15:21:04 +02:00
|
|
|
|
|
|
|
repliableNotification.packageName = statusBarNotification.getPackageName();
|
|
|
|
|
|
|
|
repliableNotification.tag = statusBarNotification.getTag();//TODO find how to pass Tag with sending PendingIntent, might fix Hangout problem
|
2017-05-31 00:08:38 +02:00
|
|
|
}
|
2018-01-03 20:20:14 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
Log.w("NotificationPlugin", "problem extracting notification wear for " + statusBarNotification.getNotification().tickerText);
|
2017-05-31 00:08:38 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-03 20:20:14 +01:00
|
|
|
|
2017-05-31 00:08:38 +02:00
|
|
|
return repliableNotification;
|
|
|
|
}
|
2017-04-13 21:26:26 +02:00
|
|
|
|
|
|
|
private String getNotificationText(Notification notification) {
|
|
|
|
final String TEXT_KEY = "android.text";
|
|
|
|
String text = "";
|
|
|
|
|
2018-01-03 20:20:14 +01:00
|
|
|
if (notification != null) {
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
2017-04-13 21:26:26 +02:00
|
|
|
try {
|
|
|
|
Bundle extras = notification.extras;
|
|
|
|
Object extraTextExtra = extras.get(TEXT_KEY);
|
|
|
|
if (extraTextExtra != null) text = extraTextExtra.toString();
|
2018-01-03 20:20:14 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
Log.w("NotificationPlugin", "problem parsing notification extras for " + notification.tickerText);
|
2017-04-13 21:26:26 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO Add compat for under Kitkat devices
|
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2013-08-19 19:57:29 +02:00
|
|
|
|
2014-03-27 23:17:43 +01:00
|
|
|
/**
|
|
|
|
* Returns the ticker text of the notification.
|
|
|
|
* If device android version is KitKat or newer, the title and text of the notification is used
|
|
|
|
* instead the ticker text.
|
|
|
|
*/
|
|
|
|
private String getTickerText(Notification notification) {
|
|
|
|
final String TITLE_KEY = "android.title";
|
|
|
|
final String TEXT_KEY = "android.text";
|
|
|
|
String ticker = "";
|
|
|
|
|
2018-01-03 20:20:14 +01:00
|
|
|
if (notification != null) {
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
2014-06-27 14:44:40 +02:00
|
|
|
try {
|
|
|
|
Bundle extras = notification.extras;
|
2018-01-06 16:05:39 +01:00
|
|
|
String extraTitle = extras.getString(TITLE_KEY);
|
2015-01-10 00:40:36 -08:00
|
|
|
String extraText = null;
|
|
|
|
Object extraTextExtra = extras.get(TEXT_KEY);
|
|
|
|
if (extraTextExtra != null) extraText = extraTextExtra.toString();
|
2014-06-27 14:44:40 +02:00
|
|
|
|
2015-01-10 00:40:36 -08:00
|
|
|
if (extraTitle != null && extraText != null && !extraText.isEmpty()) {
|
2017-04-13 21:26:26 +02:00
|
|
|
ticker = extraTitle + ": " + extraText;
|
2014-06-27 14:44:40 +02:00
|
|
|
} else if (extraTitle != null) {
|
|
|
|
ticker = extraTitle;
|
|
|
|
} else if (extraText != null) {
|
|
|
|
ticker = extraText;
|
|
|
|
}
|
2018-01-03 20:20:14 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
Log.w("NotificationPlugin", "problem parsing notification extras for " + notification.tickerText);
|
2014-06-27 14:44:40 +02:00
|
|
|
e.printStackTrace();
|
2014-03-27 23:17:43 +01:00
|
|
|
}
|
|
|
|
}
|
2013-08-19 19:57:29 +02:00
|
|
|
|
2014-03-29 01:47:15 +01:00
|
|
|
if (ticker.isEmpty()) {
|
2018-01-03 20:20:14 +01:00
|
|
|
ticker = (notification.tickerText != null) ? notification.tickerText.toString() : "";
|
2014-03-27 23:17:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ticker;
|
|
|
|
}
|
2013-08-19 19:57:29 +02:00
|
|
|
|
2018-01-06 16:06:49 +01:00
|
|
|
private void sendCurrentNotifications(NotificationReceiver service) {
|
|
|
|
StatusBarNotification[] notifications = service.getActiveNotifications();
|
|
|
|
for (StatusBarNotification notification : notifications) {
|
|
|
|
sendNotification(notification);
|
|
|
|
}
|
|
|
|
}
|
2013-08-19 19:57:29 +02:00
|
|
|
|
|
|
|
@Override
|
2018-03-04 11:31:37 +01:00
|
|
|
public boolean onPacketReceived(final NetworkPacket np) {
|
2018-01-03 20:54:00 +01:00
|
|
|
|
2013-08-19 19:57:29 +02:00
|
|
|
if (np.getBoolean("request")) {
|
|
|
|
|
2018-01-06 16:06:49 +01:00
|
|
|
if (serviceReady) {
|
2018-05-09 14:02:56 +02:00
|
|
|
NotificationReceiver.RunCommand(context, this::sendCurrentNotifications);
|
2018-01-06 16:06:49 +01:00
|
|
|
}
|
2013-08-19 19:57:29 +02:00
|
|
|
|
2013-08-20 13:41:13 +02:00
|
|
|
} else if (np.has("cancel")) {
|
2013-08-19 19:57:29 +02:00
|
|
|
|
2018-05-09 14:02:56 +02:00
|
|
|
NotificationReceiver.RunCommand(context, service -> {
|
|
|
|
String dismissedId = np.getString("cancel");
|
|
|
|
cancelNotificationCompat(service, dismissedId);
|
2018-01-03 20:52:35 +01:00
|
|
|
});
|
2013-08-19 19:57:29 +02:00
|
|
|
|
2017-05-31 00:08:38 +02:00
|
|
|
} else if (np.has("requestReplyId") && np.has("message")) {
|
2018-01-03 20:20:14 +01:00
|
|
|
|
2018-01-03 20:52:35 +01:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
|
|
|
|
replyToNotification(np.getString("requestReplyId"), np.getString("message"));
|
|
|
|
}
|
2018-01-03 20:20:14 +01:00
|
|
|
|
2013-08-19 19:57:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2014-09-20 10:52:03 +02:00
|
|
|
public AlertDialog getErrorDialog(final Activity deviceActivity) {
|
2013-08-19 19:57:29 +02:00
|
|
|
|
2018-01-03 20:52:35 +01:00
|
|
|
return new AlertDialog.Builder(deviceActivity)
|
|
|
|
.setTitle(R.string.pref_plugin_notifications)
|
|
|
|
.setMessage(R.string.no_permissions)
|
2018-05-09 14:02:56 +02:00
|
|
|
.setPositiveButton(R.string.open_settings, (dialogInterface, i) -> {
|
|
|
|
Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
|
|
|
|
deviceActivity.startActivityForResult(intent, MainActivity.RESULT_NEEDS_RELOAD);
|
2018-01-03 20:52:35 +01:00
|
|
|
})
|
2018-05-09 14:02:56 +02:00
|
|
|
.setNegativeButton(R.string.cancel, (dialogInterface, i) -> {
|
|
|
|
//Do nothing
|
2018-01-03 20:52:35 +01:00
|
|
|
})
|
|
|
|
.create();
|
2013-08-19 19:57:29 +02:00
|
|
|
|
2015-09-02 12:15:38 -07:00
|
|
|
}
|
|
|
|
|
2015-09-08 14:54:04 -07:00
|
|
|
@Override
|
2018-03-04 11:31:37 +01:00
|
|
|
public String[] getSupportedPacketTypes() {
|
|
|
|
return new String[]{PACKET_TYPE_NOTIFICATION_REQUEST, PACKET_TYPE_NOTIFICATION_REPLY};
|
2015-09-08 14:54:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-03-04 11:31:37 +01:00
|
|
|
public String[] getOutgoingPacketTypes() {
|
|
|
|
return new String[]{PACKET_TYPE_NOTIFICATION};
|
2015-09-08 14:54:04 -07:00
|
|
|
}
|
|
|
|
|
2015-10-25 11:21:43 +01:00
|
|
|
//For compat with API<21, because lollipop changed the way to cancel notifications
|
2018-01-03 21:09:04 +01:00
|
|
|
private static void cancelNotificationCompat(NotificationReceiver service, String compatKey) {
|
2015-09-02 12:15:38 -07:00
|
|
|
if (Build.VERSION.SDK_INT >= 21) {
|
|
|
|
service.cancelNotification(compatKey);
|
|
|
|
} else {
|
|
|
|
int first = compatKey.indexOf(':');
|
2016-06-06 00:01:48 +02:00
|
|
|
if (first == -1) {
|
2018-01-03 20:20:14 +01:00
|
|
|
Log.e("cancelNotificationCompa", "Not formated like a notification key: " + compatKey);
|
2016-06-06 00:01:48 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-09-02 12:15:38 -07:00
|
|
|
int last = compatKey.lastIndexOf(':');
|
|
|
|
String packageName = compatKey.substring(0, first);
|
|
|
|
String tag = compatKey.substring(first + 1, last);
|
|
|
|
if (tag.length() == 0) tag = null;
|
|
|
|
String idString = compatKey.substring(last + 1);
|
|
|
|
int id;
|
|
|
|
try {
|
|
|
|
id = Integer.parseInt(idString);
|
|
|
|
} catch (Exception e) {
|
|
|
|
id = 0;
|
|
|
|
}
|
|
|
|
service.cancelNotification(packageName, tag, id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-03 21:09:04 +01:00
|
|
|
private static String getNotificationKeyCompat(StatusBarNotification statusBarNotification) {
|
2015-12-07 21:35:31 +01:00
|
|
|
String result;
|
|
|
|
// first check if it's one of our remoteIds
|
|
|
|
String tag = statusBarNotification.getTag();
|
|
|
|
if (tag != null && tag.startsWith("kdeconnectId:"))
|
|
|
|
result = Integer.toString(statusBarNotification.getId());
|
|
|
|
else if (Build.VERSION.SDK_INT >= 21) {
|
|
|
|
result = statusBarNotification.getKey();
|
2015-09-02 12:15:38 -07:00
|
|
|
} else {
|
|
|
|
String packageName = statusBarNotification.getPackageName();
|
|
|
|
int id = statusBarNotification.getId();
|
|
|
|
String safePackageName = (packageName == null) ? "" : packageName;
|
|
|
|
String safeTag = (tag == null) ? "" : tag;
|
2015-12-07 21:35:31 +01:00
|
|
|
result = safePackageName + ":" + safeTag + ":" + id;
|
2013-08-19 19:57:29 +02:00
|
|
|
}
|
2015-12-07 21:35:31 +01:00
|
|
|
return result;
|
2013-08-19 19:57:29 +02:00
|
|
|
}
|
2017-04-13 21:26:26 +02:00
|
|
|
|
2018-01-03 21:09:04 +01:00
|
|
|
private String getChecksum(byte[] data) {
|
2017-04-13 21:26:26 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
MessageDigest md = MessageDigest.getInstance("MD5");
|
|
|
|
md.update(data);
|
|
|
|
return bytesToHex(md.digest());
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
2018-01-03 20:52:35 +01:00
|
|
|
Log.e("KDEConnect", "Error while generating checksum", e);
|
2017-04-13 21:26:26 +02:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-03 21:09:04 +01:00
|
|
|
private static String bytesToHex(byte[] bytes) {
|
2017-04-13 21:26:26 +02:00
|
|
|
char[] hexArray = "0123456789ABCDEF".toCharArray();
|
|
|
|
char[] hexChars = new char[bytes.length * 2];
|
2018-01-03 20:20:14 +01:00
|
|
|
for (int j = 0; j < bytes.length; j++) {
|
2017-04-13 21:26:26 +02:00
|
|
|
int v = bytes[j] & 0xFF;
|
|
|
|
hexChars[j * 2] = hexArray[v >>> 4];
|
|
|
|
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
|
|
|
|
}
|
|
|
|
return new String(hexChars).toLowerCase();
|
|
|
|
}
|
|
|
|
|
2018-01-03 20:52:35 +01:00
|
|
|
@Override
|
|
|
|
public int getMinSdk() {
|
|
|
|
return Build.VERSION_CODES.JELLY_BEAN_MR2;
|
|
|
|
}
|
2013-08-19 19:57:29 +02:00
|
|
|
}
|