some tests

This commit is contained in:
Thomas 2021-02-23 18:34:58 +01:00
parent 17d4325303
commit 3ee5d00812
10 changed files with 100 additions and 42 deletions

View File

@ -177,6 +177,7 @@ dependencies {
implementation "com.madgag.spongycastle:prov:1.58.0.0"
implementation "com.madgag.spongycastle:bcpkix-jdk15on:1.58.0.0"
implementation "com.madgag.spongycastle:bcpg-jdk15on:1.58.0.0"
implementation 'com.github.UnifiedPush:android-connector:1.0.0'
//Flavors

View File

@ -460,6 +460,8 @@
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
<receiver
android:name=".services.UnifiedPushService"
android:enabled="true"

View File

@ -139,7 +139,6 @@ import app.fedilab.android.helper.ExpandableHeightListView;
import app.fedilab.android.helper.Helper;
import app.fedilab.android.helper.MenuFloating;
import app.fedilab.android.interfaces.OnFilterActionInterface;
import app.fedilab.android.interfaces.OnPostSubscription;
import app.fedilab.android.interfaces.OnRetrieveEmojiAccountInterface;
import app.fedilab.android.interfaces.OnRetrieveFeedsInterface;
import app.fedilab.android.interfaces.OnRetrieveInstanceInterface;
@ -164,7 +163,7 @@ import static app.fedilab.android.helper.Helper.changeDrawableColor;
public abstract class BaseMainActivity extends BaseActivity
implements NavigationView.OnNavigationItemSelectedListener, OnRetrieveFeedsInterface, OnUpdateAccountInfoInterface, OnRetrieveMetaDataInterface, OnRetrieveInstanceInterface, OnRetrieveRemoteAccountInterface, OnRetrieveEmojiAccountInterface, OnFilterActionInterface, OnSyncTimelineInterface, OnRetrieveRelationshipInterface, OnPostSubscription {
implements NavigationView.OnNavigationItemSelectedListener, OnRetrieveFeedsInterface, OnUpdateAccountInfoInterface, OnRetrieveMetaDataInterface, OnRetrieveInstanceInterface, OnRetrieveRemoteAccountInterface, OnRetrieveEmojiAccountInterface, OnFilterActionInterface, OnSyncTimelineInterface, OnRetrieveRelationshipInterface {
public static String currentLocale;
@ -424,12 +423,7 @@ public abstract class BaseMainActivity extends BaseActivity
if (social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON || social == UpdateAccountInfoAsyncTask.SOCIAL.PLEROMA) {
String serverKey = sharedpreferences.getString(Helper.SERVER_KEY + userId + instance, null);
if (serverKey == null) {
new PostSubscriptionAsyncTask(BaseMainActivity.this, this);
}
new PostSubscriptionAsyncTask(BaseMainActivity.this, this);
// Registration registration = new Registration();
new Registration().registerAppWithDialog(BaseMainActivity.this);
}
if (social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON || social == UpdateAccountInfoAsyncTask.SOCIAL.PLEROMA || social == UpdateAccountInfoAsyncTask.SOCIAL.GNU || social == UpdateAccountInfoAsyncTask.SOCIAL.FRIENDICA) {
@ -456,26 +450,26 @@ public abstract class BaseMainActivity extends BaseActivity
Helper.changeDrawableColor(BaseMainActivity.this, R.drawable.ic_home, R.attr.iconColorMenu);
@SuppressWarnings("ConstantConditions") @SuppressLint("CutPasteId")
@SuppressLint("CutPasteId")
ImageView iconSub = pTabsub.getCustomView().findViewById(R.id.tab_icon);
iconSub.setImageResource(R.drawable.ic_subscriptions);
@SuppressWarnings("ConstantConditions") @SuppressLint("CutPasteId")
@SuppressLint("CutPasteId")
ImageView iconOver = pTabOver.getCustomView().findViewById(R.id.tab_icon);
iconOver.setImageResource(R.drawable.ic_overview);
@SuppressWarnings("ConstantConditions") @SuppressLint("CutPasteId")
@SuppressLint("CutPasteId")
ImageView iconTrend = pTabTrend.getCustomView().findViewById(R.id.tab_icon);
iconTrend.setImageResource(R.drawable.ic_trending_up);
@SuppressWarnings("ConstantConditions") @SuppressLint("CutPasteId")
@SuppressLint("CutPasteId")
ImageView iconAdded = pTabAdded.getCustomView().findViewById(R.id.tab_icon);
iconAdded.setImageResource(R.drawable.ic_recently_added);
@SuppressWarnings("ConstantConditions") @SuppressLint("CutPasteId")
@SuppressLint("CutPasteId")
ImageView iconLocal = pTabLocal.getCustomView().findViewById(R.id.tab_icon);
iconLocal.setImageResource(R.drawable.ic_home);
@ -557,18 +551,18 @@ public abstract class BaseMainActivity extends BaseActivity
//pfTabDiscover.setCustomView(R.layout.tab_badge);
@SuppressWarnings("ConstantConditions") @SuppressLint("CutPasteId")
@SuppressLint("CutPasteId")
ImageView iconHome = pfTabHome.getCustomView().findViewById(R.id.tab_icon);
iconHome.setImageResource(R.drawable.ic_home);
@SuppressWarnings("ConstantConditions") @SuppressLint("CutPasteId")
@SuppressLint("CutPasteId")
ImageView iconLocal = pfTabLocal.getCustomView().findViewById(R.id.tab_icon);
iconLocal.setImageResource(R.drawable.ic_people);
@SuppressWarnings("ConstantConditions") @SuppressLint("CutPasteId")
@SuppressLint("CutPasteId")
ImageView iconNotif = pfTabNotification.getCustomView().findViewById(R.id.tab_icon);
iconNotif.setImageResource(R.drawable.ic_notifications);
@ -576,7 +570,7 @@ public abstract class BaseMainActivity extends BaseActivity
ImageView iconDiscover = pfTabDiscover.getCustomView().findViewById(R.id.tab_icon);
iconDiscover.setImageResource(R.drawable.ic_people);*/
@SuppressWarnings("ConstantConditions") @SuppressLint("CutPasteId")
@SuppressLint("CutPasteId")
ImageView iconStories = pfStories.getCustomView().findViewById(R.id.tab_icon);
iconStories.setImageResource(R.drawable.ic_story);
@ -1707,10 +1701,6 @@ public abstract class BaseMainActivity extends BaseActivity
@Override
public void onDestroy() {
super.onDestroy();
if (social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON || social == UpdateAccountInfoAsyncTask.SOCIAL.PLEROMA) {
new Registration().unregisterApp(BaseMainActivity.this);
}
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
boolean clearCacheExit = sharedpreferences.getBoolean(Helper.SET_CLEAR_CACHE_EXIT, false);
WeakReference<Context> contextReference = new WeakReference<>(BaseMainActivity.this);
@ -2440,17 +2430,6 @@ public abstract class BaseMainActivity extends BaseActivity
protected abstract void launchOwnerNotificationsActivity();
@Override
public void onSubscription(APIResponse apiResponse) {
if (apiResponse != null && apiResponse.getPushSubscription() != null) {
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
String instance = sharedpreferences.getString(Helper.PREF_INSTANCE, Helper.getLiveInstance(BaseMainActivity.this));
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.SERVER_KEY + userId + instance, apiResponse.getPushSubscription().getServer_key());
editor.apply();
}
}
public enum iconLauncher {
BUBBLES,

View File

@ -22,6 +22,7 @@ import java.lang.ref.WeakReference;
import app.fedilab.android.client.API;
import app.fedilab.android.client.APIResponse;
import app.fedilab.android.client.Entities.Account;
import app.fedilab.android.interfaces.OnPostSubscription;
@ -30,18 +31,21 @@ public class PostSubscriptionAsyncTask {
private final OnPostSubscription listener;
private final WeakReference<Context> contextReference;
private APIResponse apiResponse;
private final String endpoint;
private final Account account;
public PostSubscriptionAsyncTask(Context context, OnPostSubscription onPostSubscription) {
public PostSubscriptionAsyncTask(Context context, Account account, String endpoint, OnPostSubscription onPostSubscription) {
this.contextReference = new WeakReference<>(context);
this.listener = onPostSubscription;
this.endpoint = endpoint;
this.account = account;
doInBackground();
}
protected void doInBackground() {
new Thread(() -> {
apiResponse = new API(contextReference.get()).pushSubscription();
apiResponse = new API(contextReference.get(), account.getInstance(), account.getToken()).pushSubscription(account, endpoint);
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> listener.onSubscription(apiResponse);
mainHandler.post(myRunnable);

View File

@ -5779,12 +5779,13 @@ public class API {
apiResponse.setLists(lists);
return apiResponse;
}
/**
* Subscribe to push notifications
*
* @return APIResponse
*/
public APIResponse pushSubscription() {
public APIResponse pushSubscription(Account account, String endpoint) {
PushSubscription pushSubscription = new PushSubscription();
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
boolean notif_follow = sharedpreferences.getBoolean(Helper.SET_NOTIF_FOLLOW, true);
@ -5801,7 +5802,7 @@ public class API {
params.put("data[alerts][reblog]", String.valueOf(notif_share));
params.put("data[alerts][poll]", String.valueOf(notif_poll));
params.put("subscription[endpoint]", getAbsoluteUrl("/streaming/user"));
params.put("subscription[endpoint]", endpoint);
ECDH ecdh = ECDH.getInstance();
String pubKey = ecdh.getPublicKey(context);

View File

@ -1638,8 +1638,8 @@ public class PeertubeAPI {
String errorM = jsonObject.get("error").toString();
message = "Error " + statusCode + " : " + errorM;
} catch (JSONException e) {
if (error.getMessage().split(".").length > 0) {
String errorM = error.getMessage().split(".")[0];
if (error.getMessage().split("\\.").length > 0) {
String errorM = error.getMessage().split("\\.")[0];
message = "Error " + statusCode + " : " + errorM;
}
}

View File

@ -278,6 +278,7 @@ public class BaseHelper {
public static final String CLIENT_NAME = "client_name";
public static final String APP_PREFS = "app_prefs";
public static final String SERVER_KEY = "server_key";
public static final String SERVER_ENDPOINT = "server_endpoint";
public static final String ID = "id";
public static final String CLIENT_ID = "client_id";
public static final String CLIENT_SECRET = "client_secret";

View File

@ -2,8 +2,14 @@ package app.fedilab.android.helper;
import android.content.Context;
import android.util.Base64;
import org.unifiedpush.android.connector.Registration;
import java.util.List;
import java.util.Random;
import app.fedilab.android.activities.BaseMainActivity;
public class PushNotifications {
public void registerPushNotifications(Context context, String endpoint) {
ECDH ecdh = new ECDH();
@ -19,6 +25,15 @@ public class PushNotifications {
ecdh.saveServerKey(context, server_key);
}
public static void getDistributors(Context context) {
List<String> distributors = new Registration().getDistributors(context);
if (distributors.isEmpty()) {
} else {
}
}
public void displayNotification(Context context, String ciphered) {
ECDH ecdh = new ECDH();
byte[] secret = ecdh.getSecret(context);

View File

@ -1,6 +1,8 @@
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;
@ -8,16 +10,41 @@ 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.activities.BaseMainActivity;
import app.fedilab.android.activities.LiveNotificationSettingsAccountsActivity;
import app.fedilab.android.asynctasks.PostSubscriptionAsyncTask;
import app.fedilab.android.client.APIResponse;
import app.fedilab.android.client.Entities.Account;
import app.fedilab.android.helper.Helper;
import app.fedilab.android.helper.PushNotifications;
import app.fedilab.android.interfaces.OnPostSubscription;
import app.fedilab.android.sqlite.AccountDAO;
import app.fedilab.android.sqlite.Sqlite;
import static android.content.Context.MODE_PRIVATE;
class handler implements MessagingReceiverHandler, OnPostSubscription {
private Context context;
class handler implements MessagingReceiverHandler {
@Override
public void onNewEndpoint(@Nullable Context context, @NotNull String s) {
Log.v(Helper.TAG, "onNewEndpoint: " + s);
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.SERVER_ENDPOINT, s);
editor.apply();
PushNotifications push = new PushNotifications();
push.registerPushNotifications(context, s);
this.context = context;
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, s, this);
}
}
@Override
@ -44,9 +71,21 @@ class handler implements MessagingReceiverHandler {
push.displayNotification(context, s);
Log.v(Helper.TAG, "onMessage: " + s);
}
@Override
public void onSubscription(APIResponse apiResponse) {
if (apiResponse != null && apiResponse.getPushSubscription() != null) {
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
String instance = sharedpreferences.getString(Helper.PREF_INSTANCE, Helper.getLiveInstance(context));
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.SERVER_KEY + userId + instance, apiResponse.getPushSubscription().getServer_key());
editor.apply();
}
}
}
class UnifiedPushService extends MessagingReceiver {
public class UnifiedPushService extends MessagingReceiver {
public UnifiedPushService() {
super(new handler());
}

View File

@ -278,6 +278,22 @@ public class AccountDAO {
}
/**
* Returns all Account in db
*
* @return Account List<Account>
*/
public List<Account> getPushNotificationAccounts() {
try {
Cursor c = db.query(Sqlite.TABLE_USER_ACCOUNT, null, "(" + Sqlite.COL_SOCIAL + " = 'MASTODON' OR " + Sqlite.COL_SOCIAL + " = 'PLEROMA') AND " + Sqlite.COL_OAUTHTOKEN + " != 'null'", null, null, null, Sqlite.COL_INSTANCE + " ASC", null);
return cursorToListUser(c);
} catch (Exception e) {
return null;
}
}
/**
* Returns an Account by token
*