Example usage for android.view View setOnGenericMotionListener

List of usage examples for android.view View setOnGenericMotionListener

Introduction

In this page you can find the example usage for android.view View setOnGenericMotionListener.

Prototype

public void setOnGenericMotionListener(OnGenericMotionListener l) 

Source Link

Document

Register a callback to be invoked when a generic motion event is sent to this view.

Usage

From source file:com.farmerbb.taskbar.activity.HomeActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
            WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

    View view = new View(this);
    view.setOnClickListener(view1 -> LocalBroadcastManager.getInstance(HomeActivity.this)
            .sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU")));

    view.setOnLongClickListener(view12 -> {
        setWallpaper();/*from   ww  w  . j  a va2  s  . c  o  m*/
        return false;
    });

    view.setOnGenericMotionListener((view13, motionEvent) -> {
        if (motionEvent.getAction() == MotionEvent.ACTION_BUTTON_PRESS
                && motionEvent.getButtonState() == MotionEvent.BUTTON_SECONDARY) {
            setWallpaper();
        }
        return false;
    });

    final GestureDetector detector = new GestureDetector(this, new GestureDetector.OnGestureListener() {
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            return false;
        }

        @Override
        public void onShowPress(MotionEvent e) {
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            return false;
        }

        @Override
        public void onLongPress(MotionEvent e) {
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            return false;
        }

        @Override
        public boolean onDown(MotionEvent e) {
            return false;
        }
    });

    detector.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener() {
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            final SharedPreferences pref = U.getSharedPreferences(HomeActivity.this);
            if (!pref.getBoolean("dont_show_double_tap_dialog", false)) {
                if (pref.getBoolean("double_tap_to_sleep", false)) {
                    U.lockDevice(HomeActivity.this);
                } else {
                    int theme = -1;
                    switch (pref.getString("theme", "light")) {
                    case "light":
                        theme = R.style.AppTheme;
                        break;
                    case "dark":
                        theme = R.style.AppTheme_Dark;
                        break;
                    }

                    AlertDialog.Builder builder = new AlertDialog.Builder(
                            new ContextThemeWrapper(HomeActivity.this, theme));
                    builder.setTitle(R.string.double_tap_to_sleep)
                            .setMessage(R.string.enable_double_tap_to_sleep)
                            .setNegativeButton(
                                    pref.getBoolean("double_tap_dialog_shown", false)
                                            ? R.string.action_dont_show_again
                                            : R.string.action_cancel,
                                    (dialog, which) -> pref.edit()
                                            .putBoolean(pref.getBoolean("double_tap_dialog_shown", false)
                                                    ? "dont_show_double_tap_dialog"
                                                    : "double_tap_dialog_shown", true)
                                            .apply())
                            .setPositiveButton(R.string.action_ok, (dialog, which) -> {
                                pref.edit().putBoolean("double_tap_to_sleep", true).apply();
                                U.lockDevice(HomeActivity.this);
                            });

                    AlertDialog dialog = builder.create();
                    dialog.show();
                }
            }

            return false;
        }

        @Override
        public boolean onDoubleTapEvent(MotionEvent e) {
            return false;
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            return false;
        }

    });

    view.setOnTouchListener((v, event) -> {
        detector.onTouchEvent(event);
        return false;
    });

    setContentView(view);

    LocalBroadcastManager.getInstance(this).registerReceiver(killReceiver,
            new IntentFilter("com.farmerbb.taskbar.KILL_HOME_ACTIVITY"));
    LocalBroadcastManager.getInstance(this).registerReceiver(forceTaskbarStartReceiver,
            new IntentFilter("com.farmerbb.taskbar.FORCE_TASKBAR_RESTART"));
}

From source file:meizhi.meizhi.malin.utils.DestroyCleanUtil.java

