com.richtodd.android.repository.FileRepositoryObjectProvider.java Source code

Java tutorial

Introduction

Here is the source code for com.richtodd.android.repository.FileRepositoryObjectProvider.java

Source

/* Copyright (c) 2013 Richard G. Todd.
 * Licensed under the terms of the GNU General Public License (GPL) Version 3.0.
 */

package com.richtodd.android.repository;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;

import org.json.JSONObject;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class FileRepositoryObjectProvider implements RepositoryObjectProvider {

    private File m_fileObject;
    private File m_fileThumbnail;
    private Bitmap m_thumbnail;
    private ThumbnailProvider m_thumbnailProvider;

    public FileRepositoryObjectProvider(File fileObject, File fileThumbnail) {
        m_fileObject = fileObject;
        m_fileThumbnail = fileThumbnail;
    }

    public FileRepositoryObjectProvider(File fileObject, Bitmap thumbnail) {
        m_fileObject = fileObject;
        m_thumbnail = thumbnail;
    }

    public FileRepositoryObjectProvider(File fileObject, ThumbnailProvider thumbnailProvider) {
        m_fileObject = fileObject;
        m_thumbnailProvider = thumbnailProvider;
    }

    @Override
    public String getObjectName() {
        return m_fileObject.getName();
    }

    @Override
    public JSONObject getObject() throws RepositoryException {
        return JSONUtility.loadJSONObject(m_fileObject);
    }

    @Override
    public Bitmap getThumbnail() throws RepositoryException {

        try {
            if (m_thumbnail != null) {
                return m_thumbnail;
            }

            if (m_fileThumbnail != null) {
                Bitmap bitmap;

                FileInputStream in = new FileInputStream(m_fileThumbnail);
                try {
                    bitmap = BitmapFactory.decodeStream(in);
                } finally {
                    in.close();
                }

                return bitmap;
            }

            if (m_thumbnailProvider != null) {
                m_thumbnail = m_thumbnailProvider.getThumbnail(getObject());
                return m_thumbnail;
            }

            return null;
        } catch (FileNotFoundException ex) {
            throw new RepositoryException(ex);
        } catch (IOException ex) {
            throw new RepositoryException(ex);
        }
    }

    @Override
    public Date getLastChangedDate() {
        return new Date(m_fileObject.lastModified());
    }
}