1/*
2 * Copyright (C) 2015 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 android.support.v7.widget.helper;
18
19import android.graphics.Canvas;
20import android.support.v4.view.ViewCompat;
21import android.support.v7.widget.RecyclerView;
22import android.view.View;
23import android.support.v7.recyclerview.R;
24
25
26/**
27 * Package private class to keep implementations. Putting them inside ItemTouchUIUtil makes them
28 * public API, which is not desired in this case.
29 */
30class ItemTouchUIUtilImpl {
31    static class Lollipop extends Honeycomb {
32        @Override
33        public void onDraw(Canvas c, RecyclerView recyclerView, View view,
34                float dX, float dY, int actionState, boolean isCurrentlyActive) {
35            if (isCurrentlyActive) {
36                Object originalElevation = view.getTag(R.id.item_touch_helper_previous_elevation);
37                if (originalElevation == null) {
38                    originalElevation = ViewCompat.getElevation(view);
39                    float newElevation = 1f + findMaxElevation(recyclerView, view);
40                    ViewCompat.setElevation(view, newElevation);
41                    view.setTag(R.id.item_touch_helper_previous_elevation, originalElevation);
42                }
43            }
44            super.onDraw(c, recyclerView, view, dX, dY, actionState, isCurrentlyActive);
45        }
46
47        private float findMaxElevation(RecyclerView recyclerView, View itemView) {
48            final int childCount = recyclerView.getChildCount();
49            float max = 0;
50            for (int i = 0; i < childCount; i++) {
51                final View child = recyclerView.getChildAt(i);
52                if (child == itemView) {
53                    continue;
54                }
55                final float elevation = ViewCompat.getElevation(child);
56                if (elevation > max) {
57                    max = elevation;
58                }
59            }
60            return max;
61        }
62
63        @Override
64        public void clearView(View view) {
65            final Object tag = view.getTag(R.id.item_touch_helper_previous_elevation);
66            if (tag != null && tag instanceof Float) {
67                ViewCompat.setElevation(view, (Float) tag);
68            }
69            view.setTag(R.id.item_touch_helper_previous_elevation, null);
70            super.clearView(view);
71        }
72    }
73
74    static class Honeycomb implements ItemTouchUIUtil {
75
76        @Override
77        public void clearView(View view) {
78            ViewCompat.setTranslationX(view, 0f);
79            ViewCompat.setTranslationY(view, 0f);
80        }
81
82        @Override
83        public void onSelected(View view) {
84
85        }
86
87        @Override
88        public void onDraw(Canvas c, RecyclerView recyclerView, View view,
89                float dX, float dY, int actionState, boolean isCurrentlyActive) {
90            ViewCompat.setTranslationX(view, dX);
91            ViewCompat.setTranslationY(view, dY);
92        }
93
94        @Override
95        public void onDrawOver(Canvas c, RecyclerView recyclerView,
96                View view, float dX, float dY, int actionState, boolean isCurrentlyActive) {
97
98        }
99    }
100
101    static class Gingerbread implements ItemTouchUIUtil {
102
103        private void draw(Canvas c, RecyclerView parent, View view,
104                float dX, float dY) {
105            c.save();
106            c.translate(dX, dY);
107            parent.drawChild(c, view, 0);
108            c.restore();
109        }
110
111        @Override
112        public void clearView(View view) {
113            view.setVisibility(View.VISIBLE);
114        }
115
116        @Override
117        public void onSelected(View view) {
118            view.setVisibility(View.INVISIBLE);
119        }
120
121        @Override
122        public void onDraw(Canvas c, RecyclerView recyclerView, View view,
123                float dX, float dY, int actionState, boolean isCurrentlyActive) {
124            if (actionState != ItemTouchHelper.ACTION_STATE_DRAG) {
125                draw(c, recyclerView, view, dX, dY);
126            }
127        }
128
129        @Override
130        public void onDrawOver(Canvas c, RecyclerView recyclerView,
131                View view, float dX, float dY,
132                int actionState, boolean isCurrentlyActive) {
133            if (actionState == ItemTouchHelper.ACTION_STATE_DRAG) {
134                draw(c, recyclerView, view, dX, dY);
135            }
136        }
137    }
138}
139