/
VoiceCompass.java
119 lines (104 loc) · 4.7 KB
/
VoiceCompass.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
package com.alejandros.npitoolkit;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class VoiceCompass extends AppCompatActivity {
private static final int VOICE_RECOGNITION = 0;
public final static String EXTRA_MESSAGE = "com.alejandros.npitoolkit.VoiceCompass";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_voice_compass);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
public boolean isConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo net = cm.getActiveNetworkInfo();
if (net!=null && net.isAvailable() && net.isConnected()) {
return true;
} else {
return false;
}
}
public void onClick(View view){
// We need internet connection for the RecognizerIntent
if(isConnected()){
// Calls the ReconizerIntent activity
Intent recognizer = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
recognizer.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
// Will provide the result as string
recognizer.putExtra(RecognizerIntent.EXTRA_PROMPT, getString(R.string.compass_initMessage));
startActivityForResult(recognizer, VOICE_RECOGNITION);
}
else{
Toast.makeText(getApplicationContext(), getString(R.string.compass_internetError), Toast.LENGTH_LONG).show();
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
// Check recognition voice Intent
if (requestCode == VOICE_RECOGNITION && resultCode == RESULT_OK){
// Results of the recognition order by the successfull trust
ArrayList<String> recognitionResult = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
String message = recognitionResult.get(0).toString().toLowerCase();
Toast.makeText(this, getString(R.string.text_recognized) + message, Toast.LENGTH_LONG).show();
int orientation = calculateProvidedOrientation(message);
int error = calculateErrorMargin(message);
int[] orientation_error = {orientation, error};
//Toast.makeText(this,"El error reconocido es:" + Integer.toString(error), Toast.LENGTH_LONG).show();
// First checks that the message have a cardinal point as first word and then starts the compass activity
if(checkMessage(message)){
Intent intent = new Intent(this, Compass.class);
intent.putExtra(EXTRA_MESSAGE, orientation_error);
startActivity(intent);
}else {
Toast.makeText(this, R.string.compass_suggestion, Toast.LENGTH_LONG).show();
}
}
}
/**
* Check that the message looks like: "<direction>"
*/
protected boolean checkMessage(String message){
List<String> myList = Arrays.asList("north","south","east","west","norte","sur","este","oeste");
boolean validMessage;
for (String point : myList) {
if (message.contains(point)) {
return true;
}
}
return false;
}
// Translate the words in degrees
protected int calculateProvidedOrientation(String message) {
if (message.startsWith("north") || message.startsWith("norte")) {
return 0;
} else if (message.startsWith("east") || message.startsWith("este")) {
return 90;
} else if (message.startsWith("south") || message.startsWith("sur")) {
return 180;
} else if (message.startsWith("west") || message.startsWith("oeste")) {
return 270;
}
else {
return 0;
}
}
// Ignore the words that isn't numbers and convert those string to int
protected int calculateErrorMargin(String message){
return 0 + Integer.parseInt(message.replaceAll("[^0-9]",""));
}
}