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