2019-05-18 11:10:30 +02:00
|
|
|
package app.fedilab.android.services;
|
2017-11-12 08:33:24 +01:00
|
|
|
/* Copyright 2017 Thomas Schneider
|
|
|
|
*
|
2019-05-18 11:10:30 +02:00
|
|
|
* This file is a part of Fedilab
|
2017-11-12 08:33:24 +01: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-11-12 08:33:24 +01: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-11-12 08:33:24 +01:00
|
|
|
* see <http://www.gnu.org/licenses>. */
|
2017-12-17 14:20:04 +01:00
|
|
|
|
2019-08-30 17:41:15 +02:00
|
|
|
import android.app.NotificationChannel;
|
|
|
|
import android.app.NotificationManager;
|
2018-10-15 19:08:30 +02:00
|
|
|
import android.app.PendingIntent;
|
2018-01-03 15:25:35 +01:00
|
|
|
import android.app.Service;
|
2017-11-12 08:33:24 +01:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2018-10-16 18:33:32 +02:00
|
|
|
import android.content.IntentFilter;
|
2017-11-12 08:33:24 +01:00
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
2017-11-30 18:18:58 +01:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.BitmapFactory;
|
2018-10-13 16:25:08 +02:00
|
|
|
import android.net.Uri;
|
2018-11-27 18:18:05 +01:00
|
|
|
import android.os.Build;
|
2017-11-30 18:18:58 +01:00
|
|
|
import android.os.Bundle;
|
2017-12-02 14:17:06 +01:00
|
|
|
import android.os.Handler;
|
2017-11-12 08:33:24 +01:00
|
|
|
import android.os.IBinder;
|
2017-12-02 14:17:06 +01:00
|
|
|
import android.os.Looper;
|
2018-10-15 19:08:30 +02:00
|
|
|
import android.preference.PreferenceManager;
|
2019-08-31 15:14:20 +02:00
|
|
|
import android.text.Html;
|
|
|
|
import android.text.SpannableString;
|
2019-08-24 17:08:33 +02:00
|
|
|
|
2019-06-11 19:38:26 +02:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
2019-08-30 17:41:15 +02:00
|
|
|
import androidx.core.app.NotificationCompat;
|
2019-06-11 19:38:26 +02:00
|
|
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
2019-09-06 17:55:14 +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;
|
|
|
|
import com.bumptech.glide.request.target.SimpleTarget;
|
|
|
|
import com.bumptech.glide.request.target.Target;
|
|
|
|
import com.bumptech.glide.request.transition.Transition;
|
2018-12-08 15:43:12 +01:00
|
|
|
import com.koushikdutta.async.ByteBufferList;
|
|
|
|
import com.koushikdutta.async.DataEmitter;
|
2018-10-26 16:06:31 +02:00
|
|
|
import com.koushikdutta.async.callback.CompletedCallback;
|
2018-12-08 15:43:12 +01:00
|
|
|
import com.koushikdutta.async.callback.DataCallback;
|
2018-10-13 16:25:08 +02:00
|
|
|
import com.koushikdutta.async.http.AsyncHttpClient;
|
|
|
|
import com.koushikdutta.async.http.AsyncHttpRequest;
|
|
|
|
import com.koushikdutta.async.http.Headers;
|
|
|
|
import com.koushikdutta.async.http.WebSocket;
|
2017-11-30 18:18:58 +01:00
|
|
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
2018-11-27 18:18:05 +01:00
|
|
|
import java.security.KeyManagementException;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
2018-10-26 16:06:31 +02:00
|
|
|
import java.util.HashMap;
|
2018-12-19 10:34:04 +01:00
|
|
|
import java.util.Iterator;
|
2017-11-12 08:33:24 +01:00
|
|
|
import java.util.List;
|
2018-12-19 10:34:04 +01:00
|
|
|
import java.util.Map;
|
2019-04-17 17:56:33 +02:00
|
|
|
import java.util.Objects;
|
2017-11-30 18:18:58 +01:00
|
|
|
|
2019-05-18 11:10:30 +02:00
|
|
|
import app.fedilab.android.client.API;
|
|
|
|
import app.fedilab.android.client.Entities.Account;
|
|
|
|
import app.fedilab.android.client.Entities.Notification;
|
|
|
|
import app.fedilab.android.client.TLSSocketFactory;
|
|
|
|
import app.fedilab.android.helper.Helper;
|
|
|
|
import app.fedilab.android.sqlite.AccountDAO;
|
|
|
|
import app.fedilab.android.sqlite.Sqlite;
|
|
|
|
import app.fedilab.android.R;
|
|
|
|
import app.fedilab.android.activities.MainActivity;
|
2017-11-30 18:18:58 +01:00
|
|
|
|
2019-08-31 15:14:20 +02:00
|
|
|
import static androidx.core.text.HtmlCompat.FROM_HTML_MODE_LEGACY;
|
2019-09-25 15:44:52 +02:00
|
|
|
import static app.fedilab.android.helper.Helper.getMainLogo;
|
|
|
|
import static app.fedilab.android.helper.Helper.getNotificationIcon;
|
2019-08-31 15:14:20 +02:00
|
|
|
|
2017-11-12 08:33:24 +01:00
|
|
|
|
|
|
|
/**
|
2017-11-30 07:16:18 +01:00
|
|
|
* Created by Thomas on 29/11/2017.
|
2017-11-12 08:33:24 +01:00
|
|
|
* Manage service for streaming api and new notifications
|
|
|
|
*/
|
|
|
|
|
2018-10-16 18:33:32 +02:00
|
|
|
public class LiveNotificationService extends Service implements NetworkStateReceiver.NetworkStateReceiverListener {
|
2017-11-12 08:33:24 +01:00
|
|
|
|
2018-10-30 13:46:24 +01:00
|
|
|
static {
|
|
|
|
Helper.installProvider();
|
|
|
|
}
|
2019-09-06 17:55:14 +02:00
|
|
|
|
|
|
|
public static String CHANNEL_ID = "live_notifications";
|
2017-11-12 08:33:24 +01:00
|
|
|
protected Account account;
|
2019-09-06 17:55:14 +02:00
|
|
|
private static HashMap<String, Thread> threads = new HashMap<>();
|
|
|
|
private static HashMap<String, String> lastNotification = new HashMap<>();
|
2018-10-16 18:33:32 +02:00
|
|
|
private NetworkStateReceiver networkStateReceiver;
|
2018-12-08 15:43:12 +01:00
|
|
|
private static HashMap<String, WebSocket> webSocketFutures = new HashMap<>();
|
2019-09-01 18:11:57 +02:00
|
|
|
private NotificationChannel channel;
|
|
|
|
public static int totalAccount = 0;
|
|
|
|
public static int eventsCount = 0;
|
2018-01-24 16:27:52 +01:00
|
|
|
|
2017-11-12 08:33:24 +01:00
|
|
|
public void onCreate() {
|
|
|
|
super.onCreate();
|
2019-09-01 14:43:13 +02:00
|
|
|
|
2018-10-16 18:33:32 +02:00
|
|
|
networkStateReceiver = new NetworkStateReceiver();
|
|
|
|
networkStateReceiver.addListener(this);
|
2018-10-26 16:06:31 +02:00
|
|
|
registerReceiver(networkStateReceiver, new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION));
|
2019-09-26 18:58:43 +02:00
|
|
|
|
2019-11-01 11:24:21 +01:00
|
|
|
|
|
|
|
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= 26) {
|
|
|
|
channel = new NotificationChannel(CHANNEL_ID,
|
|
|
|
"Live notifications",
|
|
|
|
NotificationManager.IMPORTANCE_DEFAULT);
|
|
|
|
|
|
|
|
((NotificationManager) Objects.requireNonNull(getSystemService(Context.NOTIFICATION_SERVICE))).createNotificationChannel(channel);
|
|
|
|
}
|
|
|
|
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
|
|
|
List<Account> accountStreams = new AccountDAO(getApplicationContext(), db).getAllAccountCrossAction();
|
|
|
|
totalAccount = 0;
|
|
|
|
for (Account account : accountStreams) {
|
|
|
|
if (account.getSocial() == null || account.getSocial().equals("MASTODON") || account.getSocial().equals("PLEROMA")) {
|
|
|
|
boolean allowStream = sharedpreferences.getBoolean(Helper.SET_ALLOW_STREAM + account.getId() + account.getInstance(), true);
|
|
|
|
if (allowStream) {
|
|
|
|
totalAccount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( totalAccount > 0) {
|
|
|
|
Intent myIntent = new Intent(getApplicationContext(), MainActivity.class);
|
|
|
|
PendingIntent pendingIntent = PendingIntent.getActivity(
|
|
|
|
getApplicationContext(),
|
|
|
|
0,
|
|
|
|
myIntent,
|
|
|
|
PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
android.app.Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
|
|
|
|
.setContentTitle(getString(R.string.top_notification))
|
|
|
|
.setContentIntent(pendingIntent)
|
|
|
|
.setSmallIcon(getNotificationIcon(getApplicationContext()))
|
|
|
|
.setContentText(getString(R.string.top_notification_message, String.valueOf(totalAccount), String.valueOf(eventsCount))).build();
|
|
|
|
|
|
|
|
startForeground(1, notification);
|
|
|
|
}else{
|
|
|
|
stopSelf();
|
|
|
|
}
|
2019-10-14 17:37:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void startStream() {
|
|
|
|
|
|
|
|
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
boolean liveNotifications = sharedpreferences.getBoolean(Helper.SET_LIVE_NOTIFICATIONS, true);
|
|
|
|
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
|
|
|
if (liveNotifications) {
|
|
|
|
List<Account> accountStreams = new AccountDAO(getApplicationContext(), db).getAllAccountCrossAction();
|
|
|
|
if (accountStreams != null) {
|
|
|
|
for (final Account accountStream : accountStreams) {
|
|
|
|
if (accountStream.getSocial() == null || accountStream.getSocial().equals("MASTODON") || accountStream.getSocial().equals("PLEROMA")) {
|
|
|
|
startWork(accountStream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
|
|
|
|
|
|
|
if (intent == null || intent.getBooleanExtra("stop", false)) {
|
2019-10-18 18:08:39 +02:00
|
|
|
totalAccount = 0;
|
2019-10-31 14:53:49 +01:00
|
|
|
stopForeground(true);
|
|
|
|
stopSelf();
|
2019-10-14 17:37:38 +02:00
|
|
|
}
|
|
|
|
if( totalAccount > 0) {
|
|
|
|
return START_STICKY;
|
|
|
|
}
|
|
|
|
return START_NOT_STICKY;
|
2018-01-03 15:25:35 +01:00
|
|
|
}
|
|
|
|
|
2018-10-16 18:33:32 +02:00
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
|
|
|
networkStateReceiver.removeListener(this);
|
2018-10-26 16:06:31 +02:00
|
|
|
unregisterReceiver(networkStateReceiver);
|
2018-10-16 18:33:32 +02:00
|
|
|
}
|
|
|
|
|
2019-08-28 18:40:24 +02:00
|
|
|
|
2018-01-03 15:25:35 +01:00
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public IBinder onBind(Intent intent) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-10-15 19:08:30 +02:00
|
|
|
|
2017-11-12 08:33:24 +01:00
|
|
|
|
2018-10-13 16:25:08 +02:00
|
|
|
private void taks(Account account) {
|
2019-09-01 14:43:13 +02:00
|
|
|
|
2018-10-13 16:25:08 +02:00
|
|
|
if (account != null) {
|
|
|
|
Headers headers = new Headers();
|
|
|
|
headers.add("Authorization", "Bearer " + account.getToken());
|
|
|
|
headers.add("Connection", "Keep-Alive");
|
|
|
|
headers.add("method", "GET");
|
|
|
|
headers.add("scheme", "https");
|
2019-04-17 17:56:33 +02:00
|
|
|
String notif_url = "user:notification";
|
2019-09-06 17:55:14 +02:00
|
|
|
if (account.getSocial().toUpperCase().equals("PLEROMA"))
|
2019-04-17 17:56:33 +02:00
|
|
|
notif_url = "user";
|
2019-09-06 17:55:14 +02:00
|
|
|
String urlKey = "wss://" + account.getInstance() + "/api/v1/streaming/?stream=" + notif_url + "&access_token=" + account.getToken();
|
2018-10-26 16:06:31 +02:00
|
|
|
Uri url = Uri.parse(urlKey);
|
2018-10-13 16:25:08 +02:00
|
|
|
AsyncHttpRequest.setDefaultHeaders(headers, url);
|
2019-09-06 17:55:14 +02:00
|
|
|
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
|
2018-11-27 18:18:05 +01:00
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
AsyncHttpClient.getDefaultInstance().getSSLSocketMiddleware().setSSLContext(new TLSSocketFactory(account.getInstance()).getSSLContext());
|
2018-11-27 18:18:05 +01:00
|
|
|
AsyncHttpClient.getDefaultInstance().getSSLSocketMiddleware().setConnectAllAddresses(true);
|
2019-10-19 14:52:40 +02:00
|
|
|
} catch (KeyManagementException | NoSuchAlgorithmException e) {
|
2018-12-19 18:30:54 +01:00
|
|
|
e.printStackTrace();
|
2018-11-27 18:18:05 +01:00
|
|
|
}
|
|
|
|
}
|
2019-09-01 10:45:50 +02:00
|
|
|
String key = account.getAcct() + "@" + account.getInstance();
|
|
|
|
if (webSocketFutures.get(key) == null || !Objects.requireNonNull(webSocketFutures.get(key)).isOpen()) {
|
2019-09-06 17:55:14 +02:00
|
|
|
AsyncHttpClient.getDefaultInstance().websocket("wss://" + account.getInstance() + "/api/v1/streaming/?stream=" + notif_url + "&access_token=" + account.getToken(), "wss", new AsyncHttpClient.WebSocketConnectCallback() {
|
2019-09-01 10:45:50 +02:00
|
|
|
@Override
|
|
|
|
public void onCompleted(Exception ex, WebSocket webSocket) {
|
2019-09-06 17:55:14 +02:00
|
|
|
webSocketFutures.put(account.getAcct() + "@" + account.getInstance(), webSocket);
|
2019-09-01 10:45:50 +02:00
|
|
|
if (ex != null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
webSocket.setStringCallback(new WebSocket.StringCallback() {
|
|
|
|
public void onStringAvailable(String s) {
|
|
|
|
try {
|
|
|
|
JSONObject eventJson = new JSONObject(s);
|
|
|
|
onRetrieveStreaming(account, eventJson);
|
2019-09-06 17:55:14 +02:00
|
|
|
} catch (JSONException ignored) {
|
|
|
|
}
|
2019-09-01 10:45:50 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
webSocket.setClosedCallback(new CompletedCallback() {
|
2019-08-28 18:40:24 +02:00
|
|
|
@Override
|
2019-09-01 10:45:50 +02:00
|
|
|
public void onCompleted(Exception ex) {
|
2019-09-06 17:55:14 +02:00
|
|
|
if (networkStateReceiver.connected) {
|
2019-09-01 14:43:13 +02:00
|
|
|
startWork(account);
|
|
|
|
}
|
2019-08-28 18:40:24 +02:00
|
|
|
}
|
2019-09-01 10:45:50 +02:00
|
|
|
});
|
|
|
|
webSocket.setDataCallback(new DataCallback() {
|
|
|
|
public void onDataAvailable(DataEmitter emitter, ByteBufferList byteBufferList) {
|
|
|
|
// note that this data has been read
|
|
|
|
byteBufferList.recycle();
|
|
|
|
}
|
|
|
|
});
|
2018-10-13 18:52:56 +02:00
|
|
|
}
|
2019-09-01 10:45:50 +02:00
|
|
|
});
|
|
|
|
}
|
2017-11-30 18:18:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-06 17:55:14 +02:00
|
|
|
private void startWork(Account accountStream) {
|
2019-08-28 18:40:24 +02:00
|
|
|
|
2019-09-01 14:43:13 +02:00
|
|
|
String key = accountStream.getAcct() + "@" + accountStream.getInstance();
|
2019-09-06 17:55:14 +02:00
|
|
|
if (!threads.containsKey(key) || !Objects.requireNonNull(threads.get(key)).isAlive()) {
|
2019-09-01 14:43:13 +02:00
|
|
|
Thread thread = new Thread() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
taks(accountStream);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
thread.start();
|
|
|
|
threads.put(accountStream.getAcct() + "@" + accountStream.getInstance(), thread);
|
2019-08-28 18:40:24 +02:00
|
|
|
}
|
2019-09-01 14:43:13 +02:00
|
|
|
|
2019-08-28 18:40:24 +02:00
|
|
|
}
|
|
|
|
|
2018-10-13 16:25:08 +02:00
|
|
|
private void onRetrieveStreaming(Account account, JSONObject response) {
|
2019-09-06 17:55:14 +02:00
|
|
|
if (response == null)
|
2017-11-30 18:18:58 +01:00
|
|
|
return;
|
|
|
|
final Notification notification;
|
2018-12-19 10:34:04 +01:00
|
|
|
String dataId;
|
2017-11-30 18:18:58 +01:00
|
|
|
Bundle b = new Bundle();
|
2018-10-12 18:12:10 +02:00
|
|
|
boolean canSendBroadCast = true;
|
2018-12-28 08:59:19 +01:00
|
|
|
Helper.EventStreaming event;
|
2018-11-17 14:31:03 +01:00
|
|
|
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
|
2018-10-13 16:25:08 +02:00
|
|
|
try {
|
2018-10-13 17:41:20 +02:00
|
|
|
switch (response.get("event").toString()) {
|
|
|
|
case "notification":
|
2019-09-01 18:11:57 +02:00
|
|
|
eventsCount++;
|
|
|
|
if (Build.VERSION.SDK_INT >= 26) {
|
|
|
|
channel = new NotificationChannel(CHANNEL_ID,
|
|
|
|
"Live notifications",
|
|
|
|
NotificationManager.IMPORTANCE_DEFAULT);
|
|
|
|
((NotificationManager) Objects.requireNonNull(getSystemService(Context.NOTIFICATION_SERVICE))).createNotificationChannel(channel);
|
|
|
|
}
|
2019-10-31 14:59:00 +01:00
|
|
|
android.app.Notification notificationChannel = new NotificationCompat.Builder(this, CHANNEL_ID)
|
|
|
|
.setContentTitle(getString(R.string.top_notification))
|
|
|
|
.setSmallIcon(getNotificationIcon(getApplicationContext()))
|
|
|
|
.setContentText(getString(R.string.top_notification_message, String.valueOf(totalAccount), String.valueOf(eventsCount))).build();
|
|
|
|
|
|
|
|
startForeground(1, notificationChannel);
|
2018-10-13 17:41:20 +02:00
|
|
|
event = Helper.EventStreaming.NOTIFICATION;
|
|
|
|
notification = API.parseNotificationResponse(getApplicationContext(), new JSONObject(response.get("payload").toString()));
|
2019-09-06 17:55:14 +02:00
|
|
|
if (notification == null) {
|
2019-09-01 14:43:13 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-10-13 17:41:20 +02:00
|
|
|
b.putParcelable("data", notification);
|
|
|
|
boolean liveNotifications = sharedpreferences.getBoolean(Helper.SET_LIVE_NOTIFICATIONS, true);
|
|
|
|
boolean canNotify = Helper.canNotify(getApplicationContext());
|
|
|
|
boolean notify = sharedpreferences.getBoolean(Helper.SET_NOTIFY, true);
|
|
|
|
String targeted_account = null;
|
|
|
|
Helper.NotifType notifType = Helper.NotifType.MENTION;
|
2018-10-15 19:08:30 +02:00
|
|
|
boolean activityRunning = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("isMainActivityRunning", false);
|
2019-09-01 14:49:21 +02:00
|
|
|
String key = account.getAcct() + "@" + account.getInstance();
|
2019-09-06 17:55:14 +02:00
|
|
|
if (lastNotification.containsKey(key) && notification.getId().compareTo(Objects.requireNonNull(lastNotification.get(key))) <= 0) {
|
2019-09-01 14:49:21 +02:00
|
|
|
canNotify = false;
|
|
|
|
}
|
2019-09-25 18:22:19 +02:00
|
|
|
String lastNotif = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId() + account.getInstance(), null);
|
|
|
|
if (notification.getId().compareTo(Objects.requireNonNull(lastNotif)) <= 0) {
|
|
|
|
canNotify = false;
|
|
|
|
}
|
2019-09-06 17:55:14 +02:00
|
|
|
boolean allowStream = sharedpreferences.getBoolean(Helper.SET_ALLOW_STREAM + account.getId() + account.getInstance(), true);
|
|
|
|
if (!allowStream) {
|
2019-09-01 18:11:57 +02:00
|
|
|
canNotify = false;
|
|
|
|
}
|
2019-09-25 18:22:19 +02:00
|
|
|
|
|
|
|
|
2018-10-15 19:08:30 +02:00
|
|
|
if ((userId == null || !userId.equals(account.getId()) || !activityRunning) && liveNotifications && canNotify && notify) {
|
2019-09-01 14:49:21 +02:00
|
|
|
lastNotification.put(key, notification.getId());
|
2018-10-13 17:41:20 +02:00
|
|
|
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);
|
|
|
|
boolean somethingToPush = (notif_follow || notif_add || notif_mention || notif_share || notif_poll);
|
2019-08-31 15:04:20 +02:00
|
|
|
String message = null;
|
2019-09-01 14:43:13 +02:00
|
|
|
if (somethingToPush) {
|
2018-10-13 17:41:20 +02:00
|
|
|
switch (notification.getType()) {
|
|
|
|
case "mention":
|
|
|
|
notifType = Helper.NotifType.MENTION;
|
|
|
|
if (notif_mention) {
|
|
|
|
if (notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0)
|
2019-08-31 15:04:20 +02:00
|
|
|
message = String.format("%s %s", Helper.shortnameToUnicode(notification.getAccount().getDisplay_name(), true), getString(R.string.notif_mention));
|
2018-10-13 17:41:20 +02:00
|
|
|
else
|
2019-08-31 15:04:20 +02:00
|
|
|
message = String.format("@%s %s", notification.getAccount().getAcct(), getString(R.string.notif_mention));
|
2019-09-06 17:55:14 +02:00
|
|
|
if (notification.getStatus() != null) {
|
|
|
|
if (notification.getStatus().getSpoiler_text() != null && notification.getStatus().getSpoiler_text().length() > 0) {
|
2019-08-31 15:14:20 +02:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
|
|
|
|
message = "\n" + new SpannableString(Html.fromHtml(notification.getStatus().getSpoiler_text(), FROM_HTML_MODE_LEGACY));
|
|
|
|
else
|
|
|
|
message = "\n" + new SpannableString(Html.fromHtml(notification.getStatus().getSpoiler_text()));
|
2019-09-06 17:55:14 +02:00
|
|
|
} else {
|
2019-08-31 15:14:20 +02:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
|
|
|
|
message = "\n" + new SpannableString(Html.fromHtml(notification.getStatus().getContent(), FROM_HTML_MODE_LEGACY));
|
|
|
|
else
|
|
|
|
message = "\n" + new SpannableString(Html.fromHtml(notification.getStatus().getContent()));
|
2019-08-31 15:04:20 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-13 17:41:20 +02:00
|
|
|
} else {
|
|
|
|
canSendBroadCast = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "reblog":
|
|
|
|
notifType = Helper.NotifType.BOOST;
|
|
|
|
if (notif_share) {
|
|
|
|
if (notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0)
|
2019-08-31 15:04:20 +02:00
|
|
|
message = String.format("%s %s", Helper.shortnameToUnicode(notification.getAccount().getDisplay_name(), true), getString(R.string.notif_reblog));
|
2018-10-13 17:41:20 +02:00
|
|
|
else
|
2019-08-31 15:04:20 +02:00
|
|
|
message = String.format("@%s %s", notification.getAccount().getAcct(), getString(R.string.notif_reblog));
|
2018-10-13 17:41:20 +02:00
|
|
|
} else {
|
|
|
|
canSendBroadCast = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "favourite":
|
|
|
|
notifType = Helper.NotifType.FAV;
|
|
|
|
if (notif_add) {
|
|
|
|
if (notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0)
|
2019-08-31 15:04:20 +02:00
|
|
|
message = String.format("%s %s", Helper.shortnameToUnicode(notification.getAccount().getDisplay_name(), true), getString(R.string.notif_favourite));
|
2018-10-13 17:41:20 +02:00
|
|
|
else
|
2019-08-31 15:04:20 +02:00
|
|
|
message = String.format("@%s %s", notification.getAccount().getAcct(), getString(R.string.notif_favourite));
|
2018-10-13 17:41:20 +02:00
|
|
|
} else {
|
|
|
|
canSendBroadCast = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "follow":
|
|
|
|
notifType = Helper.NotifType.FOLLLOW;
|
|
|
|
if (notif_follow) {
|
|
|
|
if (notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0)
|
2019-08-31 15:04:20 +02:00
|
|
|
message = String.format("%s %s", Helper.shortnameToUnicode(notification.getAccount().getDisplay_name(), true), getString(R.string.notif_follow));
|
2018-10-13 17:41:20 +02:00
|
|
|
else
|
2019-08-31 15:04:20 +02:00
|
|
|
message = String.format("@%s %s", notification.getAccount().getAcct(), getString(R.string.notif_follow));
|
2018-10-13 17:41:20 +02:00
|
|
|
targeted_account = notification.getAccount().getId();
|
|
|
|
} else {
|
|
|
|
canSendBroadCast = false;
|
|
|
|
}
|
|
|
|
break;
|
2019-05-04 13:07:09 +02:00
|
|
|
case "poll":
|
|
|
|
notifType = Helper.NotifType.POLL;
|
|
|
|
if (notif_poll) {
|
|
|
|
if (notification.getAccount().getId() != null && notification.getAccount().getId().equals(userId))
|
2019-08-31 15:04:20 +02:00
|
|
|
message = getString(R.string.notif_poll_self);
|
2019-05-04 13:07:09 +02:00
|
|
|
else
|
2019-08-31 15:04:20 +02:00
|
|
|
message = getString(R.string.notif_poll);
|
2019-05-04 13:07:09 +02:00
|
|
|
} else {
|
|
|
|
canSendBroadCast = false;
|
|
|
|
}
|
|
|
|
break;
|
2018-10-13 17:41:20 +02:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
//Some others notification
|
|
|
|
final Intent intent = new Intent(getApplicationContext(), MainActivity.class);
|
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
|
2019-05-18 11:10:30 +02:00
|
|
|
intent.putExtra(Helper.INTENT_ACTION, Helper.NOTIFICATION_INTENT);
|
|
|
|
intent.putExtra(Helper.PREF_KEY_ID, account.getId());
|
2019-06-05 14:35:42 +02:00
|
|
|
intent.putExtra(Helper.PREF_INSTANCE, account.getInstance());
|
2018-10-24 15:19:33 +02:00
|
|
|
if (targeted_account != null) {
|
2019-05-18 11:10:30 +02:00
|
|
|
intent.putExtra(Helper.INTENT_TARGETED_ACCOUNT, targeted_account);
|
2018-10-24 15:19:33 +02:00
|
|
|
}
|
2019-08-31 15:04:20 +02:00
|
|
|
final String finalMessage = message;
|
2018-12-01 15:32:07 +01:00
|
|
|
Handler mainHandler = new Handler(Looper.getMainLooper());
|
|
|
|
Helper.NotifType finalNotifType = notifType;
|
|
|
|
Runnable myRunnable = new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2019-08-31 15:04:20 +02:00
|
|
|
if (finalMessage != null) {
|
2018-12-01 15:32:07 +01:00
|
|
|
Glide.with(getApplicationContext())
|
|
|
|
.asBitmap()
|
|
|
|
.load(notification.getAccount().getAvatar())
|
|
|
|
.listener(new RequestListener<Bitmap>() {
|
|
|
|
@Override
|
|
|
|
public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-10-13 17:41:20 +02:00
|
|
|
|
2018-12-01 15:32:07 +01:00
|
|
|
@Override
|
|
|
|
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
|
2019-09-06 17:55:14 +02:00
|
|
|
Helper.notify_user(getApplicationContext(), account, intent, BitmapFactory.decodeResource(getResources(),
|
2019-09-25 15:44:52 +02:00
|
|
|
getMainLogo(getApplicationContext())), finalNotifType, "@" + notification.getAccount().getAcct(), finalMessage);
|
2018-12-01 15:32:07 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.into(new SimpleTarget<Bitmap>() {
|
|
|
|
@Override
|
|
|
|
public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) {
|
2019-08-01 17:41:59 +02:00
|
|
|
|
2019-09-06 17:55:14 +02:00
|
|
|
Helper.notify_user(getApplicationContext(), account, intent, resource, finalNotifType, "@" + notification.getAccount().getAcct(), finalMessage);
|
2018-12-01 15:32:07 +01:00
|
|
|
}
|
|
|
|
});
|
2018-10-13 16:25:08 +02:00
|
|
|
}
|
2018-12-01 15:32:07 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
mainHandler.post(myRunnable);
|
2018-10-13 16:25:08 +02:00
|
|
|
}
|
2017-11-30 18:18:58 +01:00
|
|
|
}
|
2019-09-01 14:49:21 +02:00
|
|
|
|
2019-09-06 17:55:14 +02:00
|
|
|
if (canSendBroadCast) {
|
2019-09-01 10:45:50 +02:00
|
|
|
b.putString("userIdService", account.getId());
|
2018-12-23 10:43:45 +01:00
|
|
|
Intent intentBC = new Intent(Helper.RECEIVE_DATA);
|
|
|
|
intentBC.putExtra("eventStreaming", event);
|
|
|
|
intentBC.putExtras(b);
|
|
|
|
b.putParcelable("data", notification);
|
|
|
|
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intentBC);
|
2019-09-01 14:43:13 +02:00
|
|
|
SharedPreferences.Editor editor = sharedpreferences.edit();
|
|
|
|
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId() + account.getInstance(), notification.getId());
|
|
|
|
editor.apply();
|
2018-12-23 10:43:45 +01:00
|
|
|
}
|
2018-10-13 17:41:20 +02:00
|
|
|
break;
|
|
|
|
case "delete":
|
|
|
|
event = Helper.EventStreaming.DELETE;
|
|
|
|
try {
|
|
|
|
dataId = response.getString("id");
|
|
|
|
b.putString("dataId", dataId);
|
2019-09-06 17:55:14 +02:00
|
|
|
} catch (JSONException ignored) {
|
|
|
|
}
|
2018-10-13 17:41:20 +02:00
|
|
|
break;
|
2017-11-30 18:18:58 +01:00
|
|
|
}
|
2019-09-06 17:55:14 +02:00
|
|
|
} catch (Exception ignored) {
|
|
|
|
}
|
2017-11-30 18:18:58 +01:00
|
|
|
}
|
2017-11-12 08:33:24 +01:00
|
|
|
|
2018-10-16 18:33:32 +02:00
|
|
|
@Override
|
|
|
|
public void networkAvailable() {
|
2019-08-28 18:40:24 +02:00
|
|
|
startStream();
|
2018-10-16 18:33:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void networkUnavailable() {
|
2018-12-19 10:34:04 +01:00
|
|
|
Iterator it = threads.entrySet().iterator();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
Map.Entry pair = (Map.Entry) it.next();
|
|
|
|
if (pair.getValue() == null || !((Thread) pair.getValue()).isAlive()) {
|
|
|
|
if ((pair.getValue()) != null)
|
|
|
|
((Thread) pair.getValue()).interrupt();
|
|
|
|
}
|
|
|
|
it.remove();
|
|
|
|
}
|
2018-10-16 18:33:32 +02:00
|
|
|
}
|
2017-11-12 08:33:24 +01:00
|
|
|
}
|