Make main menu popover intercept key events so that menu button can dismiss it

pull/14/head
Sam Bosley 14 years ago
parent cc25b593b4
commit cc80afcfef

@ -1,6 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
<com.todoroo.astrid.ui.TouchInterceptingFrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dip">
@ -24,4 +27,5 @@
android:layout_marginBottom="-8dip"
android:layout_below="@android:id/list"/>
</RelativeLayout>
</RelativeLayout>
</com.todoroo.astrid.ui.TouchInterceptingFrameLayout>

@ -1,6 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
<com.todoroo.astrid.ui.TouchInterceptingFrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dip">
@ -23,5 +26,5 @@
android:scaleType="fitCenter"
android:layout_marginBottom="-8dip"
android:layout_below="@android:id/list"/>
</RelativeLayout>
</RelativeLayout>
</com.todoroo.astrid.ui.TouchInterceptingFrameLayout>

@ -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
mainMenuPopover.suppressNextKeyEvent();
mainMenu.performClick();
return true;
}
return super.onKeyDown(keyCode, event);
}

@ -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,

@ -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;
}
}
Loading…
Cancel
Save