mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-09-01 06:35:09 +00:00
Fixed notifications plugin incompatibility with Android<21
This commit is contained in:
@@ -20,6 +20,7 @@
|
|||||||
|
|
||||||
package org.kde.kdeconnect.Plugins.NotificationsPlugin;
|
package org.kde.kdeconnect.Plugins.NotificationsPlugin;
|
||||||
|
|
||||||
|
import android.annotation.TargetApi;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
import android.app.Notification;
|
import android.app.Notification;
|
||||||
@@ -38,6 +39,7 @@ import org.kde.kdeconnect.UserInterface.DeviceActivity;
|
|||||||
import org.kde.kdeconnect.UserInterface.SettingsActivity;
|
import org.kde.kdeconnect.UserInterface.SettingsActivity;
|
||||||
import org.kde.kdeconnect_tp.R;
|
import org.kde.kdeconnect_tp.R;
|
||||||
|
|
||||||
|
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
|
||||||
public class NotificationsPlugin extends Plugin implements NotificationReceiver.NotificationListener {
|
public class NotificationsPlugin extends Plugin implements NotificationReceiver.NotificationListener {
|
||||||
/*
|
/*
|
||||||
private boolean sendIcons = false;
|
private boolean sendIcons = false;
|
||||||
@@ -90,10 +92,10 @@ public class NotificationsPlugin extends Plugin implements NotificationReceiver.
|
|||||||
for (StatusBarNotification notification : notifications) {
|
for (StatusBarNotification notification : notifications) {
|
||||||
sendNotification(notification, true);
|
sendNotification(notification, true);
|
||||||
}
|
}
|
||||||
} catch(Exception e) {
|
} catch (Exception e) {
|
||||||
Log.e("NotificationsPlugin","Exception");
|
Log.e("NotificationsPlugin", "Exception");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
@@ -121,7 +123,7 @@ public class NotificationsPlugin extends Plugin implements NotificationReceiver.
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onNotificationRemoved(StatusBarNotification statusBarNotification) {
|
public void onNotificationRemoved(StatusBarNotification statusBarNotification) {
|
||||||
String id = statusBarNotification.getKey();
|
String id = getNotificationKeyCompat(statusBarNotification);
|
||||||
NetworkPackage np = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_NOTIFICATION);
|
NetworkPackage np = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_NOTIFICATION);
|
||||||
np.set("id", id);
|
np.set("id", id);
|
||||||
np.set("isCancel", true);
|
np.set("isCancel", true);
|
||||||
@@ -138,9 +140,9 @@ public class NotificationsPlugin extends Plugin implements NotificationReceiver.
|
|||||||
Notification notification = statusBarNotification.getNotification();
|
Notification notification = statusBarNotification.getNotification();
|
||||||
AppDatabase appDatabase = new AppDatabase(context);
|
AppDatabase appDatabase = new AppDatabase(context);
|
||||||
|
|
||||||
if ((notification.flags & Notification.FLAG_FOREGROUND_SERVICE) != 0
|
if ((notification.flags & Notification.FLAG_FOREGROUND_SERVICE) != 0
|
||||||
|| (notification.flags & Notification.FLAG_ONGOING_EVENT) != 0
|
|| (notification.flags & Notification.FLAG_ONGOING_EVENT) != 0
|
||||||
|| (notification.flags & Notification.FLAG_LOCAL_ONLY) != 0) {
|
|| (notification.flags & Notification.FLAG_LOCAL_ONLY) != 0) {
|
||||||
//This is not a notification we want!
|
//This is not a notification we want!
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -152,11 +154,14 @@ public class NotificationsPlugin extends Plugin implements NotificationReceiver.
|
|||||||
}
|
}
|
||||||
appDatabase.close();
|
appDatabase.close();
|
||||||
|
|
||||||
String id = statusBarNotification.getKey();
|
String key = getNotificationKeyCompat(statusBarNotification);
|
||||||
String packageName = statusBarNotification.getPackageName();
|
String packageName = statusBarNotification.getPackageName();
|
||||||
String appName = AppsHelper.appNameLookup(context, packageName);
|
String appName = AppsHelper.appNameLookup(context, packageName);
|
||||||
|
|
||||||
if ("com.facebook.orca".equals(packageName) && "10012".equals(statusBarNotification.getId()) && appName.equals("Messenger") && notification.tickerText == null) {
|
if ("com.facebook.orca".equals(packageName) &&
|
||||||
|
(statusBarNotification.getId() == 10012) &&
|
||||||
|
"Messenger".equals(appName) &&
|
||||||
|
notification.tickerText == null) {
|
||||||
//HACK: Hide weird Facebook empty "Messenger" notification that is actually not shown in the phone
|
//HACK: Hide weird Facebook empty "Messenger" notification that is actually not shown in the phone
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -192,7 +197,7 @@ public class NotificationsPlugin extends Plugin implements NotificationReceiver.
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
np.set("id", id);
|
np.set("id", key);
|
||||||
np.set("appName", appName == null? packageName : appName);
|
np.set("appName", appName == null? packageName : appName);
|
||||||
np.set("isClearable", statusBarNotification.isClearable());
|
np.set("isClearable", statusBarNotification.isClearable());
|
||||||
np.set("ticker", getTickerText(notification));
|
np.set("ticker", getTickerText(notification));
|
||||||
@@ -296,13 +301,13 @@ public class NotificationsPlugin extends Plugin implements NotificationReceiver.
|
|||||||
@Override
|
@Override
|
||||||
public void onServiceStart(NotificationReceiver service) {
|
public void onServiceStart(NotificationReceiver service) {
|
||||||
String dismissedId = np.getString("cancel");
|
String dismissedId = np.getString("cancel");
|
||||||
service.cancelNotification(dismissedId);
|
cancelNotificationCompat(service, dismissedId);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
Log.w("NotificationsPlugin","Nothing to do");
|
Log.w("NotificationsPlugin", "Nothing to do");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -315,33 +320,67 @@ public class NotificationsPlugin extends Plugin implements NotificationReceiver.
|
|||||||
|
|
||||||
if (Build.VERSION.SDK_INT < 18) {
|
if (Build.VERSION.SDK_INT < 18) {
|
||||||
return new AlertDialog.Builder(deviceActivity)
|
return new AlertDialog.Builder(deviceActivity)
|
||||||
.setTitle(R.string.pref_plugin_notifications)
|
.setTitle(R.string.pref_plugin_notifications)
|
||||||
.setMessage(R.string.plugin_not_available)
|
.setMessage(R.string.plugin_not_available)
|
||||||
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
|
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(DialogInterface dialogInterface, int i) {
|
public void onClick(DialogInterface dialogInterface, int i) {
|
||||||
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.create();
|
.create();
|
||||||
} else {
|
} else {
|
||||||
return new AlertDialog.Builder(deviceActivity)
|
return new AlertDialog.Builder(deviceActivity)
|
||||||
.setTitle(R.string.pref_plugin_notifications)
|
.setTitle(R.string.pref_plugin_notifications)
|
||||||
.setMessage(R.string.no_permissions)
|
.setMessage(R.string.no_permissions)
|
||||||
.setPositiveButton(R.string.open_settings, new DialogInterface.OnClickListener() {
|
.setPositiveButton(R.string.open_settings, new DialogInterface.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(DialogInterface dialogInterface, int i) {
|
public void onClick(DialogInterface dialogInterface, int i) {
|
||||||
Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
|
Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
|
||||||
deviceActivity.startActivityForResult(intent, DeviceActivity.RESULT_NEEDS_RELOAD);
|
deviceActivity.startActivityForResult(intent, DeviceActivity.RESULT_NEEDS_RELOAD);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.setNegativeButton(R.string.cancel,new DialogInterface.OnClickListener() {
|
.setNegativeButton(R.string.cancel,new DialogInterface.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(DialogInterface dialogInterface, int i) {
|
public void onClick(DialogInterface dialogInterface, int i) {
|
||||||
//Do nothing
|
//Do nothing
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.create();
|
.create();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//For compat with API<21, because lollipop changed they way to cancel notifications
|
||||||
|
public static void cancelNotificationCompat(NotificationReceiver service, String compatKey) {
|
||||||
|
if (Build.VERSION.SDK_INT >= 21) {
|
||||||
|
service.cancelNotification(compatKey);
|
||||||
|
} else {
|
||||||
|
int first = compatKey.indexOf(':');
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getNotificationKeyCompat(StatusBarNotification statusBarNotification) {
|
||||||
|
if (Build.VERSION.SDK_INT >= 21) {
|
||||||
|
return statusBarNotification.getKey();
|
||||||
|
} else {
|
||||||
|
String packageName = statusBarNotification.getPackageName();
|
||||||
|
String tag = statusBarNotification.getTag();
|
||||||
|
int id = statusBarNotification.getId();
|
||||||
|
String safePackageName = (packageName == null) ? "" : packageName;
|
||||||
|
String safeTag = (tag == null) ? "" : tag;
|
||||||
|
return safePackageName + ":" + safeTag + ":" + id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user