/*
 * Copyright 2013 Urban Airship and Contributors
 */

package com.urbanairship.richpush.sample;

import android.app.Notification;
import android.app.Notification.InboxStyle;
import android.app.NotificationManager;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.RingtoneManager;
import android.text.Html;

import com.urbanairship.UAirship;
import com.urbanairship.push.BasicPushNotificationBuilder;
import com.urbanairship.richpush.RichPushInbox;
import com.urbanairship.richpush.RichPushManager;
import com.urbanairship.richpush.RichPushMessage;

import java.util.List;
import java.util.Map;

/**
 * A custom push notification builder to create inbox style notifications
 * for rich push messages.  In the case of standard push notifications, it will
 * fall back to the default behavior.
 * 
 */
public class RichNotificationBuilder extends BasicPushNotificationBuilder {

    private static final int EXTRA_MESSAGES_TO_SHOW = 2;
    private static final int INBOX_NOTIFICATION_ID = 9000000;

    @Override
    public Notification buildNotification(String alert, Map<String, String> extras) {
        if (extras != null && RichPushManager.isRichPushMessage(extras)) {
            return createInboxNotification(alert);
        } else {
            return super.buildNotification(alert, extras);
        }
    }

    @Override
    public int getNextId(String alert, Map<String, String> extras) {
        if (extras != null && extras.containsKey(PushReceiver.EXTRA_MESSAGE_ID_KEY)) {
            return INBOX_NOTIFICATION_ID;
        } else {
            return super.getNextId(alert, extras);
        }
    }

    /**
     * Creates an inbox style notification summarizing the unread messages
     * in the inbox
     * 
     * @param incomingAlert The alert message from an Urban Airship push
     * @return An inbox style notification
     */
    private Notification createInboxNotification(String incomingAlert) {
        Context context = UAirship.shared().getApplicationContext();

        List<RichPushMessage> unreadMessages = RichPushInbox.shared().getUnreadMessages();
        int inboxUnreadCount = unreadMessages.size();

        // The incoming message is not immediately made available to the inbox because it needs
        // to first fetch its contents.
        int totalUnreadCount = inboxUnreadCount + 1;

        Resources res = UAirship.shared().getApplicationContext().getResources();
        String title = res.getQuantityString(R.plurals.inbox_notification_title, totalUnreadCount, totalUnreadCount);

        Bitmap largeIcon = BitmapFactory.decodeResource(res, R.drawable.ua_launcher);

        InboxStyle style = new Notification.InboxStyle(
                new Notification.Builder(context)
                .setContentTitle(title)
                .setContentText(incomingAlert)
                .setLargeIcon(largeIcon)
                .setSmallIcon(R.drawable.ua_notification_icon)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setNumber(totalUnreadCount));

        // Add the incoming alert as the first line in bold
        style.addLine(Html.fromHtml("<b>"+incomingAlert+"</b>"));

        // Add any extra messages to the notification style
        int extraMessages =  Math.min(EXTRA_MESSAGES_TO_SHOW, inboxUnreadCount);
        for (int i = 0; i < extraMessages; i++) {
            style.addLine(unreadMessages.get(i).getTitle());
        }

        // If we have more messages to show then the EXTRA_MESSAGES_TO_SHOW, add a summary
        if (inboxUnreadCount > EXTRA_MESSAGES_TO_SHOW) {
            style.setSummaryText(context.getString(R.string.inbox_summary, inboxUnreadCount - EXTRA_MESSAGES_TO_SHOW));
        }

        return style.build();
    }

    /**
     * Dismisses the inbox style notification if it exists
     */
    public static void dismissInboxNotification() {
        NotificationManager manager = (NotificationManager) UAirship.shared().
                getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);

        manager.cancel(INBOX_NOTIFICATION_ID);
    }
}

@@ -355,46 +355,8 @@
 ry;%0A
-import android.media.RingtoneManager;%0A
 impo
@@ -467,24 +467,112 @@
 ionBuilder;%0A
+import com.urbanairship.push.PushManager;%0Aimport com.urbanairship.push.PushPreferences;%0A
 import com.u
@@ -2879,16 +2879,72 @@
 ontext)%0A
+                .setDefaults(getNotificationDefaults())%0A
         
@@ -3130,100 +3130,8 @@
 on)%0A
-                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))%0A
     
@@ -4227,11 +4227,684 @@
 );%0A    %7D
+%0A%0A    /**%0A     * Gets the notification defaults based on%0A     * the PushPreferences for quiet time, vibration enabled,%0A     * and sound enabled.%0A     *%0A     * @return Notification defaults%0A     */%0A    private int getNotificationDefaults() %7B%0A        PushPreferences prefs = PushManager.shared().getPreferences();%0A        int defaults = Notification.DEFAULT_LIGHTS;%0A%0A        if (!prefs.isInQuietTime()) %7B%0A            if (prefs.isVibrateEnabled()) %7B%0A                defaults %7C= Notification.DEFAULT_VIBRATE;%0A            %7D%0A%0A            if (prefs.isSoundEnabled()) %7B%0A                defaults %7C= Notification.DEFAULT_SOUND;%0A            %7D%0A        %7D%0A%0A        return defaults;%0A    %7D
 %0A%7D%0A

Make inbox notification respect quiet time, sound enable, and vibrate enable preference