0

I am trying to implement contextual voice commands in my Glass application described in the documentation.

I have a FrameLayout inside my layout, and in my activity I'm implementing OnKeyDown in order to recognize taps. However, when the user says "Ok glass" and then selects one of the available commands, the same activity is returned, but unresponsive (I can't even close the app by sliding down).

I tried to use requestFocus() on the FrameLayout in a couple of different ways, but it did not have any effects. How could I have the focus back without having to restart the activity?

Essentially, this is the code I have so far for the main activity:

public class MainActivity extends Activity {

    public static String TAG = "DummyProject::MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().requestFeature(WindowUtils.FEATURE_VOICE_COMMANDS);

        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreatePanelMenu(int featureId, Menu menu) {
        if (featureId == WindowUtils.FEATURE_VOICE_COMMANDS) {
            getMenuInflater().inflate(R.layout.main, menu);
            return true;
        }

        return super.onCreatePanelMenu(featureId, menu);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.layout.main, menu);
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        if (featureId == WindowUtils.FEATURE_VOICE_COMMANDS) {
            switch (item.getItemId()) {
            case R.id.awesome:
                Log.i(TAG, "Selected!");
                break;
            default:
                return true;
            }

            return true;
        }

        return super.onMenuItemSelected(featureId, item);
    }

    @Override
    public boolean onKeyDown(int keycode, KeyEvent event) {
        if(keycode == KeyEvent.KEYCODE_DPAD_CENTER) {
            Log.i(TAG, "tap!");
            return true;
        }

        return super.onKeyDown(keycode, event);
    }
}

For this example, onKeyDown does not get called after I select any of the "ok glass" menu options.

Thank you in advance.

Arthur Ferreira
  • 311
  • 1
  • 6
  • 18

2 Answers2

3

prefer GestureDetector for catching touch pad events...

...

private GestureDetector mGestureDetector;

...

@Override
protected void onCreate(Bundle savedInstanceState) {

    ...

    mGestureDetector = createGestureDetector(this);

    ...

}

...

private GestureDetector createGestureDetector(Context context) {

GestureDetector gestureDetector = new GestureDetector(context);
// Create a base listener for generic gestures
gestureDetector.setBaseListener(new GestureDetector.BaseListener() {
@Override
public boolean onGesture(Gesture gesture) {
    if (gesture == Gesture.TAP) {
        Log.d(TAG, "TAP!!!");
            return true;
    }
    return false; }
    });

gestureDetector.setFingerListener(new GestureDetector.FingerListener() {
    @Override
    public void onFingerCountChanged(int previousCount, int currentCount) {
    // do something on finger count changes
    }
});

gestureDetector.setScrollListener(new GestureDetector.ScrollListener() {
    @Override
    public boolean onScroll(float displacement, float delta, float velocity) {
    // do something on scrolling
        return true;
    }
});
return gestureDetector;
}

/*
 * Send generic motion events to the gesture detector
 */
@Override
public boolean onGenericMotionEvent(MotionEvent event) {
    if (mGestureDetector != null) {
        return mGestureDetector.onMotionEvent(event);
    }
    return false;
}
Shankar Prasad
  • 405
  • 6
  • 17
  • 1
    Hi Shankar, thanks for answering. However, this won't totally work. The "ok glass" option also goes away when the activity is returned. In the [documentation](https://developers.google.com/glass/develop/gdk/voice#contextual_voice_commands), Google says that "... the "ok, glass" voice command automatically reappears in the footer section of the screen, ready to accept a new voice command, as long as the activity remains in focus.", so my guess is that there's something related to that, but I did not find a way to get the focus back. – Arthur Ferreira Jun 20 '14 at 18:39
0

It turns out that the problem was with the Manifest file. I was setting a theme to the application, ie.

<application android:theme="@android:style/Theme.NoTitleBar"> ...

Once I removed the theme, everything worked perfectly. The GDK demo sample by Google was helpful when I was trying to find this odd error.

Arthur Ferreira
  • 311
  • 1
  • 6
  • 18