working push

This commit is contained in:
Thomas 2021-03-01 11:12:01 +01:00
parent 7402ba0779
commit df51e7351c
9 changed files with 140 additions and 117 deletions

View File

@ -177,7 +177,7 @@ dependencies {
implementation "com.madgag.spongycastle:bctls-jdk15on:1.58.0.0" implementation "com.madgag.spongycastle:bctls-jdk15on:1.58.0.0"
//implementation 'org.bouncycastle:bcprov-jdk15on:1.64' //implementation 'org.bouncycastle:bcprov-jdk15on:1.64'
implementation 'com.github.UnifiedPush:android-connector:1.0.0' implementation 'com.github.p1gp1g:android-connector-1:1.1.0-beta1'
//Flavors //Flavors
//Playstore //Playstore

View File

@ -40,10 +40,7 @@ public class PostSubscriptionAsyncTask {
protected void doInBackground() { protected void doInBackground() {
new Thread(() -> { new Thread(() -> {
apiResponse = new API(contextReference.get(), account.getInstance(), account.getToken()).getPushSubscription(); apiResponse = new API(contextReference.get(), account.getInstance(), account.getToken()).pushSubscription(endpoint, account);
if (apiResponse == null || apiResponse.getPushSubscription() == null || endpoint.compareTo(apiResponse.getPushSubscription().getEndpoint()) != 0) {
apiResponse = new API(contextReference.get(), account.getInstance(), account.getToken()).pushSubscription(endpoint, account);
}
}).start(); }).start();
} }

View File

@ -22,9 +22,10 @@ import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.text.Html; import android.text.Html;
import android.text.SpannableString; import android.text.SpannableString;
import android.util.Base64; import android.util.Log;
import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.preference.PreferenceManager;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
@ -111,6 +112,10 @@ import app.fedilab.android.sqlite.AccountDAO;
import app.fedilab.android.sqlite.Sqlite; import app.fedilab.android.sqlite.Sqlite;
import app.fedilab.android.sqlite.TimelineCacheDAO; import app.fedilab.android.sqlite.TimelineCacheDAO;
import static app.fedilab.android.helper.ECDH.kp_private;
import static app.fedilab.android.helper.ECDH.kp_public;
import static app.fedilab.android.helper.ECDH.newPair;
/** /**
* Created by Thomas on 23/04/2017. * Created by Thomas on 23/04/2017.
@ -5114,7 +5119,6 @@ public class API {
private APIResponse getNotifications(DisplayNotificationsFragment.Type type, String max_id, String since_id, int limit, boolean display) { private APIResponse getNotifications(DisplayNotificationsFragment.Type type, String max_id, String since_id, int limit, boolean display) {
HashMap<String, String> params = new HashMap<>(); HashMap<String, String> params = new HashMap<>();
if (MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.PIXELFED) { if (MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.PIXELFED) {
params.put("pg", "true"); params.put("pg", "true");
params.put("page", max_id); params.put("page", max_id);
@ -5858,6 +5862,13 @@ public class API {
boolean notif_status = sharedpreferences.getBoolean(Helper.SET_NOTIF_STATUS, true); boolean notif_status = sharedpreferences.getBoolean(Helper.SET_NOTIF_STATUS, true);
boolean notif_poll = sharedpreferences.getBoolean(Helper.SET_NOTIF_POLL, true); boolean notif_poll = sharedpreferences.getBoolean(Helper.SET_NOTIF_POLL, true);
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context);
String strPub = prefs.getString(kp_public, "");
String strPriv = prefs.getString(kp_private, "");
if (strPub.trim().isEmpty() || strPriv.trim().isEmpty()) {
newPair(context);
}
ECDH ecdh = ECDH.getInstance(); ECDH ecdh = ECDH.getInstance();
String pubKey = ecdh.getPublicKey(context); String pubKey = ecdh.getPublicKey(context);
@ -5868,7 +5879,6 @@ public class API {
try { try {
JSONObject jsonObjectSub = new JSONObject(); JSONObject jsonObjectSub = new JSONObject();
jsonObjectSub.put("endpoint", endpoint); jsonObjectSub.put("endpoint", endpoint);
JSONObject jsonObjectkey = new JSONObject(); JSONObject jsonObjectkey = new JSONObject();
jsonObjectkey.put("p256dh", pubKey); jsonObjectkey.put("p256dh", pubKey);

View File

@ -240,6 +240,7 @@ import static app.fedilab.android.helper.ThemeHelper.getAttColor;
import static app.fedilab.android.sqlite.StatusCacheDAO.ARCHIVE_CACHE; import static app.fedilab.android.sqlite.StatusCacheDAO.ARCHIVE_CACHE;
import static app.fedilab.android.sqlite.StatusCacheDAO.BOOKMARK_CACHE; import static app.fedilab.android.sqlite.StatusCacheDAO.BOOKMARK_CACHE;
import static app.fedilab.android.webview.ProxyHelper.setProxy; import static app.fedilab.android.webview.ProxyHelper.setProxy;
import static cafe.adriel.androidaudiorecorder.Util.formatSeconds;
import static com.koushikdutta.async.util.StreamUtility.copyStream; import static com.koushikdutta.async.util.StreamUtility.copyStream;
@ -598,19 +599,22 @@ public class BaseHelper {
public static void startStreaming(Context context) { public static void startStreaming(Context context) {
int liveNotifications = liveNotifType(context); int liveNotifications = liveNotifType(context);
ApplicationJob.cancelAllJob(NotificationsSyncJob.NOTIFICATION_REFRESH);
NotificationsSyncJob.schedule(false);
switch (liveNotifications) { switch (liveNotifications) {
case NOTIF_PUSH: case NOTIF_PUSH:
if (social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON || social == UpdateAccountInfoAsyncTask.SOCIAL.PLEROMA) { new Thread(() -> {
new Registration().registerAppWithDialog(context); SQLiteDatabase db = Sqlite.getInstance(context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
ApplicationJob.cancelAllJob(NotificationsSyncJob.NOTIFICATION_REFRESH); List<Account> accounts = new AccountDAO(context, db).getPushNotificationAccounts();
} else { ((Activity) context).runOnUiThread(() -> {
new Registration().unregisterApp(context); for (Account account : accounts) {
NotificationsSyncJob.schedule(false); new Registration().registerAppWithDialog(context, account.getUsername() + "@" + account.getInstance());
} }
});
}).start();
break; break;
case NOTIF_NONE: case NOTIF_NONE:
new Registration().unregisterApp(context); new Registration().unregisterApp(context);
NotificationsSyncJob.schedule(false);
break; break;
} }
} }

View File

@ -18,6 +18,7 @@ import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.util.Base64; import android.util.Base64;
import android.util.Log;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
@ -54,11 +55,11 @@ import app.fedilab.android.client.Entities.Account;
public class ECDH { public class ECDH {
private static final String kp_public = "kp_public"; public static final String kp_public = "kp_public";
public static final String peer_public = "peer_public"; public static final String peer_public = "peer_public";
public static final String PROVIDER = org.spongycastle.jce.provider.BouncyCastleProvider.PROVIDER_NAME; public static final String PROVIDER = org.spongycastle.jce.provider.BouncyCastleProvider.PROVIDER_NAME;
private static final String kp_private = "kp_private"; public static final String kp_private = "kp_private";
public static final String KEGEN_ALG = "ECDH"; public static final String KEGEN_ALG = "ECDH";
private static final String kp_public_affine_x = "kp_public_affine_x"; private static final String kp_public_affine_x = "kp_public_affine_x";
@ -127,7 +128,7 @@ public class ECDH {
} }
@SuppressLint("ApplySharedPref") @SuppressLint("ApplySharedPref")
static KeyPair newPair(Context context) { public static KeyPair newPair(Context context) {
SharedPreferences.Editor prefsEditor = PreferenceManager SharedPreferences.Editor prefsEditor = PreferenceManager
.getDefaultSharedPreferences(context).edit(); .getDefaultSharedPreferences(context).edit();
KeyPair kp; KeyPair kp;
@ -214,7 +215,7 @@ public class ECDH {
.getDefaultSharedPreferences(context); .getDefaultSharedPreferences(context);
String strPub = prefs.getString(kp_public, ""); String strPub = prefs.getString(kp_public, "");
String strPriv = prefs.getString(kp_private, ""); String strPriv = prefs.getString(kp_private, "");
if (strPub.isEmpty() || strPriv.isEmpty()) { if (strPub.trim().isEmpty() || strPriv.trim().isEmpty()) {
return newPair(context); return newPair(context);
} }
try { try {

View File

@ -20,8 +20,12 @@ import android.content.SharedPreferences;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.text.Html;
import android.text.SpannableString;
import android.util.Log;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@ -46,6 +50,7 @@ import app.fedilab.android.client.Entities.Notification;
import app.fedilab.android.client.GNUAPI; import app.fedilab.android.client.GNUAPI;
import app.fedilab.android.fragments.DisplayNotificationsFragment; import app.fedilab.android.fragments.DisplayNotificationsFragment;
import static android.text.Html.FROM_HTML_MODE_LEGACY;
import static app.fedilab.android.helper.BaseHelper.INTENT_ACTION; import static app.fedilab.android.helper.BaseHelper.INTENT_ACTION;
import static app.fedilab.android.helper.BaseHelper.INTENT_TARGETED_ACCOUNT; import static app.fedilab.android.helper.BaseHelper.INTENT_TARGETED_ACCOUNT;
import static app.fedilab.android.helper.BaseHelper.NOTIFICATION_INTENT; import static app.fedilab.android.helper.BaseHelper.NOTIFICATION_INTENT;
@ -92,7 +97,6 @@ public class NotificationsHelper {
public static void onRetrieveNotifications(Context context, APIResponse apiResponse, final Account account) { public static void onRetrieveNotifications(Context context, APIResponse apiResponse, final Account account) {
List<Notification> notificationsReceived = apiResponse.getNotifications(); List<Notification> notificationsReceived = apiResponse.getNotifications();
if (apiResponse.getError() != null || notificationsReceived == null || notificationsReceived.size() == 0 || account == null) if (apiResponse.getError() != null || notificationsReceived == null || notificationsReceived.size() == 0 || account == null)
return; return;
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
@ -122,119 +126,118 @@ public class NotificationsHelper {
int newStatus = 0; int newStatus = 0;
String notificationUrl = null; String notificationUrl = null;
String title = null; String title = null;
final String message; String message = null;
String targeted_account = null; String targeted_account = null;
Helper.NotifType notifType = Helper.NotifType.MENTION; Helper.NotifType notifType = Helper.NotifType.MENTION;
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
for (Notification notification : notifications) { for (Notification notification : notifications) {
switch (notification.getType()) { switch (notification.getType()) {
case "mention": case "mention":
notifType = Helper.NotifType.MENTION; notifType = Helper.NotifType.MENTION;
if (notif_mention) { if (notif_mention) {
newMentions++; if (notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0)
if (notificationUrl == null) { message = String.format("%s %s", notification.getAccount().getDisplay_name(), context.getString(R.string.notif_mention));
notificationUrl = notification.getAccount().getAvatar(); else
if (notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0) message = String.format("@%s %s", notification.getAccount().getAcct(), context.getString(R.string.notif_mention));
title = String.format("%s %s", notification.getAccount().getDisplay_name(), context.getString(R.string.notif_mention)); if (notification.getStatus() != null) {
else if (notification.getStatus().getSpoiler_text() != null && notification.getStatus().getSpoiler_text().length() > 0) {
title = String.format("@%s %s", notification.getAccount().getAcct(), context.getString(R.string.notif_mention)); 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()));
} else {
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()));
}
} }
newFollows++;
} }
break; break;
case "status": case "status":
notifType = Helper.NotifType.STATUS; notifType = Helper.NotifType.STATUS;
if (notif_status) { if (notif_status) {
newStatus++; if (notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0)
if (notificationUrl == null) { message = String.format("%s %s", notification.getAccount().getDisplay_name(), context.getString(R.string.notif_status));
notificationUrl = notification.getAccount().getAvatar(); else
if (notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0) message = String.format("@%s %s", notification.getAccount().getAcct(), context.getString(R.string.notif_status));
title = String.format("%s %s", notification.getAccount().getDisplay_name(), context.getString(R.string.notif_status)); if (notification.getStatus() != null) {
else if (notification.getStatus().getSpoiler_text() != null && notification.getStatus().getSpoiler_text().length() > 0) {
title = String.format("@%s %s", notification.getAccount().getAcct(), context.getString(R.string.notif_status)); 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()));
} else {
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()));
}
} }
newStatus++;
} }
break; break;
case "reblog": case "reblog":
notifType = Helper.NotifType.BOOST; notifType = Helper.NotifType.BOOST;
if (notif_share) { if (notif_share) {
if (notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0)
message = String.format("%s %s", notification.getAccount().getDisplay_name(), context.getString(R.string.notif_reblog));
else
message = String.format("@%s %s", notification.getAccount().getAcct(), context.getString(R.string.notif_reblog));
newShare++; newShare++;
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(), context.getString(R.string.notif_reblog));
else
title = String.format("@%s %s", notification.getAccount().getAcct(), context.getString(R.string.notif_reblog));
}
} }
break; break;
case "favourite": case "favourite":
notifType = Helper.NotifType.FAV; notifType = Helper.NotifType.FAV;
if (notif_add) { if (notif_add) {
if (notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0)
message = String.format("%s %s", notification.getAccount().getDisplay_name(), context.getString(R.string.notif_favourite));
else
message = String.format("@%s %s", notification.getAccount().getAcct(), context.getString(R.string.notif_favourite));
newAdds++; newAdds++;
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(), context.getString(R.string.notif_favourite));
else
title = String.format("@%s %s", notification.getAccount().getAcct(), context.getString(R.string.notif_favourite));
}
} }
break; break;
case "follow_request": case "follow_request":
notifType = Helper.NotifType.FOLLLOW; notifType = Helper.NotifType.FOLLLOW;
if (notif_follow) { if (notif_follow) {
if (notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0)
message = String.format("%s %s", notification.getAccount().getDisplay_name(), context.getString(R.string.notif_follow_request));
else
message = String.format("@%s %s", notification.getAccount().getAcct(), context.getString(R.string.notif_follow_request));
targeted_account = notification.getAccount().getId();
newFollows++; newFollows++;
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(), context.getString(R.string.notif_follow_request));
else
title = String.format("@%s %s", notification.getAccount().getAcct(), context.getString(R.string.notif_follow_request));
targeted_account = notification.getAccount().getId();
}
} }
break; break;
case "follow": case "follow":
notifType = Helper.NotifType.FOLLLOW; notifType = Helper.NotifType.FOLLLOW;
if (notif_follow) { if (notif_follow) {
if (notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0)
message = String.format("%s %s", notification.getAccount().getDisplay_name(), context.getString(R.string.notif_follow));
else
message = String.format("@%s %s", notification.getAccount().getAcct(), context.getString(R.string.notif_follow));
targeted_account = notification.getAccount().getId();
newFollows++; newFollows++;
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(), context.getString(R.string.notif_follow));
else
title = String.format("@%s %s", notification.getAccount().getAcct(), context.getString(R.string.notif_follow));
targeted_account = notification.getAccount().getId();
}
} }
break; break;
case "poll": case "poll":
notifType = Helper.NotifType.POLL; notifType = Helper.NotifType.POLL;
if (notif_poll) { if (notif_poll) {
if (notification.getAccount().getId() != null && notification.getAccount().getId().equals(userId))
message = context.getString(R.string.notif_poll_self);
else
message = context.getString(R.string.notif_poll);
newPolls++; newPolls++;
if (notificationUrl == null) {
notificationUrl = notification.getAccount().getAvatar();
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
if (notification.getAccount().getId() != null && notification.getAccount().getId().equals(userId))
title = context.getString(R.string.notif_poll_self);
else
title = context.getString(R.string.notif_poll);
}
} }
break; break;
default: default:
} }
} }
int allNotifCount = newFollows + newAdds + newMentions + newShare + newPolls + newStatus; int allNotifCount = newFollows + newAdds + newMentions + newShare + newPolls + newStatus;
if (allNotifCount > 0) { if (allNotifCount > 0) {
//Some others notification //Some others notification
int other = allNotifCount - 1;
if (other > 0)
message = context.getResources().getQuantityString(R.plurals.other_notifications, other, other);
else
message = "";
final Intent intent = new Intent(context, MainActivity.class); final Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(INTENT_ACTION, NOTIFICATION_INTENT); intent.putExtra(INTENT_ACTION, NOTIFICATION_INTENT);
@ -242,12 +245,15 @@ public class NotificationsHelper {
if (targeted_account != null && notifType == Helper.NotifType.FOLLLOW) if (targeted_account != null && notifType == Helper.NotifType.FOLLLOW)
intent.putExtra(INTENT_TARGETED_ACCOUNT, targeted_account); intent.putExtra(INTENT_TARGETED_ACCOUNT, targeted_account);
intent.putExtra(PREF_INSTANCE, account.getInstance()); intent.putExtra(PREF_INSTANCE, account.getInstance());
notificationUrl = notifications.get(0).getAccount().getAvatar();
if (notificationUrl != null) { if (notificationUrl != null) {
final String finalTitle = title;
Handler mainHandler = new Handler(Looper.getMainLooper()); Handler mainHandler = new Handler(Looper.getMainLooper());
final String finalNotificationUrl = notificationUrl; final String finalNotificationUrl = notificationUrl;
Helper.NotifType finalNotifType = notifType; Helper.NotifType finalNotifType = notifType;
String finalMessage = message;
String finalMessage1 = message;
Runnable myRunnable = () -> Glide.with(context) Runnable myRunnable = () -> Glide.with(context)
.asBitmap() .asBitmap()
.load(finalNotificationUrl) .load(finalNotificationUrl)
@ -260,9 +266,8 @@ public class NotificationsHelper {
@Override @Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
notify_user(context, account, intent, BitmapFactory.decodeResource(context.getResources(), notify_user(context, account, intent, BitmapFactory.decodeResource(context.getResources(),
getMainLogo(context)), finalNotifType, finalTitle, message); getMainLogo(context)), finalNotifType, context.getString(R.string.top_notification), finalMessage1);
String lastNotif = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId() + account.getInstance(), null); String lastNotif = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId() + account.getInstance(), null);
if (lastNotif == null || notifications.get(0).getId().compareTo(lastNotif) > 0) { if (lastNotif == null || notifications.get(0).getId().compareTo(lastNotif) > 0) {
SharedPreferences.Editor editor = sharedpreferences.edit(); SharedPreferences.Editor editor = sharedpreferences.edit();
@ -275,7 +280,7 @@ public class NotificationsHelper {
.into(new CustomTarget<Bitmap>() { .into(new CustomTarget<Bitmap>() {
@Override @Override
public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) { public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) {
notify_user(context, account, intent, resource, finalNotifType, finalTitle, message); notify_user(context, account, intent, resource, finalNotifType, context.getString(R.string.top_notification), finalMessage);
String lastNotif = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId() + account.getInstance(), null); String lastNotif = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId() + account.getInstance(), null);
if (lastNotif == null || notifications.get(0).getId().compareTo(lastNotif) > 0) { if (lastNotif == null || notifications.get(0).getId().compareTo(lastNotif) > 0) {
SharedPreferences.Editor editor = sharedpreferences.edit(); SharedPreferences.Editor editor = sharedpreferences.edit();

View File

@ -15,9 +15,6 @@ package app.fedilab.android.helper;
* see <http://www.gnu.org/licenses>. */ * see <http://www.gnu.org/licenses>. */
import android.content.Context; import android.content.Context;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import java.util.List;
import app.fedilab.android.asynctasks.PostSubscriptionAsyncTask; import app.fedilab.android.asynctasks.PostSubscriptionAsyncTask;
import app.fedilab.android.client.Entities.Account; import app.fedilab.android.client.Entities.Account;
@ -28,17 +25,13 @@ import app.fedilab.android.sqlite.Sqlite;
public class PushNotifications { public class PushNotifications {
public void registerPushNotifications(Context context, String endpoint) { public void registerPushNotifications(Context context, String endpoint, String slug) {
SQLiteDatabase db = Sqlite.getInstance(context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); SQLiteDatabase db = Sqlite.getInstance(context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
List<Account> accountPush = new AccountDAO(context, db).getPushNotificationAccounts(); String[] slugArray = slug.split("@");
for (Account account : accountPush) { Account account = new AccountDAO(context, db).getUniqAccountUsernameInstance(slugArray[0], slugArray[1]);
new PostSubscriptionAsyncTask(context, account, endpoint); new PostSubscriptionAsyncTask(context, account, endpoint);
}
} }
public void displayNotification(Context context, String ciphered) {
}
} }

View File

@ -16,13 +16,13 @@ package app.fedilab.android.services;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.unifiedpush.android.connector.MessagingReceiver; import org.unifiedpush.android.connector.MessagingReceiver;
import org.unifiedpush.android.connector.MessagingReceiverHandler; import org.unifiedpush.android.connector.MessagingReceiverHandler;
import java.util.List;
import app.fedilab.android.client.API; import app.fedilab.android.client.API;
import app.fedilab.android.client.APIResponse; import app.fedilab.android.client.APIResponse;
@ -41,48 +41,45 @@ class handler implements MessagingReceiverHandler {
@Override @Override
public void onMessage(@Nullable Context context, @NotNull String s) { public void onMessage(@Nullable Context context, @NotNull String s, @NotNull String slug) {
SQLiteDatabase db = Sqlite.getInstance(context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
final List<Account> accounts = new AccountDAO(context, db).getPushNotificationAccounts();
new Thread(() -> { new Thread(() -> {
for (Account account : accounts) { SQLiteDatabase db = Sqlite.getInstance(context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
APIResponse apiResponse; String[] slugArray = slug.split("@");
if (account.getSocial().compareTo("FRIENDICA") != 0 && account.getSocial().compareTo("GNU") != 0) { Account account = new AccountDAO(context, db).getUniqAccountUsernameInstance(slugArray[0], slugArray[1]);
API api = new API(context, account.getInstance(), account.getToken()); APIResponse apiResponse;
apiResponse = api.getNotificationsSince(DisplayNotificationsFragment.Type.ALL, null, false); if (account.getSocial().compareTo("FRIENDICA") != 0 && account.getSocial().compareTo("GNU") != 0) {
} else { API api = new API(context, account.getInstance(), account.getToken());
GNUAPI gnuApi = new GNUAPI(context, account.getInstance(), account.getToken()); apiResponse = api.getNotificationsSince(DisplayNotificationsFragment.Type.ALL, null, false);
apiResponse = gnuApi.getNotificationsSince(DisplayNotificationsFragment.Type.ALL, null); } else {
} GNUAPI gnuApi = new GNUAPI(context, account.getInstance(), account.getToken());
NotificationsHelper.onRetrieveNotifications(context, apiResponse, account); apiResponse = gnuApi.getNotificationsSince(DisplayNotificationsFragment.Type.ALL, null);
} }
NotificationsHelper.onRetrieveNotifications(context, apiResponse, account);
}).start(); }).start();
} }
@Override @Override
public void onNewEndpoint(@Nullable Context context, @NotNull String endpoint) { public void onNewEndpoint(@Nullable Context context, @NotNull String endpoint, @NotNull String slug) {
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE); final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit(); SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.SERVER_ENDPOINT, endpoint); editor.putString(Helper.SERVER_ENDPOINT, endpoint);
editor.apply(); editor.apply();
new PushNotifications() new PushNotifications()
.registerPushNotifications(context, endpoint); .registerPushNotifications(context, endpoint, slug);
} }
@Override @Override
public void onRegistrationFailed(@Nullable Context context) { public void onRegistrationFailed(@Nullable Context context, @NotNull String s) {
} }
@Override @Override
public void onRegistrationRefused(@Nullable Context context) { public void onRegistrationRefused(@Nullable Context context, @NotNull String s) {
} }
@Override @Override
public void onUnregistered(@Nullable Context context) { public void onUnregistered(@Nullable Context context, @NotNull String s) {
} }
} }

View File

@ -327,6 +327,22 @@ public class AccountDAO {
} }
} }
/**
* Returns an Account by token
*
* @param userName String
* @param instance String
* @return Account
*/
public Account getUniqAccountUsernameInstance(String userName, String instance) {
try {
Cursor c = db.query(Sqlite.TABLE_USER_ACCOUNT, null, Sqlite.COL_USERNAME + " = \"" + userName + "\" AND " + Sqlite.COL_INSTANCE + " = \"" + instance + "\"", null, null, null, null, "1");
return cursorToUser(c);
} catch (Exception e) {
return null;
}
}
/** /**
* Test if the current user is already stored in data base * Test if the current user is already stored in data base