Skip to content

Commit

Permalink
android : notification refactor
Browse files Browse the repository at this point in the history
Signed-off-by: Ionut Muthi <[email protected]>
  • Loading branch information
IonutMuthi authored and adisuciu committed Apr 20, 2022
1 parent 9c13c7e commit cca49b2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 33 deletions.
46 changes: 22 additions & 24 deletions android/src/org/adi/scopy/ScopyActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.");
}
}
}
Expand All @@ -145,7 +143,7 @@ protected void onStop()
@Override
protected void onResume()
{
cancelNotification(NOTIFICATION_ID);
cancelNotification(SCOPY_WAKELOCK_NOTIFICATION_ID);
if (wakeLock.isHeld()) {
wakeLock.release();
}
Expand All @@ -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();
}
Expand Down
12 changes: 6 additions & 6 deletions src/tool_launcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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();
Expand All @@ -2205,9 +2205,9 @@ void ToolLauncher::registerNativeMethods()
{"saveAndStopRunningToolsJNI", "()V", reinterpret_cast<void*>(saveAndStopRunningToolsJNI) },
{"saveAndStopRunningInputToolsJNI", "()V", reinterpret_cast<void*>(saveAndStopRunningInputToolsJNI) },
{"restoreRunningToolsJNI", "()V", reinterpret_cast<void*>(restoreRunningToolsJNI) },
{"nrOfToolsSaved", "()I", reinterpret_cast<void*>(nrOfToolsSaved) },
{"nrOfToolsRunning", "()I", reinterpret_cast<void*>(nrOfToolsRunning) },
{"hasCtx", "()Z", reinterpret_cast<void*>(hasCtx) },
{"nrOfToolsSavedJNI", "()I", reinterpret_cast<void*>(nrOfToolsSavedJNI) },
{"nrOfToolsRunningJNI", "()I", reinterpret_cast<void*>(nrOfToolsRunningJNI) },
{"hasCtxJNI", "()Z", reinterpret_cast<void*>(hasCtxJNI) },
};

QAndroidJniObject activity = QtAndroid::androidActivity();
Expand Down
6 changes: 3 additions & 3 deletions src/tool_launcher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit cca49b2

Please sign in to comment.