1/*
2 * Copyright (c) 2016, 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.car.radio;
18
19import android.animation.ValueAnimator;
20import android.content.Context;
21import android.support.v7.widget.RecyclerView;
22import android.view.View;
23import com.android.car.view.PagedListView;
24
25/**
26 * Listener on the preset list that will add elevation on the container holding the current
27 * playing radio station. This elevation will give the illusion of the preset list scrolling
28 * under that container.
29 */
30public class PresetListScrollListener extends RecyclerView.OnScrollListener {
31    private static final int ANIMATION_DURATION_MS = 100;
32
33    private final float mContainerElevation;
34    private final View mCurrentRadioCardContainer;
35    private final View mCurrentRadioCard;
36    private final PagedListView mPresetList;
37    private final ValueAnimator mRemoveElevationAnimator;
38
39    public PresetListScrollListener(Context context, View container, View currentRadioCard,
40            PagedListView presetList) {
41        mPresetList = presetList;
42        mCurrentRadioCardContainer = container.findViewById(R.id.preset_current_card_container);
43        mCurrentRadioCard = currentRadioCard;
44        mContainerElevation = context.getResources()
45                .getDimension(R.dimen.car_preset_container_elevation);
46
47        mRemoveElevationAnimator = ValueAnimator.ofFloat(mContainerElevation, 0.f);
48        mRemoveElevationAnimator
49                .setDuration(ANIMATION_DURATION_MS)
50                .addUpdateListener(animation -> mCurrentRadioCardContainer.setElevation(
51                        (float) animation.getAnimatedValue()));
52    }
53
54    @Override
55    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
56        if (mPresetList.getTranslationY() != 0.f) {
57            return;
58        }
59
60        if (mPresetList.getLayoutManager().isAtTop()) {
61            // Animate the removal of the elevation so that it's not jarring.
62            mRemoveElevationAnimator.start();
63        } else {
64            // No animation needed when adding the elevation because the scroll masks the adding
65            // of the elevation.
66            mCurrentRadioCardContainer.setElevation(mContainerElevation);
67            mCurrentRadioCard.setTranslationZ(mContainerElevation);
68        }
69    }
70}
71