Skip to content

Commit

Permalink
Merge branch 'develop', release v0.1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliv4945 committed Apr 24, 2017
2 parents 5b3970f + c2c6dbd commit 32909f7
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 49 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android {
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "v0.1.2"
versionName "v0.1.3"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
170 changes: 122 additions & 48 deletions app/src/main/java/net/iopush/jarvis/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
import android.preference.PreferenceManager;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import android.app.Activity;
import android.content.ActivityNotFoundException;
Expand All @@ -24,18 +28,22 @@
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.NetworkResponse;
import com.android.volley.RequestQueue;
import com.android.volley.TimeoutError;
import com.android.volley.toolbox.Volley;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.Request;
import com.android.volley.VolleyError;
import com.android.volley.Response;
import com.android.volley.ServerError;
import com.android.volley.NoConnectionError;
import com.android.volley.NetworkError;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
import java.net.URLEncoder;

import android.speech.tts.TextToSpeech;

Expand All @@ -53,7 +61,11 @@ public class MainActivity extends AppCompatActivity {

private String serverUrl;
private String serverPort;
private String serverKey;
private Boolean sttAtStart;
private Boolean muteRemoteJarvis;
private Boolean muteLocalJarvis;
private String jarvisOrder;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -79,7 +91,6 @@ public void onClick(View v) {
}
});

// jarvisConversation.setMovementMethod(new ScrollingMovementMethod());

