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;
|
2024-02-11 14:20:23 +01:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.net.Uri;
|
2020-07-22 21:32:42 +02:00
|
|
|
import android.os.Build;
|
2024-02-11 14:20:23 +01:00
|
|
|
import android.provider.Settings;
|
|
|
|
import androidx.core.app.NotificationManagerCompat;
|
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;
|
2024-02-11 14:20:23 +01:00
|
|
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
|
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;
|
2024-03-18 06:12:24 +01:00
|
|
|
import org.mian.gitnex.helpers.AppDatabaseSettings;
|
2021-03-21 16:56:54 +01:00
|
|
|
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) {
|
|
|
|
|
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
|
|
|
|
|
|
|
// 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));
|
|
|
|
|
|
|
|
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
|
|
|
|
2024-02-11 14:20:23 +01:00
|
|
|
int delay;
|
2024-03-18 06:12:24 +01:00
|
|
|
if (Integer.parseInt(
|
|
|
|
AppDatabaseSettings.getSettingsValue(
|
|
|
|
context, AppDatabaseSettings.APP_NOTIFICATIONS_DELAY_KEY))
|
|
|
|
== 0) {
|
2024-02-11 14:20:23 +01:00
|
|
|
delay = 15;
|
2024-03-18 06:12:24 +01:00
|
|
|
} else if (Integer.parseInt(
|
|
|
|
AppDatabaseSettings.getSettingsValue(
|
|
|
|
context, AppDatabaseSettings.APP_NOTIFICATIONS_DELAY_KEY))
|
|
|
|
== 1) {
|
2024-02-11 14:20:23 +01:00
|
|
|
delay = 30;
|
2024-03-18 06:12:24 +01:00
|
|
|
} else if (Integer.parseInt(
|
|
|
|
AppDatabaseSettings.getSettingsValue(
|
|
|
|
context, AppDatabaseSettings.APP_NOTIFICATIONS_DELAY_KEY))
|
|
|
|
== 2) {
|
2024-02-11 14:20:23 +01:00
|
|
|
delay = 45;
|
2024-03-18 06:12:24 +01:00
|
|
|
} else if (Integer.parseInt(
|
|
|
|
AppDatabaseSettings.getSettingsValue(
|
|
|
|
context, AppDatabaseSettings.APP_NOTIFICATIONS_DELAY_KEY))
|
|
|
|
== 3) {
|
2024-02-11 14:20:23 +01:00
|
|
|
delay = 60;
|
|
|
|
} else {
|
|
|
|
delay = Constants.defaultPollingDelay;
|
|
|
|
}
|
|
|
|
|
2024-03-18 06:12:24 +01:00
|
|
|
if (Boolean.parseBoolean(
|
|
|
|
AppDatabaseSettings.getSettingsValue(
|
|
|
|
context, AppDatabaseSettings.APP_NOTIFICATIONS_KEY))) {
|
2020-07-22 21:32:42 +02:00
|
|
|
|
2024-02-11 14:20:23 +01:00
|
|
|
if (!NotificationManagerCompat.from(context).areNotificationsEnabled()) {
|
|
|
|
MaterialAlertDialogBuilder materialAlertDialogBuilder =
|
|
|
|
new MaterialAlertDialogBuilder(context)
|
|
|
|
.setTitle(R.string.pageTitleNotifications)
|
|
|
|
.setMessage(context.getString(R.string.openAppSettings))
|
|
|
|
.setNeutralButton(
|
|
|
|
R.string.cancelButton, (dialog, which) -> dialog.dismiss())
|
|
|
|
.setPositiveButton(
|
|
|
|
R.string.isOpen,
|
|
|
|
(dialog, which) -> {
|
|
|
|
Intent intent =
|
|
|
|
new Intent(
|
|
|
|
Settings
|
|
|
|
.ACTION_APPLICATION_DETAILS_SETTINGS);
|
|
|
|
Uri uri =
|
|
|
|
Uri.fromParts(
|
|
|
|
"package",
|
|
|
|
context.getPackageName(),
|
|
|
|
null);
|
|
|
|
intent.setData(uri);
|
|
|
|
context.startActivity(intent);
|
|
|
|
});
|
|
|
|
|
|
|
|
materialAlertDialogBuilder.create().show();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2024-02-11 14:20:23 +01:00
|
|
|
constraints.setRequiresDeviceIdle(false);
|
2022-09-21 07:43:00 +02:00
|
|
|
|
|
|
|
PeriodicWorkRequest periodicWorkRequest =
|
|
|
|
new PeriodicWorkRequest.Builder(
|
2024-02-11 14:20:23 +01:00
|
|
|
NotificationsWorker.class, delay, TimeUnit.MINUTES)
|
2022-09-21 07:43:00 +02:00
|
|
|
.setConstraints(constraints.build())
|
|
|
|
.addTag(Constants.notificationsWorkerId)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
WorkManager.getInstance(context)
|
|
|
|
.enqueueUniquePeriodicWork(
|
|
|
|
Constants.notificationsWorkerId,
|
2024-02-11 14:20:23 +01:00
|
|
|
ExistingPeriodicWorkPolicy.CANCEL_AND_REENQUEUE,
|
2022-09-21 07:43:00 +02:00
|
|
|
periodicWorkRequest);
|
2021-03-21 16:56:54 +01:00
|
|
|
}
|
|
|
|
}
|
2020-07-22 21:32:42 +02:00
|
|
|
}
|