2016-06-26 17:08:47 +02: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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.kde.kdeconnect.Plugins.ReceiveNotificationsPlugin;
|
|
|
|
|
|
|
|
import android.app.Notification;
|
|
|
|
import android.app.NotificationManager;
|
|
|
|
import android.app.PendingIntent;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.BitmapFactory;
|
|
|
|
import android.os.Build;
|
|
|
|
import android.support.v4.app.NotificationCompat;
|
|
|
|
import android.support.v4.app.TaskStackBuilder;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2016-12-05 23:42:58 +01:00
|
|
|
import org.kde.kdeconnect.Helpers.NotificationHelper;
|
2018-03-04 11:31:37 +01:00
|
|
|
import org.kde.kdeconnect.NetworkPacket;
|
2016-06-26 17:08:47 +02:00
|
|
|
import org.kde.kdeconnect.Plugins.Plugin;
|
2018-03-03 17:21:16 +01:00
|
|
|
import org.kde.kdeconnect.UserInterface.MainActivity;
|
2016-06-26 17:08:47 +02:00
|
|
|
import org.kde.kdeconnect_tp.R;
|
|
|
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
|
|
public class ReceiveNotificationsPlugin extends Plugin {
|
|
|
|
|
2018-03-04 11:31:37 +01:00
|
|
|
public final static String PACKET_TYPE_NOTIFICATION = "kdeconnect.notification";
|
|
|
|
public final static String PACKET_TYPE_NOTIFICATION_REQUEST = "kdeconnect.notification.request";
|
2016-06-26 17:08:47 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getDisplayName() {
|
|
|
|
return context.getResources().getString(R.string.pref_plugin_receive_notifications);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getDescription() {
|
|
|
|
return context.getResources().getString(R.string.pref_plugin_receive_notifications_desc);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isEnabledByDefault() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCreate() {
|
|
|
|
// request all existing notifications
|
2018-03-04 11:31:37 +01:00
|
|
|
NetworkPacket np = new NetworkPacket(PACKET_TYPE_NOTIFICATION_REQUEST);
|
2016-06-26 17:08:47 +02:00
|
|
|
np.set("request", true);
|
2018-03-04 11:31:37 +01:00
|
|
|
device.sendPacket(np);
|
2016-06-26 17:08:47 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-03-04 11:31:37 +01:00
|
|
|
public boolean onPacketReceived(final NetworkPacket np) {
|
2016-06-26 17:08:47 +02:00
|
|
|
|
|
|
|
if (!np.has("ticker") || !np.has("appName") || !np.has("id")) {
|
|
|
|
Log.e("NotificationsPlugin", "Received notification package lacks properties");
|
|
|
|
} else {
|
|
|
|
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
|
2018-03-03 17:21:16 +01:00
|
|
|
stackBuilder.addParentStack(MainActivity.class);
|
|
|
|
stackBuilder.addNextIntent(new Intent(context, MainActivity.class));
|
2016-06-26 17:08:47 +02:00
|
|
|
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
|
|
|
|
0,
|
|
|
|
PendingIntent.FLAG_UPDATE_CURRENT
|
|
|
|
);
|
|
|
|
|
|
|
|
Bitmap largeIcon = null;
|
|
|
|
if (np.hasPayload()) {
|
|
|
|
int width = 64; // default icon dimensions
|
|
|
|
int height = 64;
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
|
|
|
|
width = context.getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_width);
|
|
|
|
height = context.getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_height);
|
|
|
|
}
|
|
|
|
final InputStream input = np.getPayload();
|
|
|
|
largeIcon = BitmapFactory.decodeStream(np.getPayload());
|
2018-03-03 16:06:52 +01:00
|
|
|
try {
|
|
|
|
input.close();
|
|
|
|
} catch (Exception e) {
|
|
|
|
}
|
2016-06-26 17:08:47 +02:00
|
|
|
if (largeIcon != null) {
|
|
|
|
//Log.i("NotificationsPlugin", "hasPayload: size=" + largeIcon.getWidth() + "/" + largeIcon.getHeight() + " opti=" + width + "/" + height);
|
|
|
|
if (largeIcon.getWidth() > width || largeIcon.getHeight() > height) {
|
|
|
|
// older API levels don't scale notification icons automatically, therefore:
|
|
|
|
largeIcon = Bitmap.createScaledBitmap(largeIcon, width, height, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-24 01:02:25 +01:00
|
|
|
|
|
|
|
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
|
2018-05-15 00:32:51 +02:00
|
|
|
Notification noti = new NotificationCompat.Builder(context, NotificationHelper.Channels.DEFAULT)
|
2016-06-26 17:08:47 +02:00
|
|
|
.setContentTitle(np.getString("appName"))
|
|
|
|
.setContentText(np.getString("ticker"))
|
|
|
|
.setContentIntent(resultPendingIntent)
|
|
|
|
.setTicker(np.getString("ticker"))
|
|
|
|
.setSmallIcon(R.drawable.ic_notification)
|
|
|
|
.setLargeIcon(largeIcon)
|
|
|
|
.setAutoCancel(true)
|
|
|
|
.setLocalOnly(true) // to avoid bouncing the notification back to other kdeconnect nodes
|
|
|
|
.setDefaults(Notification.DEFAULT_ALL)
|
|
|
|
.build();
|
|
|
|
|
2016-12-05 23:42:58 +01:00
|
|
|
NotificationHelper.notifyCompat(notificationManager, "kdeconnectId:" + np.getString("id", "0"), np.getInt("id", 0), noti);
|
|
|
|
|
2016-06-26 17:08:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-03-04 11:31:37 +01:00
|
|
|
public String[] getSupportedPacketTypes() {
|
|
|
|
return new String[]{PACKET_TYPE_NOTIFICATION};
|
2016-06-26 17:08:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-03-04 11:31:37 +01:00
|
|
|
public String[] getOutgoingPacketTypes() {
|
|
|
|
return new String[]{PACKET_TYPE_NOTIFICATION_REQUEST};
|
2016-06-26 17:08:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|