-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainActivity.java
294 lines (244 loc) · 10 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package com.example.app;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.Context;
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.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.example.project_aid.R;
import java.util.ArrayList;
import java.util.Locale;
import android.media.AudioManager;
import android.media.ToneGenerator;
import android.os.Vibrator;
public class MainActivity extends AppCompatActivity {
public static final Integer RecordAudioRequestCode = 1;
private SpeechRecognizer speechRecognizer;
private EditText editText;
private ImageView micButton;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(ContextCompat.checkSelfPermission(this,Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED){
checkPermission();
}
editText = findViewById(R.id.text);
micButton = findViewById(R.id.button);
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
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());
// initializing string arrays
final String[] AlphaNumeric = new String[37];
// string array for storing alphabets and numbers
final String[] AlphaNumeric1 = new String[37];
final String[] AlphaNumeric2 = new String[4];
// string array for storing corresponding morse code
// assigning alphabets to the string array Alphanumeric[]
AlphaNumeric[0] = "A";
AlphaNumeric[1] = "B";
AlphaNumeric[2] = "C";
AlphaNumeric[3] = "D";
AlphaNumeric[4] = "E";
AlphaNumeric[5] = "F";
AlphaNumeric[6] = "G";
AlphaNumeric[7] = "H";
AlphaNumeric[8] = "I";
AlphaNumeric[9] = "J";
AlphaNumeric[10] = "K";
AlphaNumeric[11] = "L";
AlphaNumeric[12] = "M";
AlphaNumeric[13] = "N";
AlphaNumeric[14] = "O";
AlphaNumeric[15] = "P";
AlphaNumeric[16] = "Q";
AlphaNumeric[17] = "R";
AlphaNumeric[18] = "S";
AlphaNumeric[19] = "T";
AlphaNumeric[20] = "U";
AlphaNumeric[21] = "V";
AlphaNumeric[22] = "W";
AlphaNumeric[23] = "X";
AlphaNumeric[24] = "Y";
AlphaNumeric[25] = "Z";
AlphaNumeric[26] = "0";
AlphaNumeric[27] = "1";
AlphaNumeric[28] = "2";
AlphaNumeric[29] = "3";
AlphaNumeric[30] = "4";
AlphaNumeric[31] = "5";
AlphaNumeric[32] = "6";
AlphaNumeric[33] = "7";
AlphaNumeric[34] = "8";
AlphaNumeric[35] = "9";
AlphaNumeric[36] = " ";
// assigning the corresponding morse code
// for each letter and number to
// Alphanumeric1[] array
AlphaNumeric1[0] = ".-";
AlphaNumeric1[1] = "-...";
AlphaNumeric1[2] = "-.-.";
AlphaNumeric1[3] = "-..";
AlphaNumeric1[4] = ".";
AlphaNumeric1[5] = "..-.";
AlphaNumeric1[6] = "--.";
AlphaNumeric1[7] = "....";
AlphaNumeric1[8] = "..";
AlphaNumeric1[9] = ".---";
AlphaNumeric1[10] = "-.-";
AlphaNumeric1[11] = ".-..";
AlphaNumeric1[12] = "--";
AlphaNumeric1[13] = "-.";
AlphaNumeric1[14] = "---";
AlphaNumeric1[15] = ".--.";
AlphaNumeric1[16] = "--.-";
AlphaNumeric1[17] = ".-.";
AlphaNumeric1[18] = "...";
AlphaNumeric1[19] = "-";
AlphaNumeric1[20] = "..-";
AlphaNumeric1[21] = "...-";
AlphaNumeric1[22] = ".--";
AlphaNumeric1[23] = "-..-";
AlphaNumeric1[24] = "-.--";
AlphaNumeric1[25] = "--..";
AlphaNumeric1[26] = "-----";
AlphaNumeric1[27] = ".----";
AlphaNumeric1[28] = "..---";
AlphaNumeric1[29] = "...--";
AlphaNumeric1[30] = "....-";
AlphaNumeric1[31] = ".....";
AlphaNumeric1[32] = "-....";
AlphaNumeric1[33] = "--...";
AlphaNumeric1[34] = "---..";
AlphaNumeric1[35] = "----.";
AlphaNumeric1[36] = "/";
AlphaNumeric2[0] = ".";
AlphaNumeric2[1] = "-";
AlphaNumeric2[2] = "/";
AlphaNumeric2[3] = "0";
speechRecognizer.setRecognitionListener(new RecognitionListener() {
@Override
public void onReadyForSpeech(Bundle bundle) {
}
@Override
public void onBeginningOfSpeech() {
editText.setText("");
editText.setHint("Listening...");
}
@Override
public void onRmsChanged(float v) {
}
@Override
public void onBufferReceived(byte[] bytes) {
}
@Override
public void onEndOfSpeech() {
}
@Override
public void onError(int i) {
}
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onResults(Bundle bundle) {
micButton.setImageResource(R.drawable.ic_mic_black_off);
ArrayList<String> data = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
String input = data.get(0);
String output = "";
// variable used to compute the output
// to get the length of the input string
int l = input.length();
// variables used in loops
int i, j;
for (i = 0; i < l; i++) {
// to extract each Token of the string at a time
String ch = input.substring(i, i + 1);
// the loop to check the extracted token with
// each letter and store the morse code in
// the output variable accordingly
for (j = 0; j < 37; j++) {
if (ch.equalsIgnoreCase(AlphaNumeric[j])) {
// concat space is used to separate
// the morse code of each token
output = output.concat(AlphaNumeric1[j]).concat(" ");
}
}
for (i = 0; i < l; i++) {
// to extract each Token of the string at a time
String bee = output.substring(i, i + 1);
// the loop to check the extracted token with
// the output variable according to the morse code
if (bee.equalsIgnoreCase(AlphaNumeric2[0])) {
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 400 milliseconds
v.vibrate(400);
}
if (bee.equalsIgnoreCase(AlphaNumeric2[1])) {
ToneGenerator toneGen1 = new ToneGenerator(AudioManager.STREAM_MUSIC, 100);
toneGen1.startTone(ToneGenerator.TONE_CDMA_PIP,400);
}
if (bee.equalsIgnoreCase(AlphaNumeric2[2])) {
ToneGenerator toneGen1 = new ToneGenerator(AudioManager.STREAM_MUSIC, 0);
toneGen1.startTone(ToneGenerator.TONE_CDMA_PIP,600);
}
if (bee.equalsIgnoreCase(AlphaNumeric2[3])) {
ToneGenerator toneGen1 = new ToneGenerator(AudioManager.STREAM_MUSIC, 0);
toneGen1.startTone(ToneGenerator.TONE_CDMA_PIP,200);
}
}
}
editText.setText(data.get(0));
}
@Override
public void onPartialResults(Bundle bundle) {
}
@Override
public void onEvent(int i, Bundle bundle) {
}
});
micButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_UP){
speechRecognizer.stopListening();
}
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN){
micButton.setImageResource(R.drawable.ic_mic_black_24dp);
speechRecognizer.startListening(speechRecognizerIntent);
}
return false;
}
});
}
@Override
protected void onDestroy() {
super.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();
}
}
}