Skip to content

Commit

Permalink
Merge pull request #2 from OTTAA-Project/devHC
Browse files Browse the repository at this point in the history
Dev hc
  • Loading branch information
hectoritr authored Jun 23, 2021
2 parents 82aca99 + 89b4fce commit 09fbb9d
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 96 deletions.
10 changes: 3 additions & 7 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,12 @@
<activity android:name=".LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<activity android:name=".MainActivity"
android:screenOrientation="sensorLandscape"
>
</activity>
</application>

Expand Down
169 changes: 107 additions & 62 deletions app/src/main/java/com/stonefacesoft/questions/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package com.stonefacesoft.questions;

import androidx.appcompat.app.AppCompatActivity;

import android.bluetooth.BluetoothAdapter;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.input.InputManager;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.InputDevice;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.firebase.analytics.FirebaseAnalytics;
import com.google.firebase.auth.FirebaseAuth;
Expand All @@ -27,31 +30,28 @@
import java.util.Locale;

//First push
public class MainActivity extends AppCompatActivity implements View.OnClickListener, TextToSpeech.OnInitListener {

private static final String TAG = "MainActivity";
public class MainActivity extends AppCompatActivity implements View.OnClickListener, TextToSpeech.OnInitListener, View.OnTouchListener {

public final static int SPEECH_RECOGNITION_CODE = 199;

private static final String TAG = "MainActivity";
TextToSpeech tts;
ArrayList<Picto> listPictos, emptyList;
private FirebaseAuth mAuth;

private FirebaseAnalytics mFirebaseAnalytics;

private FloatingActionButton btnMorePictos, btnMicrophone;

private MovableFloatingActionButton btnMovable;

TextToSpeech tts;

private TextView txtOutput;
private Picto Picto1, Picto2, Picto3, Picto4;

private Button TouchButton;
private String mStrIdioma;
private final boolean mute = false;

private boolean mute=false;

ArrayList<Picto> listPictos, emptyList;

public static String stripAccents(String s) {
s = s.toLowerCase();
s = Normalizer.normalize(s, Normalizer.Form.NFD);
s = s.replaceAll("[\\p{InCombiningDiacriticalMarks}]", "");
return s;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -66,7 +66,6 @@ protected void onCreate(Bundle savedInstanceState) {

listPictos = new ArrayList<Picto>();
emptyList = new ArrayList<Picto>();

UIbinding();

}
Expand Down Expand Up @@ -99,10 +98,7 @@ public void onClick(View view) {
} else if (id == R.id.btn_mic) {
//morePressed = 0;
//loadMore(false);
//TODO habilitar startSpeechToText cuando pueda probar
// startSpeechToText();
processText("Queres comer?");

startSpeechToText();

// Bundle bundle = new Bundle();
// bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, "Question");
Expand All @@ -120,7 +116,7 @@ public void onClick(View view) {

/**
* Start speech to text intent. This opens up Google Speech Recognition API dialog box to listen the speech input.
* */
*/
private void startSpeechToText() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
// TODO ver si podemos usar es-US
Expand All @@ -137,9 +133,10 @@ private void startSpeechToText() {
Toast.LENGTH_SHORT).show();
}
}

/**
* Callback for speech recognition activity
* */
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Expand All @@ -156,19 +153,19 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}
}

private void processText(String text){
private void processText(String text) {
txtOutput.setText(text);
if (esSiNo(text)){
if (esSiNo(text)) {
loadPictos(listPictos);
}else {
} else {
loadPictos(emptyList);
}
}

private void UIbinding(){
private void UIbinding() {
txtOutput = findViewById(R.id.txt_output);
btnMorePictos = findViewById(R.id.btn_more);
btnMicrophone=findViewById(R.id.btn_mic);
btnMicrophone = findViewById(R.id.btn_mic);

Picto1 = findViewById(R.id.picto1);
Picto1.setOnClickListener(this);
Expand All @@ -178,6 +175,9 @@ private void UIbinding(){
Picto3.setOnClickListener(this);
Picto4 = findViewById(R.id.picto4);
Picto4.setOnClickListener(this);
TouchButton = findViewById(R.id.btnBarrido);
TouchButton.setOnTouchListener(this);
TouchButton.setVisibility(View.GONE);
btnMicrophone.setOnClickListener(this);
btnMorePictos.setOnClickListener(this);

Expand All @@ -196,28 +196,52 @@ private void UIbinding(){
listPictos.add(Picto1);
listPictos.add(Picto2);


mStrIdioma = Locale.getDefault().getLanguage();
InputManager inputManager = (InputManager) this.getSystemService(Context.INPUT_SERVICE);
inputManager.registerInputDeviceListener(new InputManager.InputDeviceListener() {
@Override
public void onInputDeviceAdded(int deviceId) {
if (TouchButton.getVisibility() == View.GONE || TouchButton.getVisibility() == View.INVISIBLE) {
TouchButton.setVisibility(View.VISIBLE);
TouchButton.setOnTouchListener(MainActivity.this::onTouch);
}
}

@Override
public void onInputDeviceRemoved(int deviceId) {
if (TouchButton.getVisibility() == View.VISIBLE) {
TouchButton.setVisibility(View.GONE);
}
}

@Override
public void onInputDeviceChanged(int deviceId) {

}
}, null);

}

void loadPictos(List<Picto> array) {
cleanPictos();

if (array.size() == 0){
if (array.size() == 0) {
speak(getRandomReply());
Picto1.setVisibility(View.INVISIBLE);
Picto2.setVisibility(View.INVISIBLE);
Picto3.setVisibility(View.INVISIBLE);
Picto4.setVisibility(View.INVISIBLE);
}else if (array.size() == 2){
Picto1.setVisibility(View.VISIBLE);
Picto4.setVisibility(View.VISIBLE);
} else if (array.size() == 2) {
Picto1.setVisibility(View.VISIBLE);
Picto4.setVisibility(View.VISIBLE);
}
}

private String getRandomReply() {
String pregunta = "";
int valor=(int)(Math.random()*3+0);
switch (valor){
int valor = (int) (Math.random() * 3 + 0);
switch (valor) {
case 0:
return getResources().getString(R.string.str_WhatDidYouSay);

Expand All @@ -234,16 +258,15 @@ private String getRandomReply() {
return pregunta;
}


void cleanPictos(){
void cleanPictos() {
Picto1.setVisibility(View.INVISIBLE);
Picto2.setVisibility(View.INVISIBLE);
Picto3.setVisibility(View.INVISIBLE);
Picto4.setVisibility(View.INVISIBLE);
}

boolean esSiNo(String pregunta){
switch (mStrIdioma){
boolean esSiNo(String pregunta) {
switch (mStrIdioma) {
case "es":
pregunta = stripAccents(pregunta);
int i = pregunta.indexOf(' ');
Expand All @@ -252,7 +275,7 @@ boolean esSiNo(String pregunta){
return pregunta.equals("podes") ||
pregunta.equals("puedes") ||
pregunta.equals("tenes") ||
pregunta.equals("te")||
pregunta.equals("te") ||
pregunta.equals("conoces") ||
pregunta.equals("queres") ||
pregunta.equals("quieres") ||
Expand All @@ -267,7 +290,7 @@ boolean esSiNo(String pregunta){
pregunta.equals("sos");

case "en":
switch (pregunta.substring(0,2)){
switch (pregunta.substring(0, 2)) {
case "wh":
return false;
case "ho":
Expand All @@ -277,7 +300,7 @@ boolean esSiNo(String pregunta){
}

default:
switch (pregunta.substring(0,2)){
switch (pregunta.substring(0, 2)) {
case "wh":
return false;
case "ho":
Expand All @@ -288,25 +311,17 @@ boolean esSiNo(String pregunta){
}
}

private void speak(String utterance){
private void speak(String utterance) {
// Bundle bundle = new Bundle();
// bundle.putString(FirebaseAnalytics.Param.ITEM_ID, String.valueOf(picto.getPictoId()));
// bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, picto.getText());
// bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, "Picto");
// mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.SELECT_CONTENT, bundle);
if(!tts.isSpeaking()&&!mute){
tts.speak(utterance,TextToSpeech.QUEUE_ADD,null,null);
Toast.makeText(this,utterance,Toast.LENGTH_SHORT).show();
}
else if(mute)
Toast.makeText(this,utterance,Toast.LENGTH_SHORT).show();
}

public static String stripAccents(String s) {
s = s.toLowerCase();
s = Normalizer.normalize(s, Normalizer.Form.NFD);
s = s.replaceAll("[\\p{InCombiningDiacriticalMarks}]", "");
return s;
if (!tts.isSpeaking() && !mute) {
tts.speak(utterance, TextToSpeech.QUEUE_ADD, null, null);
Toast.makeText(this, utterance, Toast.LENGTH_SHORT).show();
} else if (mute)
Toast.makeText(this, utterance, Toast.LENGTH_SHORT).show();
}

@Override
Expand All @@ -325,14 +340,44 @@ protected void onDestroy() {
tts.shutdown();
}

private void isLogged(FirebaseUser user){
if (user == null){
Log.e(TAG, "updateUI: Account is null" );
private void isLogged(FirebaseUser user) {
if (user == null) {
Log.e(TAG, "updateUI: Account is null");
Intent openMain = new Intent(this, LoginActivity.class);
startActivity(openMain);
finish();
} else {
Log.d(TAG, "updateUI: User Logged in" );
Log.d(TAG, "updateUI: User Logged in");
}
}


@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getSource() == InputDevice.SOURCE_MOUSE) {
switch (event.getButtonState()) {
case MotionEvent.BUTTON_PRIMARY:
Log.e(TAG, "onTouchEvent: Primary Button");
Picto1.callOnClick();
return true;
case MotionEvent.BUTTON_SECONDARY:
Log.e(TAG, "onTouchEvent: Secundary Button");
Picto4.callOnClick();
return true;
case MotionEvent.BUTTON_TERTIARY:
Log.e(TAG, "onTouchEvent: Tertiary Button");
Picto4.callOnClick();
return true;
case MotionEvent.AXIS_HAT_Y:
return true;

}
return false;
} else {

}
return false;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ public boolean onTouch(View view, MotionEvent motionEvent){

}
else if (action == MotionEvent.ACTION_MOVE) {

//TODO poner que cuando se mueva mas alla del medio, se altere el orden de los botones quedando siempre el boton del microfono del lado externo, puede llegar a hacerse pasandolo como si estuviera en arabe

int viewWidth = view.getWidth();
int viewHeight = view.getHeight();

Expand Down
Loading

0 comments on commit 09fbb9d

Please sign in to comment.