2019-01-29 19:20:29 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2019-07-12 18:38:15 +00:00
|
|
|
* 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/. */
|
2019-01-29 19:20:29 +00:00
|
|
|
|
2019-01-29 20:46:11 +00:00
|
|
|
package org.mozilla.fenix
|
|
|
|
|
|
|
|
import android.app.Activity
|
|
|
|
import android.content.Intent
|
|
|
|
import android.os.Bundle
|
2019-09-18 16:47:30 +00:00
|
|
|
import androidx.annotation.VisibleForTesting
|
2019-07-22 13:49:43 +00:00
|
|
|
import kotlinx.coroutines.MainScope
|
|
|
|
import kotlinx.coroutines.launch
|
2020-02-27 15:02:55 +00:00
|
|
|
import mozilla.components.feature.intent.processing.IntentProcessor
|
|
|
|
import org.mozilla.fenix.components.IntentProcessorType
|
2019-12-10 08:02:23 +00:00
|
|
|
import org.mozilla.fenix.components.getType
|
2019-10-25 00:16:49 +00:00
|
|
|
import org.mozilla.fenix.components.metrics.Event
|
2019-01-29 20:46:11 +00:00
|
|
|
import org.mozilla.fenix.ext.components
|
2019-09-19 16:12:42 +00:00
|
|
|
import org.mozilla.fenix.ext.settings
|
2020-02-20 23:15:24 +00:00
|
|
|
import org.mozilla.fenix.perf.StartupTimeline
|
2019-09-26 17:45:19 +00:00
|
|
|
import org.mozilla.fenix.shortcut.NewTabShortcutIntentProcessor
|
2019-01-29 20:46:11 +00:00
|
|
|
|
2019-09-19 16:12:42 +00:00
|
|
|
/**
|
|
|
|
* Processes incoming intents and sends them to the corresponding activity.
|
|
|
|
*/
|
2019-01-29 20:46:11 +00:00
|
|
|
class IntentReceiverActivity : Activity() {
|
|
|
|
|
2019-09-18 16:47:30 +00:00
|
|
|
@VisibleForTesting
|
2019-01-29 20:46:11 +00:00
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
|
2019-07-22 13:49:43 +00:00
|
|
|
MainScope().launch {
|
|
|
|
// The intent property is nullable, but the rest of the code below
|
|
|
|
// assumes it is not. If it's null, then we make a new one and open
|
|
|
|
// the HomeActivity.
|
2020-05-29 00:43:40 +00:00
|
|
|
val intent = intent?.let { Intent(it) } ?: Intent()
|
2020-02-27 15:02:55 +00:00
|
|
|
intent.stripUnwantedFlags()
|
2019-09-20 18:33:55 +00:00
|
|
|
processIntent(intent)
|
|
|
|
}
|
2020-02-20 23:15:24 +00:00
|
|
|
|
|
|
|
StartupTimeline.onActivityCreateEndIntentReceiver()
|
2019-09-20 18:33:55 +00:00
|
|
|
}
|
2019-07-22 13:49:43 +00:00
|
|
|
|
2019-09-20 18:33:55 +00:00
|
|
|
suspend fun processIntent(intent: Intent) {
|
2020-02-27 15:02:55 +00:00
|
|
|
// Call process for side effects, short on the first that returns true
|
|
|
|
val processor = getIntentProcessors().firstOrNull { it.process(intent) }
|
|
|
|
val intentProcessorType = components.intentProcessors.getType(processor)
|
|
|
|
|
|
|
|
launch(intent, intentProcessorType)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun launch(intent: Intent, intentProcessorType: IntentProcessorType) {
|
|
|
|
intent.setClassName(applicationContext, intentProcessorType.activityClassName)
|
2020-05-26 11:56:36 +00:00
|
|
|
|
|
|
|
if (!intent.hasExtra(HomeActivity.OPEN_TO_BROWSER)) {
|
|
|
|
intent.putExtra(
|
|
|
|
HomeActivity.OPEN_TO_BROWSER,
|
|
|
|
intentProcessorType.shouldOpenToBrowser(intent)
|
|
|
|
)
|
|
|
|
}
|
2019-10-03 19:43:33 +00:00
|
|
|
|
2020-02-27 15:02:55 +00:00
|
|
|
startActivity(intent)
|
|
|
|
finish() // must finish() after starting the other activity
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun getIntentProcessors(): List<IntentProcessor> {
|
|
|
|
val modeDependentProcessors = if (settings().openLinksInAPrivateTab) {
|
2019-12-10 08:02:23 +00:00
|
|
|
components.analytics.metrics.track(Event.OpenedLink(Event.OpenedLink.Mode.PRIVATE))
|
2020-02-17 13:17:36 +00:00
|
|
|
intent.putExtra(HomeActivity.PRIVATE_BROWSING_MODE, true)
|
2019-12-10 08:02:23 +00:00
|
|
|
listOf(
|
|
|
|
components.intentProcessors.privateCustomTabIntentProcessor,
|
|
|
|
components.intentProcessors.privateIntentProcessor
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
components.analytics.metrics.track(Event.OpenedLink(Event.OpenedLink.Mode.NORMAL))
|
2020-02-25 19:53:48 +00:00
|
|
|
intent.putExtra(HomeActivity.PRIVATE_BROWSING_MODE, false)
|
2019-12-10 08:02:23 +00:00
|
|
|
listOf(
|
|
|
|
components.intentProcessors.customTabIntentProcessor,
|
|
|
|
components.intentProcessors.intentProcessor
|
|
|
|
)
|
|
|
|
}
|
2019-09-18 16:47:30 +00:00
|
|
|
|
2020-02-27 15:02:55 +00:00
|
|
|
return listOf(components.intentProcessors.migrationIntentProcessor) +
|
|
|
|
components.intentProcessors.externalAppIntentProcessors +
|
2020-05-22 09:15:15 +00:00
|
|
|
components.intentProcessors.fennecPageShortcutIntentProcessor +
|
2020-02-27 15:02:55 +00:00
|
|
|
modeDependentProcessors +
|
|
|
|
NewTabShortcutIntentProcessor()
|
|
|
|
}
|
|
|
|
}
|
2019-12-06 18:57:54 +00:00
|
|
|
|
2020-02-27 15:02:55 +00:00
|
|
|
private fun Intent.stripUnwantedFlags() {
|
|
|
|
// Explicitly remove the new task and clear task flags (Our browser activity is a single
|
|
|
|
// task activity and we never want to start a second task here).
|
|
|
|
flags = flags and Intent.FLAG_ACTIVITY_NEW_TASK.inv()
|
|
|
|
flags = flags and Intent.FLAG_ACTIVITY_CLEAR_TASK.inv()
|
2019-07-22 13:49:43 +00:00
|
|
|
|
2020-02-27 15:02:55 +00:00
|
|
|
// IntentReceiverActivity is started with the "excludeFromRecents" flag (set in manifest). We
|
|
|
|
// do not want to propagate this flag from the intent receiver activity to the browser.
|
|
|
|
flags = flags and Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS.inv()
|
2019-01-30 16:36:14 +00:00
|
|
|
}
|