From f88777b8c9fd403c1208eafae5ac25d0ef5f921e Mon Sep 17 00:00:00 2001
From: Florian <florian.taurer@gmx.at>
Date: Mon, 27 Aug 2018 13:45:11 +0200
Subject: [PATCH] updated notificationhelper for android 8 and 8.1, updated
 targetsdkversion

---
 SoniControl/app/build.gradle                  |  10 +-
 .../fhstp/sonicontrol/NotificationHelper.java | 244 +++++++++++++++---
 .../fhstp/sonicontrol/SettingsActivity.java   |   2 +-
 .../java/at/ac/fhstp/sonicontrol/Spoofer.java |   2 +-
 4 files changed, 212 insertions(+), 46 deletions(-)

diff --git a/SoniControl/app/build.gradle b/SoniControl/app/build.gradle
index 82cbd0d9..afd48d89 100644
--- a/SoniControl/app/build.gradle
+++ b/SoniControl/app/build.gradle
@@ -5,12 +5,12 @@ properties.load(project.rootProject.file('local.properties').newDataInputStream(
 def superpowered_sdk_path = properties.getProperty('superpowered.dir')
 
 android {
-    compileSdkVersion 25
+    compileSdkVersion 27
     buildToolsVersion '26.0.2'
     defaultConfig {
         applicationId "at.ac.fhstp.sonicontrol"
         minSdkVersion 16 // more than 95% of all active Android devices (mid 2017)
-        targetSdkVersion 25
+        targetSdkVersion 27
         versionCode 13
         versionName "1.3.1"
         testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
@@ -54,11 +54,11 @@ dependencies {
     androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
         exclude group: 'com.android.support', module: 'support-annotations'
     })
-    compile 'com.android.support:appcompat-v7:25.3.1'
+    compile 'com.android.support:appcompat-v7:26.1.0'
     compile 'com.android.support.constraint:constraint-layout:1.0.2'
     testCompile 'junit:junit:4.12'
     compile 'pl.edu.icm:JLargeArrays:1.6' // Better to have it here than in jar folder (see gitignore)
     compile 'com.jjoe64:graphview:4.2.1'
-    compile 'com.android.support:appcompat-v7:25.0.3'
-    compile 'com.android.support:design:25.4.0'
+    compile 'com.android.support:appcompat-v7:26.1.0'
+    compile 'com.android.support:design:26.1.0'
 }
diff --git a/SoniControl/app/src/main/java/at/ac/fhstp/sonicontrol/NotificationHelper.java b/SoniControl/app/src/main/java/at/ac/fhstp/sonicontrol/NotificationHelper.java
index 8c1c6396..bca74e1b 100644
--- a/SoniControl/app/src/main/java/at/ac/fhstp/sonicontrol/NotificationHelper.java
+++ b/SoniControl/app/src/main/java/at/ac/fhstp/sonicontrol/NotificationHelper.java
@@ -20,11 +20,14 @@
 package at.ac.fhstp.sonicontrol;
 
 import android.app.Notification;
+import android.app.NotificationChannel;
+import android.app.NotificationManager;
 import android.app.PendingIntent;
 import android.content.Context;
 import android.content.Intent;
 import android.media.RingtoneManager;
 import android.net.Uri;
+import android.os.Build;
 import android.support.v4.app.NotificationCompat;
 import android.support.v4.app.NotificationManagerCompat;
 
@@ -39,7 +42,11 @@ public class NotificationHelper {
     public static final int NOTIFICATION_STATUS_ID = 2;
     private static final int NOTIFICATION_DETECTION_ID = 1;
 
+    public static final String NOTIFICATION_STATUS_CHANNEL_ID = "2";
+    private static final String NOTIFICATION_DETECTION_CHANNEL_ID = "1";
+
     public static NotificationManagerCompat mNotificationManager;
+    public static NotificationManager mNotificationManagerOreoAbove;
 
     private static NotificationCompat.Builder spoofingStatusBuilder;
     private static NotificationCompat.Builder detectionAlertStatusBuilder;
@@ -55,6 +62,11 @@ public class NotificationHelper {
     private static boolean onHoldStatusNotitificationFirstBuild = true;
     private static boolean scanningStatusNotitificationFirstBuild = true;
 
+    private static CharSequence name = "SoniChannel";// The user-visible name of the channel.
+    public static int importance;
+    public static NotificationChannel mChannel;
+
+
     private static PendingIntent getPendingIntentDetectionFlagUpdateCurrent(Context context, Technology technology) {
         Intent resultIntent = new Intent(context, MainActivity.class); //the intent is still the main-activity
         resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
@@ -116,13 +128,39 @@ public class NotificationHelper {
     }
 
     private static void initSpoofingStatusNotification(Context context){
-        spoofingStatusBuilder = //create a builder for the detection notification
-                new NotificationCompat.Builder(context)
-                        .setSmallIcon(R.drawable.hearing_block) //adding the icon
-                        .setContentTitle(context.getString(R.string.StatusNotificationSpoofingTitle)) //adding the title
-                        .setContentText(context.getString(R.string.StatusNotificationSpoofingMesssage)) //adding the text
-                        //Requires API 21 .setCategory(Notification.CATEGORY_SERVICE)
-                        .setOngoing(true); //it's canceled when tapped on it
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+            mNotificationManagerOreoAbove = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
+        }else {
+            mNotificationManager = NotificationManagerCompat.from(context);
+        }
+
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+            importance = mNotificationManagerOreoAbove.IMPORTANCE_HIGH;
+            mChannel = new NotificationChannel(NOTIFICATION_STATUS_CHANNEL_ID, name, importance);
+        }
+
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+            spoofingStatusBuilder = //create a builder for the detection notification
+                    new NotificationCompat.Builder(context, NOTIFICATION_STATUS_CHANNEL_ID)
+                            .setSmallIcon(R.drawable.hearing_block) //adding the icon
+                            .setContentTitle(context.getString(R.string.StatusNotificationSpoofingTitle)) //adding the title
+                            .setContentText(context.getString(R.string.StatusNotificationSpoofingMesssage)) //adding the text
+                            //Requires API 21 .setCategory(Notification.CATEGORY_SERVICE)
+                            .setOngoing(true); //it's canceled when tapped on it
+        }else {
+            spoofingStatusBuilder = //create a builder for the detection notification
+                    new NotificationCompat.Builder(context)
+                            .setSmallIcon(R.drawable.hearing_block) //adding the icon
+                            .setContentTitle(context.getString(R.string.StatusNotificationSpoofingTitle)) //adding the title
+                            .setContentText(context.getString(R.string.StatusNotificationSpoofingMesssage)) //adding the text
+                            //Requires API 21 .setCategory(Notification.CATEGORY_SERVICE)
+                            .setOngoing(true); //it's canceled when tapped on it
+        }
+
+
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+            spoofingStatusBuilder.setChannelId(NOTIFICATION_STATUS_CHANNEL_ID);
+        }
 
         PendingIntent resultPendingIntent = getPendingIntentStatusFlagUpdateCurrent(context);
 
@@ -135,27 +173,69 @@ public class NotificationHelper {
         if(spoofingStatusNotitificationFirstBuild){ //if it's the first time that it's built
             initSpoofingStatusNotification(context); //initialize the notification
         }
-        mNotificationManager.notify(NOTIFICATION_STATUS_ID, notificationSpoofingStatus); //activate the notification with the notification itself and its id
+
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+            mNotificationManagerOreoAbove.createNotificationChannel(mChannel);
+            mNotificationManagerOreoAbove.notify(NOTIFICATION_STATUS_ID, notificationSpoofingStatus);
+        }else {
+            mNotificationManager.notify(NOTIFICATION_STATUS_ID, notificationSpoofingStatus); //activate the notification with the notification itself and its id
+        }
+
         spoofingStatusNotitificationFirstBuild = false; //notification is created
     }
 
     public static void cancelSpoofingStatusNotification(){
-        mNotificationManager.cancel(NOTIFICATION_STATUS_ID); //Cancel the notification with the help of the id
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+            mNotificationManagerOreoAbove.cancel(NOTIFICATION_STATUS_ID);
+        }else{
+            mNotificationManager.cancel(NOTIFICATION_STATUS_ID); //Cancel the notification with the help of the id
+        }
+
     }
 
     private static void initDetectionAlertStatusNotification(Context context, Technology technology){
         Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
 
-        detectionAlertStatusBuilder = //create a builder for the detection notification
-                new NotificationCompat.Builder(context)
-                        .setSmallIcon(R.drawable.hearing_found)
-                        .setContentTitle(context.getString(R.string.StatusNotificationDetectionAlertTitle))
-                        .setContentText(context.getString(R.string.StatusNotificationDetectionAlertMessage))
-                        //Requires API 21 .setCategory(Notification.CATEGORY_STATUS)
-                        .setOngoing(true) // cannot be dismissed
-                        .setPriority(Notification.PRIORITY_HIGH)
-                        //Now canceled in activateALert() .setAutoCancel(true) //it's canceled when tapped on it
-                        .setSound(alarmSound);
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+            mNotificationManagerOreoAbove = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
+        }else {
+            mNotificationManager = NotificationManagerCompat.from(context);
+        }
+
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+            importance = mNotificationManagerOreoAbove.IMPORTANCE_HIGH;
+            mChannel = new NotificationChannel(NOTIFICATION_DETECTION_CHANNEL_ID, name, importance);
+        }
+
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+            detectionAlertStatusBuilder = //create a builder for the detection notification
+                    new NotificationCompat.Builder(context, NOTIFICATION_DETECTION_CHANNEL_ID)
+                            .setSmallIcon(R.drawable.hearing_found)
+                            .setContentTitle(context.getString(R.string.StatusNotificationDetectionAlertTitle))
+                            .setContentText(context.getString(R.string.StatusNotificationDetectionAlertMessage))
+                            //Requires API 21 .setCategory(Notification.CATEGORY_STATUS)
+                            .setOngoing(true) // cannot be dismissed
+                            .setPriority(Notification.PRIORITY_HIGH)
+                            //Now canceled in activateALert() .setAutoCancel(true) //it's canceled when tapped on it
+                            .setSound(alarmSound);
+        }else {
+            detectionAlertStatusBuilder = //create a builder for the detection notification
+                    new NotificationCompat.Builder(context)
+                            .setSmallIcon(R.drawable.hearing_found)
+                            .setContentTitle(context.getString(R.string.StatusNotificationDetectionAlertTitle))
+                            .setContentText(context.getString(R.string.StatusNotificationDetectionAlertMessage))
+                            //Requires API 21 .setCategory(Notification.CATEGORY_STATUS)
+                            .setOngoing(true) // cannot be dismissed
+                            .setPriority(Notification.PRIORITY_HIGH)
+                            //Now canceled in activateALert() .setAutoCancel(true) //it's canceled when tapped on it
+                            .setSound(alarmSound);
+        }
+
+
+
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+            detectionAlertStatusBuilder.setChannelId(NOTIFICATION_DETECTION_CHANNEL_ID);
+        }
 
         PendingIntent resultPendingIntent = getPendingIntentDetectionFlagUpdateCurrent(context, technology);
 
