1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.gallery3d.filtershow.editors;
18
19import android.content.Context;
20import android.util.AttributeSet;
21import android.view.GestureDetector;
22import android.view.Menu;
23import android.view.MenuItem;
24import android.view.MotionEvent;
25import android.widget.Button;
26
27public class SwapButton extends Button implements GestureDetector.OnGestureListener {
28
29    public static int ANIM_DURATION = 200;
30
31    public interface SwapButtonListener {
32        public void swapLeft(MenuItem item);
33        public void swapRight(MenuItem item);
34    }
35
36    private GestureDetector mDetector;
37    private SwapButtonListener mListener;
38    private Menu mMenu;
39    private int mCurrentMenuIndex;
40
41    public SwapButton(Context context, AttributeSet attrs) {
42        super(context, attrs);
43        mDetector = new GestureDetector(context, this);
44    }
45
46    public SwapButtonListener getListener() {
47        return mListener;
48    }
49
50    public void setListener(SwapButtonListener listener) {
51        mListener = listener;
52    }
53
54    public boolean onTouchEvent(MotionEvent me) {
55        if (!mDetector.onTouchEvent(me)) {
56            return super.onTouchEvent(me);
57        }
58        return true;
59    }
60
61    @Override
62    public boolean onDown(MotionEvent e) {
63        return true;
64    }
65
66    @Override
67    public void onShowPress(MotionEvent e) {
68    }
69
70    @Override
71    public boolean onSingleTapUp(MotionEvent e) {
72        callOnClick();
73        return true;
74    }
75
76    @Override
77    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
78        return false;
79    }
80
81    @Override
82    public void onLongPress(MotionEvent e) {
83    }
84
85    @Override
86    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
87        if (mMenu == null) {
88            return false;
89        }
90        if (e1.getX() - e2.getX() > 0) {
91            // right to left
92            mCurrentMenuIndex++;
93            if (mCurrentMenuIndex == mMenu.size()) {
94                mCurrentMenuIndex = 0;
95            }
96            if (mListener != null) {
97                mListener.swapRight(mMenu.getItem(mCurrentMenuIndex));
98            }
99        } else {
100            // left to right
101            mCurrentMenuIndex--;
102            if (mCurrentMenuIndex < 0) {
103                mCurrentMenuIndex = mMenu.size() - 1;
104            }
105            if (mListener != null) {
106                mListener.swapLeft(mMenu.getItem(mCurrentMenuIndex));
107            }
108        }
109        return true;
110    }
111}
112