1e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar/*
2e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar * Copyright (C) 2015 The Android Open Source Project
3e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar *
4e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar * Licensed under the Apache License, Version 2.0 (the "License");
5e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar * you may not use this file except in compliance with the License.
6e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar * You may obtain a copy of the License at
7e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar *
8e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar *      http://www.apache.org/licenses/LICENSE-2.0
9e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar *
10e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar * Unless required by applicable law or agreed to in writing, software
11e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar * distributed under the License is distributed on an "AS IS" BASIS,
12e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar * See the License for the specific language governing permissions and
14e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar * limitations under the License.
15e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar */
16e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar
17e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyarpackage android.support.v7.widget.helper;
18e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar
19e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyarimport android.graphics.Canvas;
20e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyarimport android.support.v4.view.ViewCompat;
21e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyarimport android.support.v7.widget.RecyclerView;
22e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyarimport android.view.View;
23e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyarimport android.support.v7.recyclerview.R;
24e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar
25c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar
26e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar/**
27c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar * Package private class to keep implementations. Putting them inside ItemTouchUIUtil makes them
28c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar * public API, which is not desired in this case.
29e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar */
30c0459105252a3db52bc44ad62ecff4288860180dYigit Boyarclass ItemTouchUIUtilImpl {
31c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar    static class Lollipop extends Honeycomb {
32e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        @Override
33c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar        public void onDraw(Canvas c, RecyclerView recyclerView, View view,
34e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar                float dX, float dY, int actionState, boolean isCurrentlyActive) {
35e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar            if (isCurrentlyActive) {
36c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar                Object originalElevation = view.getTag(R.id.item_touch_helper_previous_elevation);
37e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar                if (originalElevation == null) {
38c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar                    originalElevation = ViewCompat.getElevation(view);
39c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar                    float newElevation = 1f + findMaxElevation(recyclerView, view);
40c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar                    ViewCompat.setElevation(view, newElevation);
41c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar                    view.setTag(R.id.item_touch_helper_previous_elevation, originalElevation);
42e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar                }
43e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar            }
44c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar            super.onDraw(c, recyclerView, view, dX, dY, actionState, isCurrentlyActive);
45e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        }
46e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar
47e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        private float findMaxElevation(RecyclerView recyclerView, View itemView) {
48e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar            final int childCount = recyclerView.getChildCount();
49e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar            float max = 0;
50e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar            for (int i = 0; i < childCount; i++) {
51e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar                final View child = recyclerView.getChildAt(i);
52e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar                if (child == itemView) {
53e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar                    continue;
54e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar                }
55e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar                final float elevation = ViewCompat.getElevation(child);
56e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar                if (elevation > max) {
57e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar                    max = elevation;
58e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar                }
59e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar            }
60e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar            return max;
61e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        }
62e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar
63e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        @Override
64c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar        public void clearView(View view) {
65c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar            final Object tag = view.getTag(R.id.item_touch_helper_previous_elevation);
66e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar            if (tag != null && tag instanceof Float) {
67c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar                ViewCompat.setElevation(view, (Float) tag);
68e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar            }
69c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar            view.setTag(R.id.item_touch_helper_previous_elevation, null);
70c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar            super.clearView(view);
71e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        }
72e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar    }
73e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar
74c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar    static class Honeycomb implements ItemTouchUIUtil {
75e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar
76e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        @Override
77c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar        public void clearView(View view) {
78c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar            ViewCompat.setTranslationX(view, 0f);
79c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar            ViewCompat.setTranslationY(view, 0f);
80e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        }
81e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar
82e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        @Override
83c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar        public void onSelected(View view) {
84e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar
85e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        }
86e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar
87e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        @Override
88c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar        public void onDraw(Canvas c, RecyclerView recyclerView, View view,
89e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar                float dX, float dY, int actionState, boolean isCurrentlyActive) {
90e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar            ViewCompat.setTranslationX(view, dX);
91e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar            ViewCompat.setTranslationY(view, dY);
92e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        }
93e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar
94e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        @Override
95e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        public void onDrawOver(Canvas c, RecyclerView recyclerView,
96c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar                View view, float dX, float dY, int actionState, boolean isCurrentlyActive) {
97e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar
98e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        }
99e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar    }
100e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar
101c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar    static class Gingerbread implements ItemTouchUIUtil {
102e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar
103c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar        private void draw(Canvas c, RecyclerView parent, View view,
104e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar                float dX, float dY) {
105e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar            c.save();
106e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar            c.translate(dX, dY);
107c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar            parent.drawChild(c, view, 0);
108e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar            c.restore();
109e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        }
110e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar
111e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        @Override
112c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar        public void clearView(View view) {
113c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar            view.setVisibility(View.VISIBLE);
114e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        }
115e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar
116e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        @Override
117c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar        public void onSelected(View view) {
118c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar            view.setVisibility(View.INVISIBLE);
119e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        }
120e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar
121e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        @Override
122c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar        public void onDraw(Canvas c, RecyclerView recyclerView, View view,
123e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar                float dX, float dY, int actionState, boolean isCurrentlyActive) {
124e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar            if (actionState != ItemTouchHelper.ACTION_STATE_DRAG) {
125c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar                draw(c, recyclerView, view, dX, dY);
126e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar            }
127e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        }
128e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar
129e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        @Override
130e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        public void onDrawOver(Canvas c, RecyclerView recyclerView,
131c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar                View view, float dX, float dY,
132e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar                int actionState, boolean isCurrentlyActive) {
133e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar            if (actionState == ItemTouchHelper.ACTION_STATE_DRAG) {
134c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar                draw(c, recyclerView, view, dX, dY);
135e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar            }
136e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        }
137e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar    }
138e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar}
139