forked from mozilla/androidspeech
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainActivity.java
276 lines (229 loc) · 10.9 KB
/
MainActivity.java
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package com.mozilla.speechapp;
import android.Manifest;
import android.app.Activity;
import android.app.DownloadManager;
import android.content.pm.PackageManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.net.Uri;
import android.os.Bundle;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;
import com.mozilla.speechlibrary.ISpeechRecognitionListener;
import com.mozilla.speechlibrary.MozillaSpeechService;
import com.mozilla.speechlibrary.STTResult;
import com.mozilla.speechmodule.R;
import java.io.File;
import net.lingala.zip4j.core.ZipFile;
import static android.support.constraint.Constraints.TAG;
public class MainActivity extends AppCompatActivity implements ISpeechRecognitionListener, CompoundButton.OnCheckedChangeListener {
private static long sDownloadId;
private static DownloadManager sDownloadManager;
private MozillaSpeechService mMozillaSpeechService;
private GraphView mGraph;
private long mDtstart;
private LineGraphSeries<DataPoint> mSeries1;
private EditText mPlain_text_input;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMozillaSpeechService = MozillaSpeechService.getInstance();
initialize();
}
private void initialize() {
Button buttonStart, buttonCancel;
EditText txtProdutTag, txtLanguage;
Switch switchTranscriptions = findViewById(R.id.switchTranscriptions);
Switch switchSamples = findViewById(R.id.switchSamples);
Switch useDeepSpeech = findViewById(R.id.useDeepSpeech);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO},
123);
}
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
124);
}
buttonStart = findViewById(R.id.button_start);
buttonCancel = findViewById(R.id.button_cancel);
txtProdutTag = findViewById(R.id.txtProdutTag);
txtLanguage = findViewById(R.id.txtLanguage);
mPlain_text_input = findViewById(R.id.plain_text_input);
buttonStart.setOnClickListener((View v) -> {
try {
mMozillaSpeechService.addListener(this);
mDtstart = System.currentTimeMillis();
mSeries1.resetData(new DataPoint[0]);
mMozillaSpeechService.setLanguage(txtLanguage.getText().toString());
mMozillaSpeechService.setProductTag(txtProdutTag.getText().toString());
mMozillaSpeechService.setModelPath(getExternalFilesDir("models").getAbsolutePath());
if (mMozillaSpeechService.ensureModelInstalled()) {
mMozillaSpeechService.start(getApplicationContext());
} else {
maybeDownloadOrExtractModel(getExternalFilesDir("models").getAbsolutePath(), mMozillaSpeechService.getLanguageDir());
}
} catch (Exception e) {
Log.d(TAG, e.getLocalizedMessage());
e.printStackTrace();
}
});
buttonCancel.setOnClickListener((View v) -> {
try {
mMozillaSpeechService.cancel();
} catch (Exception e) {
Log.d(TAG, e.getLocalizedMessage());
e.printStackTrace();
}
});
switchTranscriptions.setOnCheckedChangeListener(this);
switchSamples.setOnCheckedChangeListener(this);
useDeepSpeech.setOnCheckedChangeListener(this);
switchTranscriptions.toggle();
switchSamples.toggle();
useDeepSpeech.toggle();
mGraph = findViewById(R.id.graph);
mSeries1 = new LineGraphSeries<>(new DataPoint[0]);
mGraph.addSeries(mSeries1);
mGraph.getViewport().setXAxisBoundsManual(true);
mGraph.getViewport().setScalable(true);
mGraph.getViewport().setScalableY(true);
mGraph.getViewport().setScrollable(true); // enables horizontal scrolling
mGraph.getViewport().setScrollableY(true); // enables vertical scrolling
}
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
public void onSpeechStatusChanged(MozillaSpeechService.SpeechState aState, Object aPayload){
this.runOnUiThread(() -> {
switch (aState) {
case DECODING:
mPlain_text_input.append("Decoding... \n");
break;
case MIC_ACTIVITY:
long mPointx = System.currentTimeMillis() - mDtstart;
mSeries1.appendData(new DataPoint(Math.round(mPointx) + 1, (double)aPayload * -1), true, 3000);
break;
case STT_RESULT:
String message = String.format("Success: %s (%s)", ((STTResult)aPayload).mTranscription, ((STTResult)aPayload).mConfidence);
mPlain_text_input.append(message + "\n");
removeListener();
break;
case START_LISTEN:
mPlain_text_input.append("Started to listen\n");
break;
case NO_VOICE:
mPlain_text_input.append("No Voice detected\n");
removeListener();
break;
case CANCELED:
mPlain_text_input.append("Canceled\n");
removeListener();
break;
case ERROR:
mPlain_text_input.append("Error:" + aPayload + " \n");
removeListener();
break;
default:
break;
}
});
}
public void removeListener() {
mMozillaSpeechService.removeListener(this);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.equals(findViewById(R.id.switchTranscriptions))) {
mMozillaSpeechService.storeTranscriptions(isChecked);
} else if (buttonView.equals(findViewById(R.id.switchSamples))) {
mMozillaSpeechService.storeSamples(isChecked);
} else if (buttonView.equals(findViewById(R.id.useDeepSpeech))) {
mMozillaSpeechService.useDeepSpeech(isChecked);
}
}
private class AsyncUnzip extends AsyncTask<String, Void, Boolean> {
@Override
protected void onPreExecute() {
Toast noModel = Toast.makeText(getApplicationContext(), "Extracting downloaded model", Toast.LENGTH_LONG);
mPlain_text_input.append("Extracting downloaded model\n");
noModel.show();
}
@Override
protected Boolean doInBackground(String...params) {
String aZipFile = params[0], aRootModelsPath = params[1];
try {
ZipFile zf = new ZipFile(aZipFile);
zf.extractAll(aRootModelsPath);
} catch (Exception e) {
Log.d(TAG, e.getLocalizedMessage());
e.printStackTrace();
}
return (new File(aZipFile)).delete();
}
@Override
protected void onPostExecute(Boolean result) {
Button buttonStart = findViewById(R.id.button_start), buttonCancel = findViewById(R.id.button_cancel);
mMozillaSpeechService.start(getApplicationContext());
buttonStart.setEnabled(true);
buttonCancel.setEnabled(true);
}
}
public void maybeDownloadOrExtractModel(String aModelsPath, String aLang) {
String zipFile = aModelsPath + "/" + aLang + ".zip";
Uri modelZipURL = Uri.parse(mMozillaSpeechService.getModelDownloadURL());
Uri modelZipFile = Uri.parse("file://" + zipFile);
Button buttonStart = findViewById(R.id.button_start), buttonCancel = findViewById(R.id.button_cancel);
buttonStart.setEnabled(false);
buttonCancel.setEnabled(false);
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(downloadId);
Cursor c = sDownloadManager.query(query);
if (c.moveToFirst()) {
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
Log.d(TAG, "Download successfull");
new AsyncUnzip().execute(zipFile, aModelsPath);
}
}
}
}
};
Toast noModel = Toast.makeText(getApplicationContext(), "No model has been found for language '" + aLang + "'. Triggering download ...", Toast.LENGTH_LONG);
mPlain_text_input.append("No model has been found for language '" + aLang + "'. Triggering download ...\n");
noModel.show();
sDownloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(modelZipURL);
request.setTitle("DeepSpeech " + aLang);
request.setDescription("DeepSpeech Model");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setVisibleInDownloadsUi(false);
request.setDestinationUri(modelZipFile);
sDownloadId = sDownloadManager.enqueue(request);
getApplicationContext().registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
}