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