summaryrefslogtreecommitdiffstats
path: root/src/com/android/wallpaper/picker/individual/DesktopRotationHolder.java
blob: d2ab5198217c9d1f93b65ebaf3177ce6c4239813 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.wallpaper.picker.individual;

import android.app.Activity;
import android.view.View;
import android.widget.ImageView;

import com.android.wallpaper.R;
import com.android.wallpaper.asset.Asset;
import com.android.wallpaper.asset.Asset.DrawableLoadedListener;
import com.android.wallpaper.model.WallpaperRotationInitializer;
import com.android.wallpaper.module.InjectorProvider;
import com.android.wallpaper.module.WallpaperPreferences;
import com.android.wallpaper.picker.RotationStarter;

import androidx.recyclerview.widget.RecyclerView.ViewHolder;

/**
 * IndividualHolder subclass for a wallpaper tile in the RecyclerView for which a click should
 * set the wallpaper as the current wallpaper on the device.
 */
class DesktopRotationHolder extends ViewHolder implements View.OnClickListener,
        SelectableHolder {

    static final int CROSSFADE_DURATION_MILLIS = 2000;
    static final int CROSSFADE_DURATION_PAUSE_MILLIS = 2000;
    static final int CROSSFADE_DURATION_MILLIS_SHORT = 300;

    private WallpaperPreferences mWallpaperPreferences;
    private Activity mActivity;
    private SelectionAnimator mSelectionAnimator;
    private RotationStarter mRotationStarter;
    private View mTile;
    private ImageView mThumbnailView;

    public DesktopRotationHolder(
            Activity hostActivity, int tileHeightPx, View itemView, SelectionAnimator selectionAnimator,
            RotationStarter rotationStarter) {
        super(itemView);

        mWallpaperPreferences = InjectorProvider.getInjector().getPreferences(hostActivity);
        mActivity = hostActivity;
        mTile = itemView.findViewById(R.id.tile);
        mThumbnailView = (ImageView) itemView.findViewById(R.id.thumbnail);

        mTile.setOnClickListener(this);
        mTile.getLayoutParams().height = tileHeightPx;
        itemView.getLayoutParams().height = tileHeightPx;

        mSelectionAnimator = selectionAnimator;
        mRotationStarter = rotationStarter;
    }

    @Override
    public void setSelectionState(@SelectionState int selectionState) {
        if (selectionState == SELECTION_STATE_SELECTED) {
            mSelectionAnimator.animateSelected();
        } else if (selectionState == SELECTION_STATE_DESELECTED) {
            mSelectionAnimator.animateDeselected();
        } else if (selectionState == SELECTION_STATE_LOADING) {
            mSelectionAnimator.showLoading();
        }
    }

    @Override
    public void onClick(View view) {
        // If this is already selected, then do nothing.
        if (mSelectionAnimator.isSelected()) {
            return;
        }

        mSelectionAnimator.showLoading();
        mRotationStarter.startRotation(WallpaperRotationInitializer.NETWORK_PREFERENCE_CELLULAR_OK);
    }

    /**
     * Binds the DesktopRotationHolder to a particular collection with the given collection ID.
     */
    public void bind(String collectionId) {
        if (mWallpaperPreferences.getWallpaperPresentationMode()
                == WallpaperPreferences.PRESENTATION_MODE_ROTATING
                && collectionId.equals(mWallpaperPreferences.getHomeWallpaperCollectionId())) {
            mSelectionAnimator.selectImmediately();
        } else {
            mSelectionAnimator.deselectImmediately();
        }
    }

    /**
     * Updates the thumbnail shown by replacing the current one (if present) with the one specified
     * by {@code newThumbnailAsset}; uses a smooth transition. Calls {@code drawableLoadedListener}
     * once the transition to the new thumbnail has begun.
     */
    public void updateThumbnail(
            Asset newThumbnailAsset, DrawableLoadedListener drawableLoadedListener) {
        int placeholderColor = mActivity.getResources().getColor(R.color.secondary_color);

        // Load the first image more quickly than subsequent ones in the rotation.
        int crossfadeDuration =
                (mThumbnailView.getDrawable() == null)
                        ? CROSSFADE_DURATION_MILLIS_SHORT
                        : CROSSFADE_DURATION_MILLIS;

        newThumbnailAsset.loadDrawableWithTransition(
                mActivity, mThumbnailView, crossfadeDuration, drawableLoadedListener, placeholderColor);
    }
}