1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
import {useCallback} from 'react'
import * as IntentLauncher from 'expo-intent-launcher'
import {getTranslatorLink} from '#/locale/helpers'
import {isAndroid} from '#/platform/detection'
import {useOpenLink} from './useOpenLink'
export function useTranslate() {
const openLink = useOpenLink()
return useCallback(
async (text: string, language: string) => {
const translateUrl = getTranslatorLink(text, language)
if (isAndroid) {
try {
// use getApplicationIconAsync to determine if the translate app is installed
if (
!(await IntentLauncher.getApplicationIconAsync(
'com.google.android.apps.translate',
))
) {
throw new Error('Translate app not installed')
}
// TODO: this should only be called one at a time, use something like
// RQ's `scope` - otherwise can trigger the browser to open unexpectedly when the call throws -sfn
await IntentLauncher.startActivityAsync(
'android.intent.action.PROCESS_TEXT',
{
type: 'text/plain',
extra: {
'android.intent.extra.PROCESS_TEXT': text,
'android.intent.extra.PROCESS_TEXT_READONLY': true,
},
// note: to skip the intermediate app select, we need to specify a
// `className`. however, this isn't safe to hardcode, we'd need to query the
// package manager for the correct activity. this requires native code, so
// skip for now -sfn
// packageName: 'com.google.android.apps.translate',
// className: 'com.google.android.apps.translate.TranslateActivity',
},
)
} catch (err) {
if (__DEV__) console.error(err)
// most likely means they don't have the translate app
await openLink(translateUrl)
}
} else {
await openLink(translateUrl)
}
},
[openLink],
)
}
|