Skip to content

Commit

Permalink
more glass improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Xlythe committed Jun 29, 2014
1 parent 3e1de3c commit 209846f
Show file tree
Hide file tree
Showing 79 changed files with 688 additions and 55 deletions.
1 change: 1 addition & 0 deletions Calculator/build.gradle
Expand Up @@ -26,4 +26,5 @@ dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':ThemeEngine')
compile project(':FloatingView')
compile project(':GDK-ProgressBar')
}
@@ -1,70 +1,68 @@
package com.android2.calculator3;

import android.animation.Animator;
import android.app.Activity;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.glass.app.Card;
import com.google.android.glass.media.Sounds;
import com.google.android.glass.touchpad.Gesture;
import com.google.android.glass.touchpad.GestureDetector;
import com.google.android.glass.widget.CardScrollAdapter;
import com.google.android.glass.widget.CardScrollView;
import com.w9jds.gdk_progress_widget.SliderView;

import org.javia.arity.SyntaxException;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GlassHomeActivity extends Activity {
private static final int SPEECH_REQUEST = 1000;
private List<Card> mCards = new ArrayList<Card>();
private CardScrollView mCardScrollView;
;

private class ExampleCardScrollAdapter extends CardScrollAdapter {
@Override
public int getCount() {
return mCards.size();
}

@Override
public Object getItem(int position) {
return mCards.get(position);
}
private GestureDetector mGestureDetector;

@Override
public View getView(int position, View convertView, ViewGroup parent) {
return mCards.get(position).getView();
}

@Override
public int getPosition(Object obj) {
return mCards.indexOf(obj);
}
} @Override
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.glass_home);

if(savedInstanceState == null) displaySpeechRecognizer();

