Java tutorial
/* ApicAday - Everyday.. is different, your mood, your life. Copyright (c) 2010 Oliver Selinger <oliver.selinger@autburst.com>, Michael Greifeneder <michael.greifeneder@autburst.com> This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.autburst.picture; import java.io.File; import java.io.FileFilter; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.List; import org.apache.commons.codec.binary.Base64; import android.os.Environment; public class Utilities { public static final String NAME = "com.autburst.picture"; public static final String MUSIC = "com.autburst.picture.music"; public static final String PIC_STORE = "com.autburst.picture.store"; public static final String PORTRAIT = "portrait"; public static final String LANDSCAPE = "landscape"; public static final int MSG_FINISHED_UPLOADING = 0; public static final int MSG_ERROR_UPLOADING = 1; public static final int MSG_FINISHED_PIC_SAVING = 2; public static final int MSG_OPEN_CREATE_DIALOG = 3; public static final float SLOW_FRAME_RATE = 1.2F; public static final float MEDIUM_FRAME_RATE = 0.8F; public static final float FAST_FRAME_RATE = 0.4F; public static String getMusicFilePath() { File file = new File(Environment.getExternalStorageDirectory(), MUSIC); if (!file.exists()) file.mkdirs(); return file.getAbsolutePath() + "test.mp3"; } public static File getAlbumsDirectory() { File file = new File(Environment.getExternalStorageDirectory(), NAME); if (!file.exists()) file.mkdirs(); return file; } public static String[] getAlbums() { File file = getAlbumsDirectory(); String[] list = file.list(); return list; } public static Comparator<String> getAlbumNameComparator() { return new Comparator<String>() { @Override public int compare(String arg0, String arg1) { String decodeName0 = new String(Base64.decodeBase64(arg0.getBytes())); String decodeName1 = new String(Base64.decodeBase64(arg1.getBytes())); return decodeName0.compareTo(decodeName1); } }; } public static Comparator<File> getImageNameComparator() { return new Comparator<File>() { @Override public int compare(File arg0, File arg1) { String filename0 = arg0.getName(); String filename1 = arg1.getName(); return filename0.compareTo(filename1); } }; } public static boolean existsAlbumName(String name) { File file = getAlbumsDirectory(); String[] list = file.list(); for (String file2 : list) { if (file2.equals(name)) return true; } return false; } public static boolean hasAlbums() { File file = getAlbumsDirectory(); File[] list = file.listFiles(); if (list != null && list.length > 0) return true; else return false; } public static boolean hasAnyAlbumPics() { File file = getAlbumsDirectory(); File[] list = file.listFiles(); if (list == null) return false; for (File file2 : list) { if (file2.listFiles(new FileFilter() { @Override public boolean accept(File arg0) { String name = arg0.getName(); if (name.endsWith(".jpg") || name.endsWith(".JPG")) return true; else return false; } }).length > 0) return true; } return false; } public static List<String> getAlbumsAsList() { List<String> albums = new ArrayList<String>(); File file = getAlbumsDirectory(); String[] list = file.list(); if (list == null) { return albums; } for (int i = 0; i < list.length; i++) { albums.add(list[i]); } return albums; } public static String getEncodedString(String toEncode) { return new String(Base64.encodeBase64(toEncode.getBytes())); } public static String[] getAlbumsDecoded() { File file = getAlbumsDirectory(); String[] list = file.list(); for (int i = 0; i < list.length; i++) { list[i] = new String(Base64.decodeBase64(list[i].getBytes())); } if (list.length > 1) { Arrays.sort(list); } return list; } public static File getAlbumDirectory(String albumName) { File file = new File(Environment.getExternalStorageDirectory(), NAME); File albumDir = new File(file, albumName); if (!albumDir.exists()) albumDir.mkdirs(); return albumDir; } }