From cca49b2ba901f8d8c40327c689761c87db368fa9 Mon Sep 17 00:00:00 2001 From: Ionut Muthi Date: Tue, 19 Apr 2022 18:49:14 +0300 Subject: [PATCH] android : notification refactor Signed-off-by: Ionut Muthi --- android/src/org/adi/scopy/ScopyActivity.java | 46 ++++++++++---------- src/tool_launcher.cpp | 12 ++--- src/tool_launcher.hpp | 6 +-- 3 files changed, 31 insertions(+), 33 deletions(-) diff --git a/android/src/org/adi/scopy/ScopyActivity.java b/android/src/org/adi/scopy/ScopyActivity.java index 658c90bbc3..a3935a0290 100644 --- a/android/src/org/adi/scopy/ScopyActivity.java +++ b/android/src/org/adi/scopy/ScopyActivity.java @@ -52,53 +52,50 @@ public class ScopyActivity extends QtActivity public static native void saveAndStopRunningToolsJNI(); public static native void saveAndStopRunningInputToolsJNI(); public static native void restoreRunningToolsJNI(); - public static native int nrOfToolsSaved(); - public static native int nrOfToolsRunning(); - public static native boolean hasCtx(); - private static final int NOTIFICATION_ID = 1234567; + public static native int nrOfToolsSavedJNI(); + public static native int nrOfToolsRunningJNI(); + public static native boolean hasCtxJNI(); + private static final int SCOPY_WAKELOCK_NOTIFICATION_ID = 1; + private static final String SCOPY_NOTIFICATION_CHANNEL_ID = "scopy"; boolean initialized; - WakeLock wakeLock; + private WakeLock wakeLock; private void createNotificationChannel() { // Create the NotificationChannel, but only on API 26+ because // the NotificationChannel class is new and not in the support library - CharSequence name = "R.string.channel_name"; - String description = "R.string.channel_description"; + CharSequence name = "Scopy notifications"; + String description = "Various messages from Scopy"; int importance = NotificationManager.IMPORTANCE_DEFAULT; - NotificationChannel channel = new NotificationChannel("1234", name, importance); - channel.setDescription("description"); + NotificationChannel scopyNotificationChannel = new NotificationChannel(SCOPY_NOTIFICATION_CHANNEL_ID, name, importance); + scopyNotificationChannel.setDescription(description); // Register the channel with the system; you can't change the importance // or other notification behaviors after this NotificationManager notificationManager = getSystemService(NotificationManager.class); - notificationManager.createNotificationChannel(channel); + notificationManager.createNotificationChannel(scopyNotificationChannel); } public void createNotification(String message) { - NotificationManager notificationManager = (NotificationManager) getSystemService(NotificationManager.class); createNotificationChannel(); Intent notificationIntent = new Intent(this, ScopyActivity.class); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); - PendingIntent intent = PendingIntent.getActivity(this, 0, notificationIntent, 0); + PendingIntent openAppOnTapIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); - Notification notif = new Notification.Builder(this,"1234") - .setSmallIcon(R.drawable.icon) - .setContentTitle("Scopy") + Notification notification = new Notification.Builder(this,SCOPY_NOTIFICATION_CHANNEL_ID) + .setSmallIcon(R.drawable.icon) .setContentText(message) .setPriority(Notification.PRIORITY_DEFAULT) - .setContentIntent(intent) + .setContentIntent(openAppOnTapIntent) .setOngoing(true) .build(); - - - notificationManager.notify(NOTIFICATION_ID, notif); + notificationManager.notify(SCOPY_WAKELOCK_NOTIFICATION_ID, notification); } @Override @@ -129,13 +126,14 @@ protected void onStart() protected void onStop() { System.out.println("-- ScopyActivity: onStop"); + System.out.println(nrOfToolsSavedJNI()); if (initialized) { - if (hasCtx()) { + if (hasCtxJNI()) { saveAndStopRunningInputToolsJNI(); - if (nrOfToolsRunning() != 0) { + if (nrOfToolsRunningJNI() != 0) { System.out.println("-- Creating Notification"); wakeLock.acquire(); - createNotification("Scopy still running in the background.Device outputs enabled"); + createNotification("Scopy is still running in the background. Device outputs are still enabled."); } } } @@ -145,7 +143,7 @@ protected void onStop() @Override protected void onResume() { - cancelNotification(NOTIFICATION_ID); + cancelNotification(SCOPY_WAKELOCK_NOTIFICATION_ID); if (wakeLock.isHeld()) { wakeLock.release(); } @@ -164,7 +162,7 @@ protected void onPause(){ protected void onDestroy(){ System.out.println("-- ScopyActivity: onDestroy "); - cancelNotification(NOTIFICATION_ID); + cancelNotification(SCOPY_WAKELOCK_NOTIFICATION_ID); if (wakeLock.isHeld()) { wakeLock.release(); } diff --git a/src/tool_launcher.cpp b/src/tool_launcher.cpp index 2b1b9b1410..5483d652c9 100644 --- a/src/tool_launcher.cpp +++ b/src/tool_launcher.cpp @@ -2161,7 +2161,7 @@ void ToolLauncher::restoreRunningToolsJNI(JNIEnv *env, jobject /*thiz*/) { } } -int ToolLauncher::nrOfToolsSaved(JNIEnv *env, jobject /*thiz*/) { +int ToolLauncher::nrOfToolsSavedJNI(JNIEnv *env, jobject /*thiz*/) { qDebug()<<"-- Getting number of stopped tools JNI"; ToolLauncher* tl = getToolLauncherInstance(); if(tl) @@ -2173,7 +2173,7 @@ int ToolLauncher::nrOfToolsSaved(JNIEnv *env, jobject /*thiz*/) { return 0; } -int ToolLauncher::nrOfToolsRunning(JNIEnv *env, jobject /*thiz*/) { +int ToolLauncher::nrOfToolsRunningJNI(JNIEnv *env, jobject /*thiz*/) { qDebug()<<"-- Getting number of stopped tools JNI"; ToolLauncher* tl = getToolLauncherInstance(); if(tl) @@ -2185,7 +2185,7 @@ int ToolLauncher::nrOfToolsRunning(JNIEnv *env, jobject /*thiz*/) { return 0; } -bool ToolLauncher::hasCtx(JNIEnv *env, jobject /*thiz*/) +bool ToolLauncher::hasCtxJNI(JNIEnv *env, jobject /*thiz*/) { qDebug()<<"-- Getting number of stopped tools JNI"; ToolLauncher* tl = getToolLauncherInstance(); @@ -2205,9 +2205,9 @@ void ToolLauncher::registerNativeMethods() {"saveAndStopRunningToolsJNI", "()V", reinterpret_cast(saveAndStopRunningToolsJNI) }, {"saveAndStopRunningInputToolsJNI", "()V", reinterpret_cast(saveAndStopRunningInputToolsJNI) }, {"restoreRunningToolsJNI", "()V", reinterpret_cast(restoreRunningToolsJNI) }, - {"nrOfToolsSaved", "()I", reinterpret_cast(nrOfToolsSaved) }, - {"nrOfToolsRunning", "()I", reinterpret_cast(nrOfToolsRunning) }, - {"hasCtx", "()Z", reinterpret_cast(hasCtx) }, + {"nrOfToolsSavedJNI", "()I", reinterpret_cast(nrOfToolsSavedJNI) }, + {"nrOfToolsRunningJNI", "()I", reinterpret_cast(nrOfToolsRunningJNI) }, + {"hasCtxJNI", "()Z", reinterpret_cast(hasCtxJNI) }, }; QAndroidJniObject activity = QtAndroid::androidActivity(); diff --git a/src/tool_launcher.hpp b/src/tool_launcher.hpp index 9d7e104fa2..b21d41b983 100644 --- a/src/tool_launcher.hpp +++ b/src/tool_launcher.hpp @@ -210,9 +210,9 @@ private Q_SLOTS: static void saveAndStopRunningToolsJNI(JNIEnv *env, jobject /*thiz*/); static void saveAndStopRunningInputToolsJNI(JNIEnv *env, jobject /*thiz*/); static void restoreRunningToolsJNI(JNIEnv *env, jobject /*thiz*/); - static int nrOfToolsSaved(JNIEnv *env, jobject /*thiz*/); - static int nrOfToolsRunning(JNIEnv *env, jobject /*thiz*/); - static bool hasCtx(JNIEnv *env, jobject /*thiz*/); + static int nrOfToolsSavedJNI(JNIEnv *env, jobject /*thiz*/); + static int nrOfToolsRunningJNI(JNIEnv *env, jobject /*thiz*/); + static bool hasCtxJNI(JNIEnv *env, jobject /*thiz*/); #endif private: