Fix potential crashes when clicking on notifications

This commit is contained in:
Thomas 2024-01-17 14:12:56 +01:00
parent 886b74c171
commit a1a6005ca0
5 changed files with 89 additions and 68 deletions

View file

@ -675,32 +675,35 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt
*/ */
private static void openNotifications(Activity activity, Intent intent) { private static void openNotifications(Activity activity, Intent intent) {
if (intent != null && intent.getExtras() != null) { if (intent != null && intent.getExtras() != null) {
Bundle bundle = intent.getExtras(); Bundle args = intent.getExtras();
app.fedilab.android.mastodon.client.entities.api.Account account = (app.fedilab.android.mastodon.client.entities.api.Account) bundle.getSerializable(Helper.INTENT_TARGETED_ACCOUNT); long bundleId = args.getLong(Helper.ARG_INTENT_ID, -1);
Status status = (Status) bundle.getSerializable(Helper.INTENT_TARGETED_STATUS); new CachedBundle(activity).getBundle(bundleId, currentAccount, bundle -> {
if (account != null) { app.fedilab.android.mastodon.client.entities.api.Account account = (app.fedilab.android.mastodon.client.entities.api.Account) bundle.getSerializable(Helper.INTENT_TARGETED_ACCOUNT);
Intent intentAccount = new Intent(activity, ProfileActivity.class); Status status = (Status) bundle.getSerializable(Helper.INTENT_TARGETED_STATUS);
Bundle args = new Bundle(); if (account != null) {
args.putSerializable(Helper.ARG_ACCOUNT, account); Intent intentAccount = new Intent(activity, ProfileActivity.class);
new CachedBundle(activity).insertBundle(args, currentAccount, bundleId -> { Bundle args2 = new Bundle();
Bundle bundleCached = new Bundle(); args2.putSerializable(Helper.ARG_ACCOUNT, account);
bundleCached.putLong(Helper.ARG_INTENT_ID, bundleId); new CachedBundle(activity).insertBundle(args2, currentAccount, bundleId2 -> {
intentAccount.putExtras(bundleCached); Bundle bundleCached = new Bundle();
intentAccount.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); bundleCached.putLong(Helper.ARG_INTENT_ID, bundleId2);
activity.startActivity(intentAccount); intentAccount.putExtras(bundleCached);
}); intentAccount.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
} else if (status != null) { activity.startActivity(intentAccount);
Intent intentContext = new Intent(activity, ContextActivity.class); });
Bundle args = new Bundle(); } else if (status != null) {
args.putSerializable(Helper.ARG_STATUS, status); Intent intentContext = new Intent(activity, ContextActivity.class);
new CachedBundle(activity).insertBundle(args, currentAccount, bundleId -> { Bundle args2 = new Bundle();
Bundle bundleCached = new Bundle(); args2.putSerializable(Helper.ARG_STATUS, status);
bundleCached.putLong(Helper.ARG_INTENT_ID, bundleId); new CachedBundle(activity).insertBundle(args2, currentAccount, bundleId2 -> {
intentContext.putExtras(bundleCached); Bundle bundleCached = new Bundle();
intentContext.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); bundleCached.putLong(Helper.ARG_INTENT_ID, bundleId2);
activity.startActivity(intentContext); intentContext.putExtras(bundleCached);
}); intentContext.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
} activity.startActivity(intentContext);
});
}
});
} }
final Handler handler = new Handler(); final Handler handler = new Handler();
handler.postDelayed(() -> { handler.postDelayed(() -> {

View file

@ -171,9 +171,15 @@ public class MediaActivity extends BaseTransparentActivity implements OnDownload
if (attachments.get(mediaPosition - 1).status != null) { if (attachments.get(mediaPosition - 1).status != null) {
binding.originalMessage.setOnClickListener(v -> { binding.originalMessage.setOnClickListener(v -> {
Intent intentContext = new Intent(MediaActivity.this, ContextActivity.class); Intent intentContext = new Intent(MediaActivity.this, ContextActivity.class);
intentContext.putExtra(Helper.ARG_STATUS, attachments.get(mediaPosition - 1).status); Bundle args = new Bundle();
intentContext.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); args.putSerializable(Helper.ARG_STATUS, attachments.get(mediaPosition - 1).status);
startActivity(intentContext); new CachedBundle(MediaActivity.this).insertBundle(args, currentAccount, bundleId -> {
Bundle bundleCached = new Bundle();
bundleCached.putLong(Helper.ARG_INTENT_ID, bundleId);
intentContext.putExtras(bundleCached);
intentContext.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intentContext);
});
}); });
} }

View file

@ -82,7 +82,6 @@ public class SearchResultTabActivity extends BaseBarActivity {
Bundle b = getIntent().getExtras(); Bundle b = getIntent().getExtras();
if (b != null) { if (b != null) {
search = b.getString(Helper.ARG_SEARCH_KEYWORD, null); search = b.getString(Helper.ARG_SEARCH_KEYWORD, null);
} }
if (search == null) { if (search == null) {
Toasty.error(SearchResultTabActivity.this, getString(R.string.toast_error_search), Toast.LENGTH_LONG).show(); Toasty.error(SearchResultTabActivity.this, getString(R.string.toast_error_search), Toast.LENGTH_LONG).show();

View file

@ -54,6 +54,7 @@ import app.fedilab.android.mastodon.client.entities.api.Notifications;
import app.fedilab.android.mastodon.client.entities.api.Status; import app.fedilab.android.mastodon.client.entities.api.Status;
import app.fedilab.android.mastodon.client.entities.app.Account; import app.fedilab.android.mastodon.client.entities.app.Account;
import app.fedilab.android.mastodon.client.entities.app.BaseAccount; import app.fedilab.android.mastodon.client.entities.app.BaseAccount;
import app.fedilab.android.mastodon.client.entities.app.CachedBundle;
import app.fedilab.android.mastodon.exception.DBException; import app.fedilab.android.mastodon.exception.DBException;
import app.fedilab.android.mastodon.ui.drawer.StatusAdapter; import app.fedilab.android.mastodon.ui.drawer.StatusAdapter;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
@ -398,48 +399,48 @@ public class NotificationsHelper {
//Some others notification //Some others notification
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);
Bundle args = new Bundle();
intent.putExtra(Helper.INTENT_ACTION, Helper.NOTIFICATION_INTENT); intent.putExtra(Helper.INTENT_ACTION, Helper.NOTIFICATION_INTENT);
intent.putExtra(Helper.PREF_USER_ID, account.user_id); intent.putExtra(Helper.PREF_USER_ID, account.user_id);
if (targeted_account != null) { if (targeted_account != null) {
Bundle bundle = new Bundle(); args.putSerializable(Helper.INTENT_TARGETED_ACCOUNT, targeted_account);
bundle.putSerializable(Helper.INTENT_TARGETED_ACCOUNT, targeted_account);
intent.putExtras(bundle);
} else if (targeted_status != null) { } else if (targeted_status != null) {
Bundle bundle = new Bundle(); args.putSerializable(Helper.INTENT_TARGETED_STATUS, targeted_status);
bundle.putSerializable(Helper.INTENT_TARGETED_STATUS, targeted_status);
intent.putExtras(bundle);
} }
intent.putExtra(Helper.PREF_USER_INSTANCE, account.instance); String finalMessage1 = message;
notificationUrl = notification.account.avatar; String finalTitle1 = title;
Handler mainHandler = new Handler(Looper.getMainLooper()); Helper.NotifType finalNotifType1 = notifType;
final String finalNotificationUrl = notificationUrl; new CachedBundle(context).insertBundle(args, account, bundleId -> {
Helper.NotifType finalNotifType = notifType; Bundle bundle = new Bundle();
String finalMessage = message; bundle.putLong(Helper.ARG_INTENT_ID, bundleId);
String finalTitle = title; intent.putExtras(bundle);
StatusAdapter.sendAction(context, Helper.RECEIVE_REFRESH_NOTIFICATIONS_ACTION, null, null); intent.putExtra(Helper.PREF_USER_INSTANCE, account.instance);
Runnable myRunnable = () -> Glide.with(context) Handler mainHandler = new Handler(Looper.getMainLooper());
.asBitmap() final String finalNotificationUrl = notification.account.avatar;
.load(finalNotificationUrl != null ? finalNotificationUrl : R.drawable.fedilab_logo_bubbles) StatusAdapter.sendAction(context, Helper.RECEIVE_REFRESH_NOTIFICATIONS_ACTION, null, null);
.into(new CustomTarget<Bitmap>() { Runnable myRunnable = () -> Glide.with(context)
@Override .asBitmap()
public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) { .load(finalNotificationUrl != null ? finalNotificationUrl : R.drawable.fedilab_logo_bubbles)
Helper.notify_user(context, account, intent, resource, finalNotifType, finalTitle, finalMessage); .into(new CustomTarget<Bitmap>() {
} @Override
public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) {
Helper.notify_user(context, account, intent, resource, finalNotifType1, finalTitle1, finalMessage1);
}
@Override @Override
public void onLoadFailed(@Nullable Drawable errorDrawable) { public void onLoadFailed(@Nullable Drawable errorDrawable) {
super.onLoadFailed(errorDrawable); super.onLoadFailed(errorDrawable);
Helper.notify_user(context, account, intent, BitmapFactory.decodeResource(context.getResources(), Helper.notify_user(context, account, intent, BitmapFactory.decodeResource(context.getResources(),
getMainLogo(context)), finalNotifType, finalTitle, finalMessage); getMainLogo(context)), finalNotifType1, finalTitle1, finalMessage1);
} }
@Override @Override
public void onLoadCleared(@Nullable Drawable placeholder) { public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
mainHandler.post(myRunnable);
}
});
mainHandler.post(myRunnable);
});
} }
} }

View file

@ -2225,8 +2225,14 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
MastodonHelper.scheduleBoost(context, MastodonHelper.ScheduleType.BOOST, statusToDeal, null, null); MastodonHelper.scheduleBoost(context, MastodonHelper.ScheduleType.BOOST, statusToDeal, null, null);
} else if (itemId == R.id.action_admin) { } else if (itemId == R.id.action_admin) {
Intent intent = new Intent(context, AdminAccountActivity.class); Intent intent = new Intent(context, AdminAccountActivity.class);
intent.putExtra(Helper.ARG_ACCOUNT_ID, statusToDeal.account.id); Bundle args = new Bundle();
context.startActivity(intent); args.putString(Helper.ARG_ACCOUNT_ID, statusToDeal.account.id);
new CachedBundle(context).insertBundle(args, currentAccount, bundleId -> {
Bundle bundle = new Bundle();
bundle.putLong(Helper.ARG_INTENT_ID, bundleId);
intent.putExtras(bundle);
context.startActivity(intent);
});
} else if (itemId == R.id.action_open_browser) { } else if (itemId == R.id.action_open_browser) {
Helper.openBrowser(context, statusToDeal.url); Helper.openBrowser(context, statusToDeal.url);
} else if (itemId == R.id.action_remove) { } else if (itemId == R.id.action_remove) {
@ -2487,8 +2493,14 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
}); });
holder.binding.actionButtonQuote.setOnClickListener(v -> { holder.binding.actionButtonQuote.setOnClickListener(v -> {
Intent intent = new Intent(context, ComposeActivity.class); Intent intent = new Intent(context, ComposeActivity.class);
intent.putExtra(Helper.ARG_QUOTED_MESSAGE, statusToDeal); Bundle args = new Bundle();
context.startActivity(intent); args.putSerializable(Helper.ARG_QUOTED_MESSAGE, statusToDeal);
new CachedBundle(context).insertBundle(args, currentAccount, bundleId -> {
Bundle bundle = new Bundle();
bundle.putLong(Helper.ARG_INTENT_ID, bundleId);
intent.putExtras(bundle);
context.startActivity(intent);
});
}); });
holder.binding.actionButtonReply.setOnClickListener(v -> { holder.binding.actionButtonReply.setOnClickListener(v -> {
if (remote) { if (remote) {