/*
 * Copyright (c) OS2 2016.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
 * If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/
 */

package it_minds.dk.eindberetningmobil_android.service;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.NotificationCompat;

import it_minds.dk.eindberetningmobil_android.R;
import it_minds.dk.eindberetningmobil_android.views.MonitoringActivity;

/**
 * simple notification helper
 */
public class NotificationHelper {

    public static final int ID = 10;

    /**
     * Creates a notification.
     * @param context
     * @param title
     * @param content
     * @return
     */
    public static Notification createNotification(Context context, String title, String content) {
        Intent intent = new Intent(context, MonitoringActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, ID, intent, 0);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setContentTitle(title);
        builder.setContentText(content);
        builder.setNumber(0);
        builder.setContentIntent(pendingIntent);

        Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
                R.mipmap.ic_launcher);
        builder.setLargeIcon(icon);
        if((android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP)){
            //use siluhett icon
            builder.setSmallIcon(R.drawable.ic_sil);
        }else{
            builder.setSmallIcon(R.mipmap.ic_launcher);
        }
        builder.setAutoCancel(true);
        builder.setOngoing(true);
        Notification notification = builder.build();
        return notification;
    }


}

@@ -319,16 +319,96 @@
 cation;%0A
+import android.app.NotificationChannel;%0Aimport android.app.NotificationManager;%0A
 import a
@@ -567,16 +567,41 @@
 actory;%0A
+import android.os.Build;%0A
 import a
@@ -620,9 +620,9 @@
 rt.v
-7
+4
 .app
@@ -838,16 +838,115 @@
 lper %7B%0A%0A
+    private static final String NOTIFICATION_CHANNEL_DEFAULT_ID = %22notification_channel_default%22;%0A%0A
     publ
@@ -1362,16 +1362,17 @@
 E_TOP);%0A
+%0A
         
@@ -1452,24 +1452,25 @@
 nt, 0);%0A
+%0A
         
 Notifica
@@ -1465,94 +1465,223 @@
     
-NotificationCompat.Builder builder = new NotificationCompat.Builder(context
+if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.O) %7B%0A            int importance = NotificationManager.IMPORTANCE_DEFAULT;%0A            String notificationChannelName = context.getString(R.string.app_name
 );%0A
+%0A
         
 buil
@@ -1680,159 +1680,433 @@
     
-builder.setContentTitle(title);%0A        builder.setContentText(content);%0A        builder.setNumber(0);%0A        builder.setContentIntent(pendingIntent);
+    NotificationChannel notificationChannel = new NotificationChannel(%0A                    NOTIFICATION_CHANNEL_DEFAULT_ID,%0A                    notificationChannelName,%0A                    importance);%0A%0A            NotificationManager notificationManager =  (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);%0A            notificationManager.createNotificationChannel(notificationChannel);%0A        %7D
 %0A%0A  
@@ -2177,32 +2177,16 @@
 urces(),
-%0A               
  R.mipma
@@ -2213,59 +2213,36 @@
     
-builder.setLargeIcon(icon);%0A        if((android.os.
+int smallIcon;%0A%0A        if (
 Buil
@@ -2262,27 +2262,16 @@
 _INT %3E= 
-android.os.
 Build.VE
@@ -2291,17 +2291,17 @@
 OLLIPOP)
-)
+ 
 %7B%0A      
@@ -2310,60 +2310,17 @@
     
-//use siluhett icon%0A            builder.setS
+s
 mallIcon
 (R.d
@@ -2315,17 +2315,19 @@
 mallIcon
-(
+ = 
 R.drawab
@@ -2335,17 +2335,16 @@
 e.ic_sil
-)
 ;%0A      
@@ -2350,12 +2350,14 @@
    %7D
+ 
 else
+ 
 %7B%0A  
@@ -2370,28 +2370,17 @@
     
-builder.setS
+s
 mallIcon
 (R.m
@@ -2375,17 +2375,19 @@
 mallIcon
-(
+ = 
 R.mipmap
@@ -2390,33 +2390,32 @@
 pmap.ic_launcher
-)
 ;%0A        %7D%0A    
@@ -2410,24 +2410,25 @@
       %7D%0A
+%0A
         
 builder.
@@ -2423,151 +2423,420 @@
     
-builder.setAutoCancel(true);%0A        builder.setOngoing(true);%0A        Notification notification = builder.build();%0A        return notification
+return new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_DEFAULT_ID)%0A                .setContentTitle(title)%0A                .setContentText(content)%0A                .setNumber(0)%0A                .setContentIntent(pendingIntent)%0A                .setLargeIcon(icon)%0A                .setSmallIcon(smallIcon)%0A                .setAutoCancel(true)%0A                .setOngoing(true)%0A                .build()
 ;%0A  

Use notification channels on Android 8 and above