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.state;
18
19import android.view.DragEvent;
20import android.view.View;
21import android.widget.ArrayAdapter;
22import android.widget.LinearLayout;
23
24class DragListener implements View.OnDragListener {
25
26    private static final String LOGTAG = "DragListener";
27    private PanelTrack mStatePanelTrack;
28    private static float sSlope = 0.2f;
29
30    public DragListener(PanelTrack statePanelTrack) {
31        mStatePanelTrack = statePanelTrack;
32    }
33
34    private void setState(DragEvent event) {
35        float translation = event.getY() - mStatePanelTrack.getTouchPoint().y;
36        float alpha = 1.0f - (Math.abs(translation)
37                / mStatePanelTrack.getCurrentView().getHeight());
38        if (mStatePanelTrack.getOrientation() == LinearLayout.VERTICAL) {
39            translation = event.getX() - mStatePanelTrack.getTouchPoint().x;
40            alpha = 1.0f - (Math.abs(translation)
41                    / mStatePanelTrack.getCurrentView().getWidth());
42            mStatePanelTrack.getCurrentView().setTranslationX(translation);
43        } else {
44            mStatePanelTrack.getCurrentView().setTranslationY(translation);
45        }
46        mStatePanelTrack.getCurrentView().setBackgroundAlpha(alpha);
47    }
48
49    @Override
50    public boolean onDrag(View v, DragEvent event) {
51        switch (event.getAction()) {
52            case DragEvent.ACTION_DRAG_STARTED: {
53                break;
54            }
55            case DragEvent.ACTION_DRAG_LOCATION: {
56                if (mStatePanelTrack.getCurrentView() != null) {
57                    setState(event);
58                    View over = mStatePanelTrack.findChildAt((int) event.getX(),
59                                                             (int) event.getY());
60                    if (over != null && over != mStatePanelTrack.getCurrentView()) {
61                        StateView stateView = (StateView) over;
62                        if (stateView != mStatePanelTrack.getCurrentView()) {
63                            int pos = mStatePanelTrack.findChild(over);
64                            int origin = mStatePanelTrack.findChild(
65                                    mStatePanelTrack.getCurrentView());
66                            ArrayAdapter array = (ArrayAdapter) mStatePanelTrack.getAdapter();
67                            if (origin != -1 && pos != -1) {
68                                State current = (State) array.getItem(origin);
69                                array.remove(current);
70                                array.insert(current, pos);
71                                mStatePanelTrack.fillContent(false);
72                                mStatePanelTrack.setCurrentView(mStatePanelTrack.getChildAt(pos));
73                            }
74                        }
75                    }
76                }
77                break;
78            }
79            case DragEvent.ACTION_DRAG_ENTERED: {
80                mStatePanelTrack.setExited(false);
81                if (mStatePanelTrack.getCurrentView() != null) {
82                    mStatePanelTrack.getCurrentView().setVisibility(View.VISIBLE);
83                }
84                return true;
85            }
86            case DragEvent.ACTION_DRAG_EXITED: {
87                if (mStatePanelTrack.getCurrentView() != null) {
88                    setState(event);
89                    mStatePanelTrack.getCurrentView().setVisibility(View.INVISIBLE);
90                }
91                mStatePanelTrack.setExited(true);
92                break;
93            }
94            case DragEvent.ACTION_DROP: {
95                break;
96            }
97            case DragEvent.ACTION_DRAG_ENDED: {
98                if (mStatePanelTrack.getCurrentView() != null
99                        && mStatePanelTrack.getCurrentView().getAlpha() > sSlope) {
100                    setState(event);
101                }
102                mStatePanelTrack.checkEndState();
103                break;
104            }
105            default:
106                break;
107        }
108        return true;
109    }
110}
111