package net.nitroshare.android.transfer;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.util.SparseArray;

/**
 * Manage notifications and service lifecycle
 *
 * This class simplifies what would otherwise be a very messy process.
 * Android requires that a service running in the foreground show a
 * persistent notification. This service runs in the foreground when a
 * transfer is in progress and/or when listening for new connections. This
 * means that startForeground() will be invoked multiple times.
 *
 * Herein lies the problem - stopForeground() will pull only the most
 * recent notification. And it isn't reference counted. That's where this
 * (ugly hack) class comes in.
 *
 * All methods in the class are thread-safe.
 */
class TransferNotificationManager {

    private final SparseArray<Notification> mNotifications = new SparseArray<>();
    private Service mService;
    private NotificationManager mNotificationManager;

    /**
     * Create a notification manager for the specified service
     * @param service service to manage
     */
    TransferNotificationManager(Service service) {
        mService = service;
        mNotificationManager = (NotificationManager) mService.getSystemService(
                Service.NOTIFICATION_SERVICE);
    }

    /**
     * Start showing a notification in the foreground
     * @param notificationId unique ID for the notification
     * @param notification notification to be shown
     */
    void start(int notificationId, Notification notification) {
        synchronized (mNotifications) {
            mService.startForeground(notificationId, notification);
            mNotifications.append(notificationId, notification);
        }
    }

    /**
     * Update the specified notification
     * @param notificationId unique ID for the notification
     * @param notification notification to be updated
     */
    void update(int notificationId, Notification notification) {
        mNotificationManager.notify(notificationId, notification);
    }

    /**
     * Stop showing a notification and exit foreground if no others are left
     * @param notificationId unique ID for the notification
     *
     * If no notifications remain, the service is stopped. Otherwise, the next
     * most recent notification is then set for the foreground.
     */
    void stop(int notificationId) {
        synchronized (mNotifications) {
            mNotifications.remove(notificationId);
            if (mNotifications.size() == 0) {
                mService.stopSelf();
            } else {
                mService.startForeground(
                        mNotifications.keyAt(mNotifications.size() - 1),
                        mNotifications.valueAt(mNotifications.size() - 1)
                );
                mNotificationManager.cancel(notificationId);
            }
        }
    }

    /**
     * Stop the service if no notifications are being shown
     */
    void stop() {
        synchronized (mNotifications) {
            if (mNotifications.size() == 0) {
                mService.stopSelf();
            }
        }
    }
}
@@ -155,24 +155,195 @@
 oid.
-util.SparseArray
+content.SharedPreferences;%0Aimport android.preference.PreferenceManager;%0Aimport android.support.v4.app.NotificationCompat;%0Aimport android.util.SparseArray;%0A%0Aimport net.nitroshare.android.R
 ;%0A%0A/
@@ -1118,24 +1118,74 @@
 e mService;%0A
+    private SharedPreferences mSharedPreferences;%0A
     private 
@@ -1421,24 +1421,109 @@
  = service;%0A
+        mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(service);%0A
         mNot
@@ -3447,32 +3447,32 @@
 ;%0A            %7D%0A
-
         %7D%0A    %7D%0A
@@ -3470,10 +3470,1193 @@
  %7D%0A    %7D
+%0A%0A    /**%0A     * Show a notification with the specified information%0A     * @param id unique identifier for the notification%0A     * @param contentText text to display in the notification%0A     * @param icon icon to use for the notification%0A     * @param actions list of actions or null for none%0A     */%0A    void show(int id, CharSequence contentText, int icon, NotificationCompat.Action actions%5B%5D) %7B%0A        boolean notificationSound = mSharedPreferences.getBoolean(%0A                mService.getString(R.string.setting_notification_sound), false%0A        );%0A        NotificationCompat.Builder builder = new NotificationCompat.Builder(mService)%0A                .setDefaults(notificationSound ? NotificationCompat.DEFAULT_ALL : 0)%0A                .setContentTitle(mService.getString(R.string.service_transfer_title))%0A                .setContentText(contentText)%0A                .setStyle(new NotificationCompat.BigTextStyle().bigText(contentText))%0A                .setSmallIcon(icon);%0A        if (actions != null) %7B%0A            for (NotificationCompat.Action action : actions) %7B%0A                builder.addAction(action);%0A            %7D%0A        %7D%0A        update(id, builder.build());%0A    %7D
 %0A%7D

Add show() method to TransferNotificationManager.