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.content.Context; 20 import android.util.Log; 21 import android.view.View; 22 23 import com.android.wallpaper.R; 24 import com.android.wallpaper.asset.Asset; 25 import com.android.wallpaper.model.WallpaperInfo; 26 import com.android.wallpaper.module.Injector; 27 import com.android.wallpaper.module.InjectorProvider; 28 import com.android.wallpaper.module.UserEventLogger; 29 import com.android.wallpaper.module.UserEventLogger.WallpaperSetFailureReason; 30 import com.android.wallpaper.module.WallpaperPersister; 31 import com.android.wallpaper.module.WallpaperPersister.SetWallpaperCallback; 32 import com.android.wallpaper.util.ThrowableAnalyzer; 33 34 /** 35 * IndividualHolder subclass for a wallpaper tile in the RecyclerView for which a click should 36 * set the wallpaper as the current wallpaper on the device. 37 */ 38 class SetIndividualHolder extends IndividualHolder implements View.OnClickListener, 39 SelectableHolder { 40 41 private static final String TAG = "SetIndividualHolder"; 42 private SelectionAnimator mSelectionAnimator; 43 private OnSetListener mOnSetListener; 44 private View mTile; 45 SetIndividualHolder( Activity hostActivity, int tileHeightPx, View itemView, SelectionAnimator selectionAnimator, OnSetListener onSetListener)46 public SetIndividualHolder( 47 Activity hostActivity, int tileHeightPx, View itemView, 48 SelectionAnimator selectionAnimator, 49 OnSetListener onSetListener) { 50 super(hostActivity, tileHeightPx, itemView); 51 52 mTile = itemView.findViewById(R.id.tile); 53 54 mSelectionAnimator = selectionAnimator; 55 mOnSetListener = onSetListener; 56 } 57 58 @Override bindWallpaper(WallpaperInfo wallpaper)59 public void bindWallpaper(WallpaperInfo wallpaper) { 60 super.bindWallpaper(wallpaper); 61 62 String wallpaperId = mWallpaper.getWallpaperId(); 63 String remoteWallpaperId = InjectorProvider.getInjector().getPreferences( 64 mActivity.getApplicationContext()).getHomeWallpaperRemoteId(); 65 66 boolean selected = wallpaperId != null && wallpaperId.equals(remoteWallpaperId); 67 if (selected) { 68 mSelectionAnimator.selectImmediately(); 69 } else { 70 mSelectionAnimator.deselectImmediately(); 71 } 72 73 mTile.setOnClickListener(this); 74 } 75 76 @Override setSelectionState(@electionState int selectionState)77 public void setSelectionState(@SelectionState int selectionState) { 78 if (selectionState == SELECTION_STATE_SELECTED) { 79 mSelectionAnimator.animateSelected(); 80 } else if (selectionState == SELECTION_STATE_DESELECTED) { 81 mSelectionAnimator.animateDeselected(); 82 } else if (selectionState == SELECTION_STATE_LOADING) { 83 mSelectionAnimator.showLoading(); 84 } 85 } 86 87 @Override onClick(View unused)88 public void onClick(View unused) { 89 setWallpaper(); 90 } 91 setWallpaper()92 /* package */ void setWallpaper() { 93 // If this wallpaper is already selected, then do nothing. 94 if (mSelectionAnimator.isSelected()) { 95 return; 96 } 97 98 final int adapterPosition = getAdapterPosition(); 99 mOnSetListener.onPendingWallpaperSet(adapterPosition); 100 101 final Context appContext = mActivity.getApplicationContext(); 102 103 mSelectionAnimator.showLoading(); 104 105 Injector injector = InjectorProvider.getInjector(); 106 final UserEventLogger eventLogger = injector.getUserEventLogger(appContext); 107 eventLogger.logIndividualWallpaperSelected(mWallpaper.getCollectionId(mActivity)); 108 109 Asset desktopAsset = mWallpaper.getDesktopAsset(appContext); 110 111 WallpaperPersister wallpaperPersister = 112 InjectorProvider.getInjector().getWallpaperPersister(appContext); 113 wallpaperPersister.setIndividualWallpaper(mWallpaper, desktopAsset, null /* cropRect */, 114 1.0f /* scale */, WallpaperPersister.DEST_BOTH, new SetWallpaperCallback() { 115 @Override 116 public void onSuccess(WallpaperInfo wallpaperInfo) { 117 mOnSetListener.onWallpaperSet(adapterPosition); 118 eventLogger.logWallpaperSet( 119 mWallpaper.getCollectionId(appContext), mWallpaper.getWallpaperId()); 120 eventLogger.logWallpaperSetResult(UserEventLogger.WALLPAPER_SET_RESULT_SUCCESS); 121 } 122 123 @Override 124 public void onError(Throwable throwable) { 125 Log.e(TAG, "Could not set a wallpaper."); 126 eventLogger.logWallpaperSetResult(UserEventLogger.WALLPAPER_SET_RESULT_FAILURE); 127 @WallpaperSetFailureReason int failureReason = ThrowableAnalyzer.isOOM(throwable) 128 ? UserEventLogger.WALLPAPER_SET_FAILURE_REASON_OOM 129 : UserEventLogger.WALLPAPER_SET_FAILURE_REASON_OTHER; 130 eventLogger.logWallpaperSetFailureReason(failureReason); 131 132 mSelectionAnimator.showNotLoading(); 133 mOnSetListener.onWallpaperSetFailed(SetIndividualHolder.this); 134 } 135 }); 136 } 137 138 interface OnSetListener { 139 /** 140 * Called to signal that the wallpaper at the given adapter position is starting to be set. 141 */ onPendingWallpaperSet(int adapterPosition)142 void onPendingWallpaperSet(int adapterPosition); 143 144 /** 145 * Called once the wallpaper at the given adapter position has been set. 146 */ onWallpaperSet(int adapterPosition)147 void onWallpaperSet(int adapterPosition); 148 149 /** 150 * Called when setting the wallpaper represented by the provided holder failed. 151 */ onWallpaperSetFailed(SetIndividualHolder holder)152 void onWallpaperSetFailed(SetIndividualHolder holder); 153 } 154 } 155