@SuppressLint("ObsoleteSdkInt")
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static void unBindView(View view) {
    if (view == null)
        return;//from   w  w w. ja v  a2  s  . c  om
    Drawable drawable;
    int i;
    //1.
    try {
        view.setOnClickListener(null);
    } catch (Throwable e) {
        CrashReport.postCatchedException(e);
    }

    //2.
    try {
        view.setOnCreateContextMenuListener(null);
    } catch (Throwable e) {
        CrashReport.postCatchedException(e);
    }

    //3.
    try {
        view.setOnFocusChangeListener(null);
    } catch (Throwable e) {
        CrashReport.postCatchedException(e);
    }

    //4.
    try {
        view.setOnKeyListener(null);
    } catch (Throwable e) {
        CrashReport.postCatchedException(e);
    }

    //5.
    try {
        view.setOnLongClickListener(null);
    } catch (Throwable e) {
        CrashReport.postCatchedException(e);
    }

    //6.
    try {
        view.setOnTouchListener(null);
    } catch (Throwable e) {
        CrashReport.postCatchedException(e);
    }

    //7.
    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
            view.setOnApplyWindowInsetsListener(null);
        }
    } catch (Throwable e) {
        CrashReport.postCatchedException(e);
    }

    //8.
    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            view.setOnContextClickListener(null);
        }
    } catch (Throwable e) {
        CrashReport.postCatchedException(e);
    }

    //9.
    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            view.setOnScrollChangeListener(null);
        }
    } catch (Throwable e) {
        CrashReport.postCatchedException(e);
    }

    //10.
    try {
        view.setOnDragListener(null);
    } catch (Throwable e) {
        CrashReport.postCatchedException(e);
    }

    //11.
    try {
        view.setOnGenericMotionListener(null);
    } catch (Throwable e) {
        CrashReport.postCatchedException(e);
    }

    //12.
    try {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB_MR2) {//13
            view.setOnHoverListener(null);
        }
    } catch (Throwable e) {
        CrashReport.postCatchedException(e);
    }

    //13.
    try {
        view.setOnSystemUiVisibilityChangeListener(null);
    } catch (Throwable e) {
        CrashReport.postCatchedException(e);
    }

    /**
     * @see SwipeRefreshLayout#onDetachedFromWindow()
     */
    if (view.getBackground() != null && !view.getClass().getName().equals(CIRCLE_CLASS)) {
        try {
            view.getBackground().setCallback(null);
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {//16
                view.setBackgroundDrawable(null);
            } else {
                view.setBackground(null);
            }
        } catch (Throwable e) {
            CrashReport.postCatchedException(e);
        }
    }

    //ImageView
    if (view instanceof ImageView) {
        try {
            ImageView imageView = (ImageView) view;
            drawable = imageView.getDrawable();
            if (drawable != null) {
                drawable.setCallback(null);
            }
            imageView.setImageDrawable(null);
            imageView.setImageBitmap(null);
        } catch (Throwable e) {
            CrashReport.postCatchedException(e);
        }
    }

    //TextView
    if (view instanceof TextView) {
        try {
            TextView textView = (TextView) view;
            textView.setCompoundDrawables(null, null, null, null);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                textView.setCompoundDrawablesRelative(null, null, null, null);
            }
            textView.setCursorVisible(false);
        } catch (Throwable e) {
            CrashReport.postCatchedException(e);
        }
    }

    //ImageButton
    if (view instanceof ImageButton) {
        try {
            ImageButton imageButton = (ImageButton) view;
            drawable = imageButton.getDrawable();
            if (drawable != null) {
                drawable.setCallback(null);
            }
            imageButton.setImageDrawable(null);
            imageButton.setImageBitmap(null);
        } catch (Throwable e) {
            CrashReport.postCatchedException(e);
        }
    }

    //ListView
    if (view instanceof ListView) {
        ListView listView = (ListView) view;

        try {
            listView.setAdapter(null);
        } catch (Throwable e) {
            CrashReport.postCatchedException(e);
        }

        try {
            listView.setOnScrollListener(null);
        } catch (Throwable e) {
            CrashReport.postCatchedException(e);
        }

        try {
            listView.setOnItemClickListener(null);
        } catch (Throwable e) {
            CrashReport.postCatchedException(e);
        }

        try {
            listView.setOnItemLongClickListener(null);
        } catch (Throwable e) {
            CrashReport.postCatchedException(e);
        }

        try {
            listView.setOnItemSelectedListener(null);
        } catch (Throwable e) {
            CrashReport.postCatchedException(e);
        }
    }

    //RecyclerView
    if (view instanceof RecyclerView) {
        try {
            RecyclerView recyclerView = (RecyclerView) view;
            recyclerView.setAdapter(null);
            recyclerView.setChildDrawingOrderCallback(null);
            recyclerView.setOnScrollListener(null);
            recyclerView.addOnScrollListener(null);
            recyclerView.removeOnScrollListener(null);
            recyclerView.setRecyclerListener(null);
        } catch (Throwable e) {
            CrashReport.postCatchedException(e);
        }
    }

    //WebView
    if (view instanceof WebView) {

        WebView webView = (WebView) view;
        try {
            webView.stopLoading();
        } catch (Throwable ignored) {
            CrashReport.postCatchedException(ignored);
        }

        try {
            webView.removeAllViews();
        } catch (Throwable ignored) {
            CrashReport.postCatchedException(ignored);
        }

        try {
            webView.setWebChromeClient(null);
        } catch (Throwable ignored) {
            CrashReport.postCatchedException(ignored);
        }

        try {
            webView.setWebViewClient(null);
        } catch (Throwable ignored) {
            CrashReport.postCatchedException(ignored);
        }

        try {
            webView.destroy();
        } catch (Throwable ignored) {
            CrashReport.postCatchedException(ignored);
        }

        try {
            if (null != view.getParent() && view.getParent() instanceof ViewGroup) {
                ((ViewGroup) view.getParent()).removeView(view);
            }
        } catch (Throwable ignored) {
            CrashReport.postCatchedException(ignored);
        }

    }

    //SurfaceView
    if (view instanceof SurfaceView) {
        try {
            SurfaceView surfaceView = (SurfaceView) view;
            SurfaceHolder holder = surfaceView.getHolder();
            if (holder != null) {
                Surface surface = holder.getSurface();
                if (surface != null) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                        surface.release();
                    }
                }
            }
        } catch (Throwable ignored) {
            CrashReport.postCatchedException(ignored);
        }
    }

    view.destroyDrawingCache();
    view.clearAnimation();

    if (view instanceof ViewGroup) {
        ViewGroup viewGroup = (ViewGroup) view;
        int childCount = (viewGroup).getChildCount();
        for (i = 0; i < childCount; i++) {
            unBindView((viewGroup).getChildAt(i));
        }
    }
}