SWADroid/SWADroid/src/main/java/es/ugr/swad/swadroid/gui/AlertNotificationFactory.java
Marown dcd264adb4
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
Add support for background notification download when power save mode is enabled (#411)
## What changes were proposed in this pull request?

* Add support for background notification download when power save mode is enabled (Fixes #408)
* Remove social links

## How was this patch tested?

Manually.

Co-authored-by: Amab <juanmi1982@gmail.com>
Reviewed-on: #411
2022-12-18 14:48:17 +01:00

181 lines
7.4 KiB
Java

/*
* This file is part of SWADroid.
*
* Copyright (C) 2010 Juan Miguel Boyero Corral <juanmi1982@gmail.com>
*
* SWADroid is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SWADroid is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with SWADroid. If not, see <http://www.gnu.org/licenses/>.
*/
package es.ugr.swad.swadroid.gui;
import static es.ugr.swad.swadroid.utils.NotificationUtils.SWADROID_CHANNEL_ID;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
import es.ugr.swad.swadroid.modules.notifications.Notifications;
import es.ugr.swad.swadroid.utils.NotificationUtils;
/**
* Class for create notification alerts.
*
* @author Juan Miguel Boyero Corral <juanmi1982@gmail.com>
*/
public class AlertNotificationFactory {
public static final String CHANNEL_NAME = "Background Service";
public static NotificationCompat.Builder createAlertNotificationBuilder(Context context, String contentTitle, String contentText,
String ticker, PendingIntent pendingIntent, int smallIcon, int largeIcon,
boolean autocancel, boolean ongoing, boolean onlyAlertOnce) {
int flags = 0;
NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(context, SWADROID_CHANNEL_ID)
.setAutoCancel(autocancel)
.setSmallIcon(smallIcon)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), largeIcon))
.setContentTitle(contentTitle)
.setContentText(contentText)
.setTicker(ticker)
.setOngoing(ongoing)
.setOnlyAlertOnce(onlyAlertOnce)
.setWhen(System.currentTimeMillis());
//.setLights(Color.GREEN, 500, 500);
//Launch activity on alert click
if (pendingIntent != null) {
notifBuilder.setContentIntent(pendingIntent);
}
//Add sound, vibration and lights
flags |= Notification.DEFAULT_SOUND;
flags |= Notification.DEFAULT_VIBRATE;
flags |= Notification.DEFAULT_LIGHTS;
notifBuilder.setDefaults(flags);
return notifBuilder;
}
public static NotificationCompat.Builder createProgressNotificationBuilder(Context context, String contentTitle, String contentText,
String ticker, PendingIntent pendingIntent, int smallIcon, int largeIcon,
boolean autocancel, boolean ongoing, boolean onlyAlertOnce, int maxProgress, int progress, boolean indeterminate) {
NotificationCompat.Builder notifBuilder = createAlertNotificationBuilder(context,
contentTitle,
contentText,
ticker,
pendingIntent,
smallIcon,
largeIcon,
autocancel,
ongoing,
onlyAlertOnce);
notifBuilder.setProgress(maxProgress, progress, indeterminate);
return notifBuilder;
}
public static Notification createAlertNotification(Context context, String contentTitle, String contentText,
String ticker, PendingIntent pendingIntent, int smallIcon, int largeIcon,
boolean autocancel, boolean ongoing, boolean onlyAlertOnce) {
NotificationCompat.Builder notifBuilder = createAlertNotificationBuilder(context,
contentTitle,
contentText,
ticker,
pendingIntent,
smallIcon,
largeIcon,
autocancel,
ongoing,
onlyAlertOnce);
//Create alert
return notifBuilder.build();
}
public static Notification createProgressNotification(Context context, String contentTitle, String contentText,
String ticker, PendingIntent pendingIntent, int smallIcon, int largeIcon,
boolean autocancel, boolean ongoing, boolean onlyAlertOnce, int maxProgress, int progress, boolean indeterminate) {
NotificationCompat.Builder notifBuilder = createProgressNotificationBuilder(context,
contentTitle,
contentText,
ticker,
pendingIntent,
smallIcon,
largeIcon,
autocancel,
ongoing,
onlyAlertOnce,
maxProgress,
progress,
indeterminate);
//Create alert
return notifBuilder.build();
}
public static Notification createBackgroundNotification(Context context, String contentTitle, int smallIcon, int largeIcon, PendingIntent pendingIntent) {
NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(context, SWADROID_CHANNEL_ID)
.setOngoing(true)
.setSmallIcon(smallIcon)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), largeIcon))
.setContentTitle(contentTitle)
.setPriority(NotificationManager.IMPORTANCE_HIGH)
.setCategory(Notification.CATEGORY_SERVICE)
.setContentIntent(pendingIntent);
//Create alert
return notifBuilder.build();
}
@RequiresApi(Build.VERSION_CODES.O)
public static void createNotificationChanel(Context context) {
NotificationChannel channel = new NotificationChannel(SWADROID_CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
channel.setLightColor(Color.BLUE);
manager.createNotificationChannel(channel);
}
public static void showAlertNotification(Context context, Notification notif, int notifId) {
NotificationManager notifManager;
//Obtain a reference to the notification service
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationUtils mNotificationUtils = new NotificationUtils(context);
notifManager = mNotificationUtils.getManager();
} else {
notifManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
}
//Send alert
notifManager.notify(notifId, notif);
}
}