/*
 * Copyright (C) 2016 Oleg Kan, @Simplaapliko
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.simplaapliko.wakeup.sample.util;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import com.simplaapliko.wakeup.Alarm;
import com.simplaapliko.wakeup.AlarmController;
import com.simplaapliko.wakeup.AlarmHandleListener;
import com.simplaapliko.wakeup.sample.R;
import com.simplaapliko.wakeup.sample.ui.MainActivity;

public class SendNotification extends AlarmHandleListener {

    @Override
    public void onHandle(Context context, Intent intent) {

        Alarm alarm = intent.getParcelableExtra(AlarmController.ALARM_EXTRA);
        int smallIcon = R.mipmap.ic_launcher;

        int notificationId = (int) alarm.getId();
        long externalId = alarm.getExternalId();
        String title = alarm.getTitle();
        String text = alarm.getMessage();
        long when = alarm.getTime();

        Notification notification;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {

            Intent receiver = new Intent(context, MainActivity.class);
            receiver.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            receiver.putExtra(MainActivity.ALARM_ID_KEY, externalId);

            PendingIntent pi = PendingIntent.getActivity(context, notificationId, receiver, 0);

            notification = new Notification.Builder(context)
                    .setContentTitle(title)
                    .setContentText(text)
                    .setAutoCancel(true)
                    .setSmallIcon(smallIcon)
                    .setContentIntent(pi)
                    .build();

            if (when != Alarm.NOT_SET) {
                notification.when = when;
            }

            notification.flags |= Notification.FLAG_SHOW_LIGHTS;
            //notification.defaults |= Notification.DEFAULT_LIGHTS;
            notification.defaults |= Notification.DEFAULT_SOUND;
            notification.defaults |= Notification.DEFAULT_VIBRATE;
            notification.ledARGB = 0xFFFF0000;
            notification.ledOnMS = 1000;
            notification.ledOffMS = 4000;
        } else {
            //TODO
            throw new IllegalStateException("sample app currently doesn't support api < 16");
        }

        NotificationManager nm = getNotificationManager(context);
        nm.notify(notificationId, notification);
    }

    private NotificationManager getNotificationManager(Context context) {
        return (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);
    }

}

@@ -820,16 +820,40 @@
 .Intent;
+%0Aimport android.net.Uri;
 %0A%0Aimport
@@ -2030,33 +2030,33 @@
 );%0A%0A            
-n
+N
 otification = ne
@@ -2050,16 +2050,32 @@
 fication
+.Builder builder
  = new N
@@ -2316,16 +2316,18 @@
 tent(pi)
+;%0A
 %0A       
@@ -2335,27 +2335,77 @@
     
-        .build();%0A%0A
+if (when != Alarm.NOT_SET) %7B%0A                builder.setWhen(when);%0A 
     
@@ -2415,94 +2415,259 @@
     
- if (when != Alarm.NOT_SET) %7B%0A                notification.when = when;%0A            %7D%0A
+%7D%0A%0A            builder.setLights(0xFFFF0000, 1000, 4000);%0A            builder.setSound(Uri.parse(%22content://media/internal/audio/media/33%22));%0A            builder.setVibrate(new long%5B%5D%7B1000, 500, 500, 500%7D);%0A%0A            notification = builder.build();
 %0A   
@@ -2667,32 +2667,34 @@
 ();%0A            
+//
 notification.fla
@@ -2802,32 +2802,34 @@
 TS;%0A            
+//
 notification.def
@@ -2869,32 +2869,34 @@
 ND;%0A            
+//
 notification.def
@@ -2938,138 +2938,8 @@
 TE;%0A
-            notification.ledARGB = 0xFFFF0000;%0A            notification.ledOnMS = 1000;%0A            notification.ledOffMS = 4000;%0A
     

Use builder to create notification