@@ -167,12 +247,24 @@ public class NotificationHelper {
     public static void activateDetectionAlertStatusNotification(Context context, Technology technology){
         initDetectionAlertStatusNotification(context, technology); //initialize the notification
 
-        mNotificationManager.notify(NOTIFICATION_DETECTION_ID, notificationDetectionAlertStatus); //activate the notification with the notification itself and its id
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+            mNotificationManagerOreoAbove.createNotificationChannel(mChannel);
+            mNotificationManagerOreoAbove.notify(NOTIFICATION_DETECTION_ID, notificationDetectionAlertStatus);
+        }else {
+            mNotificationManager.notify(NOTIFICATION_DETECTION_ID, notificationDetectionAlertStatus); //activate the notification with the notification itself and its id
+        }
+
+
 
     }
 
     public static void cancelDetectionAlertStatusNotification(Context context){
-        mNotificationManager.cancel(NOTIFICATION_DETECTION_ID); //Cancel the notification with the help of the id Intent resultIntent = new Intent(this, MainActivity.class); //the intent is still the main-activity
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+            mNotificationManagerOreoAbove.cancel(NOTIFICATION_DETECTION_ID);
+        }else{
+            mNotificationManager.cancel(NOTIFICATION_DETECTION_ID); //Cancel the notification with the help of the id Intent resultIntent = new Intent(this, MainActivity.class); //the intent is still the main-activity
+        }
+
         // Cancel the linked pending intent
         PendingIntent resultPendingIntent = getPendingIntentDetectionFlagNoCreate(context);
         if (resultPendingIntent != null)
