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

424 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;
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.TimeUnit;
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 {
2022-04-28 18:41:53 +02:00
public static HashMap<String, String> since_ids = new HashMap<>();
2022-04-27 15:20:42 +02:00
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;
}
String last_notifid;
last_notifid = prefs.getString(context.getString(R.string.LAST_NOTIFICATION_ID) + slug, null);
2022-04-28 18:41:53 +02:00
if (since_ids.containsKey(slug)) {
String last_notifid_reccorded = since_ids.get(slug);
if (last_notifid_reccorded != null && last_notifid_reccorded.compareToIgnoreCase(last_notifid) == 0) {
return;
}
} else {
since_ids.put(slug, last_notifid);
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-01-07 17:47:57 +01: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
MastodonNotificationsService mastodonNotificationsService = init(context, slugArray[1]);
2022-04-28 18:41:53 +02:00
String finalLast_notifid = last_notifid;
2022-04-27 15:20:42 +02:00
new Thread(() -> {
Notifications notifications = new Notifications();
Call<List<Notification>> notificationsCall;
2022-04-28 18:41:53 +02:00
if (finalLast_notifid != null) {
notificationsCall = mastodonNotificationsService.getNotifications(accountDb.token, null, null, null, finalLast_notifid, null, 30);
2022-04-27 15:20:42 +02:00
} else {
notificationsCall = mastodonNotificationsService.getNotifications(accountDb.token, null, null, null, null, null, 5);
}
if (notificationsCall != null) {
try {
Response<List<Notification>> notificationsResponse = notificationsCall.execute();
if (notificationsResponse.isSuccessful()) {
2022-04-27 18:15:32 +02:00
notifications.notifications = notificationsResponse.body();
if (notifications.notifications != null) {
2022-04-28 18:41:53 +02:00
if (notifications.notifications.size() > 0) {
since_ids.put(slug, notifications.notifications.get(0).id);
}
2022-04-27 15:20:42 +02:00
}
2022-05-04 11:22:51 +02:00
notifications.pagination = MastodonHelper.getPagination(notificationsResponse.headers());
2022-04-27 15:20:42 +02:00
}
} catch (Exception e) {
2022-04-27 15:20:42 +02:00
e.printStackTrace();
}
}
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> onRetrieveNotifications(context, notifications, accountDb);
mainHandler.post(myRunnable);
}).start();
}
private static MastodonNotificationsService init(Context context, @NonNull String instance) {
final OkHttpClient okHttpClient = new OkHttpClient.Builder()
.readTimeout(60, TimeUnit.SECONDS)
.connectTimeout(60, TimeUnit.SECONDS)
.proxy(Helper.getProxy(context))
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://" + instance + "/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) {
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 18:15:32 +02:00
final String max_id = prefs.getString(context.getString(R.string.LAST_NOTIFICATION_ID) + key, null);
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) {
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) {
2022-05-20 10:26:03 +02:00
String lastNotif = prefs.getString(context.getString(R.string.LAST_NOTIFICATION_ID) + account.user_id + "@" + account.instance, null);
if (lastNotif == null || Helper.compareTo(notification.id, lastNotif) > 0) {
2022-05-20 10:26:03 +02:00
SharedPreferences.Editor editor = prefs.edit();
editor.putString(context.getString(R.string.LAST_NOTIFICATION_ID) + account.user_id + "@" + account.instance, notifications.get(0).id);
editor.commit();
since_ids.put(account.user_id + "@" + account.instance, lastNotif);
Helper.notify_user(context, account, intent, resource, finalNotifType, finalTitle, finalMessage);
2022-05-20 10:26:03 +02:00
}
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);
2022-05-20 10:26:03 +02:00
String lastNotif = prefs.getString(context.getString(R.string.LAST_NOTIFICATION_ID) + account.user_id + "@" + account.instance, null);
if (lastNotif == null || Helper.compareTo(notification.id, lastNotif) > 0) {
2022-05-20 10:26:03 +02:00
SharedPreferences.Editor editor = prefs.edit();
since_ids.put(account.user_id + "@" + account.instance, lastNotif);
editor.putString(context.getString(R.string.LAST_NOTIFICATION_ID) + account.user_id + "@" + account.instance, notifications.get(0).id);
editor.commit();
Helper.notify_user(context, account, intent, BitmapFactory.decodeResource(context.getResources(),
getMainLogo(context)), finalNotifType, finalTitle, finalMessage);
2022-05-20 10:26:03 +02:00
}
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
}
}