feat(learn,todo,meditate): dictate through the phone's voice input app (ADR 0038, D18)
On Android the microphone now opens the app handling RECOGNIZE_SPEECH (FUTO Voice Input, Google's), the default the person chose in Android, and puts the text it returns where the cursor stood, as Dictation does; cancelled, nothing. Only where none is installed, or it can't be opened, does speech_to_text listen in line as before. halkyon_app: VoiceInputApp (one listen, an answer once closed) on the channel app.halkyon/voice_input, which DeviceSpeechInput asks first at every listen, answering SpeechStart.finished so that Dictation doesn't listen again; speech_to_text isn't touched on that route, so our app asks for no microphone. SpeechInput.listen and Dictation.start take the prompt the voice input app shows (the field's hint). Each app: VoiceInput.kt (queryIntentActivities, startActivityForResult, the first of EXTRA_RESULTS) attached in MainActivity, which hands it activity results, and RECOGNIZE_SPEECH among the manifest's queries. The privacy pages of Learn and Meditate say so. Our own Whisper and Meditate's dream recordings are dropped (D18). Whats-New: The microphone now dictates through your phone's voice input app, such as FUTO Voice Input. Whats-New-De: Das Mikrofon diktiert jetzt über die Spracheingabe-App deines Telefons, etwa FUTO Voice Input. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Tizian 「ティツィアーン」 <raisondetredev@pm.me>
This commit is contained in:
parent
4940230ef1
commit
c36bd83c6a
6 changed files with 827 additions and 27 deletions
|
|
@ -5,7 +5,9 @@
|
|||
<uses-permission android:name="android.permission.INTERNET"/>
|
||||
<!-- A dream spoken into the journal (ADR 0038, D15): the microphone, asked
|
||||
for once the person taps it, and the device's speech recogniser, which
|
||||
Android 11 and later only shows the app when it is declared below. -->
|
||||
Android 11 and later only shows the app when it is declared below.
|
||||
Through a voice input app (D18) the microphone is that app's, and this
|
||||
one asks for none. -->
|
||||
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
|
||||
<!-- No backup and no transfer to another phone (ADR 0038, D7, as
|
||||
Train's ADR 0030, D53): what is sealed comes back from the
|
||||
|
|
@ -73,5 +75,10 @@
|
|||
<intent>
|
||||
<action android:name="android.speech.RecognitionService"/>
|
||||
</intent>
|
||||
<!-- The voice input app dictation opens first (Meditate's ADR 0038, D18),
|
||||
FUTO Voice Input or Google's, which records itself. -->
|
||||
<intent>
|
||||
<action android:name="android.speech.action.RECOGNIZE_SPEECH"/>
|
||||
</intent>
|
||||
</queries>
|
||||
</manifest>
|
||||
|
|
|
|||
|
|
@ -1,13 +1,24 @@
|
|||
package app.halkyon.meditate
|
||||
|
||||
import android.content.Intent
|
||||
import io.flutter.embedding.android.FlutterActivity
|
||||
import io.flutter.embedding.engine.FlutterEngine
|
||||
|
||||
class MainActivity : FlutterActivity() {
|
||||
/** The voice input app dictation opens (ADR 0038, D18). */
|
||||
private val voiceInput = VoiceInput(this)
|
||||
|
||||
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
|
||||
super.configureFlutterEngine(flutterEngine)
|
||||
// Secrets halkyon_app shows kept out of screenshots and recents.
|
||||
ScreenGuard.attach(this, flutterEngine.dartExecutor.binaryMessenger)
|
||||
SensitiveClipboard.attach(this, flutterEngine.dartExecutor.binaryMessenger)
|
||||
voiceInput.attach(flutterEngine.dartExecutor.binaryMessenger)
|
||||
}
|
||||
|
||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||
if (!voiceInput.onActivityResult(requestCode, resultCode, data)) {
|
||||
super.onActivityResult(requestCode, resultCode, data)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,84 @@
|
|||
package app.halkyon.meditate
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.ActivityNotFoundException
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.os.Build
|
||||
import android.speech.RecognizerIntent
|
||||
import io.flutter.plugin.common.BinaryMessenger
|
||||
import io.flutter.plugin.common.MethodChannel
|
||||
|
||||
/**
|
||||
* The device's voice input app for halkyon_app's ChannelVoiceInputApp
|
||||
* (Halkyon Meditate's ADR 0038, D18): whichever app handles
|
||||
* ACTION_RECOGNIZE_SPEECH (FUTO Voice Input, Google's) opens over the app,
|
||||
* records and returns text alone, so this app asks for no microphone.
|
||||
* `installed` says whether one is (the manifest's <queries> lets Android 11
|
||||
* on show it); `listen` opens it and answers once it is closed: its first
|
||||
* result, empty where cancelled, an error where it couldn't be opened. The
|
||||
* activity hands its results to [onActivityResult].
|
||||
*/
|
||||
class VoiceInput(private val activity: Activity) {
|
||||
private var pending: MethodChannel.Result? = null
|
||||
|
||||
fun attach(messenger: BinaryMessenger) {
|
||||
MethodChannel(messenger, "app.halkyon/voice_input").setMethodCallHandler { call, result ->
|
||||
when (call.method) {
|
||||
"installed" -> result.success(installed())
|
||||
"listen" -> {
|
||||
if (pending != null) {
|
||||
result.error("busy", "The voice input app is open already", null)
|
||||
return@setMethodCallHandler
|
||||
}
|
||||
val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
|
||||
.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
|
||||
call.argument<String>("language")?.let { intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, it) }
|
||||
call.argument<String>("prompt")?.let { intent.putExtra(RecognizerIntent.EXTRA_PROMPT, it) }
|
||||
try {
|
||||
pending = result
|
||||
activity.startActivityForResult(intent, REQUEST)
|
||||
} catch (error: ActivityNotFoundException) {
|
||||
pending = null
|
||||
result.error("noApp", error.message, null)
|
||||
} catch (error: SecurityException) {
|
||||
pending = null
|
||||
result.error("refused", error.message, null)
|
||||
}
|
||||
}
|
||||
else -> result.notImplemented()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun installed(): Boolean {
|
||||
val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
|
||||
val packages = activity.packageManager
|
||||
val found = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
packages.queryIntentActivities(intent, PackageManager.ResolveInfoFlags.of(0))
|
||||
} else {
|
||||
@Suppress("DEPRECATION")
|
||||
packages.queryIntentActivities(intent, 0)
|
||||
}
|
||||
return found.isNotEmpty()
|
||||
}
|
||||
|
||||
/** Whether [requestCode] was the voice input app's, its text handed on. */
|
||||
fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?): Boolean {
|
||||
if (requestCode != REQUEST) return false
|
||||
val result = pending ?: return true
|
||||
pending = null
|
||||
val text = if (resultCode == Activity.RESULT_OK) {
|
||||
data?.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)?.firstOrNull()
|
||||
} else {
|
||||
null
|
||||
}
|
||||
result.success(text ?: "")
|
||||
return true
|
||||
}
|
||||
|
||||
private companion object {
|
||||
/** Any number the app's other activity results don't use. */
|
||||
const val REQUEST = 0x766f
|
||||
}
|
||||
}
|
||||
|
|
@ -12,6 +12,7 @@ class const NoSpeechInput() implements SpeechInput {
|
|||
required String? language,
|
||||
required ValueChanged<String> onWords,
|
||||
required VoidCallback onDone,
|
||||
String? prompt,
|
||||
}) async => SpeechStart.unavailable;
|
||||
|
||||
@override
|
||||
|
|
|
|||
|
|
@ -95,8 +95,11 @@ class _DreamScreenState() extends State<DreamScreen> {
|
|||
final messenger = ScaffoldMessenger.of(context);
|
||||
final problem = switch (await _dictation.start(
|
||||
language: Localizations.localeOf(context).toLanguageTag(),
|
||||
// Shown by the voice input app, where it shows a prompt (D18).
|
||||
prompt: l10n.dreamTextHint,
|
||||
)) {
|
||||
SpeechStart.listening => null,
|
||||
// Listening in line, or the voice input app has listened.
|
||||
SpeechStart.listening || SpeechStart.finished => null,
|
||||
// A phone without Google's services has no recogniser to listen with.
|
||||
SpeechStart.noRecognizer => words.speechNoRecognizer,
|
||||
SpeechStart.unavailable => l10n.dreamSpeechUnavailable,
|
||||
|
|
|
|||
|
|
@ -1083,6 +1083,43 @@ void main() {
|
|||
);
|
||||
});
|
||||
|
||||
testWidgets('with a voice input app installed, the microphone opens it, '
|
||||
'asking what was dreamt, and what it returns goes where the cursor '
|
||||
'stands, once; cancelled, nothing (D18)', (tester) async {
|
||||
speech.app = true;
|
||||
await newDream(tester);
|
||||
await tester.enterText(text, 'I was there.');
|
||||
tester.widget<TextField>(text).controller!.selection =
|
||||
const TextSelection.collapsed(offset: 5);
|
||||
|
||||
await tester.tap(find.byTooltip('Dictate'));
|
||||
await tester.pump();
|
||||
expect(speech.appOpen, isTrue);
|
||||
expect(speech.listening, isFalse, reason: 'nothing else listens');
|
||||
expect(speech.prompts, ['What did you dream?']);
|
||||
expect(find.byTooltip('Stop dictating'), findsOneWidget);
|
||||
|
||||
speech.answer('at the sea');
|
||||
await tester.pumpAndSettle();
|
||||
expect(
|
||||
tester.widget<TextField>(text).controller!.text,
|
||||
'I was at the sea there.',
|
||||
);
|
||||
expect(speech.languages, hasLength(1), reason: 'not opened again');
|
||||
expect(find.byTooltip('Dictate'), findsOneWidget);
|
||||
expect(find.byType(SnackBar), findsNothing);
|
||||
|
||||
await tester.tap(find.byTooltip('Dictate'));
|
||||
await tester.pump();
|
||||
speech.cancel();
|
||||
await tester.pumpAndSettle();
|
||||
expect(
|
||||
tester.widget<TextField>(text).controller!.text,
|
||||
'I was at the sea there.',
|
||||
);
|
||||
expect(find.byTooltip('Dictate'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets("the recogniser's pause doesn't end it: it listens on, the "
|
||||
'next words after the last, until stopped', (tester) async {
|
||||
await newDream(tester);
|
||||
|
|
|
|||
Loading…
Reference in a new issue