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.car.media.drawer; 17 18 import android.content.Context; 19 import android.graphics.Bitmap; 20 import android.media.MediaDescription; 21 22 import com.android.car.app.DrawerItemViewHolder; 23 import com.android.car.apps.common.BitmapDownloader; 24 import com.android.car.apps.common.BitmapWorkerOptions; 25 import com.android.car.apps.common.UriUtils; 26 import com.android.car.media.R; 27 28 /** 29 * Component that handles fetching of items for {@link MediaDrawerAdapter}. 30 * <p> 31 * It also handles ViewHolder population and item clicks. 32 */ 33 interface MediaItemsFetcher { 34 /** 35 * Used to inform owning {@link MediaDrawerAdapter} that items have changed. 36 */ 37 interface ItemsUpdatedCallback { onItemsUpdated()38 void onItemsUpdated(); 39 } 40 41 /** 42 * Kick-off fetching/monitoring of items. 43 * 44 * @param callback Callback that is invoked when items are first loaded ar if they change 45 * subsequently. 46 */ start(ItemsUpdatedCallback callback)47 void start(ItemsUpdatedCallback callback); 48 49 /** 50 * @return Number of items currently fetched. 51 */ getItemCount()52 int getItemCount(); 53 54 /** 55 * Used by owning {@link MediaDrawerAdapter} to populate views. 56 * 57 * @param holder View-holder to populate. 58 * @param position Item position. 59 */ populateViewHolder(DrawerItemViewHolder holder, int position)60 void populateViewHolder(DrawerItemViewHolder holder, int position); 61 62 /** 63 * Used by owning {@link MediaDrawerAdapter} to handle clicks. 64 * 65 * @param position Item position. 66 */ onItemClick(int position)67 void onItemClick(int position); 68 69 /** 70 * Used when this instance is going to be released. Subclasses should release resources. 71 */ cleanup()72 void cleanup(); 73 74 /** 75 * Utility method to populate {@code holder} with details from {@code description}. It populates 76 * title, text and icon at most. 77 */ populateViewHolderFrom(DrawerItemViewHolder holder, MediaDescription description)78 static void populateViewHolderFrom(DrawerItemViewHolder holder, MediaDescription description) { 79 Context context = holder.itemView.getContext(); 80 // TODO(sriniv): Once we use smallLayout, text and rightIcon fields may be unavailable. 81 // Related to b/36573125. 82 holder.getTitle().setText(description.getTitle()); 83 holder.getText().setText(description.getSubtitle()); 84 Bitmap iconBitmap = description.getIconBitmap(); 85 holder.getIcon().setImageBitmap(iconBitmap); // Ok to set null here for clearing. 86 if (iconBitmap == null && description.getIconUri() != null) { 87 int bitmapSize = 88 context.getResources().getDimensionPixelSize(R.dimen.car_list_item_icon_size); 89 // We don't want to cache android resources as they are needed to be refreshed after 90 // configuration changes. 91 int cacheFlag = UriUtils.isAndroidResourceUri(description.getIconUri()) 92 ? (BitmapWorkerOptions.CACHE_FLAG_DISK_DISABLED 93 | BitmapWorkerOptions.CACHE_FLAG_MEM_DISABLED) 94 : 0; 95 BitmapWorkerOptions options = new BitmapWorkerOptions.Builder(context) 96 .resource(description.getIconUri()) 97 .height(bitmapSize) 98 .width(bitmapSize) 99 .cacheFlag(cacheFlag) 100 .build(); 101 BitmapDownloader.getInstance(context).loadBitmap(options, holder.getIcon()); 102 } 103 } 104 }