From e572562c7ac2f44ca92035c49c76928373c41342 Mon Sep 17 00:00:00 2001 From: Tiger Oakes Date: Thu, 26 Sep 2019 10:45:19 -0700 Subject: [PATCH] Cleanup shortcut code --- .../mozilla/fenix/IntentReceiverActivity.kt | 28 ++--------- .../shortcut/NewTabShortcutIntentProcessor.kt | 50 +++++++++++++++++++ 2 files changed, 54 insertions(+), 24 deletions(-) create mode 100644 app/src/main/java/org/mozilla/fenix/shortcut/NewTabShortcutIntentProcessor.kt diff --git a/app/src/main/java/org/mozilla/fenix/IntentReceiverActivity.kt b/app/src/main/java/org/mozilla/fenix/IntentReceiverActivity.kt index 0478b64ef..f51053432 100644 --- a/app/src/main/java/org/mozilla/fenix/IntentReceiverActivity.kt +++ b/app/src/main/java/org/mozilla/fenix/IntentReceiverActivity.kt @@ -16,7 +16,7 @@ import mozilla.components.support.utils.Browsers import org.mozilla.fenix.customtabs.ExternalAppBrowserActivity import org.mozilla.fenix.ext.components import org.mozilla.fenix.ext.settings -import org.mozilla.fenix.home.intent.StartSearchIntentProcessor +import org.mozilla.fenix.shortcut.NewTabShortcutIntentProcessor /** * Processes incoming intents and sends them to the corresponding activity. @@ -58,8 +58,9 @@ class IntentReceiverActivity : Activity() { components.intentProcessors.intentProcessor } - val intentProcessors = - components.intentProcessors.externalAppIntentProcessors + tabIntentProcessor + val intentProcessors = components.intentProcessors.externalAppIntentProcessors + + tabIntentProcessor + + NewTabShortcutIntentProcessor() intentProcessors.any { it.process(intent) } setIntentActivity(intent, tabIntentProcessor) @@ -86,24 +87,6 @@ class IntentReceiverActivity : Activity() { // from a session that the user already "erased". intent.flags and Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY == 0 } - intent.action == ACTION_OPEN_TAB || intent.action == ACTION_OPEN_PRIVATE_TAB -> { - intent.setClassName(applicationContext, HomeActivity::class.java.name) - val startPrivateMode = (intent.action == ACTION_OPEN_PRIVATE_TAB) - if (startPrivateMode) { - intent.putExtra( - HomeActivity.OPEN_TO_SEARCH, - StartSearchIntentProcessor.STATIC_SHORTCUT_NEW_PRIVATE_TAB - ) - } else { - intent.putExtra( - HomeActivity.OPEN_TO_SEARCH, - StartSearchIntentProcessor.STATIC_SHORTCUT_NEW_TAB - ) - } - intent.putExtra(HomeActivity.PRIVATE_BROWSING_MODE, startPrivateMode) - intent.flags = intent.flags or Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK - false - } else -> { intent.setClassName(applicationContext, HomeActivity::class.java.name) false @@ -116,8 +99,5 @@ class IntentReceiverActivity : Activity() { companion object { // This constant must match the metadata from the private activity-alias const val LAUNCH_PRIVATE_LINK = "org.mozilla.fenix.LAUNCH_PRIVATE_LINK" - - const val ACTION_OPEN_TAB = "org.mozilla.fenix.OPEN_TAB" - const val ACTION_OPEN_PRIVATE_TAB = "org.mozilla.fenix.OPEN_PRIVATE_TAB" } } diff --git a/app/src/main/java/org/mozilla/fenix/shortcut/NewTabShortcutIntentProcessor.kt b/app/src/main/java/org/mozilla/fenix/shortcut/NewTabShortcutIntentProcessor.kt new file mode 100644 index 000000000..1c21ae1dc --- /dev/null +++ b/app/src/main/java/org/mozilla/fenix/shortcut/NewTabShortcutIntentProcessor.kt @@ -0,0 +1,50 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +package org.mozilla.fenix.shortcut + +import android.content.Intent +import android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK +import android.content.Intent.FLAG_ACTIVITY_NEW_TASK +import mozilla.components.feature.intent.processing.IntentProcessor +import mozilla.components.support.utils.SafeIntent +import org.mozilla.fenix.HomeActivity +import org.mozilla.fenix.home.intent.StartSearchIntentProcessor + +class NewTabShortcutIntentProcessor : IntentProcessor { + + /** + * Returns true if this intent processor will handle the intent. + */ + override fun matches(intent: Intent): Boolean { + val safeIntent = SafeIntent(intent) + return safeIntent.action == ACTION_OPEN_TAB || safeIntent.action == ACTION_OPEN_PRIVATE_TAB + } + + /** + * Processes the given [Intent]. + * + * @param intent The intent to process. + * @return True if the intent was processed, otherwise false. + */ + override suspend fun process(intent: Intent): Boolean { + val safeIntent = SafeIntent(intent) + val (searchExtra, startPrivateMode) = when (safeIntent.action) { + ACTION_OPEN_TAB -> StartSearchIntentProcessor.STATIC_SHORTCUT_NEW_TAB to false + ACTION_OPEN_PRIVATE_TAB -> StartSearchIntentProcessor.STATIC_SHORTCUT_NEW_PRIVATE_TAB to true + else -> return false + } + + intent.putExtra(HomeActivity.OPEN_TO_SEARCH, searchExtra) + intent.putExtra(HomeActivity.PRIVATE_BROWSING_MODE, startPrivateMode) + intent.flags = intent.flags or FLAG_ACTIVITY_NEW_TASK or FLAG_ACTIVITY_CLEAR_TASK + + return true + } + + companion object { + const val ACTION_OPEN_TAB = "org.mozilla.fenix.OPEN_TAB" + const val ACTION_OPEN_PRIVATE_TAB = "org.mozilla.fenix.OPEN_PRIVATE_TAB" + } +}