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.PingPlugin;
|
2013-06-18 02:14:22 +02:00
|
|
|
|
2013-09-03 17:58:59 +02:00
|
|
|
import android.app.Activity;
|
2013-07-23 16:11:54 +02:00
|
|
|
import android.app.Notification;
|
|
|
|
import android.app.NotificationManager;
|
2014-01-08 01:20:24 +04:00
|
|
|
import android.app.PendingIntent;
|
2013-06-18 02:14:22 +02:00
|
|
|
import android.content.Context;
|
2014-01-08 01:20:24 +04:00
|
|
|
import android.content.Intent;
|
2013-09-03 17:58:59 +02:00
|
|
|
import android.support.v4.app.NotificationCompat;
|
2014-01-08 01:20:24 +04:00
|
|
|
import android.support.v4.app.TaskStackBuilder;
|
2013-06-18 02:14:22 +02:00
|
|
|
|
2013-09-05 01:37:59 +02:00
|
|
|
import org.kde.kdeconnect.NetworkPackage;
|
2015-09-07 01:49:12 -07:00
|
|
|
import org.kde.kdeconnect.UserInterface.MaterialActivity;
|
2013-09-05 01:37:59 +02:00
|
|
|
import org.kde.kdeconnect.Plugins.Plugin;
|
2013-09-05 01:35:12 +02:00
|
|
|
import org.kde.kdeconnect_tp.R;
|
2013-06-18 02:14:22 +02:00
|
|
|
|
2013-07-23 17:50:09 +02:00
|
|
|
|
2013-08-16 10:31:01 +02:00
|
|
|
public class PingPlugin extends Plugin {
|
2013-06-18 02:14:22 +02:00
|
|
|
|
2016-05-31 17:19:39 +02:00
|
|
|
public final static String PACKAGE_TYPE_PING = "kdeconnect.ping";
|
|
|
|
|
2013-08-19 19:57:29 +02:00
|
|
|
@Override
|
|
|
|
public String getDisplayName() {
|
2013-09-05 01:33:54 +02:00
|
|
|
return context.getResources().getString(R.string.pref_plugin_ping);
|
2013-08-19 19:57:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getDescription() {
|
2013-09-05 01:33:54 +02:00
|
|
|
return context.getResources().getString(R.string.pref_plugin_ping_desc);
|
2013-08-19 19:57:29 +02:00
|
|
|
}
|
|
|
|
|
2013-06-18 02:14:22 +02:00
|
|
|
@Override
|
2013-08-16 10:31:01 +02:00
|
|
|
public boolean onPackageReceived(NetworkPackage np) {
|
|
|
|
|
2016-05-31 17:19:39 +02:00
|
|
|
if (!np.getType().equals(PACKAGE_TYPE_PING)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Log.e("PingPackageReceiver", "was a ping!");
|
|
|
|
|
|
|
|
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
|
|
|
|
stackBuilder.addParentStack(MaterialActivity.class);
|
|
|
|
stackBuilder.addNextIntent(new Intent(context, MaterialActivity.class));
|
|
|
|
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
|
|
|
|
0,
|
|
|
|
PendingIntent.FLAG_UPDATE_CURRENT
|
|
|
|
);
|
|
|
|
|
|
|
|
int id;
|
|
|
|
String message;
|
|
|
|
if (np.has("message")) {
|
|
|
|
message = np.getString("message");
|
|
|
|
id = (int)System.currentTimeMillis();
|
|
|
|
} else {
|
|
|
|
message = "Ping!";
|
|
|
|
id = 42; //A unique id to create only one notification
|
|
|
|
}
|
2013-08-16 10:31:01 +02:00
|
|
|
|
2016-05-31 17:19:39 +02:00
|
|
|
Notification noti = new NotificationCompat.Builder(context)
|
|
|
|
.setContentTitle(device.getName())
|
|
|
|
.setContentText(message)
|
|
|
|
.setContentIntent(resultPendingIntent)
|
|
|
|
.setTicker(message)
|
|
|
|
.setSmallIcon(R.drawable.ic_notification)
|
|
|
|
.setAutoCancel(true)
|
|
|
|
.setDefaults(Notification.DEFAULT_ALL)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
try {
|
|
|
|
notificationManager.notify(id, noti);
|
|
|
|
} catch(Exception e) {
|
|
|
|
//4.1 will throw an exception about not having the VIBRATE permission, ignore it.
|
|
|
|
//https://android.googlesource.com/platform/frameworks/base/+/android-4.2.1_r1.2%5E%5E!/
|
2013-07-02 15:22:05 +02:00
|
|
|
}
|
2016-05-31 17:19:39 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
|
2013-06-18 02:14:22 +02:00
|
|
|
}
|
2013-07-23 17:50:09 +02:00
|
|
|
|
2013-08-19 19:57:29 +02:00
|
|
|
@Override
|
2015-04-12 00:11:30 -07:00
|
|
|
public String getActionName() {
|
|
|
|
return context.getString(R.string.send_ping);
|
2013-07-30 20:18:30 +02:00
|
|
|
}
|
|
|
|
|
2013-09-03 17:58:59 +02:00
|
|
|
@Override
|
2015-04-12 00:11:30 -07:00
|
|
|
public void startMainActivity(Activity activity) {
|
|
|
|
if (device != null) {
|
2016-05-31 17:19:39 +02:00
|
|
|
device.sendPackage(new NetworkPackage(PACKAGE_TYPE_PING));
|
2015-04-12 00:11:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasMainActivity() {
|
|
|
|
return true;
|
2013-09-03 17:58:59 +02:00
|
|
|
}
|
|
|
|
|
2015-04-12 00:11:30 -07:00
|
|
|
@Override
|
|
|
|
public boolean displayInContextMenu() {
|
|
|
|
return true;
|
|
|
|
}
|
2015-09-08 14:54:04 -07:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String[] getSupportedPackageTypes() {
|
2016-05-31 17:19:39 +02:00
|
|
|
return new String[]{PACKAGE_TYPE_PING};
|
2015-09-08 14:54:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String[] getOutgoingPackageTypes() {
|
2016-05-31 17:19:39 +02:00
|
|
|
return new String[]{PACKAGE_TYPE_PING};
|
2015-09-08 14:54:04 -07:00
|
|
|
}
|
|
|
|
|
2013-06-18 02:14:22 +02:00
|
|
|
}
|