下面列出了android.view.ViewConfiguration#getLongPressTimeout ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
boolean handled = false;
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_NUMPAD_ENTER:
case KeyEvent.KEYCODE_ENTER:
long duration = event.getEventTime() - event.getDownTime();
if (event.isTracking() && event.getDownTime() > mLastLongPress && duration < ViewConfiguration.getLongPressTimeout()) {
performItemClick(getSelectedView(), getSelectedItemPosition(), getSelectedItemId());
}
handled = true;
break;
}
return handled || super.onKeyUp(keyCode, event);
}
@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {
// Disable pinch-to-zoom and tap-to-focus while the camera module is paused.
if (mCameraModule.isPaused()) {
return false;
}
// Only forward the event to the pinch-to-zoom gesture detector when pinch-to-zoom is
// enabled.
if (isPinchToZoomEnabled()) {
mPinchToZoomGestureDetector.onTouchEvent(event);
}
if (event.getPointerCount() == 2 && isPinchToZoomEnabled() && isZoomSupported()) {
return true;
}
// Camera focus
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mDownEventTimestamp = System.currentTimeMillis();
break;
case MotionEvent.ACTION_UP:
if (delta() < ViewConfiguration.getLongPressTimeout()) {
mUpEvent = event;
performClick();
}
break;
default:
// Unhandled event.
return false;
}
return true;
}
public MediaSessionService(Context context) {
super(context);
mSessionManagerImpl = new SessionManagerImpl();
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
mMediaEventWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "handleMediaEvent");
mLongPressTimeout = ViewConfiguration.getLongPressTimeout();
mNotificationManager = INotificationManager.Stub.asInterface(
ServiceManager.getService(Context.NOTIFICATION_SERVICE));
mPackageManager = AppGlobals.getPackageManager();
}
public ForwardingListener(View src) {
mSrc = src;
src.setLongClickable(true);
src.addOnAttachStateChangeListener(this);
mScaledTouchSlop = ViewConfiguration.get(src.getContext()).getScaledTouchSlop();
mTapTimeout = ViewConfiguration.getTapTimeout();
// Use a medium-press timeout. Halfway between tap and long-press.
mLongPressTimeout = (mTapTimeout + ViewConfiguration.getLongPressTimeout()) / 2;
}
@Override
public void run() {
if (mTouchMode == TOUCH_MODE_DOWN) {
mTouchMode = TOUCH_MODE_TAP;
final View header = getHeaderAt(mMotionHeaderPosition);
if (header != null && !mHeaderChildBeingPressed) {
if (!mDataChanged) {
header.setPressed(true);
setPressed(true);
refreshDrawableState();
final int longPressTimeout = ViewConfiguration.getLongPressTimeout();
final boolean longClickable = isLongClickable();
if (longClickable) {
if (mPendingCheckForLongPress == null) {
mPendingCheckForLongPress = new CheckForHeaderLongPress();
}
mPendingCheckForLongPress.rememberWindowAttachCount();
postDelayed(mPendingCheckForLongPress, longPressTimeout);
} else {
mTouchMode = TOUCH_MODE_DONE_WAITING;
}
} else {
mTouchMode = TOUCH_MODE_DONE_WAITING;
}
}
}
}
private boolean handleLinkMovementMethod(TextView widget, Spannable buffer, MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_UP ||
action == MotionEvent.ACTION_DOWN) {
int x = (int) event.getX();
int y = (int) event.getY();
x -= widget.getTotalPaddingLeft();
y -= widget.getTotalPaddingTop();
x += widget.getScrollX();
y += widget.getScrollY();
Layout layout = widget.getLayout();
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);
ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);
if (link.length != 0) {
if (action == MotionEvent.ACTION_UP) {
long actionUpTime = System.currentTimeMillis();
if (actionUpTime - mLastActionDownTime > ViewConfiguration.getLongPressTimeout()) { //长按事件,取消LinkMovementMethod处理,即不处理ClickableSpan点击事件
return false;
}
link[0].onClick(widget);
Selection.removeSelection(buffer);
} else if (action == MotionEvent.ACTION_DOWN) {
Selection.setSelection(buffer, buffer.getSpanStart(link[0]), buffer.getSpanEnd(link[0]));
mLastActionDownTime = System.currentTimeMillis();
}
}
}
return false;
}
/**
* Show the tooltip.
*
* @param fromTouch {@code true} to show from the touch.
*/
private void show(boolean fromTouch) {
if (!ViewCompat.isAttachedToWindow(mAnchor)) {
return;
}
setPendingHandler(null);
if (sActiveHandler != null) {
sActiveHandler.hide();
}
sActiveHandler = this;
mFromTouch = fromTouch;
mPopup = new DynamicTooltipPopup(mAnchor.getContext(), mBackgroundColor, mTintColor);
mPopup.show(mAnchor, mAnchorX, mAnchorY, mFromTouch, mTooltipIcon, mTooltipText);
// Only listen for attach state change while the popup is being shown.
mAnchor.addOnAttachStateChangeListener(this);
final long timeout;
if (mFromTouch) {
timeout = LONG_CLICK_HIDE_TIMEOUT_MS;
} else if ((ViewCompat.getWindowSystemUiVisibility(mAnchor)
& SYSTEM_UI_FLAG_LOW_PROFILE) == SYSTEM_UI_FLAG_LOW_PROFILE) {
timeout = HOVER_HIDE_TIMEOUT_SHORT_MS - ViewConfiguration.getLongPressTimeout();
} else {
timeout = HOVER_HIDE_TIMEOUT_MS - ViewConfiguration.getLongPressTimeout();
}
mAnchor.removeCallbacks(mHideRunnable);
mAnchor.postDelayed(mHideRunnable, timeout);
}
@Override
public void run() {
if (mTouchMode == TOUCH_MODE_DOWN) {
mTouchMode = TOUCH_MODE_TAP;
final View header = getHeaderAt(mMotionHeaderPosition);
if (header != null && !mHeaderChildBeingPressed) {
if (!mDataChanged) {
header.setPressed(true);
setPressed(true);
refreshDrawableState();
final int longPressTimeout = ViewConfiguration.getLongPressTimeout();
final boolean longClickable = isLongClickable();
if (longClickable) {
if (mPendingCheckForLongPress == null) {
mPendingCheckForLongPress = new CheckForHeaderLongPress();
}
mPendingCheckForLongPress.rememberWindowAttachCount();
postDelayed(mPendingCheckForLongPress, longPressTimeout);
} else {
mTouchMode = TOUCH_MODE_DONE_WAITING;
}
} else {
mTouchMode = TOUCH_MODE_DONE_WAITING;
}
}
}
}
public SwipeHelper(int swipeDirection, Callback callback, float densityScale,
float pagingTouchSlop) {
mCallback = callback;
mHandler = new Handler();
mSwipeDirection = swipeDirection;
mVelocityTracker = VelocityTracker.obtain();
mDensityScale = densityScale;
mPagingTouchSlop = pagingTouchSlop;
mLongPressTimeout = (long) (ViewConfiguration.getLongPressTimeout() * 1.5f); // extra long-press!
}
public VolumePanel(PopupWindowManager manager) {
super(manager);
Context context = manager.getContext();
app = (NoyzeApp) context.getApplicationContext();
mStreamControls = new SparseArray<StreamControl>(StreamResources.STREAMS.length);
mHandler = new VolumeMediaHandler(context.getMainLooper());
mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
mVolumeManager = new VolumeManager(mAudioManager);
mAudioHelper = AudioHelper.getHelper(context, mAudioManager);
// mAudioFlingerProxy = new AudioFlingerProxy();
mAudioHelper.setHandler(mHandler);
mLongPressTimeout = ViewConfiguration.getLongPressTimeout();
if (MediaProviderDelegate.IS_V18)
mMediaProviderDelegate = MediaProviderDelegate.getDelegate(context);
// Register here: be SURE that the handler is not null.
if (null == mVolumeMediaReceiver) {
mVolumeMediaReceiver = new VolumeMediaReceiver(mHandler);
IntentFilter events = Utils.merge(VOLUME_MUSIC_EVENTS(), Constants.MEDIA_ACTION_FILTER());
context.registerReceiver(mVolumeMediaReceiver, events);
mVolumeMediaReceiver.setHandler(mHandler);
}
// Register for events related to the call state.
if (null == mCallStateListener) {
mCallState = mTelephonyManager.getCallState();
mCallStateListener = new CallStateListener(mHandler);
mTelephonyManager.listen(mCallStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
initState();
}
private void init(Context cntx) {
mInnerView = null;
mSwipeStart = -1;
mSwipeStartTime = -1;
mBackground = 0;
ViewConfiguration config = ViewConfiguration.get(cntx);
mTouchSlop = config.getScaledTouchSlop();
mFlingSlop = config.getScaledMinimumFlingVelocity();
mFlingCap = config.getScaledMaximumFlingVelocity();
mPressTimeout = config.getTapTimeout();
mPressRunnable = new Runnable() {
public void run() {
if (mSwipeState == STATE_PRESSED) {
mInnerView.setPressed(true);
}
}
};
mLongPressTimeout = mPressTimeout + config.getLongPressTimeout();
mLongPressRunnable = new Runnable() {
public void run() {
if (mSwipeState == STATE_PRESSED) {
performLongClick();
mInnerView.setPressed(false);
}
}
};
mUnpressRunnable = new Runnable() {
public void run() {
mInnerView.setPressed(false);
}
};
mSwipeState = STATE_READY;
mVelocityTracker = null;
mAnim = null;
mChecked = false;
mListener = null;
setClickable(true);
}
@Override
public void run() {
if (mTouchMode == TOUCH_MODE_DOWN) {
mTouchMode = TOUCH_MODE_TAP;
final View child = getChildAt(mMotionPosition - mFirstPosition);
if (child != null && !child.hasFocusable()) {
mLayoutMode = LAYOUT_NORMAL;
if (!mDataChanged) {
child.setPressed(true);
positionSelector(mMotionPosition, child);
setPressed(true);
refreshDrawableState();
final int longPressTimeout = ViewConfiguration.getLongPressTimeout();
final boolean longClickable = isLongClickable();
if (mSelector != null) {
Drawable d = mSelector.getCurrent();
if (d != null && d instanceof TransitionDrawable) {
if (longClickable) {
((TransitionDrawable) d).startTransition(longPressTimeout);
} else {
((TransitionDrawable) d).resetTransition();
}
}
}
mTouchMode = TOUCH_MODE_DONE_WAITING;
} else {
mTouchMode = TOUCH_MODE_DONE_WAITING;
}
}
}
}
@Before
public void setUp() {
context = ApplicationProvider.getApplicationContext();
cell = new DashboardCell(context);
listener = new TestOnInterceptedLongPressListener();
cell.setOnInterceptedLongPressListener(listener);
longPressTimeout = ViewConfiguration.getLongPressTimeout();
}
public long getSystemLongPressTime() {
return ViewConfiguration.getLongPressTimeout();
}
public OnTouchClickListener(View.OnClickListener listener, View.OnLongClickListener longListener) {
mListener = listener;
mLongListener = longListener;
mLongPressTimeout = ViewConfiguration.getLongPressTimeout();
}
public static DownResultHolder sendDown(
UiController uiController,
float[] coordinates,
float[] precision,
int inputDevice,
int buttonState) {
checkNotNull(uiController);
checkNotNull(coordinates);
checkNotNull(precision);
for (int retry = 0; retry < MAX_CLICK_ATTEMPTS; retry++) {
MotionEvent motionEvent = null;
try {
motionEvent = obtainDownEvent(coordinates, precision, inputDevice, buttonState);
// The down event should be considered a tap if it is long enough to be detected
// but short enough not to be a long-press. Assume that TapTimeout is set at least
// twice the detection time for a tap (no need to sleep for the whole TapTimeout since
// we aren't concerned about scrolling here).
long downTime = motionEvent.getDownTime();
long isTapAt = downTime + (ViewConfiguration.getTapTimeout() / 2);
boolean injectEventSucceeded = uiController.injectMotionEvent(motionEvent);
while (true) {
long delayToBeTap = isTapAt - SystemClock.uptimeMillis();
if (delayToBeTap <= 10) {
break;
}
// Sleep only a fraction of the time, since there may be other events in the UI queue
// that could cause us to start sleeping late, and then oversleep.
uiController.loopMainThreadForAtLeast(delayToBeTap / 4);
}
boolean longPress = false;
if (SystemClock.uptimeMillis() > (downTime + ViewConfiguration.getLongPressTimeout())) {
longPress = true;
Log.w(TAG, "Overslept and turned a tap into a long press");
}
if (!injectEventSucceeded) {
motionEvent.recycle();
motionEvent = null;
continue;
}
return new DownResultHolder(motionEvent, longPress);
} catch (InjectEventSecurityException e) {
throw new PerformException.Builder()
.withActionDescription("Send down motion event")
.withViewDescription("unknown") // likely to be replaced by FailureHandler
.withCause(e)
.build();
}
}
throw new PerformException.Builder()
.withActionDescription(
String.format(Locale.ROOT, "click (after %s attempts)", MAX_CLICK_ATTEMPTS))
.withViewDescription("unknown") // likely to be replaced by FailureHandler
.build();
}
private boolean isLongPress() {
return (System.currentTimeMillis() - mTimePressed) >= ViewConfiguration.getLongPressTimeout();
}
private void addIconToScreen(final String packageName, int resourceId, int x, int y) {
if(viewList.size() >= 5) {
return;
}
final ImageView iconView = new ImageView(this);
viewList.add(iconView);
Drawable draw = getIcon(packageName, resourceId);
iconView.setImageDrawable(draw);
IconHolder iconHolder = new IconHolder(iconView, draw, packageName);
iconView.setTag(iconHolder);
final WindowManager.LayoutParams params = new WindowManager.LayoutParams();
setWindowParams(params, x, y);
windowManager.addView(iconView, params);
ViewConfiguration vc = ViewConfiguration.get(iconView.getContext());
final int mScaledTouchSlop = 20;
final int mLongPressTimeOut = vc.getLongPressTimeout();
final int mTapTimeOut = vc.getTapTimeout();
iconView.setOnTouchListener(new View.OnTouchListener() {
private WindowManager.LayoutParams paramsF = params;
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d("AppFloat", "Action Down");
initialX = paramsF.x;
initialY = paramsF.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
return false;
case MotionEvent.ACTION_MOVE:
Log.d("AppFloat", "Action Move");
paramsF.x = initialX + (int) (event.getRawX() - initialTouchX);
paramsF.y = initialY + (int) (event.getRawY() - initialTouchY);
windowManager.updateViewLayout(iconView, paramsF);
return false;
case MotionEvent.ACTION_UP:
Log.d("AppFloat", "Action Up");
Log.d("AppFloat", "DistanceX: " + Math.abs(initialTouchX - event.getRawX()));
Log.d("AppFloat", "DistanceY: " + Math.abs(initialTouchY - event.getRawY()));
Log.d("AppFloat", "elapsed gesture time: " + (event.getEventTime() - event.getDownTime()));
if((Math.abs(initialTouchX - event.getRawX()) <= mScaledTouchSlop) && (Math.abs(initialTouchY - event.getRawY()) <= mScaledTouchSlop)) {
if((event.getEventTime() - event.getDownTime()) < mTapTimeOut ) {
Log.d("AppFloat", "Click Detected");
startAppActivity(packageName);
} else if((event.getEventTime() - event.getDownTime()) >= mLongPressTimeOut) {
Log.d("AppFloat", "Long Click Detected");
}
}
default:
Log.d("AppFloat", "Action Default");
break;
}
return false;
}
});
}
public TextSelectionHelper() {
longpressDelay = ViewConfiguration.getLongPressTimeout();
touchSlop = ViewConfiguration.get(ApplicationLoader.applicationContext).getScaledTouchSlop();
}
/**
* Create a description for a long press gesture. Gestures outside the screen will be offset to
* fit in the screen.
*
* @param context The context in which the gesture is being created
* @param x The x coordinate of the long press
* @param y The y coordinate of the long press
* @return A description of a long press at ({@code x}, {@code y})
*/
public static GestureDescription createLongPress(Context context, int x, int y) {
Path path = new Path();
path.moveTo(x, y);
int durationMs = ViewConfiguration.getLongPressTimeout() * 2;
return createGestureDescription(new StrokeDescription(path, 0, durationMs));
}