From 1f31e23ec2ebe5a82366652bd6dea15cefdd2aea Mon Sep 17 00:00:00 2001 From: nordfalk Date: Fri, 8 Jul 2022 13:30:32 +0200 Subject: [PATCH] Movis al pako "dk.nordfalk.esperanto.parolrekono" kaj faris apartan bonvenigan ekranon --- app/build.gradle | 2 +- app/src/main/AndroidManifest.xml | 13 +- .../esperanto/parolrekono/MainActivity.java | 155 ++++++++++++++++++ .../parolrekono}/SpeechActivity.java | 6 +- app/src/main/res/layout/speech_activity.xml | 4 +- app/src/main/res/values/strings.xml | 5 +- 6 files changed, 173 insertions(+), 12 deletions(-) create mode 100644 app/src/main/java/dk/nordfalk/esperanto/parolrekono/MainActivity.java rename app/src/main/java/{cat/oreilly/localstt => dk/nordfalk/esperanto/parolrekono}/SpeechActivity.java (98%) diff --git a/app/build.gradle b/app/build.gradle index 1d743de..7427128 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -17,7 +17,7 @@ android { } compileSdkVersion 30 defaultConfig { - applicationId "cat.oreilly.localstt" + applicationId "dk.nordfalk.esperanto.parolrekono" minSdkVersion 21 targetSdkVersion 30 versionCode 1 diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 20a0cbd..9ad8fbb 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,5 +1,6 @@ @@ -23,7 +24,7 @@ android:theme="@style/AppTheme"> @@ -87,19 +88,21 @@ --> + + - - - + diff --git a/app/src/main/java/dk/nordfalk/esperanto/parolrekono/MainActivity.java b/app/src/main/java/dk/nordfalk/esperanto/parolrekono/MainActivity.java new file mode 100644 index 0000000..e4fb21a --- /dev/null +++ b/app/src/main/java/dk/nordfalk/esperanto/parolrekono/MainActivity.java @@ -0,0 +1,155 @@ +// Copyright 2020 Ciaran O'Reilly +// Copyright 2011-2020, Institute of Cybernetics at Tallinn University of Technology +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +package dk.nordfalk.esperanto.parolrekono; + +import android.Manifest; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.os.Build; +import android.os.Bundle; +import android.speech.RecognitionListener; +import android.speech.RecognizerIntent; +import android.speech.SpeechRecognizer; +import android.util.Log; +import android.widget.EditText; +import android.widget.Toast; + +import androidx.annotation.NonNull; +import androidx.appcompat.app.AppCompatActivity; +import androidx.core.app.ActivityCompat; +import androidx.core.content.ContextCompat; + +import java.util.ArrayList; +import java.util.Locale; + +import cat.oreilly.localstt.R; + +public class MainActivity extends AppCompatActivity { + protected static final String TAG = MainActivity.class.getSimpleName(); + + public static final Integer RecordAudioRequestCode = 1; + private SpeechRecognizer speechRecognizer; + private EditText editText; + + protected void toast(String message) { + Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show(); + } + + @Override + protected void onCreate(final Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.speech_activity); + if (ContextCompat.checkSelfPermission(this, + Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) { + checkPermission(); + } + + editText = findViewById(R.id.editText); + speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this); + + speechRecognizer.setRecognitionListener(new RecognitionListener() { + @Override + public void onReadyForSpeech(Bundle bundle) { + + } + + @Override + public void onBeginningOfSpeech() { + editText.setText(""); + editText.setHint(R.string.speaknow); + } + + @Override + public void onRmsChanged(float v) { + + } + + @Override + public void onBufferReceived(byte[] bytes) { + + } + + @Override + public void onEndOfSpeech() { + speechRecognizer.stopListening(); + } + + @Override + public void onError(int error) { + Log.i(TAG, "onError "+error); + toast("Okazis eraro dum rekono ("+error+")"); + } + + @Override + public void onResults(Bundle bundle) { + Log.i(TAG, "onResults"); + ArrayList results = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); + Log.i(TAG, results.toString()); + editText.setText(results.get(0)); + toast(String.format(getString(R.string.recognized), results.get(0))); + } + + @Override + public void onPartialResults(Bundle bundle) { + Log.i(TAG, "onPartialResults"); + ArrayList results = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); + Log.i(TAG, results.toString()); + editText.setText(results.get(0)); + } + + @Override + public void onEvent(int i, Bundle bundle) { + Log.d(TAG, bundle.toString()); + } + }); + + } + + @Override + public void onStart() { + super.onStart(); + Log.i(TAG, "onStart"); + final Intent speechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); + speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); + speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault()); + speechRecognizer.startListening(speechRecognizerIntent); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + Log.i(TAG, "onDestroy"); + speechRecognizer.destroy(); + } + + private void checkPermission() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.RECORD_AUDIO }, + RecordAudioRequestCode); + } + } + + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, + @NonNull int[] grantResults) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults); + if (requestCode == RecordAudioRequestCode && grantResults.length > 0) { + if (grantResults[0] == PackageManager.PERMISSION_GRANTED) + Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT).show(); + } + } + +} diff --git a/app/src/main/java/cat/oreilly/localstt/SpeechActivity.java b/app/src/main/java/dk/nordfalk/esperanto/parolrekono/SpeechActivity.java similarity index 98% rename from app/src/main/java/cat/oreilly/localstt/SpeechActivity.java rename to app/src/main/java/dk/nordfalk/esperanto/parolrekono/SpeechActivity.java index ca39538..7eb1033 100644 --- a/app/src/main/java/cat/oreilly/localstt/SpeechActivity.java +++ b/app/src/main/java/dk/nordfalk/esperanto/parolrekono/SpeechActivity.java @@ -13,7 +13,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -package cat.oreilly.localstt; +package dk.nordfalk.esperanto.parolrekono; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; @@ -42,6 +42,8 @@ import java.util.Locale; import java.util.List; +import cat.oreilly.localstt.R; + public class SpeechActivity extends AppCompatActivity { protected static final String TAG = SpeechActivity.class.getSimpleName(); @@ -104,7 +106,7 @@ protected void onCreate(final Bundle savedInstanceState) { checkPermission(); } - editText = findViewById(R.id.text); + editText = findViewById(R.id.editText); speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this); speechRecognizer.setRecognitionListener(new RecognitionListener() { diff --git a/app/src/main/res/layout/speech_activity.xml b/app/src/main/res/layout/speech_activity.xml index d03e8c1..db00cad 100644 --- a/app/src/main/res/layout/speech_activity.xml +++ b/app/src/main/res/layout/speech_activity.xml @@ -4,7 +4,7 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - tools:context=".SpeechActivity"> + tools:context="dk.nordfalk.esperanto.parolrekono.SpeechActivity"> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8423f38..0f664db 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,8 +1,9 @@ - Esperanto Parolrekono - Esperanto parolrekona servo + Esperanto parolrekono + Esperanto parolrekona servo + Parolrekono en Esperanto, bazita sur Vosk, farita de Jacob Nordfalk por la telegrama grupo \'Esperanto kaj Teĥniko\' Rekonis: %1$s Pretiĝas... Parolu nun!