2020-07-22 21:32:42 +02:00
|
|
|
package org.mian.gitnex.notifications;
|
|
|
|
|
2021-03-21 16:56:54 +01:00
|
|
|
import android.app.NotificationChannel;
|
|
|
|
import android.app.NotificationManager;
|
2020-07-22 21:32:42 +02:00
|
|
|
import android.content.Context;
|
2021-03-21 16:56:54 +01:00
|
|
|
import android.graphics.Color;
|
2020-07-22 21:32:42 +02:00
|
|
|
import android.os.Build;
|
2022-09-14 20:40:44 +02:00
|
|
|
import androidx.work.Constraints;
|
|
|
|
import androidx.work.ExistingPeriodicWorkPolicy;
|
|
|
|
import androidx.work.NetworkType;
|
|
|
|
import androidx.work.PeriodicWorkRequest;
|
|
|
|
import androidx.work.WorkManager;
|
2022-09-21 07:43:00 +02:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2021-03-21 16:56:54 +01:00
|
|
|
import org.mian.gitnex.R;
|
|
|
|
import org.mian.gitnex.helpers.Constants;
|
2020-07-22 21:32:42 +02:00
|
|
|
import org.mian.gitnex.helpers.TinyDB;
|
|
|
|
|
|
|
|
/**
|
2022-07-29 14:08:44 +02:00
|
|
|
* @author opyale
|
2020-07-22 21:32:42 +02:00
|
|
|
*/
|
2021-02-13 20:31:38 +01:00
|
|
|
public class Notifications {
|
2020-07-22 21:32:42 +02:00
|
|
|
|
2021-03-21 16:56:54 +01:00
|
|
|
public static int uniqueNotificationId(Context context) {
|
2020-07-22 21:32:42 +02:00
|
|
|
|
2021-03-21 16:56:54 +01:00
|
|
|
TinyDB tinyDB = TinyDB.getInstance(context);
|
2020-07-22 21:32:42 +02:00
|
|
|
|
2021-03-21 16:56:54 +01:00
|
|
|
int previousNotificationId = tinyDB.getInt("previousNotificationId", 0);
|
2022-09-21 07:43:00 +02:00
|
|
|
int nextPreviousNotificationId =
|
|
|
|
previousNotificationId == Integer.MAX_VALUE ? 0 : previousNotificationId + 1;
|
2021-03-21 16:56:54 +01:00
|
|
|
|
|
|
|
tinyDB.putInt("previousNotificationId", nextPreviousNotificationId);
|
|
|
|
return previousNotificationId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void createChannels(Context context) {
|
|
|
|
|
|
|
|
TinyDB tinyDB = TinyDB.getInstance(context);
|
2022-09-21 07:43:00 +02:00
|
|
|
NotificationManager notificationManager =
|
|
|
|
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
2021-03-21 16:56:54 +01:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
2021-03-21 16:56:54 +01:00
|
|
|
|
|
|
|
// Delete old notification channels
|
2022-09-21 07:43:00 +02:00
|
|
|
notificationManager.deleteNotificationChannel(
|
|
|
|
context.getPackageName()); // TODO Can be removed in future versions
|
2021-03-21 16:56:54 +01:00
|
|
|
|
|
|
|
// Create new notification channels
|
2022-09-21 07:43:00 +02:00
|
|
|
NotificationChannel mainChannel =
|
|
|
|
new NotificationChannel(
|
|
|
|
Constants.mainNotificationChannelId,
|
|
|
|
context.getString(R.string.mainNotificationChannelName),
|
|
|
|
NotificationManager.IMPORTANCE_DEFAULT);
|
|
|
|
mainChannel.setDescription(
|
|
|
|
context.getString(R.string.mainNotificationChannelDescription));
|
|
|
|
|
|
|
|
if (tinyDB.getBoolean("notificationsEnableVibration", true)) {
|
2021-04-13 20:56:50 +02:00
|
|
|
mainChannel.setVibrationPattern(Constants.defaultVibrationPattern);
|
2021-03-21 16:56:54 +01:00
|
|
|
mainChannel.enableVibration(true);
|
2022-09-21 07:43:00 +02:00
|
|
|
} else {
|
2021-03-21 16:56:54 +01:00
|
|
|
mainChannel.enableVibration(false);
|
|
|
|
}
|
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
if (tinyDB.getBoolean("notificationsEnableLights", true)) {
|
2021-03-21 16:56:54 +01:00
|
|
|
mainChannel.setLightColor(tinyDB.getInt("notificationsLightColor", Color.GREEN));
|
|
|
|
mainChannel.enableLights(true);
|
2022-09-21 07:43:00 +02:00
|
|
|
} else {
|
2021-03-21 16:56:54 +01:00
|
|
|
mainChannel.enableLights(false);
|
|
|
|
}
|
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
NotificationChannel downloadChannel =
|
|
|
|
new NotificationChannel(
|
|
|
|
Constants.downloadNotificationChannelId,
|
|
|
|
context.getString(R.string.fileViewerNotificationChannelName),
|
|
|
|
NotificationManager.IMPORTANCE_LOW);
|
|
|
|
downloadChannel.setDescription(
|
|
|
|
context.getString(R.string.fileViewerNotificationChannelDescription));
|
2021-03-21 16:56:54 +01:00
|
|
|
|
|
|
|
notificationManager.createNotificationChannel(mainChannel);
|
|
|
|
notificationManager.createNotificationChannel(downloadChannel);
|
2020-07-22 21:32:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-13 20:31:38 +01:00
|
|
|
public static void stopWorker(Context context) {
|
2020-07-22 21:32:42 +02:00
|
|
|
|
2021-04-13 20:56:50 +02:00
|
|
|
WorkManager.getInstance(context).cancelAllWorkByTag(Constants.notificationsWorkerId);
|
2020-07-22 21:32:42 +02:00
|
|
|
}
|
|
|
|
|
2021-02-13 20:31:38 +01:00
|
|
|
public static void startWorker(Context context) {
|
2020-07-22 21:32:42 +02:00
|
|
|
|
2020-11-02 16:17:00 +01:00
|
|
|
TinyDB tinyDB = TinyDB.getInstance(context);
|
2020-07-22 21:32:42 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
if (tinyDB.getBoolean("notificationsEnabled", true)) {
|
2020-07-22 21:32:42 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
Constraints.Builder constraints =
|
|
|
|
new Constraints.Builder()
|
|
|
|
.setRequiredNetworkType(NetworkType.CONNECTED)
|
|
|
|
.setRequiresBatteryNotLow(false)
|
|
|
|
.setRequiresStorageNotLow(false)
|
|
|
|
.setRequiresCharging(false);
|
2020-11-03 20:14:24 +01:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
2021-04-13 20:56:50 +02:00
|
|
|
constraints.setRequiresDeviceIdle(false);
|
2020-11-03 20:14:24 +01:00
|
|
|
}
|
2021-03-21 16:56:54 +01:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
int pollingDelayMinutes =
|
|
|
|
Math.max(
|
|
|
|
tinyDB.getInt("pollingDelayMinutes", Constants.defaultPollingDelay),
|
|
|
|
15);
|
|
|
|
|
|
|
|
PeriodicWorkRequest periodicWorkRequest =
|
|
|
|
new PeriodicWorkRequest.Builder(
|
|
|
|
NotificationsWorker.class,
|
|
|
|
pollingDelayMinutes,
|
|
|
|
TimeUnit.MINUTES)
|
|
|
|
.setConstraints(constraints.build())
|
|
|
|
.addTag(Constants.notificationsWorkerId)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
WorkManager.getInstance(context)
|
|
|
|
.enqueueUniquePeriodicWork(
|
|
|
|
Constants.notificationsWorkerId,
|
|
|
|
ExistingPeriodicWorkPolicy.KEEP,
|
|
|
|
periodicWorkRequest);
|
2021-03-21 16:56:54 +01:00
|
|
|
}
|
|
|
|
}
|
2020-07-22 21:32:42 +02:00
|
|
|
}
|