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-08-19 19:57:29 +02:00
|
|
|
import android.app.AlertDialog;
|
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-08-19 19:57:29 +02:00
|
|
|
import android.graphics.drawable.Drawable;
|
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-09-03 17:58:59 +02:00
|
|
|
import android.view.View;
|
|
|
|
import android.widget.Button;
|
2013-06-18 02:14:22 +02:00
|
|
|
|
2013-09-05 01:37:59 +02:00
|
|
|
import org.kde.kdeconnect.NetworkPackage;
|
|
|
|
import org.kde.kdeconnect.Plugins.Plugin;
|
2014-01-08 01:20:24 +04:00
|
|
|
import org.kde.kdeconnect.UserInterface.MainActivity;
|
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
|
|
|
|
2013-08-19 19:57:29 +02:00
|
|
|
@Override
|
|
|
|
public String getPluginName() {
|
|
|
|
return "plugin_ping";
|
|
|
|
}
|
|
|
|
|
|
|
|
@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
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Drawable getIcon() {
|
2013-09-05 01:33:54 +02:00
|
|
|
return context.getResources().getDrawable(R.drawable.icon);
|
2013-08-19 19:57:29 +02:00
|
|
|
}
|
|
|
|
|
2014-09-16 15:45:31 +02:00
|
|
|
@Override
|
|
|
|
public boolean hasSettings() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-19 19:57:29 +02:00
|
|
|
@Override
|
|
|
|
public boolean isEnabledByDefault() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-31 18:02:22 +02:00
|
|
|
@Override
|
2013-08-16 10:31:01 +02:00
|
|
|
public boolean onCreate() {
|
2013-07-31 18:02:22 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
|
2013-06-18 02:14:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-08-16 10:31:01 +02:00
|
|
|
public boolean onPackageReceived(NetworkPackage np) {
|
|
|
|
|
2013-07-29 18:42:12 +02:00
|
|
|
//Log.e("PingPackageReceiver", "onPackageReceived");
|
2013-07-23 16:11:54 +02:00
|
|
|
if (np.getType().equals(NetworkPackage.PACKAGE_TYPE_PING)) {
|
2013-07-29 18:42:12 +02:00
|
|
|
//Log.e("PingPackageReceiver", "was a ping!");
|
2013-07-23 16:11:54 +02:00
|
|
|
|
2014-01-08 01:20:24 +04:00
|
|
|
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
|
|
|
|
stackBuilder.addParentStack(MainActivity.class);
|
|
|
|
stackBuilder.addNextIntent(new Intent(context, MainActivity.class));
|
|
|
|
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
|
|
|
|
0,
|
|
|
|
PendingIntent.FLAG_UPDATE_CURRENT
|
|
|
|
);
|
|
|
|
|
2014-09-26 17:21:34 +02:00
|
|
|
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-09-03 17:58:59 +02:00
|
|
|
Notification noti = new NotificationCompat.Builder(context)
|
2013-08-16 10:31:01 +02:00
|
|
|
.setContentTitle(device.getName())
|
2014-09-26 17:21:34 +02:00
|
|
|
.setContentText(message)
|
2014-01-08 01:20:24 +04:00
|
|
|
.setContentIntent(resultPendingIntent)
|
2014-09-26 17:21:34 +02:00
|
|
|
.setTicker(message)
|
2013-09-05 01:33:54 +02:00
|
|
|
.setSmallIcon(android.R.drawable.ic_dialog_alert)
|
2013-07-23 16:11:54 +02:00
|
|
|
.setAutoCancel(true)
|
2014-09-17 16:05:16 +02:00
|
|
|
.setDefaults(Notification.DEFAULT_ALL)
|
2013-07-23 16:11:54 +02:00
|
|
|
.build();
|
|
|
|
|
|
|
|
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
2014-09-26 17:21:34 +02:00
|
|
|
notificationManager.notify(id, noti);
|
2013-07-26 16:23:26 +02:00
|
|
|
return true;
|
2013-08-16 10:31:01 +02:00
|
|
|
|
2013-07-02 15:22:05 +02:00
|
|
|
}
|
2013-07-26 16:23:26 +02:00
|
|
|
return false;
|
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
|
2014-09-20 10:52:03 +02:00
|
|
|
public AlertDialog getErrorDialog(Activity deviceActivity) {
|
2013-08-19 19:57:29 +02:00
|
|
|
return null;
|
2013-07-30 20:18:30 +02:00
|
|
|
}
|
|
|
|
|
2013-09-03 17:58:59 +02:00
|
|
|
@Override
|
|
|
|
public Button getInterfaceButton(Activity activity) {
|
|
|
|
Button b = new Button(activity);
|
2013-09-05 01:33:54 +02:00
|
|
|
b.setText(R.string.send_ping);
|
2013-09-03 17:58:59 +02:00
|
|
|
b.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
|
|
|
device.sendPackage(new NetworkPackage(NetworkPackage.PACKAGE_TYPE_PING));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2013-06-18 02:14:22 +02:00
|
|
|
}
|