@@ -180,13 +272,40 @@ public class NotificationHelper {
     }
 
     private static void initOnHoldStatusNotification(Context context){
-        onHoldStatusBuilder = //create a builder for the detection notification
-                new NotificationCompat.Builder(context)
-                        .setSmallIcon(R.drawable.hearing_pause) //adding the icon
-                        .setContentTitle(context.getString(R.string.StatusNotificationOnHoldTitle)) //adding the title
-                        .setContentText(context.getString(R.string.StatusNotificationOnHoldMessage)) //adding the text
-                        //Requires API 21 .setCategory(Notification.CATEGORY_STATUS)
-                        .setOngoing(true); //it's canceled when tapped on it
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+            mNotificationManagerOreoAbove = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
+        }else {
+            mNotificationManager = NotificationManagerCompat.from(context);
+        }
+
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+            importance = mNotificationManagerOreoAbove.IMPORTANCE_HIGH;
+            mChannel = new NotificationChannel(NOTIFICATION_STATUS_CHANNEL_ID, name, importance);
+        }
+
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+            onHoldStatusBuilder = //create a builder for the detection notification
+                    new NotificationCompat.Builder(context, NOTIFICATION_STATUS_CHANNEL_ID)
+                            .setSmallIcon(R.drawable.hearing_pause) //adding the icon
+                            .setContentTitle(context.getString(R.string.StatusNotificationOnHoldTitle)) //adding the title
+                            .setContentText(context.getString(R.string.StatusNotificationOnHoldMessage)) //adding the text
+                            //Requires API 21 .setCategory(Notification.CATEGORY_STATUS)
+                            .setOngoing(true); //it's canceled when tapped on it
+        }else {
+            onHoldStatusBuilder = //create a builder for the detection notification
+                    new NotificationCompat.Builder(context)
+                            .setSmallIcon(R.drawable.hearing_pause) //adding the icon
+                            .setContentTitle(context.getString(R.string.StatusNotificationOnHoldTitle)) //adding the title
+                            .setContentText(context.getString(R.string.StatusNotificationOnHoldMessage)) //adding the text
+                            //Requires API 21 .setCategory(Notification.CATEGORY_STATUS)
+                            .setOngoing(true); //it's canceled when tapped on it
+        }
+
+
+
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+            onHoldStatusBuilder.setChannelId(NOTIFICATION_STATUS_CHANNEL_ID);
+        }
 
         PendingIntent resultPendingIntent = getPendingIntentStatusFlagUpdateCurrent(context);
 
