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 '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
//Playstore

View File

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

View File

@ -22,9 +22,10 @@ import android.os.Build;
import android.os.Bundle;
import android.text.Html;
import android.text.SpannableString;
import android.util.Base64;
import android.util.Log;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.preference.PreferenceManager;
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.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.
@ -5114,7 +5119,6 @@ public class API {
private APIResponse getNotifications(DisplayNotificationsFragment.Type type, String max_id, String since_id, int limit, boolean display) {
HashMap<String, String> params = new HashMap<>();
if (MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.PIXELFED) {
params.put("pg", "true");
params.put("page", max_id);
@ -5858,6 +5862,13 @@ public class API {
boolean notif_status = sharedpreferences.getBoolean(Helper.SET_NOTIF_STATUS, 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();
String pubKey = ecdh.getPublicKey(context);
@ -5868,7 +5879,6 @@ public class API {
try {
JSONObject jsonObjectSub = new JSONObject();
jsonObjectSub.put("endpoint", endpoint);
JSONObject jsonObjectkey = new JSONObject();
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.BOOKMARK_CACHE;
import static app.fedilab.android.webview.ProxyHelper.setProxy;
import static cafe.adriel.androidaudiorecorder.Util.formatSeconds;
import static com.koushikdutta.async.util.StreamUtility.copyStream;
@ -598,19 +599,22 @@ public class BaseHelper {
public static void startStreaming(Context context) {
int liveNotifications = liveNotifType(context);
ApplicationJob.cancelAllJob(NotificationsSyncJob.NOTIFICATION_REFRESH);
NotificationsSyncJob.schedule(false);
switch (liveNotifications) {
case NOTIF_PUSH:
if (social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON || social == UpdateAccountInfoAsyncTask.SOCIAL.PLEROMA) {
new Registration().registerAppWithDialog(context);
ApplicationJob.cancelAllJob(NotificationsSyncJob.NOTIFICATION_REFRESH);
} else {
new Registration().unregisterApp(context);
NotificationsSyncJob.schedule(false);
}
new Thread(() -> {
SQLiteDatabase db = Sqlite.getInstance(context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
List<Account> accounts = new AccountDAO(context, db).getPushNotificationAccounts();
((Activity) context).runOnUiThread(() -> {
for (Account account : accounts) {
new Registration().registerAppWithDialog(context, account.getUsername() + "@" + account.getInstance());
}
});
}).start();
break;
case NOTIF_NONE:
new Registration().unregisterApp(context);
NotificationsSyncJob.schedule(false);
break;
}
}

View File

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

View File

@ -20,8 +20,12 @@ 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 android.util.Log;
import androidx.annotation.NonNull;
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.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_TARGETED_ACCOUNT;
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) {
List<Notification> notificationsReceived = apiResponse.getNotifications();
if (apiResponse.getError() != null || notificationsReceived == null || notificationsReceived.size() == 0 || account == null)
return;
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
@ -122,119 +126,118 @@ public class NotificationsHelper {
int newStatus = 0;
String notificationUrl = null;
String title = null;
final String message;
String message = null;
String targeted_account = null;
Helper.NotifType notifType = Helper.NotifType.MENTION;
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
for (Notification notification : notifications) {
switch (notification.getType()) {
case "mention":
notifType = Helper.NotifType.MENTION;
if (notif_mention) {
newMentions++;
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_mention));
else
title = String.format("@%s %s", notification.getAccount().getAcct(), context.getString(R.string.notif_mention));
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_mention));
else
message = String.format("@%s %s", notification.getAccount().getAcct(), context.getString(R.string.notif_mention));
if (notification.getStatus() != null) {
if (notification.getStatus().getSpoiler_text() != null && notification.getStatus().getSpoiler_text().length() > 0) {
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;
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(), context.getString(R.string.notif_status));
else
title = String.format("@%s %s", notification.getAccount().getAcct(), context.getString(R.string.notif_status));
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_status));
else
message = String.format("@%s %s", notification.getAccount().getAcct(), context.getString(R.string.notif_status));
if (notification.getStatus() != null) {
if (notification.getStatus().getSpoiler_text() != null && notification.getStatus().getSpoiler_text().length() > 0) {
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;
case "reblog":
notifType = Helper.NotifType.BOOST;
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++;
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;
case "favourite":
notifType = Helper.NotifType.FAV;
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++;
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;
case "follow_request":
notifType = Helper.NotifType.FOLLLOW;
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++;
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;
case "follow":
notifType = Helper.NotifType.FOLLLOW;
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++;
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;
case "poll":
notifType = Helper.NotifType.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++;
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;
default:
}
}
int allNotifCount = newFollows + newAdds + newMentions + newShare + newPolls + newStatus;
if (allNotifCount > 0) {
//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);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(INTENT_ACTION, NOTIFICATION_INTENT);
@ -242,12 +245,15 @@ public class NotificationsHelper {
if (targeted_account != null && notifType == Helper.NotifType.FOLLLOW)
intent.putExtra(INTENT_TARGETED_ACCOUNT, targeted_account);
intent.putExtra(PREF_INSTANCE, account.getInstance());
notificationUrl = notifications.get(0).getAccount().getAvatar();
if (notificationUrl != null) {
final String finalTitle = title;
Handler mainHandler = new Handler(Looper.getMainLooper());
final String finalNotificationUrl = notificationUrl;
Helper.NotifType finalNotifType = notifType;
String finalMessage = message;
String finalMessage1 = message;
Runnable myRunnable = () -> Glide.with(context)
.asBitmap()
.load(finalNotificationUrl)
@ -260,9 +266,8 @@ public class NotificationsHelper {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
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);
if (lastNotif == null || notifications.get(0).getId().compareTo(lastNotif) > 0) {
SharedPreferences.Editor editor = sharedpreferences.edit();
@ -275,7 +280,7 @@ public class NotificationsHelper {
.into(new CustomTarget<Bitmap>() {
@Override
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);
if (lastNotif == null || notifications.get(0).getId().compareTo(lastNotif) > 0) {
SharedPreferences.Editor editor = sharedpreferences.edit();

View File

@ -15,9 +15,6 @@ package app.fedilab.android.helper;
* see <http://www.gnu.org/licenses>. */
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import java.util.List;
import app.fedilab.android.asynctasks.PostSubscriptionAsyncTask;
import app.fedilab.android.client.Entities.Account;
@ -28,17 +25,13 @@ import app.fedilab.android.sqlite.Sqlite;
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();
List<Account> accountPush = new AccountDAO(context, db).getPushNotificationAccounts();
for (Account account : accountPush) {
new PostSubscriptionAsyncTask(context, account, endpoint);
}
String[] slugArray = slug.split("@");
Account account = new AccountDAO(context, db).getUniqAccountUsernameInstance(slugArray[0], slugArray[1]);
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.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.unifiedpush.android.connector.MessagingReceiver;
import org.unifiedpush.android.connector.MessagingReceiverHandler;
import java.util.List;
import app.fedilab.android.client.API;
import app.fedilab.android.client.APIResponse;
@ -41,48 +41,45 @@ class handler implements MessagingReceiverHandler {
@Override
public void onMessage(@Nullable Context context, @NotNull String s) {
SQLiteDatabase db = Sqlite.getInstance(context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
final List<Account> accounts = new AccountDAO(context, db).getPushNotificationAccounts();
public void onMessage(@Nullable Context context, @NotNull String s, @NotNull String slug) {
new Thread(() -> {
for (Account account : accounts) {
APIResponse apiResponse;
if (account.getSocial().compareTo("FRIENDICA") != 0 && account.getSocial().compareTo("GNU") != 0) {
API api = new API(context, account.getInstance(), account.getToken());
apiResponse = api.getNotificationsSince(DisplayNotificationsFragment.Type.ALL, null, false);
} else {
GNUAPI gnuApi = new GNUAPI(context, account.getInstance(), account.getToken());
apiResponse = gnuApi.getNotificationsSince(DisplayNotificationsFragment.Type.ALL, null);
}
NotificationsHelper.onRetrieveNotifications(context, apiResponse, account);
SQLiteDatabase db = Sqlite.getInstance(context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
String[] slugArray = slug.split("@");
Account account = new AccountDAO(context, db).getUniqAccountUsernameInstance(slugArray[0], slugArray[1]);
APIResponse apiResponse;
if (account.getSocial().compareTo("FRIENDICA") != 0 && account.getSocial().compareTo("GNU") != 0) {
API api = new API(context, account.getInstance(), account.getToken());
apiResponse = api.getNotificationsSince(DisplayNotificationsFragment.Type.ALL, null, false);
} else {
GNUAPI gnuApi = new GNUAPI(context, account.getInstance(), account.getToken());
apiResponse = gnuApi.getNotificationsSince(DisplayNotificationsFragment.Type.ALL, null);
}
NotificationsHelper.onRetrieveNotifications(context, apiResponse, account);
}).start();
}
@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);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.SERVER_ENDPOINT, endpoint);
editor.apply();
new PushNotifications()
.registerPushNotifications(context, endpoint);
.registerPushNotifications(context, endpoint, slug);
}
@Override
public void onRegistrationFailed(@Nullable Context context) {
public void onRegistrationFailed(@Nullable Context context, @NotNull String s) {
}
@Override
public void onRegistrationRefused(@Nullable Context context) {
public void onRegistrationRefused(@Nullable Context context, @NotNull String s) {
}
@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