Last active
November 24, 2021 16:34
-
-
Save pchmielowski/d236f06f168c05efa3f5b26f1e2f0af9 to your computer and use it in GitHub Desktop.
Opening Google Translate from your app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.chmielowski.translatetest | |
import android.content.ActivityNotFoundException | |
import android.content.Context | |
import android.content.Intent | |
import android.content.pm.ActivityInfo | |
import android.content.pm.ResolveInfo | |
import android.os.Bundle | |
import androidx.activity.ComponentActivity | |
import androidx.activity.compose.setContent | |
import androidx.compose.foundation.layout.padding | |
import androidx.compose.material.Icon | |
import androidx.compose.material.IconButton | |
import androidx.compose.material.MaterialTheme | |
import androidx.compose.material.Scaffold | |
import androidx.compose.material.Text | |
import androidx.compose.material.TopAppBar | |
import androidx.compose.material.icons.Icons | |
import androidx.compose.material.icons.filled.GTranslate | |
import androidx.compose.runtime.Composable | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.unit.dp | |
import net.chmielowski.translatetest.ui.theme.TranslateTestTheme | |
// Source: https://en.wikipedia.org/wiki/Dog | |
private const val dogDescription = | |
"The dog or domestic dog, (Canis familiaris or Canis lupus familiaris) is a domesticated descendant of the wolf which is characterized by an upturning tail. The dog derived from an ancient, extinct wolf, and the modern grey wolf is the dog's nearest living relative. The dog was the first species to be domesticated, by hunter–gatherers over 15,000 years ago, before the development of agriculture." | |
class MainActivity : ComponentActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
TranslateTestTheme { | |
val translateActivity = googleTranslateActivityInfo() | |
MainScreen( | |
title = "Dog", | |
body = dogDescription, | |
showTranslateButton = translateActivity != null, | |
onTranslateClick = { openGoogleTranslate(translateActivity!!, dogDescription) }, | |
) | |
} | |
} | |
} | |
} | |
@Composable | |
private fun MainScreen( | |
title: String, | |
body: String, | |
showTranslateButton: Boolean, | |
onTranslateClick: () -> Unit, | |
) { | |
Scaffold( | |
topBar = { | |
TopAppBar( | |
title = { Text(text = title) }, | |
actions = { | |
if (showTranslateButton) { | |
IconButton(onClick = onTranslateClick) { | |
Icon(Icons.Default.GTranslate, contentDescription = "Translate") | |
} | |
} | |
} | |
) | |
}, | |
content = { | |
Text( | |
text = body, | |
style = MaterialTheme.typography.body2, | |
modifier = Modifier.padding(16.dp), | |
) | |
}, | |
) | |
} | |
fun Context.openGoogleTranslate(activity: ActivityInfo, text: String) { | |
val intent = Intent() | |
.setAction(Intent.ACTION_PROCESS_TEXT) | |
.setType("text/plain") | |
.putExtra(Intent.EXTRA_PROCESS_TEXT, text) | |
.putExtra(Intent.EXTRA_PROCESS_TEXT_READONLY, true) | |
.setClassName(activity.packageName, activity.name) | |
try { | |
startActivity(intent) | |
} catch (e: ActivityNotFoundException) { | |
// TODO: Show error | |
} | |
} | |
/** | |
* Remember to add to AndroidManifest.xml the following: | |
* | |
* <queries> | |
* <intent> | |
* <action android:name="android.intent.action.PROCESS_TEXT" /> | |
* <data android:mimeType="text/plain" /> | |
* </intent> | |
* </queries> | |
*/ | |
private fun Context.queryProcessTextActivities(): List<ResolveInfo> { | |
val intent = Intent() | |
.setAction(Intent.ACTION_PROCESS_TEXT) | |
.setType("text/plain") | |
return packageManager.queryIntentActivities(intent, 0) | |
} | |
private fun Context.googleTranslateActivityInfo() = queryProcessTextActivities() | |
.firstOrNull { it.activityInfo.packageName == "com.google.android.apps.translate" } | |
?.activityInfo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment