1 /* 2 * Copyright (C) 2018 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.example.android.wearable.datalayer; 17 18 import static com.example.android.wearable.datalayer.DataLayerScreen.TYPE_CAPABILITY_DISCOVERY; 19 import static com.example.android.wearable.datalayer.DataLayerScreen.TYPE_EVENT_LOGGING; 20 import static com.example.android.wearable.datalayer.DataLayerScreen.TYPE_IMAGE_ASSET; 21 22 import android.graphics.Bitmap; 23 import android.util.Log; 24 import android.view.LayoutInflater; 25 import android.view.View; 26 import android.view.ViewGroup; 27 import android.widget.ImageView; 28 import android.widget.TextView; 29 30 import androidx.annotation.NonNull; 31 import androidx.recyclerview.widget.RecyclerView; 32 import androidx.recyclerview.widget.RecyclerView.ViewHolder; 33 import androidx.wear.widget.WearableRecyclerView; 34 35 import com.example.android.wearable.datalayer.DataLayerScreen.DataLayerScreenData; 36 import com.example.android.wearable.datalayer.DataLayerScreen.EventLoggingData; 37 import com.example.android.wearable.datalayer.DataLayerScreen.ImageAssetData; 38 39 import java.util.ArrayList; 40 41 /** 42 * Populates a {@link WearableRecyclerView}. 43 * 44 * <p>Provides a custom {@link ViewHolder} for each unique row associated with a feature from the 45 * Data Layer APIs (one for transferring images, one for event logging, and one for checking 46 * capabilities). Data for each {@link ViewHolder} populated by {@link DataLayerScreen}. 47 */ 48 public class CustomRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { 49 50 private static final String TAG = "CustomRecyclerAdapter"; 51 52 private ArrayList<DataLayerScreenData> mDataSet; 53 CustomRecyclerAdapter(ArrayList<DataLayerScreenData> dataSet)54 public CustomRecyclerAdapter(ArrayList<DataLayerScreenData> dataSet) { 55 mDataSet = dataSet; 56 } 57 58 @Override onCreateViewHolder(ViewGroup viewGroup, int viewType)59 public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { 60 Log.d(TAG, "onCreateViewHolder(): viewType: " + viewType); 61 62 RecyclerView.ViewHolder viewHolder = null; 63 64 switch (viewType) { 65 case TYPE_IMAGE_ASSET: 66 viewHolder = 67 new ImageAssetViewHolder( 68 LayoutInflater.from(viewGroup.getContext()) 69 .inflate( 70 R.layout.recycler_row_image_asset, 71 viewGroup, 72 false)); 73 break; 74 75 case TYPE_EVENT_LOGGING: 76 viewHolder = 77 new EventLoggingViewHolder( 78 LayoutInflater.from(viewGroup.getContext()) 79 .inflate( 80 R.layout.recycler_row_event_logging, 81 viewGroup, 82 false)); 83 break; 84 85 case TYPE_CAPABILITY_DISCOVERY: 86 viewHolder = 87 new CapabilityDiscoveryViewHolder( 88 LayoutInflater.from(viewGroup.getContext()) 89 .inflate( 90 R.layout.recycler_row_capability_discovery, 91 viewGroup, 92 false)); 93 break; 94 } 95 96 return viewHolder; 97 } 98 99 @Override onBindViewHolder(ViewHolder viewHolder, final int position)100 public void onBindViewHolder(ViewHolder viewHolder, final int position) { 101 Log.d(TAG, "Element " + position + " set."); 102 103 switch (viewHolder.getItemViewType()) { 104 case TYPE_IMAGE_ASSET: 105 ImageAssetData imageAssetData = (ImageAssetData) mDataSet.get(position); 106 107 ImageAssetViewHolder imageAssetViewHolder = (ImageAssetViewHolder) viewHolder; 108 imageAssetViewHolder.setBackgroundImage(imageAssetData.getBitmap()); 109 break; 110 111 case TYPE_EVENT_LOGGING: 112 EventLoggingData eventLoggingData = (EventLoggingData) mDataSet.get(position); 113 114 EventLoggingViewHolder eventLoggingViewHolder = (EventLoggingViewHolder) viewHolder; 115 116 String log = eventLoggingData.getLog(); 117 118 if (log.length() > 0) { 119 eventLoggingViewHolder.logDataLayerInformation(eventLoggingData.getLog()); 120 } 121 break; 122 123 case TYPE_CAPABILITY_DISCOVERY: 124 // This view never changes, as it contains just two buttons that trigger 125 // capabilities requests to other devices. 126 break; 127 } 128 } 129 130 @Override getItemCount()131 public int getItemCount() { 132 return mDataSet.size(); 133 } 134 135 @Override getItemViewType(int position)136 public int getItemViewType(int position) { 137 DataLayerScreenData dataLayerScreenData = mDataSet.get(position); 138 return dataLayerScreenData.getType(); 139 } 140 findItemIndex(@onNull int assetType)141 private int findItemIndex(@NonNull int assetType) { 142 143 for (int index = 0; index < mDataSet.size(); index++) { 144 if (mDataSet.get(index).getType() == assetType) { 145 return index; 146 } 147 } 148 return -1; 149 } 150 appendToDataEventLog(@onNull String eventName, @NonNull String details)151 public void appendToDataEventLog(@NonNull String eventName, @NonNull String details) { 152 int index = findItemIndex(TYPE_EVENT_LOGGING); 153 154 if (index > -1) { 155 EventLoggingData dataItemType = (EventLoggingData) mDataSet.get(index); 156 dataItemType.addEventLog(eventName, details); 157 158 notifyItemChanged(index); 159 } 160 } 161 setImageAsset(Bitmap bitmap)162 public int setImageAsset(Bitmap bitmap) { 163 164 int index = findItemIndex(TYPE_IMAGE_ASSET); 165 166 if ((index > -1) && (bitmap != null)) { 167 ImageAssetData imageAssetData = (ImageAssetData) mDataSet.get(index); 168 imageAssetData.setBitmap(bitmap); 169 notifyItemChanged(index); 170 171 return index; 172 173 } else { 174 return -1; 175 } 176 } 177 178 /** ***** Classes representing custom {@link ViewHolder}. ****** */ 179 180 /** 181 * Displays {@link Bitmap} passed from other devices via the {@link 182 * com.google.android.gms.wearable.Asset} API. 183 */ 184 public static class ImageAssetViewHolder extends RecyclerView.ViewHolder { 185 186 private final ImageView mImageView; 187 ImageAssetViewHolder(View view)188 public ImageAssetViewHolder(View view) { 189 super(view); 190 mImageView = view.findViewById(R.id.image); 191 } 192 setBackgroundImage(Bitmap bitmap)193 public void setBackgroundImage(Bitmap bitmap) { 194 mImageView.setImageBitmap(bitmap); 195 } 196 } 197 198 /** 199 * Displays text log of data passed from other devices via the {@link 200 * com.google.android.gms.wearable.MessageClient} API. 201 */ 202 public static class EventLoggingViewHolder extends RecyclerView.ViewHolder { 203 204 private final TextView mIntroTextView; 205 private final TextView mDataLogTextView; 206 EventLoggingViewHolder(View view)207 public EventLoggingViewHolder(View view) { 208 super(view); 209 mIntroTextView = view.findViewById(R.id.intro); 210 mDataLogTextView = view.findViewById(R.id.event_logging); 211 } 212 213 @Override toString()214 public String toString() { 215 return (String) mDataLogTextView.getText(); 216 } 217 logDataLayerInformation(String log)218 public void logDataLayerInformation(String log) { 219 mIntroTextView.setVisibility(View.INVISIBLE); 220 mDataLogTextView.setText(log); 221 } 222 } 223 224 /** 225 * Displays two buttons for querying device capabilities via {@link 226 * com.google.android.gms.wearable.CapabilityClient}. 227 */ 228 public static class CapabilityDiscoveryViewHolder extends RecyclerView.ViewHolder { 229 CapabilityDiscoveryViewHolder(View view)230 public CapabilityDiscoveryViewHolder(View view) { 231 super(view); 232 } 233 } 234 } 235