File indexing completed on 2024-12-22 04:41:40

0001 /*
0002  * SPDX-FileCopyrightText: 2014 Albert Vaca Cintora <albertvaka@gmail.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 package org.kde.kdeconnect.Plugins.NotificationsPlugin;
0008 
0009 import android.app.Service;
0010 import android.content.Context;
0011 import android.content.Intent;
0012 import android.service.notification.NotificationListenerService;
0013 import android.service.notification.StatusBarNotification;
0014 
0015 import java.util.ArrayList;
0016 import java.util.concurrent.locks.Lock;
0017 import java.util.concurrent.locks.ReentrantLock;
0018 
0019 public class NotificationReceiver extends NotificationListenerService {
0020 
0021     private boolean connected;
0022 
0023     public interface NotificationListener {
0024         void onNotificationPosted(StatusBarNotification statusBarNotification);
0025 
0026         void onNotificationRemoved(StatusBarNotification statusBarNotification);
0027 
0028         void onListenerConnected(NotificationReceiver service);
0029     }
0030 
0031     private final ArrayList<NotificationListener> listeners = new ArrayList<>();
0032 
0033     public void addListener(NotificationListener listener) {
0034         listeners.add(listener);
0035     }
0036 
0037     public void removeListener(NotificationListener listener) {
0038         listeners.remove(listener);
0039     }
0040 
0041     @Override
0042     public void onNotificationPosted(StatusBarNotification statusBarNotification) {
0043         //Log.e("NotificationReceiver.onNotificationPosted","listeners: " + listeners.size());
0044         for (NotificationListener listener : listeners) {
0045             listener.onNotificationPosted(statusBarNotification);
0046         }
0047     }
0048 
0049     @Override
0050     public void onNotificationRemoved(StatusBarNotification statusBarNotification) {
0051         for (NotificationListener listener : listeners) {
0052             listener.onNotificationRemoved(statusBarNotification);
0053         }
0054     }
0055 
0056     @Override
0057     public void onListenerConnected() {
0058         super.onListenerConnected();
0059         for (NotificationListener listener : listeners) {
0060             listener.onListenerConnected(this);
0061         }
0062         connected = true;
0063     }
0064 
0065     @Override
0066     public void onListenerDisconnected() {
0067         super.onListenerDisconnected();
0068         connected = false;
0069     }
0070 
0071     public boolean isConnected() {
0072         return connected;
0073     }
0074 
0075     //To use the service from the outer (name)space
0076 
0077     public interface InstanceCallback {
0078         void onServiceStart(NotificationReceiver service);
0079     }
0080 
0081     private final static ArrayList<InstanceCallback> callbacks = new ArrayList<>();
0082 
0083     private final static Lock mutex = new ReentrantLock(true);
0084 
0085     //This will be called for each intent launch, even if the service is already started and is reused
0086     @Override
0087     public int onStartCommand(Intent intent, int flags, int startId) {
0088         //Log.e("NotificationReceiver", "onStartCommand");
0089         mutex.lock();
0090         try {
0091             for (InstanceCallback c : callbacks) {
0092                 c.onServiceStart(this);
0093             }
0094             callbacks.clear();
0095         } finally {
0096             mutex.unlock();
0097         }
0098         return Service.START_STICKY;
0099     }
0100 
0101 
0102     public static void Start(Context c) {
0103         RunCommand(c, null);
0104     }
0105 
0106     public static void RunCommand(Context c, final InstanceCallback callback) {
0107         if (callback != null) {
0108             mutex.lock();
0109             try {
0110                 callbacks.add(callback);
0111             } finally {
0112                 mutex.unlock();
0113             }
0114         }
0115         Intent serviceIntent = new Intent(c, NotificationReceiver.class);
0116         c.startService(serviceIntent);
0117     }
0118 
0119 }