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.NotificationsPlugin;
|
2013-08-19 19:57:29 +02:00
|
|
|
|
|
|
|
import android.app.Service;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
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
|
|
|
|
|
|
|
public class NotificationReceiver extends NotificationListenerService {
|
|
|
|
|
|
|
|
public interface NotificationListener {
|
|
|
|
void onNotificationPosted(StatusBarNotification statusBarNotification);
|
|
|
|
void onNotificationRemoved(StatusBarNotification statusBarNotification);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
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());
|
2013-08-19 19:57:29 +02:00
|
|
|
for(NotificationListener listener : listeners) {
|
|
|
|
listener.onNotificationPosted(statusBarNotification);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onNotificationRemoved(StatusBarNotification statusBarNotification) {
|
|
|
|
for(NotificationListener listener : listeners) {
|
|
|
|
listener.onNotificationRemoved(statusBarNotification);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//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();
|
2013-08-19 19:57:29 +02:00
|
|
|
for (InstanceCallback c : callbacks) {
|
|
|
|
c.onServiceStart(this);
|
|
|
|
}
|
|
|
|
callbacks.clear();
|
2014-11-16 20:13:29 -08:00
|
|
|
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();
|
|
|
|
callbacks.add(callback);
|
|
|
|
mutex.unlock();
|
|
|
|
}
|
2013-08-19 19:57:29 +02:00
|
|
|
Intent serviceIntent = new Intent(c, NotificationReceiver.class);
|
|
|
|
c.startService(serviceIntent);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|