com.ferjuarez.androidthingsdemo.PhotoEntryAdapter.java Source code

Java tutorial

Introduction

Here is the source code for com.ferjuarez.androidthingsdemo.PhotoEntryAdapter.java

Source

/*
 * 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.ferjuarez.androidthingsdemo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.format.DateUtils;
import android.util.Base64;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;

/**
 * RecyclerView adapter to populate doorbell entries from Firebase.
 */
public class PhotoEntryAdapter
        extends FirebaseRecyclerAdapter<PhotoEntry, PhotoEntryAdapter.DoorbellEntryViewHolder> {

    /**
     * ViewHolder for each doorbell entry
     */
    public static class DoorbellEntryViewHolder extends RecyclerView.ViewHolder {

        public final ImageView image;
        public final TextView time;

        public DoorbellEntryViewHolder(View itemView) {
            super(itemView);

            this.image = (ImageView) itemView.findViewById(R.id.imageView1);
            this.time = (TextView) itemView.findViewById(R.id.textView1);
        }
    }

    private Context mApplicationContext;

    public PhotoEntryAdapter(Context context, DatabaseReference ref) {
        super(PhotoEntry.class, R.layout.photo_entry, DoorbellEntryViewHolder.class, ref);

        mApplicationContext = context.getApplicationContext();
    }

    @Override
    protected void populateViewHolder(DoorbellEntryViewHolder viewHolder, PhotoEntry model, int position) {
        // Display the timestamp
        if (model != null && model.getTimestamp() != null) {
            CharSequence prettyTime = DateUtils.getRelativeDateTimeString(mApplicationContext, model.getTimestamp(),
                    DateUtils.SECOND_IN_MILLIS, DateUtils.WEEK_IN_MILLIS, 0);
            viewHolder.time.setText(prettyTime);

            // Display the image
            if (model.getThumbnail() != null) {
                // Decode image data encoded by the Cloud Vision library
                byte[] imageBytes = Base64.decode(model.getThumbnail(), Base64.NO_WRAP | Base64.URL_SAFE);
                Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
                if (bitmap != null) {
                    viewHolder.image.setImageBitmap(bitmap);
                } else {
                    Drawable placeholder = ContextCompat.getDrawable(mApplicationContext, R.mipmap.ic_launcher);
                    viewHolder.image.setImageDrawable(placeholder);
                }
            }
        }

        // Display the metadata
        /*if (model.getAnnotations() != null) {
        ArrayList<String> keywords = new ArrayList<>(model.getAnnotations().keySet());
            
        int limit = Math.min(keywords.size(), 3);
        viewHolder.metadata.setText(TextUtils.join("\n", keywords.subList(0, limit)));
        } else {
        viewHolder.metadata.setText("no annotations yet");
        }*/
    }

}