1 /*
2  * Copyright (C) 2017 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 package com.android.wallpaper.picker.individual;
17 
18 import android.app.Activity;
19 import android.view.View;
20 import android.widget.ImageView;
21 
22 import androidx.recyclerview.widget.RecyclerView.ViewHolder;
23 
24 import com.android.wallpaper.R;
25 import com.android.wallpaper.asset.Asset;
26 import com.android.wallpaper.asset.Asset.DrawableLoadedListener;
27 import com.android.wallpaper.model.WallpaperRotationInitializer;
28 import com.android.wallpaper.module.InjectorProvider;
29 import com.android.wallpaper.module.WallpaperPreferences;
30 import com.android.wallpaper.picker.RotationStarter;
31 import com.android.wallpaper.util.ResourceUtils;
32 
33 /**
34  * IndividualHolder subclass for a wallpaper tile in the RecyclerView for which a click should
35  * set the wallpaper as the current wallpaper on the device.
36  */
37 class DesktopRotationHolder extends ViewHolder implements View.OnClickListener,
38         SelectableHolder {
39 
40     static final int CROSSFADE_DURATION_MILLIS = 2000;
41     static final int CROSSFADE_DURATION_PAUSE_MILLIS = 2000;
42     static final int CROSSFADE_DURATION_MILLIS_SHORT = 300;
43 
44     private WallpaperPreferences mWallpaperPreferences;
45     private Activity mActivity;
46     private SelectionAnimator mSelectionAnimator;
47     private RotationStarter mRotationStarter;
48     private View mTile;
49     private ImageView mThumbnailView;
50 
DesktopRotationHolder( Activity hostActivity, int tileHeightPx, View itemView, SelectionAnimator selectionAnimator, RotationStarter rotationStarter)51     public DesktopRotationHolder(
52             Activity hostActivity, int tileHeightPx, View itemView, SelectionAnimator selectionAnimator,
53             RotationStarter rotationStarter) {
54         super(itemView);
55 
56         mWallpaperPreferences = InjectorProvider.getInjector().getPreferences(hostActivity);
57         mActivity = hostActivity;
58         mTile = itemView.findViewById(R.id.tile);
59         mThumbnailView = (ImageView) itemView.findViewById(R.id.thumbnail);
60 
61         mTile.setOnClickListener(this);
62         mTile.getLayoutParams().height = tileHeightPx;
63         itemView.getLayoutParams().height = tileHeightPx;
64 
65         mSelectionAnimator = selectionAnimator;
66         mRotationStarter = rotationStarter;
67     }
68 
69     @Override
setSelectionState(@electionState int selectionState)70     public void setSelectionState(@SelectionState int selectionState) {
71         if (selectionState == SELECTION_STATE_SELECTED) {
72             mSelectionAnimator.animateSelected();
73         } else if (selectionState == SELECTION_STATE_DESELECTED) {
74             mSelectionAnimator.animateDeselected();
75         } else if (selectionState == SELECTION_STATE_LOADING) {
76             mSelectionAnimator.showLoading();
77         }
78     }
79 
80     @Override
onClick(View view)81     public void onClick(View view) {
82         // If this is already selected, then do nothing.
83         if (mSelectionAnimator.isSelected()) {
84             return;
85         }
86 
87         mSelectionAnimator.showLoading();
88         mRotationStarter.startRotation(WallpaperRotationInitializer.NETWORK_PREFERENCE_CELLULAR_OK);
89     }
90 
91     /**
92      * Binds the DesktopRotationHolder to a particular collection with the given collection ID.
93      */
bind(String collectionId)94     public void bind(String collectionId) {
95         if (mWallpaperPreferences.getWallpaperPresentationMode()
96                 == WallpaperPreferences.PRESENTATION_MODE_ROTATING
97                 && collectionId.equals(mWallpaperPreferences.getHomeWallpaperCollectionId())) {
98             mSelectionAnimator.selectImmediately();
99         } else {
100             mSelectionAnimator.deselectImmediately();
101         }
102     }
103 
104     /**
105      * Updates the thumbnail shown by replacing the current one (if present) with the one specified
106      * by {@code newThumbnailAsset}; uses a smooth transition. Calls {@code drawableLoadedListener}
107      * once the transition to the new thumbnail has begun.
108      */
updateThumbnail( Asset newThumbnailAsset, DrawableLoadedListener drawableLoadedListener)109     public void updateThumbnail(
110             Asset newThumbnailAsset, DrawableLoadedListener drawableLoadedListener) {
111         int placeholderColor =
112                 ResourceUtils.getColorAttr(mActivity, android.R.attr.colorSecondary);
113 
114         // Load the first image more quickly than subsequent ones in the rotation.
115         int crossfadeDuration =
116                 (mThumbnailView.getDrawable() == null)
117                         ? CROSSFADE_DURATION_MILLIS_SHORT
118                         : CROSSFADE_DURATION_MILLIS;
119 
120         newThumbnailAsset.loadDrawableWithTransition(
121                 mActivity, mThumbnailView, crossfadeDuration, drawableLoadedListener, placeholderColor);
122     }
123 }
124