@@ -0,0 +1,3112 @@
+package net.nitroshare.android.transfer;%0A%0Aimport android.app.Notification;%0Aimport android.app.NotificationManager;%0Aimport android.app.Service;%0Aimport android.util.SparseArray;%0A%0A/**%0A * Manage notifications and service lifecycle%0A *%0A * This class simplifies what would otherwise be a very messy process.%0A * Android requires that a service running in the foreground show a%0A * persistent notification. This service runs in the foreground when a%0A * transfer is in progress and/or when listening for new connections. This%0A * means that startForeground() will be invoked multiple times.%0A *%0A * Herein lies the problem - stopForeground() will pull only the most%0A * recent notification. And it isn't reference counted. That's where this%0A * (ugly hack) class comes in.%0A *%0A * All methods in the class are thread-safe.%0A */%0Aclass TransferNotificationManager %7B%0A%0A    private final SparseArray%3CNotification%3E mNotifications = new SparseArray%3C%3E();%0A    private Service mService;%0A    private NotificationManager mNotificationManager;%0A%0A    /**%0A     * Create a notification manager for the specified service%0A     * @param service service to manage%0A     */%0A    TransferNotificationManager(Service service) %7B%0A        mService = service;%0A        mNotificationManager = (NotificationManager) mService.getSystemService(%0A                Service.NOTIFICATION_SERVICE);%0A    %7D%0A%0A    /**%0A     * Start showing a notification in the foreground%0A     * @param notificationId unique ID for the notification%0A     * @param notification notification to be shown%0A     */%0A    void begin(int notificationId, Notification notification) %7B%0A        synchronized (mNotifications) %7B%0A            mService.startForeground(notificationId, notification);%0A            mNotifications.append(notificationId, notification);%0A        %7D%0A    %7D%0A%0A    /**%0A     * Update the specified notification%0A     * @param notificationId unique ID for the notification%0A     * @param notification notification to be updated%0A     */%0A    void update(int notificationId, Notification notification) %7B%0A        mNotificationManager.notify(notificationId, notification);%0A    %7D%0A%0A    /**%0A     * Stop showing a notification and exit foreground if no others are left%0A     * @param notificationId unique ID for the notification%0A     *%0A     * If no notifications remain, the service is stopped. Otherwise, the next%0A     * most recent notification is then set for the foreground.%0A     */%0A    void stop(int notificationId) %7B%0A        synchronized (mNotifications) %7B%0A            mNotifications.remove(notificationId);%0A            if (mNotifications.size() == 0) %7B%0A                mService.stopSelf();%0A            %7D else %7B%0A                mService.startForeground(%0A                        mNotifications.keyAt(mNotifications.size() - 1),%0A                        mNotifications.valueAt(mNotifications.size() - 1)%0A                );%0A            %7D%0A        %7D%0A    %7D%0A%0A    /**%0A     * Stop the service if no notifications are being shown%0A     */%0A    void stop() %7B%0A        synchronized (mNotifications) %7B%0A            if (mNotifications.size() == 0) %7B%0A                mService.stopSelf();%0A            %7D%0A        %7D%0A    %7D%0A%7D

Add TransferNotificationManager class.