1
0
Fork 0
fenix/app/src/main/java/org/mozilla/fenix/ext/Context.kt

60 lines
1.9 KiB
Kotlin

/* 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.ext
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.content.Intent.*
import androidx.annotation.StringRes
import mozilla.components.support.base.log.Log
import mozilla.components.support.base.log.Log.Priority.WARN
import org.mozilla.fenix.FenixApplication
import org.mozilla.fenix.R
import org.mozilla.fenix.components.Components
/**
* Get the BrowserApplication object from a context.
*/
val Context.application: FenixApplication
get() = applicationContext as FenixApplication
/**
* Get the requireComponents of this application.
*/
val Context.components: Components
get() = application.components
fun Context.getPreferenceKey(@StringRes resourceId: Int): String =
resources.getString(resourceId)
/**
* Shares content via [ACTION_SEND] intent.
*
* @param text the data to be shared [EXTRA_TEXT]
* @param subject of the intent [EXTRA_TEXT]
* @return true it is able to share false otherwise.
*/
fun Context.share(text: String, subject: String = ""): Boolean {
return try {
val intent = Intent(ACTION_SEND).apply {
type = "text/plain"
putExtra(EXTRA_SUBJECT, subject)
putExtra(EXTRA_TEXT, text)
flags = FLAG_ACTIVITY_NEW_TASK
}
val shareIntent = Intent.createChooser(intent, getString(R.string.menu_share_with)).apply {
flags = FLAG_ACTIVITY_NEW_TASK
}
startActivity(shareIntent)
true
} catch (e: ActivityNotFoundException) {
Log.log(WARN, message = "No activity to share to found", throwable = e, tag = "Reference-Browser")
false
}
}