content: /*
   * Copyright 2016, The Android Open Source Project
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package com.example.androidthings.doorbell;
  
  import android.content.Context;
  import android.support.v7.widget.RecyclerView;
  import android.text.TextUtils;
  import android.text.format.DateUtils;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.ImageView;
  import android.widget.TextView;
  
  import com.firebase.ui.database.FirebaseRecyclerAdapter;
  import com.firebase.ui.database.FirebaseRecyclerOptions;
  import com.google.firebase.database.DatabaseReference;
  import com.google.firebase.storage.FirebaseStorage;
  import com.google.firebase.storage.StorageReference;
  
  import java.util.ArrayList;
  
  /**
   * RecyclerView adapter to populate doorbell entries from Firebase.
   */
  public class DoorbellEntryAdapter extends FirebaseRecyclerAdapter<DoorbellEntry, DoorbellEntryAdapter.DoorbellEntryViewHolder> {
  
      /**
       * ViewHolder for each doorbell entry
       */
      public static class DoorbellEntryViewHolder extends RecyclerView.ViewHolder {
  
          public final ImageView image;
          public final TextView time;
          public final TextView metadata;
  
          public DoorbellEntryViewHolder(View itemView) {
              super(itemView);
  
              this.image = (ImageView) itemView.findViewById(R.id.imageView1);
              this.time = (TextView) itemView.findViewById(R.id.textView1);
              this.metadata = (TextView) itemView.findViewById(R.id.textView2);
          }
      }
  
      private Context mApplicationContext;
      private FirebaseStorage mFirebaseStorage;
  
      public DoorbellEntryAdapter(Context context, DatabaseReference ref) {
          super(new FirebaseRecyclerOptions.Builder<DoorbellEntry>()
                  .setQuery(ref, DoorbellEntry.class)
                  .build());
  
          mApplicationContext = context.getApplicationContext();
          mFirebaseStorage = FirebaseStorage.getInstance();
      }
  
      @Override
      public DoorbellEntryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          View entryView = LayoutInflater.from(parent.getContext())
                  .inflate(R.layout.doorbell_entry, parent, false);
  
          return new DoorbellEntryViewHolder(entryView);
      }
  
      @Override
      protected void onBindViewHolder(DoorbellEntryViewHolder holder, int position, DoorbellEntry model) {
          // Display the timestamp
          CharSequence prettyTime = DateUtils.getRelativeDateTimeString(mApplicationContext,
                  model.getTimestamp(), DateUtils.SECOND_IN_MILLIS, DateUtils.WEEK_IN_MILLIS, 0);
          holder.time.setText(prettyTime);
  
          // Display the image
          if (model.getImage() != null) {
              StorageReference imageRef = mFirebaseStorage.getReferenceFromUrl(model.getImage());
  
              GlideApp.with(mApplicationContext)
                      .load(imageRef)
                      .placeholder(R.drawable.ic_image)
                      .into(holder.image);
          }
  
          // Display the metadata
          if (model.getAnnotations() != null) {
              ArrayList<String> keywords = new ArrayList<>(model.getAnnotations().keySet());
  
              int limit = Math.min(keywords.size(), 3);
              holder.metadata.setText(TextUtils.join("\n", keywords.subList(0, limit)));
          } else {
              holder.metadata.setText("no annotations yet");
          }
      }
  }
  