| 1 | /* |
| 2 | * This file is a part of X-Android |
| 3 | * Copyright © Vyacheslav Krylov 2014 |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | package me.vkryl.android; |
| 19 | |
| 20 | import android.graphics.drawable.Drawable; |
| 21 | import android.os.Build; |
| 22 | import android.view.HapticFeedbackConstants; |
| 23 | import android.view.MotionEvent; |
| 24 | import android.view.SoundEffectConstants; |
| 25 | import android.view.View; |
| 26 | import android.view.ViewTreeObserver; |
| 27 | |
| 28 | import java.lang.reflect.Method; |
| 29 | |
| 30 | public final class ViewUtils { |
| 31 | public static void onClick (View v) { |
| 32 | if (v != null) { |
| 33 | v.playSoundEffect(SoundEffectConstants.CLICK); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | @SuppressWarnings("deprecation") |
| 38 | public static void setBackground (View view, Drawable drawable) { |
| 39 | if (view != null) { |
| 40 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { |
| 41 | view.setBackground(drawable); |
| 42 | } else { |
| 43 | view.setBackgroundDrawable(drawable); |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | public static String getActionName (MotionEvent e) { |
| 49 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { |
| 50 | return MotionEvent.actionToString(e.getAction()); |
| 51 | } else { |
| 52 | switch (e.getAction()) { |
| 53 | case MotionEvent.ACTION_DOWN: return "ACTION_DOWN"; |
| 54 | case MotionEvent.ACTION_UP: return "ACTION_UP"; |
| 55 | case MotionEvent.ACTION_MOVE: return "ACTION_MOVE"; |
| 56 | case MotionEvent.ACTION_CANCEL: return "ACTION_CANCEL"; |
| 57 | } |
| 58 | return "ACTION:" + e.getAction(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | public static void runJustBeforeBeingDrawn (final View view, final Runnable runnable) { |
| 63 | final ViewTreeObserver.OnPreDrawListener preDrawListener = new ViewTreeObserver.OnPreDrawListener() { |
| 64 | @Override |
| 65 | public boolean onPreDraw() { |
| 66 | view.getViewTreeObserver().removeOnPreDrawListener(this); |
| 67 | runnable.run(); |
| 68 | return true; |
| 69 | } |
| 70 | }; |
| 71 | view.getViewTreeObserver().addOnPreDrawListener(preDrawListener); |
| 72 | } |
| 73 | |
| 74 | @SuppressWarnings("deprecation") |
| 75 | public static void hapticVibrate (View view, boolean isForce, boolean ignoreSetting) { |
| 76 | if (view != null) { |
| 77 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && ignoreSetting) { |
| 78 | // TODO[sdk]: replacement for HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING |
| 79 | } |
| 80 | int feedbackConstant = isForce ? HapticFeedbackConstants.LONG_PRESS : HapticFeedbackConstants.KEYBOARD_TAP; |
| 81 | int flags = ignoreSetting ? HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING : 0; |
| 82 | view.performHapticFeedback(feedbackConstant, flags); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | public static void fixViewPager (View pager) { |
| 87 | // Whenever ViewPager gets re-attached to window (e.g. inside RecyclerView while scrolling), |
| 88 | // first touch event gets processed improperly |
| 89 | try { |
| 90 | Method method = pager.getClass().getDeclaredMethod("resetTouch"); |
| 91 | method.setAccessible(true); |
| 92 | method.invoke(pager); |
| 93 | } catch (Throwable ignored) { } |
| 94 | // pager.requestLayout(); |
| 95 | } |
| 96 | } |
| 97 | |