From dc9e9f2aebfd3feb057fee4c3ac72b05cbc75a12 Mon Sep 17 00:00:00 2001 From: Konrad Pozniak Date: Sat, 19 Aug 2023 12:31:47 +0200 Subject: [PATCH] Improve account switching intents (#3732) Before, intent creation was scattered across multiple sites, with account switching logic in both `ComposeActivity` and `MainActivity`. Now, intents are only created in `MainActivity` Companion, and account switching only occurs in `MainActivity` Fixes #3695 --- app/lint-baseline.xml | 542 +++++++++--------- .../com/keylesspalace/tusky/BaseActivity.java | 5 +- .../com/keylesspalace/tusky/MainActivity.kt | 129 ++++- .../components/compose/ComposeActivity.kt | 35 +- .../notifications/NotificationHelper.java | 27 +- .../tusky/service/SendStatusService.kt | 4 +- .../tusky/service/TuskyTileService.kt | 8 +- .../tusky/util/ShareShortcutHelper.kt | 3 +- 8 files changed, 394 insertions(+), 359 deletions(-) diff --git a/app/lint-baseline.xml b/app/lint-baseline.xml index a09fe5dae..4cf240803 100644 --- a/app/lint-baseline.xml +++ b/app/lint-baseline.xml @@ -19,7 +19,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"> @@ -30,7 +30,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"> @@ -41,7 +41,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"> @@ -84,6 +84,17 @@ column="21"/> + + + + - - - - @@ -333,7 +333,7 @@ errorLine2=" ^"> @@ -355,7 +355,7 @@ errorLine2=" ^"> @@ -366,7 +366,7 @@ errorLine2=" ^"> @@ -388,7 +388,7 @@ errorLine2=" ^"> @@ -410,7 +410,7 @@ errorLine2=" ^"> @@ -421,7 +421,7 @@ errorLine2=" ^"> @@ -432,7 +432,7 @@ errorLine2=" ^"> @@ -443,7 +443,7 @@ errorLine2=" ^"> @@ -454,7 +454,7 @@ errorLine2=" ^"> @@ -465,7 +465,7 @@ errorLine2=" ^"> @@ -476,7 +476,7 @@ errorLine2=" ^"> @@ -487,7 +487,7 @@ errorLine2=" ^"> @@ -498,7 +498,7 @@ errorLine2=" ^"> @@ -509,7 +509,7 @@ errorLine2=" ^"> @@ -520,7 +520,7 @@ errorLine2=" ^"> @@ -531,7 +531,7 @@ errorLine2=" ^"> @@ -542,7 +542,7 @@ errorLine2=" ^"> @@ -553,7 +553,7 @@ errorLine2=" ^"> @@ -751,7 +751,7 @@ errorLine2=" ^"> @@ -762,7 +762,7 @@ errorLine2=" ^"> @@ -773,7 +773,7 @@ errorLine2=" ^"> @@ -784,7 +784,7 @@ errorLine2=" ^"> @@ -795,7 +795,7 @@ errorLine2=" ^"> @@ -817,7 +817,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"> @@ -861,7 +861,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"> @@ -872,7 +872,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"> @@ -1136,7 +1136,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"> @@ -1928,7 +1928,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"> @@ -1939,7 +1939,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~"> @@ -1950,7 +1950,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~"> @@ -1961,7 +1961,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~"> @@ -1972,7 +1972,7 @@ errorLine2=" ~~~~~~~~~~~~"> @@ -1983,7 +1983,7 @@ errorLine2=" ~~~~~~~~~~~~~~"> @@ -1994,7 +1994,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~"> @@ -2005,7 +2005,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~"> @@ -2016,7 +2016,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~"> @@ -2027,7 +2027,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~"> @@ -2038,7 +2038,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~"> @@ -2049,7 +2049,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~"> @@ -2060,7 +2060,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"> @@ -2071,7 +2071,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~"> @@ -2082,7 +2082,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"> @@ -2093,7 +2093,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"> @@ -2104,7 +2104,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"> @@ -2115,7 +2115,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~"> @@ -2126,7 +2126,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~"> @@ -2137,7 +2137,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~"> @@ -2148,7 +2148,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"> @@ -2159,7 +2159,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~"> @@ -2181,7 +2181,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"> @@ -2192,7 +2192,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"> @@ -2203,7 +2203,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"> @@ -2533,7 +2533,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~"> @@ -2544,7 +2544,7 @@ errorLine2=" ~~~~~~~~~"> @@ -2555,7 +2555,7 @@ errorLine2=" ~~~~~~~~~"> @@ -2643,7 +2643,7 @@ errorLine2=" ~~~~~~~~~"> @@ -2654,7 +2654,7 @@ errorLine2=" ~~~~~~~~~"> @@ -2665,7 +2665,7 @@ errorLine2=" ~~~~~~~~~"> @@ -2676,7 +2676,7 @@ errorLine2=" ~~~~~~~~~"> @@ -3122,12 +3122,12 @@ + message="Access to `private` method `forwardToComposeActivity` of class `MainActivity` requires synthetic accessor" + errorLine1=" forwardToComposeActivity(intent)" + errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~"> @@ -3138,7 +3138,7 @@ errorLine2=" ~~~~~~~~~~~~~"> @@ -3149,7 +3149,7 @@ errorLine2=" ~~~~~~~"> @@ -3160,7 +3160,7 @@ errorLine2=" ~~~~~~~"> @@ -3171,7 +3171,7 @@ errorLine2=" ~~~~~~~"> @@ -3182,54 +3182,10 @@ errorLine2=" ~~~~~~~"> - - - - - - - - - - - - - - - - @@ -3281,7 +3237,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~"> @@ -3292,7 +3248,7 @@ errorLine2=" ~~~~~~~"> @@ -3303,7 +3259,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~"> @@ -3314,7 +3270,7 @@ errorLine2=" ~~~~~~~"> @@ -3325,7 +3281,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~"> @@ -3336,7 +3292,7 @@ errorLine2=" ~~~~~~~"> @@ -3347,7 +3303,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~"> @@ -3358,18 +3314,40 @@ errorLine2=" ~~~~~~~"> + message="Access to `private` method `primaryDrawerItem` of class `MainActivityKt` requires synthetic accessor" + errorLine1=" primaryDrawerItem {" + errorLine2=" ~~~~~~~~~~~~~~~~~"> + + + + + + + + @@ -3378,31 +3356,9 @@ message="Access to `private` method `setOnClick` of class `MainActivityKt` requires synthetic accessor" errorLine1=" onClick = {" errorLine2=" ~~~~~~~"> - - - - - - - - @@ -3413,7 +3369,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~"> @@ -3424,7 +3380,7 @@ errorLine2=" ~~~~~~~"> @@ -3435,7 +3391,51 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~"> + + + + + + + + + + + + + + + + @@ -3446,7 +3446,7 @@ errorLine2=" ~~~~~~~"> @@ -3457,7 +3457,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~"> @@ -3468,7 +3468,7 @@ errorLine2=" ~~~~~~~"> @@ -3479,7 +3479,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~"> @@ -3490,7 +3490,7 @@ errorLine2=" ~~~~~~~"> @@ -3501,7 +3501,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~"> @@ -3512,7 +3512,7 @@ errorLine2=" ~~~~~~~"> @@ -3523,7 +3523,7 @@ errorLine2=" ~~~~~~~"> @@ -3534,7 +3534,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~"> @@ -3545,7 +3545,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~"> @@ -3556,7 +3556,7 @@ errorLine2=" ~~~~~~~"> @@ -3567,7 +3567,7 @@ errorLine2=" ~~~~~~~"> @@ -3578,7 +3578,7 @@ errorLine2=" ~~~~~~~"> @@ -3589,7 +3589,7 @@ errorLine2=" ~~~~~~~"> @@ -3600,7 +3600,7 @@ errorLine2=" ~~~~~~~"> @@ -3611,7 +3611,7 @@ errorLine2=" ~~~~~~~"> @@ -4062,7 +4062,7 @@ errorLine2=" ~~~~~~~"> @@ -4073,7 +4073,7 @@ errorLine2=" ~~~~~~~~~~~~"> @@ -4084,7 +4084,7 @@ errorLine2=" ~~~~~~~~~~~~"> @@ -4095,7 +4095,7 @@ errorLine2=" ~~~~~~~"> @@ -4106,10 +4106,21 @@ errorLine2=" ~~~~~~~"> + + + + - - - - + + + + - - - - @@ -4194,7 +4194,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"> @@ -4238,7 +4238,7 @@ errorLine2=" ~~~~~~~~~~"> @@ -4916,7 +4916,7 @@ errorLine2=" ~~~~~~~~"> @@ -4927,7 +4927,7 @@ errorLine2=" ~~~~~~~~"> @@ -4938,7 +4938,7 @@ errorLine2=" ~~~~~~~~"> @@ -4949,7 +4949,7 @@ errorLine2=" ~~~~~~~~"> @@ -4960,7 +4960,7 @@ errorLine2=" ~~~~~~~~"> @@ -4971,7 +4971,7 @@ errorLine2=" ~~~~~~~~"> @@ -5136,7 +5136,7 @@ errorLine2=" ~~~~~~~~"> @@ -5147,7 +5147,7 @@ errorLine2=" ~~~~~~~~"> @@ -5158,7 +5158,7 @@ errorLine2=" ~~~~~~~~"> @@ -5169,7 +5169,7 @@ errorLine2=" ~~~~~~~~"> @@ -5180,7 +5180,7 @@ errorLine2=" ~~~~~~~~"> @@ -5191,7 +5191,7 @@ errorLine2=" ~~~~~~~~"> @@ -5202,7 +5202,7 @@ errorLine2=" ~~~~~~~~"> @@ -5554,7 +5554,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"> @@ -5983,7 +5983,7 @@ errorLine2=" ~~~~~~~~"> @@ -5994,7 +5994,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~"> @@ -6005,7 +6005,7 @@ errorLine2=" ~~~~~~~"> @@ -6016,7 +6016,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~"> @@ -6027,7 +6027,7 @@ errorLine2=" ~~~~~~~~~~~~"> @@ -6038,7 +6038,7 @@ errorLine2=" ~~~~~~~~~~~~~"> @@ -6049,7 +6049,7 @@ errorLine2=" ~~~~~~~"> @@ -6060,7 +6060,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~"> @@ -6071,7 +6071,7 @@ errorLine2=" ~~~~~~~~~~~~~"> @@ -6082,7 +6082,7 @@ errorLine2=" ~~~~~~~"> @@ -6093,7 +6093,7 @@ errorLine2=" ~~~~~~~"> @@ -6104,7 +6104,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~"> @@ -6115,7 +6115,7 @@ errorLine2=" ~~~~~~~~~~~~~"> @@ -6192,7 +6192,7 @@ errorLine2=" ~~~~"> @@ -6203,7 +6203,7 @@ errorLine2=" ~~~~~~"> @@ -6214,7 +6214,7 @@ errorLine2=" ~~~~~~~~~~~"> @@ -6225,7 +6225,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~"> @@ -6236,7 +6236,7 @@ errorLine2=" ~~~~~~"> @@ -6247,7 +6247,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~"> @@ -6258,7 +6258,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~"> @@ -6269,7 +6269,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~"> @@ -6280,7 +6280,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~"> @@ -6291,7 +6291,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~"> @@ -6302,7 +6302,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~"> @@ -6313,7 +6313,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~"> @@ -6324,7 +6324,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~"> @@ -6335,7 +6335,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~"> @@ -6346,7 +6346,7 @@ errorLine2=" ~~~~~~"> @@ -6357,7 +6357,7 @@ errorLine2=" ~~~~~~"> @@ -6368,7 +6368,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~"> @@ -6379,7 +6379,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~"> @@ -6390,7 +6390,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~"> @@ -6401,7 +6401,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~"> @@ -6412,7 +6412,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~"> @@ -6423,7 +6423,7 @@ errorLine2=" ~~~~~~~~~~~~"> @@ -6434,7 +6434,7 @@ errorLine2=" ~~~~~~~"> @@ -6445,7 +6445,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~"> @@ -6456,7 +6456,7 @@ errorLine2=" ~~~~~~~~~~~~"> @@ -6467,7 +6467,7 @@ errorLine2=" ~~~~~~~"> @@ -6478,7 +6478,7 @@ errorLine2=" ~~~~~~~~~~~~"> @@ -6489,7 +6489,7 @@ errorLine2=" ~~~~~~~"> @@ -6500,7 +6500,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~"> @@ -6511,7 +6511,7 @@ errorLine2=" ~~~~~~~~~~~~"> @@ -6522,7 +6522,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~"> @@ -6533,7 +6533,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~"> diff --git a/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java b/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java index 62709d7c3..6cdc471a0 100644 --- a/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java @@ -256,9 +256,8 @@ public abstract class BaseActivity extends AppCompatActivity implements Injectab public void openAsAccount(@NonNull String url, @NonNull AccountEntity account) { accountManager.setActiveAccount(account.getId()); - Intent intent = new Intent(this, MainActivity.class); - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); - intent.putExtra(MainActivity.REDIRECT_URL, url); + Intent intent = MainActivity.redirectIntent(this, account.getId(), url); + startActivity(intent); finishWithoutSlideOutAnimation(); } diff --git a/app/src/main/java/com/keylesspalace/tusky/MainActivity.kt b/app/src/main/java/com/keylesspalace/tusky/MainActivity.kt index da060aa8a..ccd3cd954 100644 --- a/app/src/main/java/com/keylesspalace/tusky/MainActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/MainActivity.kt @@ -16,6 +16,7 @@ package com.keylesspalace.tusky import android.Manifest +import android.app.NotificationManager import android.content.Context import android.content.DialogInterface import android.content.Intent @@ -41,6 +42,7 @@ import androidx.appcompat.content.res.AppCompatResources import androidx.coordinatorlayout.widget.CoordinatorLayout import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat +import androidx.core.content.IntentCompat import androidx.core.content.pm.ShortcutManagerCompat import androidx.core.view.GravityCompat import androidx.core.view.MenuProvider @@ -182,30 +184,39 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidInje ?: return // will be redirected to LoginActivity by BaseActivity var showNotificationTab = false - if (intent != null) { + + // check for savedInstanceState in order to not handle intent events more than once + if (intent != null && savedInstanceState == null) { + val notificationId = intent.getIntExtra(NOTIFICATION_ID, -1) + if (notificationId != -1) { + // opened from a notification action, cancel the notification + val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager + notificationManager.cancel(intent.getStringExtra(NOTIFICATION_TAG), notificationId) + } + /** there are two possibilities the accountId can be passed to MainActivity: - * - from our code as long 'account_id' + * - from our code as Long Intent Extra TUSKY_ACCOUNT_ID * - from share shortcuts as String 'android.intent.extra.shortcut.ID' */ - var accountId = intent.getLongExtra(NotificationHelper.ACCOUNT_ID, -1) - if (accountId == -1L) { + var tuskyAccountId = intent.getLongExtra(TUSKY_ACCOUNT_ID, -1) + if (tuskyAccountId == -1L) { val accountIdString = intent.getStringExtra(ShortcutManagerCompat.EXTRA_SHORTCUT_ID) if (accountIdString != null) { - accountId = accountIdString.toLong() + tuskyAccountId = accountIdString.toLong() } } - val accountRequested = accountId != -1L - if (accountRequested && accountId != activeAccount.id) { - accountManager.setActiveAccount(accountId) + val accountRequested = tuskyAccountId != -1L + if (accountRequested && tuskyAccountId != activeAccount.id) { + accountManager.setActiveAccount(tuskyAccountId) } val openDrafts = intent.getBooleanExtra(OPEN_DRAFTS, false) - if (canHandleMimeType(intent.type)) { + if (canHandleMimeType(intent.type) || intent.hasExtra(COMPOSE_OPTIONS)) { // Sharing to Tusky from an external app if (accountRequested) { // The correct account is already active - forwardShare(intent) + forwardToComposeActivity(intent) } else { // No account was provided, show the chooser showAccountChooserDialog( @@ -216,10 +227,10 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidInje val requestedId = account.id if (requestedId == activeAccount.id) { // The correct account is already active - forwardShare(intent) + forwardToComposeActivity(intent) } else { // A different account was requested, restart the activity - intent.putExtra(NotificationHelper.ACCOUNT_ID, requestedId) + intent.putExtra(TUSKY_ACCOUNT_ID, requestedId) changeAccount(requestedId, intent) } } @@ -229,10 +240,10 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidInje } else if (openDrafts) { val intent = DraftsActivity.newIntent(this) startActivity(intent) - } else if (accountRequested && savedInstanceState == null) { + } else if (accountRequested && intent.hasExtra(NOTIFICATION_TYPE)) { // user clicked a notification, show follow requests for type FOLLOW_REQUEST, // otherwise show notification tab - if (intent.getStringExtra(NotificationHelper.TYPE) == Notification.Type.FOLLOW_REQUEST.name) { + if (intent.getSerializableExtra(NOTIFICATION_TYPE) == Notification.Type.FOLLOW_REQUEST) { val intent = AccountListActivity.newIntent(this, AccountListActivity.Type.FOLLOW_REQUESTS) startActivityWithSlideInAnimation(intent) } else { @@ -422,12 +433,19 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidInje } } - private fun forwardShare(intent: Intent) { - val composeIntent = Intent(this, ComposeActivity::class.java) - composeIntent.action = intent.action - composeIntent.type = intent.type - composeIntent.putExtras(intent) - composeIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK + private fun forwardToComposeActivity(intent: Intent) { + val composeOptions = IntentCompat.getParcelableExtra(intent, COMPOSE_OPTIONS, ComposeActivity.ComposeOptions::class.java) + + val composeIntent = if (composeOptions != null) { + ComposeActivity.startIntent(this, composeOptions) + } else { + Intent(this, ComposeActivity::class.java).apply { + action = intent.action + type = intent.type + putExtras(intent) + flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK + } + } startActivity(composeIntent) finish() } @@ -1043,8 +1061,75 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidInje private const val TAG = "MainActivity" // logging tag private const val DRAWER_ITEM_ADD_ACCOUNT: Long = -13 private const val DRAWER_ITEM_ANNOUNCEMENTS: Long = 14 - const val REDIRECT_URL = "redirectUrl" - const val OPEN_DRAFTS = "draft" + private const val REDIRECT_URL = "redirectUrl" + private const val OPEN_DRAFTS = "draft" + private const val TUSKY_ACCOUNT_ID = "tuskyAccountId" + private const val COMPOSE_OPTIONS = "composeOptions" + private const val NOTIFICATION_TYPE = "notificationType" + private const val NOTIFICATION_TAG = "notificationTag" + private const val NOTIFICATION_ID = "notificationId" + + /** + * Switches the active account to the provided accountId and then stays on MainActivity + */ + @JvmStatic + fun accountSwitchIntent(context: Context, tuskyAccountId: Long): Intent { + return Intent(context, MainActivity::class.java).apply { + putExtra(TUSKY_ACCOUNT_ID, tuskyAccountId) + } + } + + /** + * Switches the active account to the accountId and takes the user to the correct place according to the notification they clicked + */ + @JvmStatic + fun openNotificationIntent(context: Context, tuskyAccountId: Long, type: Notification.Type): Intent { + return accountSwitchIntent(context, tuskyAccountId).apply { + putExtra(NOTIFICATION_TYPE, type) + } + } + + /** + * Switches the active account to the accountId and then opens ComposeActivity with the provided options + * @param tuskyAccountId the id of the Tusky account to open the screen with. Set to -1 for current account. + * @param notificationId optional id of the notification that should be cancelled when this intent is opened + * @param notificationTag optional tag of the notification that should be cancelled when this intent is opened + */ + @JvmStatic + fun composeIntent( + context: Context, + options: ComposeActivity.ComposeOptions, + tuskyAccountId: Long = -1, + notificationTag: String? = null, + notificationId: Int = -1 + ): Intent { + return accountSwitchIntent(context, tuskyAccountId).apply { + action = Intent.ACTION_SEND // so it can be opened via shortcuts + putExtra(COMPOSE_OPTIONS, options) + putExtra(NOTIFICATION_TAG, notificationTag) + putExtra(NOTIFICATION_ID, notificationId) + } + } + + /** + * switches the active account to the accountId and then tries to resolve and show the provided url + */ + @JvmStatic + fun redirectIntent(context: Context, tuskyAccountId: Long, url: String): Intent { + return accountSwitchIntent(context, tuskyAccountId).apply { + putExtra(REDIRECT_URL, url) + flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK + } + } + + /** + * switches the active account to the provided accountId and then opens drafts + */ + fun draftIntent(context: Context, tuskyAccountId: Long): Intent { + return accountSwitchIntent(context, tuskyAccountId).apply { + putExtra(OPEN_DRAFTS, true) + } + } } } diff --git a/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt b/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt index f26279712..a2828f3a3 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt @@ -16,7 +16,6 @@ package com.keylesspalace.tusky.components.compose import android.Manifest -import android.app.NotificationManager import android.app.ProgressDialog import android.content.ClipData import android.content.Context @@ -207,22 +206,7 @@ class ComposeActivity : public override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - val notificationId = intent.getIntExtra(NOTIFICATION_ID_EXTRA, -1) - if (notificationId != -1) { - // ComposeActivity was opened from a notification, delete the notification - val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager - notificationManager.cancel(notificationId) - } - - // If started from an intent then compose as the account ID from the intent. - // Otherwise use the active account. If null then the user is not logged in, - // and return from the activity. - val intentAccountId = intent.getLongExtra(ACCOUNT_ID_EXTRA, -1) - activeAccount = if (intentAccountId != -1L) { - accountManager.getAccountById(intentAccountId) - } else { - accountManager.activeAccount - } ?: return + activeAccount = accountManager.activeAccount ?: return val theme = preferences.getString("appTheme", APP_THEME_DEFAULT) if (theme == "black") { @@ -280,7 +264,7 @@ class ComposeActivity : binding.composeScheduleView.setDateTime(composeOptions?.scheduledAt) } - setupLanguageSpinner(getInitialLanguages(composeOptions?.language, accountManager.activeAccount)) + setupLanguageSpinner(getInitialLanguages(composeOptions?.language, activeAccount)) setupComposeField(preferences, viewModel.startingText) setupContentWarningField(composeOptions?.contentWarning) setupPollView() @@ -1355,8 +1339,6 @@ class ComposeActivity : private const val PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 1 internal const val COMPOSE_OPTIONS_EXTRA = "COMPOSE_OPTIONS" - private const val NOTIFICATION_ID_EXTRA = "NOTIFICATION_ID" - private const val ACCOUNT_ID_EXTRA = "ACCOUNT_ID" private const val PHOTO_UPLOAD_URI_KEY = "PHOTO_UPLOAD_URI" private const val VISIBILITY_KEY = "VISIBILITY" private const val SCHEDULED_TIME_KEY = "SCHEDULE" @@ -1364,26 +1346,15 @@ class ComposeActivity : /** * @param options ComposeOptions to configure the ComposeActivity - * @param notificationId the id of the notification that starts the Activity - * @param accountId the id of the account to compose with, null for the current account * @return an Intent to start the ComposeActivity */ @JvmStatic - @JvmOverloads fun startIntent( context: Context, - options: ComposeOptions, - notificationId: Int? = null, - accountId: Long? = null + options: ComposeOptions ): Intent { return Intent(context, ComposeActivity::class.java).apply { putExtra(COMPOSE_OPTIONS_EXTRA, options) - if (notificationId != null) { - putExtra(NOTIFICATION_ID_EXTRA, notificationId) - } - if (accountId != null) { - putExtra(ACCOUNT_ID_EXTRA, accountId) - } } } diff --git a/app/src/main/java/com/keylesspalace/tusky/components/notifications/NotificationHelper.java b/app/src/main/java/com/keylesspalace/tusky/components/notifications/NotificationHelper.java index 481098934..a0c5a8ed5 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/notifications/NotificationHelper.java +++ b/app/src/main/java/com/keylesspalace/tusky/components/notifications/NotificationHelper.java @@ -85,13 +85,6 @@ public class NotificationHelper { /** Dynamic notification IDs start here */ private static int notificationId = NOTIFICATION_ID_PRUNE_CACHE + 1; - /** - * constants used in Intents - */ - public static final String ACCOUNT_ID = "account_id"; - - public static final String TYPE = APPLICATION_ID + ".notification.type"; - private static final String TAG = "NotificationHelper"; public static final String REPLY_ACTION = "REPLY_ACTION"; @@ -325,11 +318,10 @@ public class NotificationHelper { // Create a notification that summarises the other notifications in this group // All notifications in this group have the same type, so get it from the first. - String notificationType = members.get(0).getNotification().extras.getString(EXTRA_NOTIFICATION_TYPE); + Notification.Type notificationType = (Notification.Type)members.get(0).getNotification().extras.getSerializable(EXTRA_NOTIFICATION_TYPE); + + Intent summaryResultIntent = MainActivity.openNotificationIntent(context, accountId, notificationType); - Intent summaryResultIntent = new Intent(context, MainActivity.class); - summaryResultIntent.putExtra(ACCOUNT_ID, (long) accountId); - summaryResultIntent.putExtra(TYPE, notificationType); TaskStackBuilder summaryStackBuilder = TaskStackBuilder.create(context); summaryStackBuilder.addParentStack(MainActivity.class); summaryStackBuilder.addNextIntent(summaryResultIntent); @@ -373,10 +365,8 @@ public class NotificationHelper { private static NotificationCompat.Builder newAndroidNotification(Context context, Notification body, AccountEntity account) { - // we have to switch account here - Intent eventResultIntent = new Intent(context, MainActivity.class); - eventResultIntent.putExtra(ACCOUNT_ID, account.getId()); - eventResultIntent.putExtra(TYPE, body.getType().name()); + Intent eventResultIntent = MainActivity.openNotificationIntent(context, account.getId(), body.getType()); + TaskStackBuilder eventStackBuilder = TaskStackBuilder.create(context); eventStackBuilder.addParentStack(MainActivity.class); eventStackBuilder.addNextIntent(eventResultIntent); @@ -464,12 +454,7 @@ public class NotificationHelper { composeOptions.setLanguage(actionableStatus.getLanguage()); composeOptions.setKind(ComposeActivity.ComposeKind.NEW); - Intent composeIntent = ComposeActivity.startIntent( - context, - composeOptions, - notificationId, - account.getId() - ); + Intent composeIntent = MainActivity.composeIntent(context, composeOptions, account.getId(), body.getId(), (int)account.getId()); composeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); diff --git a/app/src/main/java/com/keylesspalace/tusky/service/SendStatusService.kt b/app/src/main/java/com/keylesspalace/tusky/service/SendStatusService.kt index cf03115b3..3aaad1b70 100644 --- a/app/src/main/java/com/keylesspalace/tusky/service/SendStatusService.kt +++ b/app/src/main/java/com/keylesspalace/tusky/service/SendStatusService.kt @@ -379,9 +379,7 @@ class SendStatusService : Service(), Injectable { accountId: Long, statusId: Int ): Notification { - val intent = Intent(this, MainActivity::class.java) - intent.putExtra(NotificationHelper.ACCOUNT_ID, accountId) - intent.putExtra(MainActivity.OPEN_DRAFTS, true) + val intent = MainActivity.draftIntent(this, accountId) val pendingIntent = PendingIntent.getActivity( this, diff --git a/app/src/main/java/com/keylesspalace/tusky/service/TuskyTileService.kt b/app/src/main/java/com/keylesspalace/tusky/service/TuskyTileService.kt index 1e170da84..2bf761f9f 100644 --- a/app/src/main/java/com/keylesspalace/tusky/service/TuskyTileService.kt +++ b/app/src/main/java/com/keylesspalace/tusky/service/TuskyTileService.kt @@ -19,6 +19,7 @@ import android.annotation.TargetApi import android.content.Intent import android.service.quicksettings.TileService import com.keylesspalace.tusky.MainActivity +import com.keylesspalace.tusky.components.compose.ComposeActivity /** * Small Addition that adds in a QuickSettings tile @@ -29,11 +30,8 @@ import com.keylesspalace.tusky.MainActivity class TuskyTileService : TileService() { override fun onClick() { - val intent = Intent(this, MainActivity::class.java).apply { - addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - action = Intent.ACTION_SEND - type = "text/plain" - } + val intent = MainActivity.composeIntent(this, ComposeActivity.ComposeOptions()) + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) startActivityAndCollapse(intent) } } diff --git a/app/src/main/java/com/keylesspalace/tusky/util/ShareShortcutHelper.kt b/app/src/main/java/com/keylesspalace/tusky/util/ShareShortcutHelper.kt index 9d8e4b238..800671ead 100644 --- a/app/src/main/java/com/keylesspalace/tusky/util/ShareShortcutHelper.kt +++ b/app/src/main/java/com/keylesspalace/tusky/util/ShareShortcutHelper.kt @@ -29,7 +29,6 @@ import androidx.core.graphics.drawable.IconCompat import com.bumptech.glide.Glide import com.keylesspalace.tusky.MainActivity import com.keylesspalace.tusky.R -import com.keylesspalace.tusky.components.notifications.NotificationHelper import com.keylesspalace.tusky.db.AccountEntity import io.reactivex.rxjava3.core.Single import io.reactivex.rxjava3.schedulers.Schedulers @@ -72,7 +71,7 @@ fun updateShortcut(context: Context, account: AccountEntity) { val intent = Intent(context, MainActivity::class.java).apply { action = Intent.ACTION_SEND type = "text/plain" - putExtra(NotificationHelper.ACCOUNT_ID, account.id) + putExtra(ShortcutManagerCompat.EXTRA_SHORTCUT_ID, account.id.toString()) } val shortcutInfo = ShortcutInfoCompat.Builder(context, account.id.toString())