Fix issue #538 - Open a message with another account

This commit is contained in:
Thomas 2022-11-25 09:04:24 +01:00
parent bfa50d19c4
commit d2644b22a7
5 changed files with 79 additions and 2 deletions

View File

@ -811,10 +811,11 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt
String action = intent.getAction();
String type = intent.getType();
Bundle extras = intent.getExtras();
String userIdIntent, instanceIntent;
String userIdIntent, instanceIntent, urlOfMessage;
if (extras != null && extras.containsKey(Helper.INTENT_ACTION)) {
userIdIntent = extras.getString(Helper.PREF_KEY_ID); //Id of the account in the intent
instanceIntent = extras.getString(Helper.PREF_INSTANCE);
urlOfMessage = extras.getString(Helper.PREF_MESSAGE_URL);
if (extras.getInt(Helper.INTENT_ACTION) == Helper.NOTIFICATION_INTENT) {
if (userIdIntent != null && instanceIntent != null && userIdIntent.equals(currentUserID) && instanceIntent.equals(currentInstance)) {
openNotifications(intent);
@ -840,6 +841,23 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt
}
} else if (extras.getInt(Helper.INTENT_ACTION) == Helper.OPEN_NOTIFICATION) {
openNotifications(intent);
} else if (extras.getInt(Helper.INTENT_ACTION) == Helper.OPEN_WITH_ANOTHER_ACCOUNT) {
CrossActionHelper.fetchRemoteStatus(BaseMainActivity.this, MainActivity.currentAccount, urlOfMessage, new CrossActionHelper.Callback() {
@Override
public void federatedStatus(Status status) {
if (status != null) {
Intent intent = new Intent(BaseMainActivity.this, ContextActivity.class);
intent.putExtra(Helper.ARG_STATUS, status);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
@Override
public void federatedAccount(app.fedilab.android.client.entities.api.Account account) {
}
});
}
} else if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {

View File

@ -302,12 +302,14 @@ public class Helper {
public static final String PREF_IS_MODERATOR = "PREF_IS_MODERATOR";
public static final String PREF_IS_ADMINISTRATOR = "PREF_IS_ADMINISTRATOR";
public static final String PREF_KEY_ID = "PREF_KEY_ID";
public static final String PREF_MESSAGE_URL = "PREF_MESSAGE_URL";
public static final String PREF_INSTANCE = "PREF_INSTANCE";
public static final String SET_SECURITY_PROVIDER = "SET_SECURITY_PROVIDER";
public static final int NOTIFICATION_INTENT = 1;
public static final int OPEN_NOTIFICATION = 2;
public static final int OPEN_WITH_ANOTHER_ACCOUNT = 3;
public static final String INTENT_TARGETED_ACCOUNT = "INTENT_TARGETED_ACCOUNT";
public static final String INTENT_SEND_MODIFIED_IMAGE = "INTENT_SEND_MODIFIED_IMAGE";
public static final String INTENT_COMPOSE_ERROR_MESSAGE = "INTENT_COMPOSE_ERROR_MESSAGE";

View File

@ -24,6 +24,7 @@ import static app.fedilab.android.BaseMainActivity.regex_public;
import static app.fedilab.android.BaseMainActivity.show_boosts;
import static app.fedilab.android.BaseMainActivity.show_replies;
import static app.fedilab.android.activities.ContextActivity.expand;
import static app.fedilab.android.helper.Helper.PREF_USER_TOKEN;
import android.annotation.SuppressLint;
import android.app.Activity;
@ -38,6 +39,8 @@ import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Looper;
import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;
@ -103,6 +106,7 @@ import app.fedilab.android.R;
import app.fedilab.android.activities.ComposeActivity;
import app.fedilab.android.activities.ContextActivity;
import app.fedilab.android.activities.CustomSharingActivity;
import app.fedilab.android.activities.MainActivity;
import app.fedilab.android.activities.MediaActivity;
import app.fedilab.android.activities.ProfileActivity;
import app.fedilab.android.activities.ReportActivity;
@ -114,6 +118,7 @@ import app.fedilab.android.client.entities.api.Poll;
import app.fedilab.android.client.entities.api.Reaction;
import app.fedilab.android.client.entities.api.Status;
import app.fedilab.android.client.entities.app.Account;
import app.fedilab.android.client.entities.app.BaseAccount;
import app.fedilab.android.client.entities.app.StatusCache;
import app.fedilab.android.client.entities.app.StatusDraft;
import app.fedilab.android.client.entities.app.Timeline;
@ -2011,6 +2016,54 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
b.putSerializable(Helper.ARG_STATUS_MENTION, statusToDeal);
intent.putExtras(b);
context.startActivity(intent);
} else if (itemId == R.id.action_open_with) {
new Thread(() -> {
try {
List<BaseAccount> accounts = new Account(context).getCrossAccounts();
if (accounts.size() > 1) {
List<app.fedilab.android.client.entities.api.Account> accountList = new ArrayList<>();
for (BaseAccount account : accounts) {
accountList.add(account.mastodon_account);
}
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> {
AlertDialog.Builder builderSingle = new AlertDialog.Builder(context, Helper.dialogStyle());
builderSingle.setTitle(context.getString(R.string.choose_accounts));
final AccountsSearchAdapter accountsSearchAdapter = new AccountsSearchAdapter(context, accountList);
final BaseAccount[] accountArray = new BaseAccount[accounts.size()];
int i = 0;
for (BaseAccount account : accounts) {
accountArray[i] = account;
i++;
}
builderSingle.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss());
builderSingle.setAdapter(accountsSearchAdapter, (dialog, which) -> {
BaseAccount account = accountArray[which];
Toasty.info(context, context.getString(R.string.toast_account_changed, "@" + account.mastodon_account.acct + "@" + account.instance), Toasty.LENGTH_LONG).show();
BaseMainActivity.currentToken = account.token;
BaseMainActivity.currentUserID = account.user_id;
BaseMainActivity.currentInstance = account.instance;
MainActivity.currentAccount = account;
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(PREF_USER_TOKEN, account.token);
editor.commit();
Intent mainActivity = new Intent(context, MainActivity.class);
mainActivity.putExtra(Helper.INTENT_ACTION, Helper.OPEN_WITH_ANOTHER_ACCOUNT);
mainActivity.putExtra(Helper.PREF_MESSAGE_URL, statusToDeal.url);
context.startActivity(mainActivity);
((Activity) context).finish();
dialog.dismiss();
});
builderSingle.show();
};
mainHandler.post(myRunnable);
}
} catch (DBException e) {
e.printStackTrace();
}
}).start();
}
return true;
});

View File

@ -83,5 +83,8 @@
android:id="@+id/action_mention"
android:title="@string/more_action_7"
app:showAsAction="never" />
<item
android:id="@+id/action_open_with"
android:title="@string/open_with_account"
app:showAsAction="never" />
</menu>

View File

@ -1930,4 +1930,5 @@
<string name="set_notif_update">Notify for updates</string>
<string name="set_notif_user_sign_up">New sign-up (moderators)</string>
<string name="set_notif_admin_report">New report (moderators)</string>
<string name="open_with_account">Open with another account</string>
</resources>