1

I am trying to add a menu to my GDK app. I am using an immersion and followed the instructions here: https://developers.google.com/glass/develop/gdk/ui/immersion-menus

Everything compiles ok, but the actual options menu doesn't work. When I tap on the touchpad nothing happens. In fact here seems be an additional side effect here where now no key presses work. I can't even swipe down to close the application. My glass essentially freezes until i force remove the app with adb.

Here is my onKeyDown method:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
        openOptionsMenu();
        return true;
    }
    return false;
}

What am I doing wrong? I copied it exactly from the docs. Do I need to be passing the event into the super class or something?

calumb
  • 1,051
  • 2
  • 10
  • 24

2 Answers2

0

I just uploaded a camera preview and zoom project to github at https://github.com/xjefftang/smartcamera which has an activity with a menu. You may want to check it out. Or see the source code to see how I get it implemented:

public class ImageViewActivity extends Activity {
    public static String TAG = "ImageViewActivity";
    ImageView mImageview;
    private GestureDetector mGestureDetector;
    File mPictureFilePath;


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

        setContentView(R.layout.imageview);
        Bundle extras = getIntent().getExtras();
        mPictureFilePath = (File)extras.get("picturefilepath");

        Log.v(TAG, "pictureFilePath=" + mPictureFilePath.getAbsolutePath());
        mImageview =  (ImageView) findViewById(R.id.picture);

        Bitmap myBitmap = BitmapFactory.decodeFile(mPictureFilePath.getAbsolutePath());
        int h = (int) ( myBitmap.getHeight() * (640.0 / myBitmap.getWidth()) );

        Bitmap scaled = Bitmap.createScaledBitmap(myBitmap, 640, h, true);
        mImageview.setImageBitmap(scaled);        

        mGestureDetector = new GestureDetector(this);

        mGestureDetector.setBaseListener(new GestureDetector.BaseListener() {
            @Override
            public boolean onGesture(Gesture gesture) {
                if (gesture == Gesture.TAP) {
                    Log.v(TAG, "TAP");
                    openOptionsMenu();

                    return true;
                } else if (gesture == Gesture.TWO_TAP) {
                    Log.v(TAG, "TWO_TAP");
                    return true;
                } else if (gesture == Gesture.SWIPE_RIGHT) {
                    Log.v(TAG, "SWIPE_RIGHT");
                    return true;
                } else if (gesture == Gesture.SWIPE_LEFT) {
                    return true;
                } else if (gesture == Gesture.LONG_PRESS) {
                    Log.v(TAG, "LONG_PRESS");                   
                    return true;
                } else if (gesture == Gesture.SWIPE_DOWN) {
                    Log.v(TAG, "SWIPE_DOWN");
                    return false;
                } else if (gesture == Gesture.SWIPE_UP) {
                    Log.v(TAG, "SWIPE_UP");
                    return true;
                } else if (gesture == Gesture.THREE_LONG_PRESS) {
                    Log.v(TAG, "THREE_LONG_PRESS");
                    return true;
                } else if (gesture == Gesture.THREE_TAP) {
                    Log.v(TAG, "THREE_TAP");
                    return true;
                } else if (gesture == Gesture.TWO_LONG_PRESS) {
                    Log.v(TAG, "TWO_LONG_PRESS");
                    return true;
                } else if (gesture == Gesture.TWO_SWIPE_DOWN) {
                    Log.v(TAG, "TWO_SWIPE_DOWN");
                    return false;
                } else if (gesture == Gesture.TWO_SWIPE_LEFT) {
                    Log.v(TAG, "TWO_SWIPE_LEFT");
                    return true;
                } else if (gesture == Gesture.TWO_SWIPE_RIGHT) {
                    Log.v(TAG, "TWO_SWIPE_RIGHT");
                    return true;
                } else if (gesture == Gesture.TWO_SWIPE_UP) {
                    Log.v(TAG, "TWO_SWIPE_UP");
                    return true;
                }

                return false;
            }
        });
    }



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




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.imageview, menu);

        return true;
    } 

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.upload:
            Uri imgUri = Uri.parse("file://" + mPictureFilePath.getAbsolutePath());
            Intent shareIntent = ShareCompat.IntentBuilder.from(this)
                    .setText("Share image taken by Glass")
                    .setType("image/jpeg")
                    .setStream(imgUri )
                    .getIntent()
                    .setPackage("com.google.android.apps.docs");

            startActivity(shareIntent);         

            return true;

        case R.id.delete:
            mPictureFilePath.delete();
            Toast.makeText(ImageViewActivity.this, "Deleted", Toast.LENGTH_SHORT).show();
            finish();
            return true;


        default:
            return super.onOptionsItemSelected(item);
        }
    }             

}
Jeff Tang
  • 1,804
  • 15
  • 16
0

Here's a minimal implementation that will allow both opening of the menu and also swiping down to dismiss the current Activity. If you have a one Activity Immersion, it will close your app.

// Display menu when user taps on touchpad or dismisses this activity if user swipes down
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
      if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
          openOptionsMenu();
          return true;
      } else if (keyCode == KeyEvent.KEYCODE_BACK) {
          finish();
      }
      return false;
}
louielouie
  • 14,881
  • 3
  • 26
  • 31