2014-11-16 23:14:06 -08:00
|
|
|
/*
|
2020-08-17 16:17:20 +02:00
|
|
|
* SPDX-FileCopyrightText: 2014 Albert Vaca Cintora <albertvaka@gmail.com>
|
2014-11-16 23:14:06 -08:00
|
|
|
*
|
2020-08-17 16:17:20 +02:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2014-11-16 23:14:06 -08:00
|
|
|
*/
|
|
|
|
|
2013-09-05 01:37:59 +02:00
|
|
|
package org.kde.kdeconnect.Plugins.NotificationsPlugin;
|
2013-08-19 19:57:29 +02:00
|
|
|
|
|
|
|
import android.app.Service;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2018-01-03 20:52:35 +01:00
|
|
|
import android.os.Build;
|
2013-08-19 19:57:29 +02:00
|
|
|
import android.service.notification.NotificationListenerService;
|
|
|
|
import android.service.notification.StatusBarNotification;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2014-11-16 20:13:29 -08:00
|
|
|
import java.util.concurrent.locks.Lock;
|
|
|
|
import java.util.concurrent.locks.ReentrantLock;
|
2013-08-19 19:57:29 +02:00
|
|
|
|
2018-12-27 15:40:57 +01:00
|
|
|
import androidx.annotation.RequiresApi;
|
|
|
|
|
2018-01-03 20:52:35 +01:00
|
|
|
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
|
2013-08-19 19:57:29 +02:00
|
|
|
public class NotificationReceiver extends NotificationListenerService {
|
|
|
|
|
2018-01-06 16:06:49 +01:00
|
|
|
private boolean connected;
|
|
|
|
|
2013-08-19 19:57:29 +02:00
|
|
|
public interface NotificationListener {
|
|
|
|
void onNotificationPosted(StatusBarNotification statusBarNotification);
|
2018-01-03 20:20:14 +01:00
|
|
|
|
2013-08-19 19:57:29 +02:00
|
|
|
void onNotificationRemoved(StatusBarNotification statusBarNotification);
|
2018-01-06 16:06:49 +01:00
|
|
|
|
|
|
|
void onListenerConnected(NotificationReceiver service);
|
2013-08-19 19:57:29 +02:00
|
|
|
}
|
|
|
|
|
2015-08-10 00:26:58 -07:00
|
|
|
private final ArrayList<NotificationListener> listeners = new ArrayList<>();
|
2013-08-19 19:57:29 +02:00
|
|
|
|
|
|
|
public void addListener(NotificationListener listener) {
|
|
|
|
listeners.add(listener);
|
|
|
|
}
|
2018-01-03 20:20:14 +01:00
|
|
|
|
2013-08-19 19:57:29 +02:00
|
|
|
public void removeListener(NotificationListener listener) {
|
|
|
|
listeners.remove(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onNotificationPosted(StatusBarNotification statusBarNotification) {
|
2013-11-06 21:13:37 +01:00
|
|
|
//Log.e("NotificationReceiver.onNotificationPosted","listeners: " + listeners.size());
|
2018-01-03 20:20:14 +01:00
|
|
|
for (NotificationListener listener : listeners) {
|
2013-08-19 19:57:29 +02:00
|
|
|
listener.onNotificationPosted(statusBarNotification);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onNotificationRemoved(StatusBarNotification statusBarNotification) {
|
2018-01-03 20:20:14 +01:00
|
|
|
for (NotificationListener listener : listeners) {
|
2013-08-19 19:57:29 +02:00
|
|
|
listener.onNotificationRemoved(statusBarNotification);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-06 16:06:49 +01:00
|
|
|
@Override
|
|
|
|
public void onListenerConnected() {
|
|
|
|
super.onListenerConnected();
|
|
|
|
for (NotificationListener listener : listeners) {
|
|
|
|
listener.onListenerConnected(this);
|
|
|
|
}
|
|
|
|
connected = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onListenerDisconnected() {
|
|
|
|
super.onListenerDisconnected();
|
|
|
|
connected = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isConnected() {
|
|
|
|
return connected;
|
|
|
|
}
|
2013-08-19 19:57:29 +02:00
|
|
|
|
|
|
|
//To use the service from the outer (name)space
|
|
|
|
|
2014-11-16 20:13:29 -08:00
|
|
|
public interface InstanceCallback {
|
|
|
|
void onServiceStart(NotificationReceiver service);
|
|
|
|
}
|
|
|
|
|
2015-08-10 00:26:58 -07:00
|
|
|
private final static ArrayList<InstanceCallback> callbacks = new ArrayList<>();
|
2014-11-16 20:13:29 -08:00
|
|
|
|
|
|
|
private final static Lock mutex = new ReentrantLock(true);
|
|
|
|
|
2013-08-19 19:57:29 +02:00
|
|
|
//This will be called for each intent launch, even if the service is already started and is reused
|
|
|
|
@Override
|
|
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
2013-11-06 21:13:37 +01:00
|
|
|
//Log.e("NotificationReceiver", "onStartCommand");
|
2014-11-16 20:13:29 -08:00
|
|
|
mutex.lock();
|
2015-10-04 00:23:50 +02:00
|
|
|
try {
|
|
|
|
for (InstanceCallback c : callbacks) {
|
|
|
|
c.onServiceStart(this);
|
|
|
|
}
|
|
|
|
callbacks.clear();
|
|
|
|
} finally {
|
|
|
|
mutex.unlock();
|
2013-08-19 19:57:29 +02:00
|
|
|
}
|
|
|
|
return Service.START_STICKY;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void Start(Context c) {
|
|
|
|
RunCommand(c, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void RunCommand(Context c, final InstanceCallback callback) {
|
2014-11-16 20:13:29 -08:00
|
|
|
if (callback != null) {
|
|
|
|
mutex.lock();
|
2015-10-04 00:23:50 +02:00
|
|
|
try {
|
|
|
|
callbacks.add(callback);
|
|
|
|
} finally {
|
|
|
|
mutex.unlock();
|
|
|
|
}
|
2014-11-16 20:13:29 -08:00
|
|
|
}
|
2013-08-19 19:57:29 +02:00
|
|
|
Intent serviceIntent = new Intent(c, NotificationReceiver.class);
|
|
|
|
c.startService(serviceIntent);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|