Java tutorial
/* 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.FileNotFoundException; import java.io.FileOutputStream; import java.io.FilenameFilter; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import org.json.JSONObject; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; public class FileRepositoryContainerProvider implements RepositoryContainerProvider { private static final String THUMBNAIL_PREFIX = "thumbnail-"; private File m_folderFile; private ThumbnailProvider m_thumbnailProvider; public FileRepositoryContainerProvider(File folderFile, ThumbnailProvider thumbnailProvider) { m_folderFile = folderFile; m_thumbnailProvider = thumbnailProvider; } @Override public String getContainerName() { return m_folderFile.getName(); } @Override public List<RepositoryObjectProvider> getObjects(boolean loadThumbnails) { ArrayList<RepositoryObjectProvider> m_entries = new ArrayList<RepositoryObjectProvider>(); // List and process thumbnails. // HashMap<String, File> thumbnails = new HashMap<String, File>(); if (loadThumbnails) { File[] thumbnailFiles = listFiles(true); for (int i = 0; i < thumbnailFiles.length; ++i) { File thumbnailFile = thumbnailFiles[i]; String themeName = thumbnailFile.getName().substring(THUMBNAIL_PREFIX.length()); thumbnails.put(themeName, thumbnailFile); } } // List and process theme files. // File[] objectFiles = listFiles(false); for (int i = 0; i < objectFiles.length; ++i) { File objectFile = objectFiles[i]; String objectName = objectFile.getName(); File thumbnailFile = thumbnails.get(objectName); FileRepositoryObjectProvider entry; if (thumbnailFile != null) { entry = new FileRepositoryObjectProvider(objectFile, thumbnailFile); } else if (loadThumbnails) { entry = new FileRepositoryObjectProvider(objectFile, m_thumbnailProvider); } else { entry = new FileRepositoryObjectProvider(objectFile, (Bitmap) null); } m_entries.add(entry); } return m_entries; } @Override public RepositoryObjectProvider getObject(String objectName, boolean loadThumbnail) { File objectFile = new File(m_folderFile, objectName); File thumbnailFile = new File(m_folderFile, THUMBNAIL_PREFIX + objectName); if (thumbnailFile.exists()) { return new FileRepositoryObjectProvider(objectFile, thumbnailFile); } else if (loadThumbnail) { return new FileRepositoryObjectProvider(objectFile, m_thumbnailProvider); } else { return new FileRepositoryObjectProvider(objectFile, (Bitmap) null); } } @Override public void createObject(String objectName, JSONObject object, Bitmap thumbnail) throws RepositoryException { File objectFile = new File(m_folderFile, objectName); JSONUtility.saveJSONObject(objectFile, object); if (thumbnail == null && m_thumbnailProvider != null) { thumbnail = m_thumbnailProvider.getThumbnail(object); } if (thumbnail != null) { File thumbnailFile = new File(m_folderFile, THUMBNAIL_PREFIX + objectName); saveBitmap(thumbnailFile, thumbnail); } } @Override public void deleteObject(String objectName) { File objectFile = new File(m_folderFile, objectName); objectFile.delete(); File thumbnailFile = new File(m_folderFile, THUMBNAIL_PREFIX + objectName); thumbnailFile.delete(); } @Override public boolean objectExists(String objectName) { File objectFile = new File(m_folderFile, objectName); return objectFile.exists(); } @Override public void clear() { File[] files = m_folderFile.listFiles(); for (File file : files) { file.delete(); } } @Override public int getCount() throws RepositoryException { return m_folderFile.list().length; } private File[] listFiles(boolean listThumbnails) { final boolean localListThumbnails = listThumbnails; File[] files = m_folderFile.listFiles(new FilenameFilter() { @Override public boolean accept(File dir, String filename) { return localListThumbnails == filename.startsWith(THUMBNAIL_PREFIX); } }); return files; } private void saveBitmap(File file, Bitmap bitmap) throws RepositoryException { try { OutputStream out = new FileOutputStream(file); try { bitmap.compress(CompressFormat.PNG, 100, out); } finally { out.close(); } } catch (FileNotFoundException ex) { throw new RepositoryException(ex); } catch (IOException ex) { throw new RepositoryException(ex); } } }