@@ -199,22 +318,58 @@ public class NotificationHelper {
         if(onHoldStatusNotitificationFirstBuild){ //if it's the first time that it's built
             initOnHoldStatusNotification(context); //initialize the notification
         }
-        mNotificationManager.notify(NOTIFICATION_STATUS_ID, notificationOnHoldStatus); //activate the notification with the notification itself and its id
+
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+            mNotificationManagerOreoAbove.createNotificationChannel(mChannel);
+            mNotificationManagerOreoAbove.notify(NOTIFICATION_STATUS_ID, notificationOnHoldStatus);
+        }else {
+            mNotificationManager.notify(NOTIFICATION_STATUS_ID, notificationOnHoldStatus); //activate the notification with the notification itself and its id
+        }
         onHoldStatusNotitificationFirstBuild = false; //notification is created
     }
 
     public static void cancelOnHoldStatusNotification(){
-        mNotificationManager.cancel(NOTIFICATION_STATUS_ID); //Cancel the notification with the help of the id
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+            mNotificationManagerOreoAbove.cancel(NOTIFICATION_STATUS_ID);
+        }else{
+            mNotificationManager.cancel(NOTIFICATION_STATUS_ID); //Cancel the notification with the help of the id
+        }
+
     }
 
     public static Notification initScanningStatusNotification(Context context){
-        scanningStatusBuilder = //create a builder for the detection notification
-                new NotificationCompat.Builder(context)
-                        .setSmallIcon(R.drawable.ic_hearing_white_48dp) //adding the icon
-                        .setContentTitle(context.getString(R.string.StatusNotificationScanningTitle)) //adding the title
-                        .setContentText(context.getString(R.string.StatusNotificationScanningMessage)) //adding the text
-                        //Requires API 21 .setCategory(Notification.CATEGORY_SERVICE)
-                        .setOngoing(true); //it's canceled when tapped on it
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+            mNotificationManagerOreoAbove = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
+        }else {
+            mNotificationManager = NotificationManagerCompat.from(context);
+        }
+
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+            importance = mNotificationManagerOreoAbove.IMPORTANCE_HIGH;
+            mChannel = new NotificationChannel(NOTIFICATION_STATUS_CHANNEL_ID, name, importance);
+        }
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+            scanningStatusBuilder = //create a builder for the detection notification
+                    new NotificationCompat.Builder(context, NOTIFICATION_STATUS_CHANNEL_ID)
+                            .setSmallIcon(R.drawable.ic_hearing_white_48dp) //adding the icon
+                            .setContentTitle(context.getString(R.string.StatusNotificationScanningTitle)) //adding the title
+                            .setContentText(context.getString(R.string.StatusNotificationScanningMessage)) //adding the text
+                            //Requires API 21 .setCategory(Notification.CATEGORY_SERVICE)
+                            .setOngoing(true); //it's canceled when tapped on it
+        }else {
+            scanningStatusBuilder = //create a builder for the detection notification
+                    new NotificationCompat.Builder(context)
+                            .setSmallIcon(R.drawable.ic_hearing_white_48dp) //adding the icon
+                            .setContentTitle(context.getString(R.string.StatusNotificationScanningTitle)) //adding the title
+                            .setContentText(context.getString(R.string.StatusNotificationScanningMessage)) //adding the text
+                            //Requires API 21 .setCategory(Notification.CATEGORY_SERVICE)
+                            .setOngoing(true); //it's canceled when tapped on it
+        }
+
+
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+            scanningStatusBuilder.setChannelId(NOTIFICATION_STATUS_CHANNEL_ID);
+        }
 
         PendingIntent resultPendingIntent = getPendingIntentStatusFlagUpdateCurrent(context);
 
