fedilab-Android-App/app/src/main/java/app/fedilab/android/mastodon/helper/NotificationsHelper.java

430 lines
25 KiB
Java
Raw Normal View History

2023-01-22 15:22:59 +01:00
package app.fedilab.android.mastodon.helper;
2022-04-27 15:20:42 +02:00
/* Copyright 2021 Thomas Schneider
*
* This file is a part of Fedilab
*
* This program 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.
*
* Fedilab 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 Fedilab; if not,
* see <http://www.gnu.org/licenses>. */
import static android.text.Html.FROM_HTML_MODE_LEGACY;
2023-01-22 15:22:59 +01:00
import static app.fedilab.android.mastodon.helper.LogoHelper.getMainLogo;
2022-04-27 15:20:42 +02:00
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.text.Html;
import android.text.SpannableString;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.preference.PreferenceManager;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
2023-07-27 14:46:05 +02:00
import java.net.IDN;
2022-04-27 15:20:42 +02:00
import java.util.ArrayList;
2022-04-28 18:41:53 +02:00
import java.util.HashMap;
2022-04-27 15:20:42 +02:00
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;
2022-04-27 15:20:42 +02:00
2022-06-30 14:44:52 +02:00
import app.fedilab.android.BaseMainActivity;
2022-04-27 15:20:42 +02:00
import app.fedilab.android.R;
2022-06-30 15:59:43 +02:00
import app.fedilab.android.activities.MainActivity;
2023-01-22 15:22:59 +01:00
import app.fedilab.android.mastodon.client.endpoints.MastodonNotificationsService;
import app.fedilab.android.mastodon.client.entities.api.Notification;
import app.fedilab.android.mastodon.client.entities.api.Notifications;
import app.fedilab.android.mastodon.client.entities.app.Account;
import app.fedilab.android.mastodon.client.entities.app.BaseAccount;
import app.fedilab.android.mastodon.exception.DBException;
import app.fedilab.android.mastodon.ui.drawer.StatusAdapter;
2022-04-27 15:20:42 +02:00
import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class NotificationsHelper {
2023-07-14 15:50:54 +02:00
public static HashMap<String, List<String>> pushed_notifications = new HashMap<>();
2022-04-27 15:20:42 +02:00
private static final HashMap<String, ReentrantLock> lockMap = new HashMap<>();
private static ReentrantLock getLock(String slug) {
synchronized (lockMap) {
if (lockMap.containsKey(slug)) {
return lockMap.get(slug);
}
ReentrantLock lock = new ReentrantLock();
lockMap.put(slug, lock);
return lock;
}
}
public static synchronized void task(Context context, String slug) throws DBException {
2022-04-27 15:20:42 +02:00
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context);
String[] slugArray = slug.split("@");
BaseAccount accountDb = new Account(context).getUniqAccount(slugArray[0], slugArray[1]);
2022-04-27 15:20:42 +02:00
if (accountDb == null) {
return;
}
2022-04-28 18:41:53 +02:00
2022-04-27 15:20:42 +02:00
//Check which notifications the user wants to see
boolean notif_follow = prefs.getBoolean(context.getString(R.string.SET_NOTIF_FOLLOW), true);
boolean notif_mention = prefs.getBoolean(context.getString(R.string.SET_NOTIF_MENTION), true);
boolean notif_share = prefs.getBoolean(context.getString(R.string.SET_NOTIF_SHARE), true);
boolean notif_poll = prefs.getBoolean(context.getString(R.string.SET_NOTIF_POLL), true);
boolean notif_fav = prefs.getBoolean(context.getString(R.string.SET_NOTIF_FAVOURITE), true);
2023-01-07 17:47:57 +01:00
boolean notif_status = prefs.getBoolean(context.getString(R.string.SET_NOTIF_STATUS), true);
boolean notif_updates = prefs.getBoolean(context.getString(R.string.SET_NOTIF_UPDATE), true);
boolean notif_signup = prefs.getBoolean(context.getString(R.string.SET_NOTIF_ADMIN_SIGNUP), true);
boolean notif_report = prefs.getBoolean(context.getString(R.string.SET_NOTIF_ADMIN_REPORT), true);
2022-04-27 15:20:42 +02:00
//User disagree with all notifications
2023-06-08 16:27:46 +02:00
if (!notif_follow && !notif_fav && !notif_mention && !notif_share && !notif_poll && !notif_status && !notif_updates && !notif_signup && !notif_report) {
2022-04-27 15:20:42 +02:00
return; //Nothing is done
2023-06-08 16:27:46 +02:00
}
2022-04-27 15:20:42 +02:00
new Thread(() -> {
ReentrantLock lock = getLock(slug);
try {
// fetch if we get the lock, or ignore, another thread is doing the job
if (lock.tryLock()) {
MastodonNotificationsService mastodonNotificationsService = init(context, slugArray[1]);
Notifications notifications = new Notifications();
Call<List<Notification>> notificationsCall;
String last_notif_id = prefs.getString(context.getString(R.string.LAST_NOTIFICATION_ID) + slug, null);
notificationsCall = mastodonNotificationsService.getNotifications(accountDb.token, null, null, null, last_notif_id, null, 30);
if (notificationsCall != null) {
try {
Response<List<Notification>> notificationsResponse = notificationsCall.execute();
if (notificationsResponse.isSuccessful()) {
notifications.notifications = notificationsResponse.body();
if (notifications.notifications != null) {
if (notifications.notifications.size() > 0) {
prefs.edit().putString(
context.getString(R.string.LAST_NOTIFICATION_ID) + slug,
notifications.notifications.get(0).id
).apply();
}
}
notifications.pagination = MastodonHelper.getPagination(notificationsResponse.headers());
2022-04-28 18:41:53 +02:00
}
} catch (Exception e) {
e.printStackTrace();
2022-04-27 15:20:42 +02:00
}
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> onRetrieveNotifications(context, notifications, accountDb, last_notif_id);
mainHandler.post(myRunnable);
2022-04-27 15:20:42 +02:00
}
}
} finally {
if (lock.isHeldByCurrentThread()) {
lock.unlock();
}
2022-04-27 15:20:42 +02:00
}
}).start();
}
2023-07-30 11:13:14 +02:00
private static MastodonNotificationsService init(Context context, String instance) {
2022-04-27 15:20:42 +02:00
2023-09-04 11:02:40 +02:00
final OkHttpClient okHttpClient = Helper.myOkHttpClient(context);
2022-04-27 15:20:42 +02:00
Retrofit retrofit = new Retrofit.Builder()
2023-07-30 11:13:14 +02:00
.baseUrl("https://" + (instance != null ? IDN.toASCII(instance, IDN.ALLOW_UNASSIGNED) : null) + "/api/v1/")
2022-07-02 11:01:14 +02:00
.addConverterFactory(GsonConverterFactory.create(Helper.getDateBuilder()))
2022-04-27 15:20:42 +02:00
.client(okHttpClient)
.build();
return retrofit.create(MastodonNotificationsService.class);
}
public static void onRetrieveNotifications(Context context, Notifications newNotifications, final BaseAccount account, String max_id) {
if (newNotifications == null || newNotifications.notifications == null || newNotifications.notifications.size() == 0 || account == null) {
2022-04-27 15:20:42 +02:00
return;
}
List<Notification> notificationsReceived = newNotifications.notifications;
2022-04-27 15:20:42 +02:00
String key = account.user_id + "@" + account.instance;
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context);
2022-04-27 15:20:42 +02:00
boolean notif_follow = prefs.getBoolean(context.getString(R.string.SET_NOTIF_FOLLOW), true);
boolean notif_mention = prefs.getBoolean(context.getString(R.string.SET_NOTIF_MENTION), true);
boolean notif_share = prefs.getBoolean(context.getString(R.string.SET_NOTIF_SHARE), true);
boolean notif_poll = prefs.getBoolean(context.getString(R.string.SET_NOTIF_POLL), true);
boolean notif_fav = prefs.getBoolean(context.getString(R.string.SET_NOTIF_FAVOURITE), true);
boolean notif_status = prefs.getBoolean(context.getString(R.string.SET_NOTIF_STATUS), true);
boolean notif_update = prefs.getBoolean(context.getString(R.string.SET_NOTIF_UPDATE), true);
boolean notif_signup = prefs.getBoolean(context.getString(R.string.SET_NOTIF_ADMIN_SIGNUP), true);
boolean notif_report = prefs.getBoolean(context.getString(R.string.SET_NOTIF_ADMIN_REPORT), true);
2022-04-27 15:20:42 +02:00
final List<Notification> notifications = new ArrayList<>();
int pos = 0;
for (Notification notif : notificationsReceived) {
if (max_id == null || Helper.compareTo(notif.id, max_id) > 0) {
2022-04-27 15:20:42 +02:00
notifications.add(pos, notif);
pos++;
}
}
if (notifications.size() == 0) {
2022-04-27 15:20:42 +02:00
return;
}
2022-04-27 15:20:42 +02:00
//No previous notifications in cache, so no notification will be sent
2022-05-20 10:26:03 +02:00
2022-04-27 15:20:42 +02:00
for (Notification notification : notifications) {
2023-07-14 15:50:54 +02:00
List<String> notificationIDList;
if (pushed_notifications.containsKey(key)) {
notificationIDList = pushed_notifications.get(key);
if (notificationIDList != null && notificationIDList.contains(notification.id)) {
continue;
}
}
notificationIDList = pushed_notifications.get(key);
if (notificationIDList == null) {
notificationIDList = new ArrayList<>();
}
notificationIDList.add(notification.id);
pushed_notifications.put(key, notificationIDList);
2022-05-20 10:26:03 +02:00
String notificationUrl;
String title = null;
String message = null;
String targeted_account = null;
Helper.NotifType notifType = Helper.NotifType.MENTION;
2022-04-27 15:20:42 +02:00
switch (notification.type) {
case "mention":
if (notif_mention) {
if (notification.account.display_name != null && notification.account.display_name.length() > 0)
2022-09-08 17:58:49 +02:00
title = String.format("%s %s", notification.account.display_name, context.getString(R.string.notif_mention));
2022-04-27 15:20:42 +02:00
else
2022-09-08 17:58:49 +02:00
title = String.format("@%s %s", notification.account.acct, context.getString(R.string.notif_mention));
2022-04-27 15:20:42 +02:00
if (notification.status != null) {
if (notification.status.spoiler_text != null && notification.status.spoiler_text.length() > 0) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
2022-09-08 17:58:49 +02:00
message = new SpannableString(Html.fromHtml(notification.status.spoiler_text, FROM_HTML_MODE_LEGACY)).toString();
2022-04-27 15:20:42 +02:00
else
2022-09-08 17:58:49 +02:00
message = new SpannableString(Html.fromHtml(notification.status.spoiler_text)).toString();
2022-04-27 15:20:42 +02:00
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
2022-09-08 17:58:49 +02:00
message = new SpannableString(Html.fromHtml(notification.status.content, FROM_HTML_MODE_LEGACY)).toString();
2022-04-27 15:20:42 +02:00
else
2022-09-08 17:58:49 +02:00
message = new SpannableString(Html.fromHtml(notification.status.content)).toString();
2022-04-27 15:20:42 +02:00
}
}
}
break;
case "status":
notifType = Helper.NotifType.STATUS;
if (notif_status) {
if (notification.account.display_name != null && notification.account.display_name.length() > 0)
2022-09-08 17:58:49 +02:00
title = String.format("%s %s", notification.account.display_name, context.getString(R.string.notif_status));
2022-04-27 15:20:42 +02:00
else
2022-09-08 17:58:49 +02:00
title = String.format("@%s %s", notification.account.acct, context.getString(R.string.notif_status));
2022-04-27 15:20:42 +02:00
if (notification.status != null) {
if (notification.status.spoiler_text != null && notification.status.spoiler_text.length() > 0) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
2022-09-08 17:58:49 +02:00
message = new SpannableString(Html.fromHtml(notification.status.spoiler_text, FROM_HTML_MODE_LEGACY)).toString();
2022-04-27 15:20:42 +02:00
else
2022-09-08 17:58:49 +02:00
message = new SpannableString(Html.fromHtml(notification.status.spoiler_text)).toString();
2022-04-27 15:20:42 +02:00
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
2022-09-08 17:58:49 +02:00
message = new SpannableString(Html.fromHtml(notification.status.content, FROM_HTML_MODE_LEGACY)).toString();
2022-04-27 15:20:42 +02:00
else
2022-09-08 17:58:49 +02:00
message = new SpannableString(Html.fromHtml(notification.status.content)).toString();
2022-04-27 15:20:42 +02:00
}
}
}
break;
case "reblog":
notifType = Helper.NotifType.BOOST;
if (notif_share) {
if (notification.account.display_name != null && notification.account.display_name.length() > 0)
2022-09-08 17:58:49 +02:00
title = String.format("%s %s", notification.account.display_name, context.getString(R.string.notif_reblog));
2022-04-27 15:20:42 +02:00
else
2022-09-08 17:58:49 +02:00
title = String.format("@%s %s", notification.account.acct, context.getString(R.string.notif_reblog));
if (notification.status != null) {
if (notification.status.spoiler_text != null && notification.status.spoiler_text.length() > 0) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
message = new SpannableString(Html.fromHtml(notification.status.spoiler_text, FROM_HTML_MODE_LEGACY)).toString();
else
message = new SpannableString(Html.fromHtml(notification.status.spoiler_text)).toString();
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
message = new SpannableString(Html.fromHtml(notification.status.content, FROM_HTML_MODE_LEGACY)).toString();
else
message = new SpannableString(Html.fromHtml(notification.status.content)).toString();
}
2022-09-08 17:58:49 +02:00
}
2022-04-27 15:20:42 +02:00
}
break;
case "favourite":
notifType = Helper.NotifType.FAV;
if (notif_fav) {
if (notification.account.display_name != null && notification.account.display_name.length() > 0)
2022-09-08 17:58:49 +02:00
title = String.format("%s %s", notification.account.display_name, context.getString(R.string.notif_favourite));
2022-04-27 15:20:42 +02:00
else
2022-09-08 17:58:49 +02:00
title = String.format("@%s %s", notification.account.acct, context.getString(R.string.notif_favourite));
if (notification.status != null) {
if (notification.status.spoiler_text != null && notification.status.spoiler_text.length() > 0) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
message = new SpannableString(Html.fromHtml(notification.status.spoiler_text, FROM_HTML_MODE_LEGACY)).toString();
else
message = new SpannableString(Html.fromHtml(notification.status.spoiler_text)).toString();
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
message = new SpannableString(Html.fromHtml(notification.status.content, FROM_HTML_MODE_LEGACY)).toString();
else
message = new SpannableString(Html.fromHtml(notification.status.content)).toString();
}
2022-09-08 17:58:49 +02:00
}
2022-04-27 15:20:42 +02:00
}
break;
case "follow_request":
notifType = Helper.NotifType.FOLLLOW;
if (notif_follow) {
title = context.getString(R.string.channel_notif_follow);
2022-04-27 15:20:42 +02:00
if (notification.account.display_name != null && notification.account.display_name.length() > 0)
message = String.format("%s %s", notification.account.display_name, context.getString(R.string.notif_follow_request));
else
message = String.format("@%s %s", notification.account.acct, context.getString(R.string.notif_follow_request));
targeted_account = notification.account.id;
}
break;
case "follow":
notifType = Helper.NotifType.FOLLLOW;
if (notif_follow) {
title = context.getString(R.string.channel_notif_follow);
2022-04-27 15:20:42 +02:00
if (notification.account.display_name != null && notification.account.display_name.length() > 0)
message = String.format("%s %s", notification.account.display_name, context.getString(R.string.notif_follow));
else
message = String.format("@%s %s", notification.account.acct, context.getString(R.string.notif_follow));
targeted_account = notification.account.id;
}
break;
case "poll":
notifType = Helper.NotifType.POLL;
if (notif_poll) {
2022-06-30 14:44:52 +02:00
if (notification.account.id != null && notification.account.id.equals(BaseMainActivity.currentUserID))
2022-09-08 17:58:49 +02:00
title = context.getString(R.string.notif_poll_self);
2022-04-27 15:20:42 +02:00
else
2022-09-08 17:58:49 +02:00
title = context.getString(R.string.notif_poll);
if (notification.status != null) {
if (notification.status.spoiler_text != null && notification.status.spoiler_text.length() > 0) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
message = new SpannableString(Html.fromHtml(notification.status.spoiler_text, FROM_HTML_MODE_LEGACY)).toString();
else
message = new SpannableString(Html.fromHtml(notification.status.spoiler_text)).toString();
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
message = new SpannableString(Html.fromHtml(notification.status.content, FROM_HTML_MODE_LEGACY)).toString();
else
message = new SpannableString(Html.fromHtml(notification.status.content)).toString();
}
}
2022-09-08 17:58:49 +02:00
}
break;
case "update":
notifType = Helper.NotifType.UPDATE;
if (notif_update) {
title = context.getString(R.string.notif_update_push);
if (notification.status != null) {
if (notification.status.spoiler_text != null && notification.status.spoiler_text.length() > 0) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
message = new SpannableString(Html.fromHtml(notification.status.spoiler_text, FROM_HTML_MODE_LEGACY)).toString();
else
message = new SpannableString(Html.fromHtml(notification.status.spoiler_text)).toString();
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
message = new SpannableString(Html.fromHtml(notification.status.content, FROM_HTML_MODE_LEGACY)).toString();
else
message = new SpannableString(Html.fromHtml(notification.status.content)).toString();
}
2022-09-08 17:58:49 +02:00
}
2022-04-27 15:20:42 +02:00
}
break;
case "admin.sign_up":
notifType = Helper.NotifType.SIGN_UP;
if (notif_signup) {
title = context.getString(R.string.notif_sign_up);
if (notification.account.display_name != null && notification.account.display_name.length() > 0)
message = String.format("%s %s", notification.account.display_name, context.getString(R.string.notif_signed_up));
else
message = String.format("@%s %s", notification.account.acct, context.getString(R.string.notif_signed_up));
targeted_account = notification.account.id;
}
break;
case "admin.report":
notifType = Helper.NotifType.REPORT;
if (notif_report) {
title = context.getString(R.string.notif_report);
if (notification.account.display_name != null && notification.account.display_name.length() > 0)
message = String.format("%s %s", notification.account.display_name, context.getString(R.string.notif_reported));
else
message = String.format("@%s %s", notification.account.acct, context.getString(R.string.notif_reported));
targeted_account = notification.account.id;
}
break;
2022-04-27 15:20:42 +02:00
default:
}
2022-05-20 10:26:03 +02:00
if (message != null) {
//Some others notification
2022-06-30 15:59:43 +02:00
final Intent intent = new Intent(context, MainActivity.class);
2022-05-20 10:26:03 +02:00
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Helper.INTENT_ACTION, Helper.NOTIFICATION_INTENT);
2023-01-23 15:01:44 +01:00
intent.putExtra(Helper.PREF_USER_ID, account.user_id);
if (targeted_account != null)
2022-05-20 10:26:03 +02:00
intent.putExtra(Helper.INTENT_TARGETED_ACCOUNT, targeted_account);
2023-01-24 18:08:58 +01:00
intent.putExtra(Helper.PREF_USER_INSTANCE, account.instance);
2022-05-20 10:26:03 +02:00
notificationUrl = notification.account.avatar;
Handler mainHandler = new Handler(Looper.getMainLooper());
final String finalNotificationUrl = notificationUrl;
Helper.NotifType finalNotifType = notifType;
String finalMessage = message;
String finalTitle = title;
StatusAdapter.sendAction(context, Helper.RECEIVE_REFRESH_NOTIFICATIONS_ACTION, null, null);
2022-05-20 10:26:03 +02:00
Runnable myRunnable = () -> Glide.with(context)
.asBitmap()
.load(finalNotificationUrl != null ? finalNotificationUrl : R.drawable.fedilab_logo_bubbles)
.into(new CustomTarget<Bitmap>() {
2022-05-20 10:26:03 +02:00
@Override
public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) {
2023-07-14 15:50:54 +02:00
Helper.notify_user(context, account, intent, resource, finalNotifType, finalTitle, finalMessage);
2022-04-27 15:20:42 +02:00
}
2022-05-20 10:26:03 +02:00
@Override
public void onLoadFailed(@Nullable Drawable errorDrawable) {
super.onLoadFailed(errorDrawable);
2023-07-14 15:50:54 +02:00
Helper.notify_user(context, account, intent, BitmapFactory.decodeResource(context.getResources(),
getMainLogo(context)), finalNotifType, finalTitle, finalMessage);
2022-04-27 15:20:42 +02:00
}
2022-05-20 10:26:03 +02:00
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
2022-04-27 15:20:42 +02:00
2022-05-20 10:26:03 +02:00
}
});
mainHandler.post(myRunnable);
2022-04-27 15:20:42 +02:00
2022-05-20 10:26:03 +02:00
}
2022-04-27 15:20:42 +02:00
}
2022-05-20 10:26:03 +02:00
2022-04-27 15:20:42 +02:00
}
}