2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-23 18:37:23 +00:00

146 lines
4.5 KiB
Java
Raw Normal View History

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/>.
*/
package org.kde.kdeconnect.Plugins.PingPlugin;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.view.View;
import android.widget.Button;
import org.kde.kdeconnect.NetworkPackage;
import org.kde.kdeconnect.Plugins.Plugin;
import org.kde.kdeconnect.UserInterface.MainActivity;
import org.kde.kdeconnect_tp.R;
public class PingPlugin extends Plugin {
@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);
}
@Override
public String getDescription() {
2013-09-05 01:33:54 +02:00
return context.getResources().getString(R.string.pref_plugin_ping_desc);
}
@Override
public Drawable getIcon() {
2013-09-05 01:33:54 +02:00
return context.getResources().getDrawable(R.drawable.icon);
}
@Override
public boolean hasSettings() {
return false;
}
@Override
public boolean isEnabledByDefault() {
return true;
}
@Override
public boolean onCreate() {
return true;
}
@Override
public void onDestroy() {
}
@Override
public boolean onPackageReceived(NetworkPackage np) {
//Log.e("PingPackageReceiver", "onPackageReceived");
if (np.getType().equals(NetworkPackage.PACKAGE_TYPE_PING)) {
//Log.e("PingPackageReceiver", "was a ping!");
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
}
Notification noti = new NotificationCompat.Builder(context)
.setContentTitle(device.getName())
2014-09-26 17:21:34 +02:00
.setContentText(message)
.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)
.setAutoCancel(true)
2014-09-17 16:05:16 +02:00
.setDefaults(Notification.DEFAULT_ALL)
.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-07-02 15:22:05 +02:00
}
2013-07-26 16:23:26 +02:00
return false;
}
@Override
public AlertDialog getErrorDialog(Activity deviceActivity) {
return null;
}
@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);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
device.sendPackage(new NetworkPackage(NetworkPackage.PACKAGE_TYPE_PING));
}
});
return b;
}
}