@@ -228,12 +383,23 @@ public class NotificationHelper {
         if(scanningStatusNotitificationFirstBuild){ //if it's the first time that it's built
             initScanningStatusNotification(context); //initialize the notification
         }
-        mNotificationManager.notify(NOTIFICATION_STATUS_ID, notificationScanningStatus); //activate the notification with the notification itself and its id
+
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+            mNotificationManagerOreoAbove.createNotificationChannel(mChannel);
+            mNotificationManagerOreoAbove.notify(NOTIFICATION_STATUS_ID, notificationScanningStatus);
+        }else {
+            mNotificationManager.notify(NOTIFICATION_STATUS_ID, notificationScanningStatus); //activate the notification with the notification itself and its id
+        }
+
         scanningStatusNotitificationFirstBuild = false; //notification is created
     }
 
     public static void cancelScanningStatusNotification(){
-        mNotificationManager.cancel(NOTIFICATION_STATUS_ID); //Cancel the notification with the help of the id
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+            mNotificationManagerOreoAbove.cancel(NOTIFICATION_STATUS_ID);
+        }else{
+            mNotificationManager.cancel(NOTIFICATION_STATUS_ID); //Cancel the notification with the help of the id
+        }
     }
 
 }
diff --git a/SoniControl/app/src/main/java/at/ac/fhstp/sonicontrol/SettingsActivity.java b/SoniControl/app/src/main/java/at/ac/fhstp/sonicontrol/SettingsActivity.java
index 7e24bbdd..0b6940dd 100644
--- a/SoniControl/app/src/main/java/at/ac/fhstp/sonicontrol/SettingsActivity.java
+++ b/SoniControl/app/src/main/java/at/ac/fhstp/sonicontrol/SettingsActivity.java
@@ -21,7 +21,7 @@ package at.ac.fhstp.sonicontrol;
 
 
 import android.os.Bundle;
-import android.support.v7.app.ActionBarActivity;
+//import android.support.v7.app.ActionBarActivity;
 import android.support.v7.app.AppCompatActivity;
 import android.view.Menu;
 import android.view.MenuInflater;
diff --git a/SoniControl/app/src/main/java/at/ac/fhstp/sonicontrol/Spoofer.java b/SoniControl/app/src/main/java/at/ac/fhstp/sonicontrol/Spoofer.java
index 9eedc3fc..beb8fa33 100644
--- a/SoniControl/app/src/main/java/at/ac/fhstp/sonicontrol/Spoofer.java
+++ b/SoniControl/app/src/main/java/at/ac/fhstp/sonicontrol/Spoofer.java
@@ -114,7 +114,7 @@ public class Spoofer {
                 AudioManager audioManager = (AudioManager) main.getSystemService(Context.AUDIO_SERVICE);
                 // not used ? int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_ALARM);
                 //Log.d("Spoofer", "Streamtype: " + String.valueOf(AudioManager.STREAM_MUSIC));
-                audioManager.setStreamVolume(3, (int) Math.round((audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC) * 0.75D)), 0);
+                audioManager.setStreamVolume(3, (int) Math.round((audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC) * 1.00D)), 0);
 
                 if (playingHandler) {
                     playtime = genNoise.getPlayertime(); //get the playertime depending on the generated whitenoise
-- 
GitLab