// Init TTS
ttsEngine = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
Expand All @@ -98,7 +109,10 @@ public void onInit(int status) {
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
serverUrl = SP.getString("serverUrl", "NA");
serverPort = SP.getString("serverPort", "NA");
serverKey = SP.getString("serverKey", "");
sttAtStart = SP.getBoolean("sttAtStart", false);
muteRemoteJarvis = SP.getBoolean("muteRemoteJarvis", false);
muteLocalJarvis = SP.getBoolean("muteLocalJarvis", false);
if (serverUrl == "NA") {
Intent i = new Intent(this, MyPreferencesActivity.class);
startActivity(i);
Expand All @@ -117,6 +131,9 @@ public void onResume() {
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
serverUrl = SP.getString("serverUrl", "NA");
serverPort = SP.getString("serverPort", "NA");
serverKey = SP.getString("serverKey", "");
muteRemoteJarvis = SP.getBoolean("muteRemoteJarvis", false);
muteLocalJarvis = SP.getBoolean("muteLocalJarvis", false);
// Add "http;//" if it is missing, test only the first 4 characters in case of secure address
if (!serverUrl.substring(0, 4).equals("http")) {
serverUrl = "http://" + serverUrl;
Expand Down Expand Up @@ -162,61 +179,118 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {

ArrayList<String> result = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
jarvisConversationList.add(0, new ConversationObject("You", result.get(0)));
jarvisOrder = result.get(0);
jarvisConversationList.add(0, new ConversationObject("You", jarvisOrder));
recyclerViewConversation.getAdapter().notifyItemInserted(0);
recyclerViewConversation.smoothScrollToPosition(0);
Log.i("Jarvis", "STT: " + result.get(0));

Log.i("Jarvis", "STT: " + jarvisOrder);

// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String requestUrl = serverUrl+":"+serverPort+"/?order=" + URLEncoder.encode(result.get(0));

// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, requestUrl,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("Jarvis", "Server answer: " + response);
// Parse answer
try {
JSONArray jObject = new JSONArray(response);
Log.i("Jarvis", "jObject" + jObject.toString());
for (int i=0; i<jObject.length(); i++) {
JSONObject c = jObject.getJSONObject(i);
Log.i("Jarvis", "Answer: " + c.toString());
jarvisConversationList.add(0, new ConversationObject("Jarvis", c.getString("Jarvis")));
recyclerViewConversation.getAdapter().notifyItemInserted(0);
recyclerViewConversation.smoothScrollToPosition(0);
if (android.os.Build.VERSION.SDK_INT >= 21) {
ttsEngine.speak(c.getString("Jarvis"), TextToSpeech.QUEUE_ADD, null, c.getString("Jarvis"));
} else {
ttsEngine.speak(c.getString("Jarvis"), TextToSpeech.QUEUE_ADD, null);
String requestUrl = serverUrl+":"+serverPort + "/";

// Instantiate JSON object
try {
final JSONObject jsonBody = new JSONObject();
jsonBody.put("order", jarvisOrder);
jsonBody.put("key", serverKey);
jsonBody.put("mute", muteRemoteJarvis.toString());

// Request a string response from the provided URL.
// TODO - Change to JSONArrayRequest if Json-api change
StringRequest stringRequest = new StringRequest(Request.Method.POST, requestUrl,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Parse answer
try {
JSONArray jObject = new JSONArray(response);
Log.i("Jarvis", "jObject" + jObject.toString());
for ( int i=0; i<jObject.length(); i++ ) {
JSONObject c = jObject.getJSONObject(i);
Log.i("Jarvis", "Answer: " + c.toString());
jarvisConversationList.add(0, new ConversationObject("Jarvis", c.getString("Jarvis")));
recyclerViewConversation.getAdapter().notifyItemInserted(0);
recyclerViewConversation.smoothScrollToPosition(0);
if (!muteLocalJarvis) {
if (android.os.Build.VERSION.SDK_INT >= 21) {
ttsEngine.speak(c.getString("Jarvis"), TextToSpeech.QUEUE_ADD, null, c.getString("Jarvis"));
} else {
ttsEngine.speak(c.getString("Jarvis"), TextToSpeech.QUEUE_ADD, null);
}
}
}
}
} catch (final JSONException e) {
Log.e("Main", "Json parsing error: " + e.getMessage());
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();

} catch (final JSONException e) {
Log.e("Main", "Json parsing error: " + e.getMessage());
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();

}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
String defaultMessage = new String(getString(R.string.volleyError));
Log.e("Volley", error.toString());
if (error instanceof ServerError) {
// Wrong port number
defaultMessage = getString(R.string.timeoutServerNetworkError);
Log.e("Jarvis-Volley", defaultMessage);
} else if (error instanceof NetworkError || error instanceof TimeoutError) {
// Jarvis down or wrong address
defaultMessage = getString(R.string.timeoutServerNetworkError);
Log.e("Jarvis-Volley", defaultMessage );
} else if (error instanceof NoConnectionError) {
defaultMessage = getString(R.string.timeoutServerNetworkError);
Log.e("Jarvis-Volley", defaultMessage);
} else {
NetworkResponse response = error.networkResponse;
if(response != null && response.data != null){
switch(response.statusCode) {
case 400:
String json = new String(response.data);
if (json.contains("Invalid API Key")) {
defaultMessage = getString(R.string.invalidAPIKey);
}
if (json.contains("Missing API Key") ||
json.contains("Empty API Key")) {
defaultMessage = getString(R.string.missingAPIKey);
}
break;
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY", error.toString());
// TODO - Snackbar action button
Snackbar snackbarVolleyError = Snackbar
.make(findViewById(R.id.mainActivity), R.string.volleyError, Snackbar.LENGTH_LONG);

snackbarVolleyError.show();
recyclerViewConversation.smoothScrollToPosition(0);
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
// TODO - Snackbar action button
Snackbar snackbarVolleyError = Snackbar
.make(findViewById(R.id.mainActivity), defaultMessage, Snackbar.LENGTH_LONG);

snackbarVolleyError.show();
}
}){
@Override
public byte[] getBody() {
try {
return jsonBody.toString().getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
Log.e("Jarvis", "Unsupported Encoding while trying to get the bytes of "+jsonBody.toString()+" using utf-8");
return null;
}
}
@Override
public Map<String, String> getHeaders() {
Map<String,String> params = new HashMap<String, String>();
params.put("Content-Type","application/json; charset=utf-8");
return params;
}
};
// Add the request to the RequestQueue.
queue.add(stringRequest);
} catch (final JSONException e) {
Log.e("Jarvis", e.toString());
}
}
// Hide progressBar show mic button.
btnSpeak.setVisibility(View.VISIBLE);
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,12 @@
<string name="tap_on_mic">Touchez le micro pour parler</string>
<string name="volleyError">Erreur, réessayez</string>
<string name="pref_sttAtStart">Reconnaissance vocale à l\'ouverture</string>
<string name="pref_serverKey_summary">Clef si définie, sinon rien</string>
<string name="pref_serverKey_title">Entrez la clef définie pour jarvis-api</string>
<string name="invalidAPIKey">Mauvaise clef Jarvis-api</string>
<string name="missingAPIKey">Clef Jarvis-api manquante</string>
<string name="pref_muteRemoteJarvis">Ne pas répondre sur le serveur</string>
<string name="pref_muteLocalJarvis">Ne pas répondre dans l\'application</string>
<string name="noConnectionError">Pas de connexion réseau</string>
<string name="timeoutServerNetworkError">Vérifiez l\'adresse et le numéro de port de Jarvis, est-il démarré ?</string>
</resources>
8 changes: 8 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
<string name="pref_serverUrl_summary">http://0.0.0.0 or http://jarvis.com</string>
<string name="pref_serverPort_title">Please enter the jarvis-api port number</string>
<string name="pref_serverPort_summary">Jarvis server port</string>
<string name="pref_serverKey_title">Please enter the jarvis-api app key</string>
<string name="pref_serverKey_summary">Key if used, leave blank otherwise</string>
<string name="pref_sttAtStart">Start app with voice recognition</string>
<string name="pref_muteRemoteJarvis">Mute answer on remote Jarvis</string>
<string name="pref_muteLocalJarvis">Mute answer in the app</string>
<string name="volleyError">Error, try again</string>
<string name="invalidAPIKey">Invalid Jarvis-api key</string>
<string name="missingAPIKey">Missing Jarvis-api key</string>
<string name="timeoutServerNetworkError">Please check Jarvis address and port. Does it run ?</string>
<string name="noConnectionError">No network connection</string>
</resources>
14 changes: 14 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@
android:singleLine="true"
android:summary="@string/pref_serverPort_summary"
android:title="@string/pref_serverPort_title" />
<EditTextPreference
android:key="serverKey"
android:selectAllOnFocus="true"
android:singleLine="true"
android:summary="@string/pref_serverKey_summary"
android:title="@string/pref_serverKey_title" />
<SwitchPreference
android:defaultValue="false"
android:key="muteRemoteJarvis"
android:title="@string/pref_muteRemoteJarvis" />
<SwitchPreference
android:defaultValue="false"
android:key="muteLocalJarvis"
android:title="@string/pref_muteLocalJarvis" />
<SwitchPreference
android:defaultValue="false"
android:key="sttAtStart"
Expand Down

0 comments on commit 32909f7

Please sign in to comment.