1 /*
2  * Copyright (C) 2021 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 
17 package com.android.providers.media.photopicker.ui;
18 
19 import android.content.Context;
20 import android.view.View;
21 import android.view.ViewGroup;
22 
23 import androidx.annotation.NonNull;
24 import androidx.recyclerview.widget.RecyclerView;
25 
26 import com.android.providers.media.photopicker.data.MuteStatus;
27 import com.android.providers.media.photopicker.data.model.Item;
28 import com.android.providers.media.photopicker.ui.remotepreview.RemotePreviewHandler;
29 
30 import java.util.ArrayList;
31 import java.util.List;
32 
33 /**
34  * Adapter for Preview RecyclerView to preview all images and videos.
35  */
36 public class PreviewAdapter extends RecyclerView.Adapter<BaseViewHolder> {
37 
38     private static final int ITEM_TYPE_IMAGE = 1;
39     private static final int ITEM_TYPE_VIDEO = 2;
40 
41     private List<Item> mItemList = new ArrayList<>();
42     private final ImageLoader mImageLoader;
43     private final RemotePreviewHandler mRemotePreviewHandler;
44     private final OnVideoPreviewClickListener mOnVideoPreviewClickListener;
45 
PreviewAdapter(Context context, MuteStatus muteStatus, @NonNull OnCreateSurfaceController onCreateSurfaceController, @NonNull OnVideoPreviewClickListener onVideoPreviewClickListener)46     PreviewAdapter(Context context, MuteStatus muteStatus,
47             @NonNull OnCreateSurfaceController onCreateSurfaceController,
48             @NonNull OnVideoPreviewClickListener onVideoPreviewClickListener) {
49         mImageLoader = new ImageLoader(context);
50         mRemotePreviewHandler = new RemotePreviewHandler(context, muteStatus,
51                 onCreateSurfaceController);
52         mOnVideoPreviewClickListener = onVideoPreviewClickListener;
53     }
54 
55     @NonNull
56     @Override
onCreateViewHolder(@onNull ViewGroup viewGroup, int viewType)57     public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
58         if (viewType == ITEM_TYPE_IMAGE) {
59             return new PreviewImageHolder(viewGroup.getContext(), viewGroup, mImageLoader);
60         }
61         return new PreviewVideoHolder(viewGroup.getContext(), viewGroup, mImageLoader,
62                 mOnVideoPreviewClickListener);
63     }
64 
65     @Override
onBindViewHolder(@onNull BaseViewHolder holder, int position)66     public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
67         final Item item = getItem(position);
68         holder.itemView.setContentDescription(
69                 item.getContentDescription(holder.itemView.getContext()));
70         holder.itemView.setTag(item);
71         holder.bind();
72     }
73 
74     @Override
onViewAttachedToWindow(@onNull BaseViewHolder holder)75     public void onViewAttachedToWindow(@NonNull BaseViewHolder holder) {
76         super.onViewAttachedToWindow(holder);
77 
78         final Item item = (Item) holder.itemView.getTag();
79         if (item.isVideo()) {
80             PreviewVideoHolder videoHolder = (PreviewVideoHolder) holder;
81             mRemotePreviewHandler.onViewAttachedToWindow(videoHolder, item);
82         }
83     }
84 
85     @Override
getItemCount()86     public int getItemCount() {
87         return mItemList.size();
88     }
89 
90     @Override
getItemViewType(int position)91     public int getItemViewType(int position) {
92         if (mItemList.get(position).isVideo()) {
93             return ITEM_TYPE_VIDEO;
94         }
95         // Everything other than video mimeType are previewed using PreviewImageHolder. This also
96         // includes GIF which uses Glide to load image.
97         return ITEM_TYPE_IMAGE;
98     }
99 
onHandlePageSelected(View itemView)100     void onHandlePageSelected(View itemView) {
101         final Item item = (Item) itemView.getTag();
102         mRemotePreviewHandler.onHandlePageSelected(item);
103     }
104 
onStop()105     void onStop() {
106         mRemotePreviewHandler.onStop();
107     }
108 
onDestroy()109     void onDestroy() {
110         mRemotePreviewHandler.onDestroy();
111     }
112 
getItem(int position)113     Item getItem(int position) {
114         return mItemList.get(position);
115     }
116 
updateItemList(List<Item> itemList)117     void updateItemList(List<Item> itemList) {
118         mItemList = itemList;
119         notifyDataSetChanged();
120     }
121 
122     interface OnVideoPreviewClickListener {
logMuteButtonClick()123         void logMuteButtonClick();
124     }
125 
126     /**
127      * Log metrics related to the surface controller creation
128      */
129     public interface OnCreateSurfaceController {
130         /**
131          * Log metrics to notify create surface controller triggered
132          * @param authority the authority of the provider
133          */
logStart(String authority)134         void logStart(String authority);
135 
136         /**
137          * Log metrics to notify create surface controller ended
138          * @param authority the authority of the provider
139          */
logEnd(String authority)140         void logEnd(String authority);
141     }
142 }
143