/*
 * 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 android.net.Uri;

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.EXTRA_ALARM);
        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.Builder builder = new Notification.Builder(context)
                    .setContentTitle(title)
                    .setContentText(text)
                    .setAutoCancel(true)
                    .setSmallIcon(smallIcon)
                    .setContentIntent(pi);

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

            builder.setLights(0xFFFF0000, 1000, 4000);
            builder.setSound(Uri.parse("content://media/internal/audio/media/33"));
            builder.setVibrate(new long[]{1000, 500, 500, 500});

            notification = builder.build();
            //notification.flags |= Notification.FLAG_SHOW_LIGHTS;
            //notification.defaults |= Notification.DEFAULT_LIGHTS;
            //notification.defaults |= Notification.DEFAULT_SOUND;
            //notification.defaults |= Notification.DEFAULT_VIBRATE;
        } 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);
    }

}

@@ -844,16 +844,66 @@
 net.Uri;
+%0Aimport android.support.v4.app.NotificationCompat;
 %0A%0Aimport
@@ -1650,106 +1650,9 @@
 on;%0A
-        if (android.os.Build.VERSION.SDK_INT %3E= android.os.Build.VERSION_CODES.JELLY_BEAN) %7B%0A%0A    
+%0A
     
@@ -1714,20 +1714,16 @@
 class);%0A
-    
         
@@ -1811,28 +1811,24 @@
 P);%0A        
-   
- 
 receiver.put
@@ -1870,28 +1870,24 @@
 ternalId);%0A%0A
-    
         Pend
@@ -1971,28 +1971,24 @@
 );%0A%0A        
-    
 Notification
@@ -1979,32 +1979,38 @@
     Notification
+Compat
 .Builder builder
@@ -2028,16 +2028,22 @@
 fication
+Compat
 .Builder
@@ -2048,28 +2048,24 @@
 er(context)%0A
-    
             
@@ -2100,36 +2100,32 @@
                 
-    
 .setContentText(
@@ -2142,28 +2142,24 @@
             
-  
-  
 .setAutoCanc
@@ -2167,20 +2167,16 @@
 l(true)%0A
-    
         
@@ -2208,20 +2208,16 @@
 llIcon)%0A
-    
         
@@ -2256,20 +2256,16 @@
         
-    
 if (when
@@ -2289,36 +2289,32 @@
 ) %7B%0A            
-    
 builder.setWhen(
@@ -2324,35 +2324,27 @@
 n);%0A        
-  
-  
 %7D%0A%0A
-    
         buil
@@ -2382,20 +2382,16 @@
  4000);%0A
-    
         
@@ -2462,36 +2462,32 @@
 /33%22));%0A        
-    
 builder.setVibra
@@ -2532,20 +2532,16 @@
         
-    
 notifica
@@ -2564,36 +2564,32 @@
 uild();%0A        
-   
- 
 //notification.f
@@ -2619,36 +2619,32 @@
 AG_SHOW_LIGHTS;%0A
-    
         //notifi
@@ -2691,36 +2691,32 @@
 LIGHTS;%0A        
-    
 //notification.d
@@ -2758,28 +2758,24 @@
 ND;%0A        
- 
-   
 //notificati
@@ -2822,148 +2822,8 @@
 ATE;
-%0A        %7D else %7B%0A            //TODO%0A            throw new IllegalStateException(%22sample app currently doesn't support api %3C 16%22);%0A        %7D
 %0A%0A  

Use NotificationCompat