Skip to content

Commit 6231ab8

Browse files
committed
Allow sloppy touch targeting on Action Bar home/up
Bug 6521608 When there are no custom views or other actionable views next to the action bar's home/up affordance, expand the touch target by a generous slop value. This helps frustrating cases where a user accidentally hits just to the right of Up and nothing happens. Change-Id: I09ef604ce70ad380aab62a373b04c4b02007c644
1 parent 3cd4624 commit 6231ab8

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

core/java/com/android/internal/widget/ActionBarView.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import android.content.pm.PackageManager.NameNotFoundException;
3636
import android.content.res.Configuration;
3737
import android.content.res.TypedArray;
38+
import android.graphics.Rect;
3839
import android.graphics.drawable.Drawable;
3940
import android.os.Parcel;
4041
import android.os.Parcelable;
@@ -47,6 +48,7 @@
4748
import android.view.Menu;
4849
import android.view.MenuItem;
4950
import android.view.MotionEvent;
51+
import android.view.TouchDelegate;
5052
import android.view.View;
5153
import android.view.ViewGroup;
5254
import android.view.ViewParent;
@@ -134,6 +136,10 @@ public class ActionBarView extends AbsActionBarView {
134136

135137
Window.Callback mWindowCallback;
136138

139+
private final Rect mTempRect = new Rect();
140+
private int mMaxHomeSlop;
141+
private static final int MAX_HOME_SLOP = 32; // dp
142+
137143
private final AdapterView.OnItemSelectedListener mNavItemSelectedListener =
138144
new AdapterView.OnItemSelectedListener() {
139145
public void onItemSelected(AdapterView parent, View view, int position, long id) {
@@ -250,6 +256,9 @@ public ActionBarView(Context context, AttributeSet attrs) {
250256
if (getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
251257
setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
252258
}
259+
260+
mMaxHomeSlop =
261+
(int) (MAX_HOME_SLOP * context.getResources().getDisplayMetrics().density + 0.5f);
253262
}
254263

255264
@Override
@@ -595,6 +604,7 @@ public void setDisplayOptions(int options) {
595604
final boolean homeAsUp = (mDisplayOptions & ActionBar.DISPLAY_HOME_AS_UP) != 0;
596605
mTitleUpView.setVisibility(!showHome ? (homeAsUp ? VISIBLE : INVISIBLE) : GONE);
597606
mTitleLayout.setEnabled(!showHome && homeAsUp);
607+
mTitleLayout.setClickable(!showHome && homeAsUp);
598608
}
599609

600610
if ((flagsChanged & ActionBar.DISPLAY_SHOW_CUSTOM) != 0 && mCustomNavView != null) {
@@ -775,8 +785,10 @@ private void initTitle() {
775785

776786
final boolean homeAsUp = (mDisplayOptions & ActionBar.DISPLAY_HOME_AS_UP) != 0;
777787
final boolean showHome = (mDisplayOptions & ActionBar.DISPLAY_SHOW_HOME) != 0;
778-
mTitleUpView.setVisibility(!showHome ? (homeAsUp ? VISIBLE : INVISIBLE) : GONE);
779-
mTitleLayout.setEnabled(homeAsUp && !showHome);
788+
final boolean showTitleUp = !showHome;
789+
mTitleUpView.setVisibility(showTitleUp ? (homeAsUp ? VISIBLE : INVISIBLE) : GONE);
790+
mTitleLayout.setEnabled(homeAsUp && showTitleUp);
791+
mTitleLayout.setClickable(homeAsUp && showTitleUp);
780792
}
781793

782794
addView(mTitleLayout);
@@ -1008,9 +1020,14 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
10081020
}
10091021

10101022
HomeView homeLayout = mExpandedActionView != null ? mExpandedHomeLayout : mHomeLayout;
1023+
boolean needsTouchDelegate = false;
1024+
int homeSlop = mMaxHomeSlop;
1025+
int homeRight = 0;
10111026
if (homeLayout.getVisibility() != GONE) {
10121027
final int leftOffset = homeLayout.getLeftOffset();
10131028
x += positionChild(homeLayout, x + leftOffset, y, contentHeight) + leftOffset;
1029+
needsTouchDelegate = homeLayout == mHomeLayout;
1030+
homeRight = x;
10141031
}
10151032

10161033
if (mExpandedActionView == null) {
@@ -1026,12 +1043,14 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
10261043
case ActionBar.NAVIGATION_MODE_LIST:
10271044
if (mListNavLayout != null) {
10281045
if (showTitle) x += mItemPadding;
1046+
homeSlop = Math.min(homeSlop, Math.max(x - homeRight, 0));
10291047
x += positionChild(mListNavLayout, x, y, contentHeight) + mItemPadding;
10301048
}
10311049
break;
10321050
case ActionBar.NAVIGATION_MODE_TABS:
10331051
if (mTabScrollView != null) {
10341052
if (showTitle) x += mItemPadding;
1053+
homeSlop = Math.min(homeSlop, Math.max(x - homeRight, 0));
10351054
x += positionChild(mTabScrollView, x, y, contentHeight) + mItemPadding;
10361055
}
10371056
break;
@@ -1124,6 +1143,7 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
11241143
final int customWidth = customView.getMeasuredWidth();
11251144
customView.layout(xpos, ypos, xpos + customWidth,
11261145
ypos + customView.getMeasuredHeight());
1146+
homeSlop = Math.min(homeSlop, Math.max(xpos - homeRight, 0));
11271147
x += customWidth;
11281148
}
11291149

@@ -1133,6 +1153,14 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
11331153
mProgressView.layout(mProgressBarPadding, -halfProgressHeight,
11341154
mProgressBarPadding + mProgressView.getMeasuredWidth(), halfProgressHeight);
11351155
}
1156+
1157+
if (needsTouchDelegate) {
1158+
mTempRect.set(homeLayout.getLeft(), homeLayout.getTop(),
1159+
homeLayout.getRight() + homeSlop, homeLayout.getBottom());
1160+
setTouchDelegate(new TouchDelegate(mTempRect, homeLayout));
1161+
} else {
1162+
setTouchDelegate(null);
1163+
}
11361164
}
11371165

11381166
@Override

0 commit comments

Comments
 (0)