mCardScrollView = new CardScrollView(this);
ExampleCardScrollAdapter adapter = new ExampleCardScrollAdapter();
mCardScrollView.setAdapter(adapter);
mCardScrollView.setOnItemClickListener(new OnItemClickListener() {
mGestureDetector = new GestureDetector(this);
mGestureDetector.setBaseListener(new GestureDetector.BaseListener() {
@Override
public void onItemClick(AdapterView<?> av, View v, int position, long id) {
mCards.clear();
mCardScrollView.getAdapter().notifyDataSetChanged();
displaySpeechRecognizer();
public boolean onGesture(Gesture gesture) {
if (gesture == Gesture.TAP) {
((AudioManager) getSystemService(AUDIO_SERVICE)).playSoundEffect(Sounds.TAP);
displaySpeechRecognizer();
return true;
}

return false;
}
});
mCardScrollView.activate();
setContentView(mCardScrollView);
}

if(savedInstanceState == null) displaySpeechRecognizer();
@Override
public boolean onGenericMotionEvent(MotionEvent event) {
if (mGestureDetector != null) {
return mGestureDetector.onMotionEvent(event);
}
return false;
}

@Override
Expand All @@ -84,14 +82,17 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == SPEECH_REQUEST) {
if(resultCode == RESULT_OK) {
List<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String spokenText = results.get(0).toLowerCase(Locale.US).replace("point", ".").replace("minus", "-").replace("plus", "+").replace("divided by", "/").replace("times", "*").replace("x", "*").replace(" ", "");
spokenText = SpellContext.replaceAllWithNumbers(spokenText);
spokenText = spokenText.replaceAll("[a-z]", "");
String spokenText = parseText(results.get(0));

if(spokenText.isEmpty()) {
detectionFailed();
return;
}

Log.v("Calculator", "Glass user queried \"" + spokenText + "\"");

Logic logic = new Logic(getBaseContext());
logic.setLineLength(100);
logic.setLineLength(10);

String result;
try {
Expand All @@ -112,13 +113,47 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}

private void detectionFailed() {
mCards.clear();

Card card = new Card(this);
card.setText(R.string.voice_detection_failed);
mCards.add(card);
mCardScrollView.getAdapter().notifyDataSetChanged();
findViewById(R.id.layout).setVisibility(View.VISIBLE);
}

private String parseText(String text) {
EquationFormatter formatter = new EquationFormatter();
text = text.toLowerCase(Locale.US);
text = text.replace("point", ".");
text = text.replace("minus", "-");
text = text.replace("plus", "+");
text = text.replace("divided", "/");
text = text.replace("over", "/");
text = text.replace("times", "*");
text = text.replace("x", "*");
text = text.replace("multiplied", "*");
text = text.replace(" ", "");
text = text.replace("sign", "sin(");
text = text.replace("cosine", "cos(");
text = text.replace("tangent", "tan(");
text = text.replace("pie", getString(R.string.pi));
text = text.replace("pi", getString(R.string.pi));
text = SpellContext.replaceAllWithNumbers(text);
text = removeChars(text);
text = formatter.appendParenthesis(text);
return text;
}

private String removeChars(String input) {
Pattern pattern = Pattern.compile("[a-z]");
String text = "";
for(int i = 0; i < input.length(); i++) {
if(input.substring(i).startsWith("sin(")
|| input.substring(i).startsWith("cos(")
|| input.substring(i).startsWith("tan(")) {
text += input.substring(i, i+4);
i+=3;
}
else {
Matcher matcher = pattern.matcher(input.substring(i, i+1));
if(!matcher.matches()) text += input.substring(i, i+1);
}
}
return text;
}
}
Expand Up @@ -8,20 +8,22 @@
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.widget.TextView;

import com.google.android.glass.media.Sounds;
import com.google.android.glass.touchpad.Gesture;
import com.google.android.glass.touchpad.GestureDetector;

public class GlassResultActivity extends Activity {
public static String EXTRA_QUESTION;
public static String EXTRA_RESULT;
public static final String EXTRA_QUESTION = "question";
public static final String EXTRA_RESULT = "result";

private String mQuestion;
private String mResult;
private boolean mIsTextToSpeechInit = false;
private TextToSpeech mTextToSpeech;
private GestureDetector mGestureDetector;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -33,8 +35,8 @@ protected void onCreate(Bundle savedInstanceState) {
TextView resultView = (TextView) findViewById(R.id.result);
resultView.setText(mQuestion + " = " + mResult);

GestureDetector detector = new GestureDetector(this);
detector.setBaseListener(new GestureDetector.BaseListener() {
mGestureDetector = new GestureDetector(this);
mGestureDetector.setBaseListener(new GestureDetector.BaseListener() {
@Override
public boolean onGesture(Gesture gesture) {
if (gesture == Gesture.TAP) {
Expand All @@ -48,6 +50,14 @@ public boolean onGesture(Gesture gesture) {
});
}

@Override
public boolean onGenericMotionEvent(MotionEvent event) {
if (mGestureDetector != null) {
return mGestureDetector.onMotionEvent(event);
}
return false;
}

private void askNewQuestion() {
startActivity(new Intent(getBaseContext(), GlassHomeActivity.class));
finish();
Expand Down Expand Up @@ -98,7 +108,19 @@ private void speakResult() {
// Speech can't say "-1". It says "1" instead.
mResult = getString(R.string.speech_helper_negative, mResult.substring(1));
}
mTextToSpeech.speak(getString(R.string.speech_helper_equals, mQuestion, mResult), TextToSpeech.QUEUE_ADD, null);
String question = formatQuestion(mQuestion);
mTextToSpeech.speak(getString(R.string.speech_helper_equals, question, mResult), TextToSpeech.QUEUE_ADD, null);
}
}

private String formatQuestion(String question) {
String text = question;
text = text.replace("-", " minus ");
text = text.replace("*", " times ");
text = text.replace("*", " times ");
text = text.replace("sin", " sine of ");
text = text.replace("cos", " cosine of ");
text = text.replace("tan", " tangent of ");
return text;
}
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions Calculator/src/main/res/layout/glass_home.xml
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15dp" >

<TextView
android:id="@+id/text2"
android:text="Tap to speak again"
android:textColor="#ffffff"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true" />

<TextView
android:id="@+id/text1"
android:text="No message recorded"
android:textColor="#ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_above="@id/text2" />

<ImageView
android:src="@drawable/ic_warning_150"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_above="@id/text1"
android:layout_margin="20dp"/>

</RelativeLayout>
5 changes: 1 addition & 4 deletions Calculator/src/main/res/layout/glass_result.xml
@@ -1,11 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/body_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="40dp"
tools:ignore="UselessLeaf" >
android:layout_margin="40dp" >

<TextView
android:id="@+id/result"
Expand Down
1 change: 1 addition & 0 deletions GDK-ProgressBar/.gitignore
@@ -0,0 +1 @@
/build
29 changes: 29 additions & 0 deletions GDK-ProgressBar/build.gradle
@@ -0,0 +1,29 @@
apply plugin: 'android-library'

android {
compileSdkVersion 19
buildToolsVersion "20"

defaultConfig {
minSdkVersion 14
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}

packagingOptions {
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
}
}

dependencies {
compile 'com.android.support:appcompat-v7:19.+'
compile fileTree(dir: 'libs', include: ['*.jar'])
}
17 changes: 17 additions & 0 deletions GDK-ProgressBar/proguard-rules.txt
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in C:/Users/Will/AppData/Local/Android/android-studio1/sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
9 changes: 9 additions & 0 deletions GDK-ProgressBar/src/main/AndroidManifest.xml
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.w9jds.gdk_progress_widget"
android:versionCode="1"
android:versionName="1.0">
<application/>
</manifest>

0 comments on commit 209846f

Please sign in to comment.