1fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard/*
2fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard * Copyright (C) 2013 The Android Open Source Project
3fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard *
4fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard * Licensed under the Apache License, Version 2.0 (the "License");
5fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard * you may not use this file except in compliance with the License.
6fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard * You may obtain a copy of the License at
7fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard *
8fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard *      http://www.apache.org/licenses/LICENSE-2.0
9fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard *
10fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard * Unless required by applicable law or agreed to in writing, software
11fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard * distributed under the License is distributed on an "AS IS" BASIS,
12fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard * See the License for the specific language governing permissions and
14fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard * limitations under the License.
15fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard */
16fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard
17fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroardpackage com.android.gallery3d.filtershow.state;
18fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard
19fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroardimport android.view.DragEvent;
20fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroardimport android.view.View;
21fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroardimport android.widget.ArrayAdapter;
22fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroardimport android.widget.LinearLayout;
23fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard
24fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroardclass DragListener implements View.OnDragListener {
25fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard
26fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    private static final String LOGTAG = "DragListener";
27fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    private PanelTrack mStatePanelTrack;
28fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    private static float sSlope = 0.2f;
29fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard
30fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    public DragListener(PanelTrack statePanelTrack) {
31fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        mStatePanelTrack = statePanelTrack;
32fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    }
33fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard
34fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    private void setState(DragEvent event) {
35fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        float translation = event.getY() - mStatePanelTrack.getTouchPoint().y;
36fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        float alpha = 1.0f - (Math.abs(translation)
37fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                / mStatePanelTrack.getCurrentView().getHeight());
38fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        if (mStatePanelTrack.getOrientation() == LinearLayout.VERTICAL) {
39fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard            translation = event.getX() - mStatePanelTrack.getTouchPoint().x;
40fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard            alpha = 1.0f - (Math.abs(translation)
41fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                    / mStatePanelTrack.getCurrentView().getWidth());
42fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard            mStatePanelTrack.getCurrentView().setTranslationX(translation);
43fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        } else {
44fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard            mStatePanelTrack.getCurrentView().setTranslationY(translation);
45fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        }
46fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        mStatePanelTrack.getCurrentView().setBackgroundAlpha(alpha);
47fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    }
48fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard
49fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    @Override
50fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    public boolean onDrag(View v, DragEvent event) {
51fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        switch (event.getAction()) {
52fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard            case DragEvent.ACTION_DRAG_STARTED: {
53fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                break;
54fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard            }
55fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard            case DragEvent.ACTION_DRAG_LOCATION: {
56fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                if (mStatePanelTrack.getCurrentView() != null) {
57fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                    setState(event);
58fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                    View over = mStatePanelTrack.findChildAt((int) event.getX(),
59fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                                                             (int) event.getY());
60fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                    if (over != null && over != mStatePanelTrack.getCurrentView()) {
61fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                        StateView stateView = (StateView) over;
62fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                        if (stateView != mStatePanelTrack.getCurrentView()) {
63fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                            int pos = mStatePanelTrack.findChild(over);
64fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                            int origin = mStatePanelTrack.findChild(
65fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                                    mStatePanelTrack.getCurrentView());
66fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                            ArrayAdapter array = (ArrayAdapter) mStatePanelTrack.getAdapter();
67fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                            if (origin != -1 && pos != -1) {
68fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                                State current = (State) array.getItem(origin);
69fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                                array.remove(current);
70fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                                array.insert(current, pos);
71fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                                mStatePanelTrack.fillContent(false);
72fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                                mStatePanelTrack.setCurrentView(mStatePanelTrack.getChildAt(pos));
73fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                            }
74fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                        }
75fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                    }
76fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                }
77fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                break;
78fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard            }
79fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard            case DragEvent.ACTION_DRAG_ENTERED: {
80fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                mStatePanelTrack.setExited(false);
81fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                if (mStatePanelTrack.getCurrentView() != null) {
82fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                    mStatePanelTrack.getCurrentView().setVisibility(View.VISIBLE);
83fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                }
84fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                return true;
85fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard            }
86fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard            case DragEvent.ACTION_DRAG_EXITED: {
87fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                if (mStatePanelTrack.getCurrentView() != null) {
88fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                    setState(event);
89fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                    mStatePanelTrack.getCurrentView().setVisibility(View.INVISIBLE);
90fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                }
91fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                mStatePanelTrack.setExited(true);
92fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                break;
93fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard            }
94fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard            case DragEvent.ACTION_DROP: {
95fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                break;
96fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard            }
97fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard            case DragEvent.ACTION_DRAG_ENDED: {
98fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                if (mStatePanelTrack.getCurrentView() != null
99fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                        && mStatePanelTrack.getCurrentView().getAlpha() > sSlope) {
100fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                    setState(event);
101fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                }
102fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                mStatePanelTrack.checkEndState();
103fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                break;
104fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard            }
105fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard            default:
106fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                break;
107fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        }
108fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        return true;
109fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    }
110fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard}
111