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;
21ca03208c6ef5bd79af99309d0e14db4a238cb691Aurimas Liutikasimport android.support.v7.recyclerview.R;
22e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyarimport android.support.v7.widget.RecyclerView;
23e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyarimport android.view.View;
24c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar
25e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar/**
26c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar * Package private class to keep implementations. Putting them inside ItemTouchUIUtil makes them
27c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar * public API, which is not desired in this case.
28e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar */
29c0459105252a3db52bc44ad62ecff4288860180dYigit Boyarclass ItemTouchUIUtilImpl {
30f715449617ee8846dcf3452d5e4a934f9ced5879Aurimas Liutikas    static class Api21Impl extends BaseImpl {
31e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        @Override
32c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar        public void onDraw(Canvas c, RecyclerView recyclerView, View view,
33e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar                float dX, float dY, int actionState, boolean isCurrentlyActive) {
34e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar            if (isCurrentlyActive) {
35c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar                Object originalElevation = view.getTag(R.id.item_touch_helper_previous_elevation);
36e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar                if (originalElevation == null) {
37c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar                    originalElevation = ViewCompat.getElevation(view);
38c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar                    float newElevation = 1f + findMaxElevation(recyclerView, view);
39c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar                    ViewCompat.setElevation(view, newElevation);
40c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar                    view.setTag(R.id.item_touch_helper_previous_elevation, originalElevation);
41e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar                }
42e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar            }
43c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar            super.onDraw(c, recyclerView, view, dX, dY, actionState, isCurrentlyActive);
44e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        }
45e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar
46e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        private float findMaxElevation(RecyclerView recyclerView, View itemView) {
47e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar            final int childCount = recyclerView.getChildCount();
48e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar            float max = 0;
49e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar            for (int i = 0; i < childCount; i++) {
50e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar                final View child = recyclerView.getChildAt(i);
51e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar                if (child == itemView) {
52e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar                    continue;
53e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar                }
54e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar                final float elevation = ViewCompat.getElevation(child);
55e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar                if (elevation > max) {
56e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar                    max = elevation;
57e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar                }
58e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar            }
59e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar            return max;
60e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        }
61e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar
62e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        @Override
63c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar        public void clearView(View view) {
64c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar            final Object tag = view.getTag(R.id.item_touch_helper_previous_elevation);
65e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar            if (tag != null && tag instanceof Float) {
66c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar                ViewCompat.setElevation(view, (Float) tag);
67e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar            }
68c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar            view.setTag(R.id.item_touch_helper_previous_elevation, null);
69c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar            super.clearView(view);
70e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        }
71e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar    }
72e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar
73f715449617ee8846dcf3452d5e4a934f9ced5879Aurimas Liutikas    static class BaseImpl implements ItemTouchUIUtil {
74e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar
75e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        @Override
76c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar        public void clearView(View view) {
77ca03208c6ef5bd79af99309d0e14db4a238cb691Aurimas Liutikas            view.setTranslationX(0f);
78ca03208c6ef5bd79af99309d0e14db4a238cb691Aurimas Liutikas            view.setTranslationY(0f);
79e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        }
80e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar
81e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        @Override
82c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar        public void onSelected(View view) {
83e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar
84e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        }
85e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar
86e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        @Override
87c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar        public void onDraw(Canvas c, RecyclerView recyclerView, View view,
88e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar                float dX, float dY, int actionState, boolean isCurrentlyActive) {
89ca03208c6ef5bd79af99309d0e14db4a238cb691Aurimas Liutikas            view.setTranslationX(dX);
90ca03208c6ef5bd79af99309d0e14db4a238cb691Aurimas Liutikas            view.setTranslationY(dY);
91e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        }
92e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar
93e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        @Override
94e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        public void onDrawOver(Canvas c, RecyclerView recyclerView,
95c0459105252a3db52bc44ad62ecff4288860180dYigit Boyar                View view, float dX, float dY, int actionState, boolean isCurrentlyActive) {
96e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar
97e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar        }
98e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar    }
99e71a1df9b3c0e1bd3c21a1b3dd20a41790d4a950Yigit Boyar}
100