From cc80afcfefd1382b9f96694e1bbe08d43384e5c3 Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Tue, 6 Mar 2012 11:42:07 -0800 Subject: [PATCH] Make main menu popover intercept key events so that menu button can dismiss it --- astrid/res/layout/main_menu_popover.xml | 52 ++++++++++--------- .../res/layout/main_menu_popover_tablet.xml | 51 +++++++++--------- .../astrid/activity/TaskListActivity.java | 7 ++- .../todoroo/astrid/ui/MainMenuPopover.java | 29 ++++++++++- .../ui/TouchInterceptingFrameLayout.java | 36 +++++++++++++ 5 files changed, 122 insertions(+), 53 deletions(-) create mode 100644 astrid/src/com/todoroo/astrid/ui/TouchInterceptingFrameLayout.java diff --git a/astrid/res/layout/main_menu_popover.xml b/astrid/res/layout/main_menu_popover.xml index 04c62b34c..320849240 100644 --- a/astrid/res/layout/main_menu_popover.xml +++ b/astrid/res/layout/main_menu_popover.xml @@ -1,27 +1,31 @@ - - - - - - - - - + android:layout_height="wrap_content"> + + + + + + + + + + diff --git a/astrid/res/layout/main_menu_popover_tablet.xml b/astrid/res/layout/main_menu_popover_tablet.xml index 252de068d..5ce7ca3d6 100644 --- a/astrid/res/layout/main_menu_popover_tablet.xml +++ b/astrid/res/layout/main_menu_popover_tablet.xml @@ -1,27 +1,30 @@ - - - - - - - - - + android:layout_height="wrap_content"> + + + + + + + + + \ No newline at end of file diff --git a/astrid/src/com/todoroo/astrid/activity/TaskListActivity.java b/astrid/src/com/todoroo/astrid/activity/TaskListActivity.java index 9b169e444..696624892 100644 --- a/astrid/src/com/todoroo/astrid/activity/TaskListActivity.java +++ b/astrid/src/com/todoroo/astrid/activity/TaskListActivity.java @@ -497,10 +497,9 @@ public class TaskListActivity extends AstridActivity implements MainMenuListener @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_MENU) { - if (mainMenuPopover.isShowing()) - mainMenuPopover.dismiss(); - else - mainMenu.performClick(); + mainMenuPopover.suppressNextKeyEvent(); + mainMenu.performClick(); + return true; } return super.onKeyDown(keyCode, event); } diff --git a/astrid/src/com/todoroo/astrid/ui/MainMenuPopover.java b/astrid/src/com/todoroo/astrid/ui/MainMenuPopover.java index 7209fd4aa..18dbf6121 100644 --- a/astrid/src/com/todoroo/astrid/ui/MainMenuPopover.java +++ b/astrid/src/com/todoroo/astrid/ui/MainMenuPopover.java @@ -3,6 +3,7 @@ package com.todoroo.astrid.ui; import android.content.Context; import android.content.Intent; import android.graphics.drawable.Drawable; +import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; @@ -14,8 +15,9 @@ import android.widget.TextView; import com.timsu.astrid.R; import com.todoroo.astrid.activity.AstridActivity; import com.todoroo.astrid.service.ThemeService; +import com.todoroo.astrid.ui.TouchInterceptingFrameLayout.InterceptTouchListener; -public class MainMenuPopover extends FragmentPopover { +public class MainMenuPopover extends FragmentPopover implements InterceptTouchListener { public static final int MAIN_MENU_ITEM_LISTS = R.string.TLA_menu_lists; public static final int MAIN_MENU_ITEM_FRIENDS = R.string.TLA_menu_friends; @@ -34,6 +36,7 @@ public class MainMenuPopover extends FragmentPopover { private final LinearLayout topFixed; private final LinearLayout bottomFixed; private final int rowLayout; + private boolean suppressNextKeyEvent = false; public void setMenuListener(MainMenuListener listener) { this.mListener = listener; @@ -42,6 +45,9 @@ public class MainMenuPopover extends FragmentPopover { public MainMenuPopover(Context context, int layout, boolean isTablet) { super(context, layout); + TouchInterceptingFrameLayout rootLayout = (TouchInterceptingFrameLayout) getContentView(); + rootLayout.setInterceptTouchListener(this); + if (AstridActivity.shouldUseThreePane(context)) rowLayout = R.layout.main_menu_row_tablet; else @@ -57,6 +63,27 @@ public class MainMenuPopover extends FragmentPopover { addFixedItems(isTablet); } + public boolean didInterceptTouch(KeyEvent event) { + int keyCode = event.getKeyCode(); + if (!suppressNextKeyEvent) { + if ((keyCode == KeyEvent.KEYCODE_MENU || keyCode == KeyEvent.KEYCODE_BACK) && isShowing()) { + dismiss(); + return true; + } + } + suppressNextKeyEvent = false; + return false; + } + + public void suppressNextKeyEvent() { + suppressNextKeyEvent = true; + } + + @Override + public void setBackgroundDrawable(Drawable background) { + super.setBackgroundDrawable(null); + } + private void addFixedItems(boolean isTablet) { if (!isTablet) addMenuItem(R.string.TLA_menu_lists, diff --git a/astrid/src/com/todoroo/astrid/ui/TouchInterceptingFrameLayout.java b/astrid/src/com/todoroo/astrid/ui/TouchInterceptingFrameLayout.java new file mode 100644 index 000000000..d7c8f1ca3 --- /dev/null +++ b/astrid/src/com/todoroo/astrid/ui/TouchInterceptingFrameLayout.java @@ -0,0 +1,36 @@ +package com.todoroo.astrid.ui; + +import android.content.Context; +import android.graphics.Color; +import android.util.AttributeSet; +import android.view.KeyEvent; +import android.widget.FrameLayout; + +public class TouchInterceptingFrameLayout extends FrameLayout { + + public interface InterceptTouchListener { + public boolean didInterceptTouch(KeyEvent event); + } + + private InterceptTouchListener mListener; + + public TouchInterceptingFrameLayout(Context context, AttributeSet attrs) { + super(context, attrs); + setBackgroundColor(Color.TRANSPARENT); + } + + @Override + public boolean dispatchKeyEvent(KeyEvent event) { + if (mListener != null && mListener.didInterceptTouch(event)) + return true; + return super.dispatchKeyEvent(event); + } + + public InterceptTouchListener getInterceptTouchListener() { + return mListener; + } + + public void setInterceptTouchListener(InterceptTouchListener mListener) { + this.mListener = mListener; + } +}