2019-05-18 11:10:30 +02:00
|
|
|
package app.fedilab.android.jobs;
|
2017-08-29 16:22:57 +02:00
|
|
|
/* Copyright 2017 Thomas Schneider
|
|
|
|
*
|
2019-05-18 11:10:30 +02:00
|
|
|
* This file is a part of Fedilab
|
2017-08-29 16:22:57 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2019-05-18 11:10:30 +02:00
|
|
|
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
2017-08-29 16:22:57 +02:00
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
2019-05-18 11:10:30 +02:00
|
|
|
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
|
2017-08-29 16:22:57 +02:00
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.BitmapFactory;
|
2020-04-09 14:59:36 +02:00
|
|
|
import android.graphics.drawable.Drawable;
|
2018-01-18 19:51:33 +01:00
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.Looper;
|
2019-09-06 17:55:14 +02:00
|
|
|
|
2019-06-11 19:38:26 +02:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
2017-08-29 16:22:57 +02:00
|
|
|
|
2017-12-02 11:02:25 +01:00
|
|
|
import com.bumptech.glide.Glide;
|
|
|
|
import com.bumptech.glide.load.DataSource;
|
|
|
|
import com.bumptech.glide.load.engine.GlideException;
|
|
|
|
import com.bumptech.glide.request.RequestListener;
|
2020-04-09 14:59:36 +02:00
|
|
|
import com.bumptech.glide.request.target.CustomTarget;
|
2017-12-02 11:02:25 +01:00
|
|
|
import com.bumptech.glide.request.target.Target;
|
|
|
|
import com.bumptech.glide.request.transition.Transition;
|
2017-08-29 16:22:57 +02:00
|
|
|
import com.evernote.android.job.Job;
|
|
|
|
import com.evernote.android.job.JobManager;
|
|
|
|
import com.evernote.android.job.JobRequest;
|
|
|
|
|
2017-11-06 07:20:52 +01:00
|
|
|
import java.util.ArrayList;
|
2017-08-29 16:22:57 +02:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
2019-01-13 14:31:32 +01:00
|
|
|
|
2019-05-18 11:10:30 +02:00
|
|
|
import app.fedilab.android.R;
|
|
|
|
import app.fedilab.android.activities.MainActivity;
|
|
|
|
import app.fedilab.android.client.API;
|
|
|
|
import app.fedilab.android.client.APIResponse;
|
|
|
|
import app.fedilab.android.client.Entities.Account;
|
|
|
|
import app.fedilab.android.client.Entities.Notification;
|
2020-02-02 10:43:17 +01:00
|
|
|
import app.fedilab.android.client.GNUAPI;
|
2019-05-18 11:10:30 +02:00
|
|
|
import app.fedilab.android.fragments.DisplayNotificationsFragment;
|
|
|
|
import app.fedilab.android.helper.Helper;
|
|
|
|
import app.fedilab.android.sqlite.AccountDAO;
|
|
|
|
import app.fedilab.android.sqlite.Sqlite;
|
2017-08-29 16:22:57 +02:00
|
|
|
|
2019-05-18 11:10:30 +02:00
|
|
|
import static app.fedilab.android.helper.Helper.INTENT_ACTION;
|
|
|
|
import static app.fedilab.android.helper.Helper.INTENT_TARGETED_ACCOUNT;
|
|
|
|
import static app.fedilab.android.helper.Helper.NOTIFICATION_INTENT;
|
|
|
|
import static app.fedilab.android.helper.Helper.PREF_INSTANCE;
|
|
|
|
import static app.fedilab.android.helper.Helper.PREF_KEY_ID;
|
|
|
|
import static app.fedilab.android.helper.Helper.canNotify;
|
2019-09-25 15:44:52 +02:00
|
|
|
import static app.fedilab.android.helper.Helper.getMainLogo;
|
2019-05-18 11:10:30 +02:00
|
|
|
import static app.fedilab.android.helper.Helper.notify_user;
|
2017-08-29 16:22:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by Thomas on 29/04/2017.
|
|
|
|
* Notifications refresh job
|
|
|
|
*/
|
|
|
|
|
2020-07-14 17:25:10 +02:00
|
|
|
public class BaseNotificationsSyncJob extends Job {
|
2017-08-29 16:22:57 +02:00
|
|
|
|
2019-10-22 16:59:31 +02:00
|
|
|
public static final String NOTIFICATION_REFRESH = "job_notification";
|
2019-09-06 17:55:14 +02:00
|
|
|
|
2018-08-16 15:26:39 +02:00
|
|
|
public static int schedule(boolean updateCurrent) {
|
2017-08-29 16:22:57 +02:00
|
|
|
|
|
|
|
Set<JobRequest> jobRequests = JobManager.instance().getAllJobRequestsForTag(NOTIFICATION_REFRESH);
|
|
|
|
if (!jobRequests.isEmpty() && !updateCurrent) {
|
|
|
|
return jobRequests.iterator().next().getJobId();
|
|
|
|
}
|
|
|
|
|
2018-08-16 15:26:39 +02:00
|
|
|
int jobRequestschedule = -1;
|
|
|
|
try {
|
2020-07-14 17:25:10 +02:00
|
|
|
jobRequestschedule = new JobRequest.Builder(BaseNotificationsSyncJob.NOTIFICATION_REFRESH)
|
2018-08-16 15:26:39 +02:00
|
|
|
.setPeriodic(TimeUnit.MINUTES.toMillis(Helper.MINUTES_BETWEEN_NOTIFICATIONS_REFRESH), TimeUnit.MINUTES.toMillis(5))
|
|
|
|
.setUpdateCurrent(updateCurrent)
|
|
|
|
.setRequiredNetworkType(JobRequest.NetworkType.METERED)
|
|
|
|
.setRequirementsEnforced(false)
|
|
|
|
.build()
|
|
|
|
.schedule();
|
2019-09-06 17:55:14 +02:00
|
|
|
} catch (Exception ignored) {
|
|
|
|
}
|
2018-08-16 15:26:39 +02:00
|
|
|
|
|
|
|
return jobRequestschedule;
|
2017-08-29 16:22:57 +02:00
|
|
|
}
|
|
|
|
|
2019-11-15 16:32:25 +01:00
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
protected Result onRunJob(@NonNull Params params) {
|
|
|
|
//Code refresh here
|
|
|
|
callAsynchronousTask();
|
|
|
|
return Result.SUCCESS;
|
|
|
|
}
|
2017-08-29 16:22:57 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Task in background starts here.
|
|
|
|
*/
|
|
|
|
private void callAsynchronousTask() {
|
2019-09-06 17:55:14 +02:00
|
|
|
if (!canNotify(getContext()))
|
2017-08-29 16:22:57 +02:00
|
|
|
return;
|
2019-10-18 18:13:35 +02:00
|
|
|
int liveNotifications = Helper.liveNotifType(getContext());
|
2019-11-15 16:32:25 +01:00
|
|
|
if (liveNotifications != Helper.NOTIF_NONE) {
|
2019-10-18 18:13:35 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-04-09 18:57:12 +02:00
|
|
|
SQLiteDatabase db = Sqlite.getInstance(getContext().getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
2017-08-29 16:22:57 +02:00
|
|
|
//If an Internet connection and user agrees with notification refresh
|
|
|
|
final SharedPreferences sharedpreferences = getContext().getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
//Check which notifications the user wants to see
|
|
|
|
boolean notif_follow = sharedpreferences.getBoolean(Helper.SET_NOTIF_FOLLOW, true);
|
|
|
|
boolean notif_add = sharedpreferences.getBoolean(Helper.SET_NOTIF_ADD, true);
|
|
|
|
boolean notif_mention = sharedpreferences.getBoolean(Helper.SET_NOTIF_MENTION, true);
|
|
|
|
boolean notif_share = sharedpreferences.getBoolean(Helper.SET_NOTIF_SHARE, true);
|
2019-05-04 13:07:09 +02:00
|
|
|
boolean notif_poll = sharedpreferences.getBoolean(Helper.SET_NOTIF_POLL, true);
|
2017-08-29 16:22:57 +02:00
|
|
|
//User disagree with all notifications
|
2019-09-06 17:55:14 +02:00
|
|
|
if (!notif_follow && !notif_add && !notif_mention && !notif_share && !notif_poll)
|
2017-08-29 16:22:57 +02:00
|
|
|
return; //Nothing is done
|
|
|
|
//No account connected, the service is stopped
|
2019-09-06 17:55:14 +02:00
|
|
|
if (!Helper.isLoggedIn(getContext()))
|
2017-08-29 16:22:57 +02:00
|
|
|
return;
|
|
|
|
//If WIFI only and on WIFI OR user defined any connections to use the service.
|
2019-09-06 17:55:14 +02:00
|
|
|
if (!sharedpreferences.getBoolean(Helper.SET_WIFI_ONLY, false) || Helper.isOnWIFI(getContext())) {
|
|
|
|
List<Account> accounts = new AccountDAO(getContext(), db).getAllAccountCrossAction();
|
2017-08-29 16:22:57 +02:00
|
|
|
//It means there is no user in DB.
|
2019-09-06 17:55:14 +02:00
|
|
|
if (accounts == null)
|
2017-08-29 16:22:57 +02:00
|
|
|
return;
|
|
|
|
//Retrieve users in db that owner has.
|
2019-09-06 17:55:14 +02:00
|
|
|
for (Account account : accounts) {
|
2020-02-02 10:43:17 +01:00
|
|
|
APIResponse apiResponse;
|
2020-03-08 10:29:06 +01:00
|
|
|
if (account.getSocial().compareTo("FRIENDICA") != 0 && account.getSocial().compareTo("GNU") != 0) {
|
2019-01-13 14:31:32 +01:00
|
|
|
API api = new API(getContext(), account.getInstance(), account.getToken());
|
2020-02-02 10:43:17 +01:00
|
|
|
apiResponse = api.getNotificationsSince(DisplayNotificationsFragment.Type.ALL, null, false);
|
2020-03-08 10:29:06 +01:00
|
|
|
} else {
|
2020-02-02 10:43:17 +01:00
|
|
|
GNUAPI gnuApi = new GNUAPI(getContext(), account.getInstance(), account.getToken());
|
2020-03-08 11:50:09 +01:00
|
|
|
apiResponse = gnuApi.getNotificationsSince(DisplayNotificationsFragment.Type.ALL, null);
|
2019-01-13 14:31:32 +01:00
|
|
|
}
|
2020-02-02 10:43:17 +01:00
|
|
|
onRetrieveNotifications(apiResponse, account);
|
2017-08-29 16:22:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-16 18:26:10 +01:00
|
|
|
private void onRetrieveNotifications(APIResponse apiResponse, final Account account) {
|
2017-11-06 07:20:52 +01:00
|
|
|
List<Notification> notificationsReceived = apiResponse.getNotifications();
|
2019-09-06 17:55:14 +02:00
|
|
|
if (apiResponse.getError() != null || notificationsReceived == null || notificationsReceived.size() == 0 || account == null)
|
2017-08-29 16:22:57 +02:00
|
|
|
return;
|
|
|
|
final SharedPreferences sharedpreferences = getContext().getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
boolean notif_follow = sharedpreferences.getBoolean(Helper.SET_NOTIF_FOLLOW, true);
|
|
|
|
boolean notif_add = sharedpreferences.getBoolean(Helper.SET_NOTIF_ADD, true);
|
|
|
|
boolean notif_mention = sharedpreferences.getBoolean(Helper.SET_NOTIF_MENTION, true);
|
|
|
|
boolean notif_share = sharedpreferences.getBoolean(Helper.SET_NOTIF_SHARE, true);
|
2019-05-04 13:07:09 +02:00
|
|
|
boolean notif_poll = sharedpreferences.getBoolean(Helper.SET_NOTIF_POLL, true);
|
2021-01-23 09:39:39 +01:00
|
|
|
boolean notif_status = sharedpreferences.getBoolean(Helper.SET_NOTIF_STATUS, true);
|
2017-12-28 17:32:17 +01:00
|
|
|
final String max_id = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId() + account.getInstance(), null);
|
2019-09-06 17:55:14 +02:00
|
|
|
final List<Notification> notifications = new ArrayList<>();
|
2017-11-06 07:20:52 +01:00
|
|
|
int pos = 0;
|
2019-09-06 17:55:14 +02:00
|
|
|
for (Notification notif : notificationsReceived) {
|
|
|
|
if (max_id == null || notif.getId().compareTo(max_id) > 0) {
|
2017-11-06 07:20:52 +01:00
|
|
|
notifications.add(pos, notif);
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
}
|
2019-09-06 17:55:14 +02:00
|
|
|
if (notifications.size() == 0)
|
2017-11-06 18:30:52 +01:00
|
|
|
return;
|
2017-08-29 16:22:57 +02:00
|
|
|
//No previous notifications in cache, so no notification will be sent
|
|
|
|
int newFollows = 0;
|
|
|
|
int newAdds = 0;
|
|
|
|
int newMentions = 0;
|
|
|
|
int newShare = 0;
|
2019-05-04 13:07:09 +02:00
|
|
|
int newPolls = 0;
|
2021-01-23 09:39:39 +01:00
|
|
|
int newStatus = 0;
|
2017-08-29 16:22:57 +02:00
|
|
|
String notificationUrl = null;
|
|
|
|
String title = null;
|
|
|
|
final String message;
|
2018-01-06 14:07:49 +01:00
|
|
|
String targeted_account = null;
|
2018-09-16 18:03:24 +02:00
|
|
|
Helper.NotifType notifType = Helper.NotifType.MENTION;
|
|
|
|
|
2019-09-06 17:55:14 +02:00
|
|
|
for (Notification notification : notifications) {
|
|
|
|
switch (notification.getType()) {
|
2017-08-29 16:22:57 +02:00
|
|
|
case "mention":
|
2018-09-16 18:03:24 +02:00
|
|
|
notifType = Helper.NotifType.MENTION;
|
2019-09-06 17:55:14 +02:00
|
|
|
if (notif_mention) {
|
2017-08-29 16:22:57 +02:00
|
|
|
newMentions++;
|
2019-09-06 17:55:14 +02:00
|
|
|
if (notificationUrl == null) {
|
2017-08-29 16:22:57 +02:00
|
|
|
notificationUrl = notification.getAccount().getAvatar();
|
2019-09-06 17:55:14 +02:00
|
|
|
if (notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0)
|
2020-04-08 15:29:02 +02:00
|
|
|
title = String.format("%s %s", notification.getAccount().getDisplay_name(), getContext().getString(R.string.notif_mention));
|
2017-08-29 16:22:57 +02:00
|
|
|
else
|
2019-09-06 17:55:14 +02:00
|
|
|
title = String.format("@%s %s", notification.getAccount().getAcct(), getContext().getString(R.string.notif_mention));
|
2017-08-29 16:22:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2021-01-23 09:39:39 +01:00
|
|
|
case "status":
|
|
|
|
notifType = Helper.NotifType.STATUS;
|
|
|
|
if (notif_status) {
|
|
|
|
newStatus++;
|
|
|
|
if (notificationUrl == null) {
|
|
|
|
notificationUrl = notification.getAccount().getAvatar();
|
|
|
|
if (notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0)
|
|
|
|
title = String.format("%s %s", notification.getAccount().getDisplay_name(), getContext().getString(R.string.notif_mention));
|
|
|
|
else
|
|
|
|
title = String.format("@%s %s", notification.getAccount().getAcct(), getContext().getString(R.string.notif_mention));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2017-08-29 16:22:57 +02:00
|
|
|
case "reblog":
|
2018-09-16 18:03:24 +02:00
|
|
|
notifType = Helper.NotifType.BOOST;
|
2019-09-06 17:55:14 +02:00
|
|
|
if (notif_share) {
|
2017-08-29 16:22:57 +02:00
|
|
|
newShare++;
|
2019-09-06 17:55:14 +02:00
|
|
|
if (notificationUrl == null) {
|
2017-08-29 16:22:57 +02:00
|
|
|
notificationUrl = notification.getAccount().getAvatar();
|
2019-09-06 17:55:14 +02:00
|
|
|
if (notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0)
|
2020-04-08 15:29:02 +02:00
|
|
|
title = String.format("%s %s", notification.getAccount().getDisplay_name(), getContext().getString(R.string.notif_reblog));
|
2017-08-29 16:22:57 +02:00
|
|
|
else
|
2019-09-06 17:55:14 +02:00
|
|
|
title = String.format("@%s %s", notification.getAccount().getAcct(), getContext().getString(R.string.notif_reblog));
|
2017-08-29 16:22:57 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "favourite":
|
2018-09-16 18:03:24 +02:00
|
|
|
notifType = Helper.NotifType.FAV;
|
2019-09-06 17:55:14 +02:00
|
|
|
if (notif_add) {
|
2017-08-29 16:22:57 +02:00
|
|
|
newAdds++;
|
2019-09-06 17:55:14 +02:00
|
|
|
if (notificationUrl == null) {
|
2017-08-29 16:22:57 +02:00
|
|
|
notificationUrl = notification.getAccount().getAvatar();
|
2019-09-06 17:55:14 +02:00
|
|
|
if (notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0)
|
2020-04-08 15:29:02 +02:00
|
|
|
title = String.format("%s %s", notification.getAccount().getDisplay_name(), getContext().getString(R.string.notif_favourite));
|
2017-08-29 16:22:57 +02:00
|
|
|
else
|
2019-09-06 17:55:14 +02:00
|
|
|
title = String.format("@%s %s", notification.getAccount().getAcct(), getContext().getString(R.string.notif_favourite));
|
2017-08-29 16:22:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2019-12-17 10:24:25 +01:00
|
|
|
case "follow_request":
|
|
|
|
notifType = Helper.NotifType.FOLLLOW;
|
|
|
|
if (notif_follow) {
|
|
|
|
newFollows++;
|
|
|
|
if (notificationUrl == null) {
|
|
|
|
notificationUrl = notification.getAccount().getAvatar();
|
|
|
|
if (notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0)
|
2020-04-08 15:29:02 +02:00
|
|
|
title = String.format("%s %s", notification.getAccount().getDisplay_name(), getContext().getString(R.string.notif_follow_request));
|
2019-12-17 10:24:25 +01:00
|
|
|
else
|
|
|
|
title = String.format("@%s %s", notification.getAccount().getAcct(), getContext().getString(R.string.notif_follow_request));
|
|
|
|
targeted_account = notification.getAccount().getId();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2017-08-29 16:22:57 +02:00
|
|
|
case "follow":
|
2018-09-16 18:03:24 +02:00
|
|
|
notifType = Helper.NotifType.FOLLLOW;
|
2019-09-06 17:55:14 +02:00
|
|
|
if (notif_follow) {
|
2017-08-29 16:22:57 +02:00
|
|
|
newFollows++;
|
2019-09-06 17:55:14 +02:00
|
|
|
if (notificationUrl == null) {
|
2017-08-29 16:22:57 +02:00
|
|
|
notificationUrl = notification.getAccount().getAvatar();
|
2019-09-06 17:55:14 +02:00
|
|
|
if (notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0)
|
2020-04-08 15:29:02 +02:00
|
|
|
title = String.format("%s %s", notification.getAccount().getDisplay_name(), getContext().getString(R.string.notif_follow));
|
2017-08-29 16:22:57 +02:00
|
|
|
else
|
2019-09-06 17:55:14 +02:00
|
|
|
title = String.format("@%s %s", notification.getAccount().getAcct(), getContext().getString(R.string.notif_follow));
|
2018-01-06 14:07:49 +01:00
|
|
|
targeted_account = notification.getAccount().getId();
|
2017-08-29 16:22:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2019-05-04 13:07:09 +02:00
|
|
|
case "poll":
|
|
|
|
notifType = Helper.NotifType.POLL;
|
2019-09-06 17:55:14 +02:00
|
|
|
if (notif_poll) {
|
2019-05-04 13:07:09 +02:00
|
|
|
newPolls++;
|
2019-09-06 17:55:14 +02:00
|
|
|
if (notificationUrl == null) {
|
2019-05-04 13:07:09 +02:00
|
|
|
notificationUrl = notification.getAccount().getAvatar();
|
|
|
|
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
|
|
|
|
if (notification.getAccount().getId() != null && notification.getAccount().getId().equals(userId))
|
|
|
|
title = getContext().getString(R.string.notif_poll_self);
|
|
|
|
else
|
|
|
|
title = getContext().getString(R.string.notif_poll);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2017-08-29 16:22:57 +02:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
2017-09-03 15:06:07 +02:00
|
|
|
|
2021-01-23 09:39:39 +01:00
|
|
|
int allNotifCount = newFollows + newAdds + newMentions + newShare + newPolls + newStatus;
|
2019-09-06 17:55:14 +02:00
|
|
|
if (allNotifCount > 0) {
|
2017-08-29 16:22:57 +02:00
|
|
|
//Some others notification
|
2019-09-06 17:55:14 +02:00
|
|
|
int other = allNotifCount - 1;
|
|
|
|
if (other > 0)
|
2017-08-29 16:22:57 +02:00
|
|
|
message = getContext().getResources().getQuantityString(R.plurals.other_notifications, other, other);
|
|
|
|
else
|
|
|
|
message = "";
|
|
|
|
final Intent intent = new Intent(getContext(), MainActivity.class);
|
2019-09-06 17:55:14 +02:00
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
|
2017-08-29 16:22:57 +02:00
|
|
|
intent.putExtra(INTENT_ACTION, NOTIFICATION_INTENT);
|
2017-12-28 17:32:17 +01:00
|
|
|
intent.putExtra(PREF_KEY_ID, account.getId());
|
2019-09-06 17:55:14 +02:00
|
|
|
if (targeted_account != null && notifType == Helper.NotifType.FOLLLOW)
|
2018-01-06 14:07:49 +01:00
|
|
|
intent.putExtra(INTENT_TARGETED_ACCOUNT, targeted_account);
|
2017-12-28 17:32:17 +01:00
|
|
|
intent.putExtra(PREF_INSTANCE, account.getInstance());
|
2019-09-06 17:55:14 +02:00
|
|
|
if (notificationUrl != null) {
|
2017-08-29 16:22:57 +02:00
|
|
|
final String finalTitle = title;
|
2018-01-18 19:51:33 +01:00
|
|
|
Handler mainHandler = new Handler(Looper.getMainLooper());
|
2017-12-02 11:29:29 +01:00
|
|
|
|
2018-01-18 19:51:33 +01:00
|
|
|
final String finalNotificationUrl = notificationUrl;
|
2018-09-16 18:03:24 +02:00
|
|
|
Helper.NotifType finalNotifType = notifType;
|
2020-03-08 11:50:09 +01:00
|
|
|
Runnable myRunnable = () -> Glide.with(getContext())
|
|
|
|
.asBitmap()
|
|
|
|
.load(finalNotificationUrl)
|
|
|
|
.listener(new RequestListener<Bitmap>() {
|
2018-01-18 19:51:33 +01:00
|
|
|
|
2020-03-08 11:50:09 +01:00
|
|
|
@Override
|
|
|
|
public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-18 19:51:33 +01:00
|
|
|
|
2020-03-08 11:50:09 +01:00
|
|
|
@Override
|
|
|
|
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
|
2019-09-25 15:18:41 +02:00
|
|
|
|
2020-03-08 11:50:09 +01:00
|
|
|
notify_user(getContext(), account, intent, BitmapFactory.decodeResource(getContext().getResources(),
|
|
|
|
getMainLogo(getContext())), finalNotifType, finalTitle, message);
|
|
|
|
String lastNotif = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId() + account.getInstance(), null);
|
|
|
|
if (lastNotif == null || notifications.get(0).getId().compareTo(lastNotif) > 0) {
|
|
|
|
SharedPreferences.Editor editor = sharedpreferences.edit();
|
|
|
|
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId() + account.getInstance(), notifications.get(0).getId());
|
|
|
|
editor.apply();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
})
|
2020-04-09 14:59:36 +02:00
|
|
|
.into(new CustomTarget<Bitmap>() {
|
2020-03-08 11:50:09 +01:00
|
|
|
@Override
|
|
|
|
public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) {
|
|
|
|
notify_user(getContext(), account, intent, resource, finalNotifType, finalTitle, message);
|
|
|
|
String lastNotif = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId() + account.getInstance(), null);
|
|
|
|
if (lastNotif == null || notifications.get(0).getId().compareTo(lastNotif) > 0) {
|
|
|
|
SharedPreferences.Editor editor = sharedpreferences.edit();
|
|
|
|
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId() + account.getInstance(), notifications.get(0).getId());
|
|
|
|
editor.apply();
|
|
|
|
}
|
|
|
|
}
|
2020-04-09 14:59:36 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoadCleared(@Nullable Drawable placeholder) {
|
|
|
|
|
|
|
|
}
|
2020-03-08 11:50:09 +01:00
|
|
|
});
|
2018-01-18 19:51:33 +01:00
|
|
|
mainHandler.post(myRunnable);
|
2017-12-02 11:02:25 +01:00
|
|
|
|
2017-08-29 16:22:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|