From 9b451c9b2912022119efcbb49587c1df3c6b128a Mon Sep 17 00:00:00 2001 From: Greg Alexander Date: Sun, 26 May 2019 20:47:59 -0400 Subject: [PATCH] Define the channel for the notification, which requires using NotificationCompat, which I decided to do using "AndroidX". Anyways, this builds, but I don't know if it works. --- app/build.gradle | 3 ++ .../galexander/sshd/SimpleSSHDService.java | 49 ++++++++++++++----- gradle.properties | 2 + 3 files changed, 41 insertions(+), 13 deletions(-) create mode 100644 gradle.properties diff --git a/app/build.gradle b/app/build.gradle index ac4aaa8..6d0b94d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -36,3 +36,6 @@ android { } } } +dependencies { + implementation 'androidx.legacy:legacy-support-v4:1.0.0' +} diff --git a/app/src/main/java/org/galexander/sshd/SimpleSSHDService.java b/app/src/main/java/org/galexander/sshd/SimpleSSHDService.java index 2f83061..8fd3c72 100644 --- a/app/src/main/java/org/galexander/sshd/SimpleSSHDService.java +++ b/app/src/main/java/org/galexander/sshd/SimpleSSHDService.java @@ -1,12 +1,16 @@ package org.galexander.sshd; import android.app.Notification; +import android.app.NotificationChannel; +import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.Intent; import android.content.Context; +import android.os.Build; import android.os.IBinder; import android.widget.RemoteViews; +import androidx.core.app.NotificationCompat; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; @@ -62,28 +66,47 @@ public class SimpleSSHDService extends Service { private void do_foreground() { foregrounded = Prefs.get_foreground(); if (foregrounded) { - Notification n = new Notification( - R.drawable.notification_icon, - "SimpleSSHD", 0); - n.tickerText = "SimpleSSHD"; - n.contentIntent = PendingIntent.getActivity(this, 0, - new Intent(this, SimpleSSHD.class), - PendingIntent.FLAG_UPDATE_CURRENT); + create_notification_channel(); - n.contentView = new RemoteViews(getPackageName(), + RemoteViews rv = new RemoteViews(getPackageName(), R.layout.notification); - /* for some reason icon cannot be defined in xml: */ - n.contentView.setImageViewResource(R.id.n_icon, - R.drawable.icon); - n.contentView.setTextViewText(R.id.n_text, + rv.setImageViewResource(R.id.n_icon, R.drawable.icon); + rv.setTextViewText(R.id.n_text, "SimpleSSHD listening on " + SimpleSSHD.get_ip(false) + ":" + Prefs.get_port()); - + PendingIntent pi = PendingIntent.getActivity(this, 0, + new Intent(this, SimpleSSHD.class), + PendingIntent.FLAG_UPDATE_CURRENT); + Notification n = new NotificationCompat.Builder( + this, "main") + .setSmallIcon(R.drawable.notification_icon) + .setTicker("SimpleSSHD") + .setContent(rv) + .setContentIntent(pi) + .setOngoing(true) + .setPriority(NotificationCompat.PRIORITY_LOW) + .setLocalOnly(true) + .setVisibility( + NotificationCompat.VISIBILITY_PUBLIC) + .build(); startForeground(1, n); } } + private void create_notification_channel() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + NotificationChannel nc = new NotificationChannel( + "main", "SimpleSSHD", + NotificationManager.IMPORTANCE_LOW); + nc.enableLights(false); + nc.enableVibration(false); + nc.setSound(null, null); + getSystemService(NotificationManager.class) + .createNotificationChannel(nc); + } + } + public static boolean is_started() { return (sshd_pid != 0); } diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..646c51b --- /dev/null +++ b/gradle.properties @@ -0,0 +1,2 @@ +android.useAndroidX=true +android.enableJetifier=true