This repository has been archived on 2021-06-24. You can view files and clone it, but cannot push or open issues or pull requests.
ui/android/src/ca/openprivacy/cwtch/ui/CwtchActivity.java

128 lines
5.2 KiB
Java

package ca.openprivacy.cwtch.ui;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.NotificationChannel;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.os.Bundle;
import android.content.ComponentName;
import static android.app.Notification.DEFAULT_LIGHTS;
import static android.app.Notification.DEFAULT_SOUND;
import static android.app.Notification.DEFAULT_VIBRATE;
import static android.app.NotificationManager.IMPORTANCE_DEFAULT;
import static android.app.NotificationManager.IMPORTANCE_LOW;
import static android.content.Context.NOTIFICATION_SERVICE;
import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
import static android.os.Build.VERSION.SDK_INT;
import static android.app.Notification.CATEGORY_SERVICE;
public class CwtchActivity extends org.qtproject.qt5.android.bindings.QtActivity
{
private static NotificationManager m_notificationManager;
private static Notification.Builder m_builder;
private static Notification.Builder m_builderOngoing;
private static CwtchActivity m_instance;
private static int PRIORITY_MIN = -2; // From NotificationCompat
private static int PRIORITY_DEFAULT = 0; // From NotificationCompat
private static String NOTIFICATION_CHANNEL_ID = "cwtch_notification_channel";
private static int ONGOING_NOTIFICATION_ID = 0;
private static String ONGOING_NOTIFICATION_ID_NAME = "ongoing";
private static int CONTENT_NOTIFICATION_ID = 1;
private static String CONTENT_NOTIFICATION_ID_NAME = "content";
public CwtchActivity() {
m_instance = this;
}
// https://github.com/bbernhard/qtandroidservices_example/blob/master/source/java/MyCustomAppActivity.java
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
Log.i("CwtchActivity", "Starting service!");
Intent serviceIntent = new Intent(this, ca.openprivacy.cwtch.ui.CwtchService.class);
ComponentName ret = startService(serviceIntent);
if (ret == null) {
Log.i("CwtchActivity", "Started Service: FAILED with null");
} else {
Log.i("CwtchActivity", "Started Service: " + ret.flattenToString());
}
}
public static void notify(String s)
{
if (m_notificationManager == null) {
m_notificationManager = (NotificationManager)m_instance.getSystemService(Context.NOTIFICATION_SERVICE);
createNotificationChannel();
}
if (m_builder == null) {
m_builder = new Notification.Builder(m_instance);
m_builder.setSmallIcon(R.drawable.ic_launcher);
m_builder.setContentTitle("Cwtch");
m_builder.setPriority(PRIORITY_DEFAULT);
}
m_builder.setContentText(s);
m_notificationManager.notify(CONTENT_NOTIFICATION_ID, m_builder.build());
}
public static void ongoingNotify(String s)
{
if (m_notificationManager == null) {
m_notificationManager = (NotificationManager)m_instance.getSystemService(Context.NOTIFICATION_SERVICE);
createNotificationChannel();
}
if (m_builderOngoing == null) {
m_builderOngoing = new Notification.Builder(m_instance);
m_builderOngoing.setSmallIcon(R.drawable.ic_launcher);
m_builderOngoing.setContentTitle("Cwtch");
m_builderOngoing.setPriority(PRIORITY_MIN);
m_builderOngoing.setWhen(0); // Don't show the time
m_builderOngoing.setOngoing(true);
if (SDK_INT >= 21) {
m_builderOngoing.setCategory(CATEGORY_SERVICE);
//m_builder.setVisibility(VISIBILITY_SECRET);
}
}
m_builderOngoing.setContentText(s);
m_notificationManager.notify(ONGOING_NOTIFICATION_ID, m_builderOngoing.build());
}
private static void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (SDK_INT >= 26) {
String description = "Cwtch Ongoing Notification Channel";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, ONGOING_NOTIFICATION_ID_NAME, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
m_notificationManager.createNotificationChannel(channel);
description = "Cwtch Content Notification Channel";
importance = NotificationManager.IMPORTANCE_DEFAULT;
channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, CONTENT_NOTIFICATION_ID_NAME, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
m_notificationManager.createNotificationChannel(channel);
}
}
}