Merge "Replace Toast-based cheat sheets with tooltips"
This commit is contained in:
committed by
Android (Google) Code Review
commit
ac30b3bb5f
@@ -119,7 +119,6 @@ public class MediaRouteActionProvider extends ActionProvider {
|
||||
}
|
||||
|
||||
mButton = new MediaRouteButton(mContext);
|
||||
mButton.setCheatSheetEnabled(true);
|
||||
mButton.setRouteTypes(mRouteTypes);
|
||||
mButton.setExtendedSettingsClickListener(mExtendedSettingsListener);
|
||||
mButton.setLayoutParams(new ViewGroup.LayoutParams(
|
||||
|
||||
@@ -24,18 +24,13 @@ import android.content.Context;
|
||||
import android.content.ContextWrapper;
|
||||
import android.content.res.TypedArray;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Rect;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.media.MediaRouter;
|
||||
import android.media.MediaRouter.RouteGroup;
|
||||
import android.media.MediaRouter.RouteInfo;
|
||||
import android.text.TextUtils;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.Gravity;
|
||||
import android.view.HapticFeedbackConstants;
|
||||
import android.view.SoundEffectConstants;
|
||||
import android.view.View;
|
||||
import android.widget.Toast;
|
||||
|
||||
public class MediaRouteButton extends View {
|
||||
private final MediaRouter mRouter;
|
||||
@@ -47,7 +42,6 @@ public class MediaRouteButton extends View {
|
||||
|
||||
private Drawable mRemoteIndicator;
|
||||
private boolean mRemoteActive;
|
||||
private boolean mCheatSheetEnabled;
|
||||
private boolean mIsConnecting;
|
||||
|
||||
private int mMinWidth;
|
||||
@@ -98,7 +92,6 @@ public class MediaRouteButton extends View {
|
||||
a.recycle();
|
||||
|
||||
setClickable(true);
|
||||
setLongClickable(true);
|
||||
|
||||
setRouteTypes(routeTypes);
|
||||
}
|
||||
@@ -178,12 +171,10 @@ public class MediaRouteButton extends View {
|
||||
throw new IllegalStateException("The MediaRouteButton's Context is not an Activity.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets whether to enable showing a toast with the content descriptor of the
|
||||
* button when the button is long pressed.
|
||||
*/
|
||||
void setCheatSheetEnabled(boolean enable) {
|
||||
mCheatSheetEnabled = enable;
|
||||
@Override
|
||||
public void setContentDescription(CharSequence contentDescription) {
|
||||
super.setContentDescription(contentDescription);
|
||||
setTooltip(contentDescription);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -196,47 +187,6 @@ public class MediaRouteButton extends View {
|
||||
return showDialogInternal() || handled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean performLongClick() {
|
||||
if (super.performLongClick()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!mCheatSheetEnabled) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final CharSequence contentDesc = getContentDescription();
|
||||
if (TextUtils.isEmpty(contentDesc)) {
|
||||
// Don't show the cheat sheet if we have no description
|
||||
return false;
|
||||
}
|
||||
|
||||
final int[] screenPos = new int[2];
|
||||
final Rect displayFrame = new Rect();
|
||||
getLocationOnScreen(screenPos);
|
||||
getWindowVisibleDisplayFrame(displayFrame);
|
||||
|
||||
final Context context = getContext();
|
||||
final int width = getWidth();
|
||||
final int height = getHeight();
|
||||
final int midy = screenPos[1] + height / 2;
|
||||
final int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
|
||||
|
||||
Toast cheatSheet = Toast.makeText(context, contentDesc, Toast.LENGTH_SHORT);
|
||||
if (midy < displayFrame.height()) {
|
||||
// Show along the top; follow action buttons
|
||||
cheatSheet.setGravity(Gravity.TOP | Gravity.END,
|
||||
screenWidth - screenPos[0] - width / 2, height);
|
||||
} else {
|
||||
// Show along the bottom center
|
||||
cheatSheet.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, height);
|
||||
}
|
||||
cheatSheet.show();
|
||||
performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int[] onCreateDrawableState(int extraSpace) {
|
||||
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
|
||||
|
||||
@@ -20,28 +20,22 @@ import android.content.Context;
|
||||
import android.content.res.Configuration;
|
||||
import android.content.res.Resources;
|
||||
import android.content.res.TypedArray;
|
||||
import android.graphics.Rect;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Parcelable;
|
||||
import android.text.TextUtils;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.Gravity;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.view.ViewConfiguration;
|
||||
import android.view.accessibility.AccessibilityEvent;
|
||||
import android.view.accessibility.AccessibilityManager;
|
||||
import android.widget.ActionMenuView;
|
||||
import android.widget.ForwardingListener;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
/**
|
||||
* @hide
|
||||
*/
|
||||
public class ActionMenuItemView extends TextView
|
||||
implements MenuView.ItemView, View.OnClickListener, View.OnLongClickListener,
|
||||
ActionMenuView.ActionMenuChildView {
|
||||
implements MenuView.ItemView, View.OnClickListener, ActionMenuView.ActionMenuChildView {
|
||||
private static final String TAG = "ActionMenuItemView";
|
||||
|
||||
private MenuItemImpl mItemData;
|
||||
@@ -59,9 +53,6 @@ public class ActionMenuItemView extends TextView
|
||||
private static final int MAX_ICON_SIZE = 32; // dp
|
||||
private int mMaxIconSize;
|
||||
|
||||
private Toast mTooltip;
|
||||
private Runnable mShowTooltipRunnable = () -> showTooltip(Toast.LENGTH_LONG);
|
||||
|
||||
public ActionMenuItemView(Context context) {
|
||||
this(context, null);
|
||||
}
|
||||
@@ -88,7 +79,6 @@ public class ActionMenuItemView extends TextView
|
||||
mMaxIconSize = (int) (MAX_ICON_SIZE * density + 0.5f);
|
||||
|
||||
setOnClickListener(this);
|
||||
setOnLongClickListener(this);
|
||||
|
||||
mSavedPaddingLeft = -1;
|
||||
setSaveEnabled(false);
|
||||
@@ -193,6 +183,9 @@ public class ActionMenuItemView extends TextView
|
||||
(mItemData.showsTextAsAction() && (mAllowTextWithIcon || mExpandedFormat));
|
||||
|
||||
setText(visible ? mTitle : null);
|
||||
|
||||
// Show the tooltip for items that do not already show text.
|
||||
setTooltip(visible ? null : mTitle);
|
||||
}
|
||||
|
||||
public void setIcon(Drawable icon) {
|
||||
@@ -249,7 +242,6 @@ public class ActionMenuItemView extends TextView
|
||||
|
||||
@Override
|
||||
public boolean dispatchHoverEvent(MotionEvent event) {
|
||||
updateTooltip(event);
|
||||
// Don't allow children to hover; we want this to be treated as a single component.
|
||||
return onHoverEvent(event);
|
||||
}
|
||||
@@ -266,68 +258,6 @@ public class ActionMenuItemView extends TextView
|
||||
return hasText();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onLongClick(View v) {
|
||||
return showTooltip(Toast.LENGTH_SHORT);
|
||||
}
|
||||
|
||||
private boolean showTooltip(@Toast.Duration int duration) {
|
||||
if (hasText()) {
|
||||
// Don't show the cheat sheet for items that already show text.
|
||||
return false;
|
||||
}
|
||||
|
||||
final int[] screenPos = new int[2];
|
||||
final Rect displayFrame = new Rect();
|
||||
getLocationOnScreen(screenPos);
|
||||
getWindowVisibleDisplayFrame(displayFrame);
|
||||
|
||||
final Context context = getContext();
|
||||
final int width = getWidth();
|
||||
final int height = getHeight();
|
||||
final int midy = screenPos[1] + height / 2;
|
||||
int referenceX = screenPos[0] + width / 2;
|
||||
if (getLayoutDirection() == View.LAYOUT_DIRECTION_LTR) {
|
||||
final int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
|
||||
referenceX = screenWidth - referenceX; // mirror
|
||||
}
|
||||
hideTooltip ();
|
||||
mTooltip = Toast.makeText(context, mItemData.getTitle(), duration);
|
||||
if (midy < displayFrame.height()) {
|
||||
// Show along the top; follow action buttons
|
||||
mTooltip.setGravity(Gravity.TOP | Gravity.END, referenceX,
|
||||
screenPos[1] + height - displayFrame.top);
|
||||
} else {
|
||||
// Show along the bottom center
|
||||
mTooltip.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, height);
|
||||
}
|
||||
mTooltip.show();
|
||||
return true;
|
||||
}
|
||||
|
||||
private void hideTooltip() {
|
||||
if (mTooltip != null) {
|
||||
mTooltip.cancel();
|
||||
mTooltip = null;
|
||||
}
|
||||
getHandler().removeCallbacks(mShowTooltipRunnable);
|
||||
}
|
||||
|
||||
private void updateTooltip(MotionEvent event) {
|
||||
AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
|
||||
if (manager.isEnabled() && manager.isTouchExplorationEnabled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
final int action = event.getAction();
|
||||
if (action == MotionEvent.ACTION_HOVER_MOVE) {
|
||||
hideTooltip();
|
||||
getHandler().postDelayed(mShowTooltipRunnable, ViewConfiguration.getLongPressTimeout());
|
||||
} else if (action == MotionEvent.ACTION_HOVER_EXIT) {
|
||||
hideTooltip();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||
final boolean textVisible = hasText();
|
||||
|
||||
@@ -40,7 +40,6 @@ import android.widget.LinearLayout;
|
||||
import android.widget.ListView;
|
||||
import android.widget.Spinner;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
/**
|
||||
* This widget implements the dynamic action bar tab behavior that can change
|
||||
@@ -360,7 +359,7 @@ public class ScrollingTabContainerView extends HorizontalScrollView
|
||||
tabView.getTab().select();
|
||||
}
|
||||
|
||||
private class TabView extends LinearLayout implements OnLongClickListener {
|
||||
private class TabView extends LinearLayout {
|
||||
private ActionBar.Tab mTab;
|
||||
private TextView mTextView;
|
||||
private ImageView mIconView;
|
||||
@@ -472,35 +471,10 @@ public class ScrollingTabContainerView extends HorizontalScrollView
|
||||
if (mIconView != null) {
|
||||
mIconView.setContentDescription(tab.getContentDescription());
|
||||
}
|
||||
|
||||
if (!hasText && !TextUtils.isEmpty(tab.getContentDescription())) {
|
||||
setOnLongClickListener(this);
|
||||
} else {
|
||||
setOnLongClickListener(null);
|
||||
setLongClickable(false);
|
||||
}
|
||||
setTooltip(hasText? null : tab.getContentDescription());
|
||||
}
|
||||
}
|
||||
|
||||
public boolean onLongClick(View v) {
|
||||
final int[] screenPos = new int[2];
|
||||
getLocationOnScreen(screenPos);
|
||||
|
||||
final Context context = getContext();
|
||||
final int width = getWidth();
|
||||
final int height = getHeight();
|
||||
final int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
|
||||
|
||||
Toast cheatSheet = Toast.makeText(context, mTab.getContentDescription(),
|
||||
Toast.LENGTH_SHORT);
|
||||
// Show under the tab
|
||||
cheatSheet.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL,
|
||||
(screenPos[0] + width / 2) - screenWidth / 2, height);
|
||||
|
||||
cheatSheet.show();
|
||||
return true;
|
||||
}
|
||||
|
||||
public ActionBar.Tab getTab() {
|
||||
return mTab;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user