/CameraFrame/src/com/nokia/cameraframe/views/GalleryView.java
https://bitbucket.org/atchariya/nokia · Java · 290 lines · 233 code · 40 blank · 17 comment · 51 complexity · 2e3ed7facc69de79997264beed058055 MD5 · raw file
- package com.nokia.cameraframe.views;
-
- import java.io.IOException;
- import java.util.Enumeration;
- import java.util.Vector;
-
- import javax.microedition.io.Connector;
- import javax.microedition.io.file.FileConnection;
- import javax.microedition.io.file.FileSystemRegistry;
- import javax.microedition.lcdui.Command;
- import javax.microedition.lcdui.CommandListener;
- import javax.microedition.lcdui.Displayable;
- import javax.microedition.lcdui.Image;
- import javax.microedition.lcdui.List;
- import javax.microedition.lcdui.Ticker;
-
- import com.nokia.cameraframe.MainMIDlet;
- import com.nokia.cameraframe.utils.Commands;
- import com.nokia.cameraframe.utils.Constants;
- import com.nokia.cameraframe.utils.ImageUtils;
- import com.nokia.cameraframe.utils.Log;
- import com.nokia.cameraframe.views.component.Operation;
- import com.nokia.cameraframe.views.component.OperationsQueue;
-
- class GalleryView extends List implements CommandListener {
-
- private final MainMIDlet midlet;
-
- private PhotoEditorView photoEditorView;
- private Ticker ticker;
-
- // Gallery
- private static final Image ROOT_IMAGE = ImageUtils.loadImage("/root.png");
- private static final Image FOLDER_IMAGE = ImageUtils.loadImage("/folder.png");
- private static final Image FILE_IMAGE = ImageUtils.loadImage("/file.png");
-
- private final OperationsQueue queue;
- private FileConnection fileConnection;
- private Vector rootsList;
-
- // Operation value
- private final static int OPRATION_INITIAL = 1;
- private final static int OPRATION_OPEN = 2;
-
- public GalleryView(MainMIDlet midlet) {
- super("Photos", List.IMPLICIT);
-
- this.midlet = midlet;
-
- ticker = new Ticker("");
- setTicker(ticker);
-
- // Add command
- setSelectCommand(Commands.OPEN_ITEM);
- addCommand(Commands.OPEN_ITEM);
- addCommand(Commands.MENU_ABOUT);
- addCommand(Commands.MENU_HELP);
- addCommand(Commands.MENU_EXIT);
- addCommand(Commands.EXIT);
- setCommandListener(this);
-
- // Initialize
- queue = new OperationsQueue();
- rootsList = new Vector();
- }
-
- public void initialize() {
- queue.enqueueOperation(new ImageViewerOperations(OPRATION_INITIAL));
- }
-
- public void commandAction(Command command, Displayable displayable) {
- if (command == Commands.OPEN_ITEM) {
- queue.enqueueOperation(new ImageViewerOperations(OPRATION_OPEN));
- } else if (command == Commands.MENU_ABOUT) {
- midlet.viewForward(midlet.getAboutView());
- midlet.getCameraView().setCategoryBarInvisible();
- } else if (command == Commands.MENU_HELP) {
- midlet.viewForward(midlet.getHelpView());
- midlet.getCameraView().setCategoryBarInvisible();
- } else if (command == Commands.MENU_EXIT || command == Commands.EXIT) {
- midlet.destroyApp(true);
- }
- }
-
- private void displayAllRoots() {
- ticker.setString("Roots");
- deleteAll();
- Enumeration roots = rootsList.elements();
- while (roots.hasMoreElements()) {
- String root = (String) roots.nextElement();
- root = root.replace('/', Constants.FILE_SEPARATOR.charAt(0));
- append(root.substring(1), ROOT_IMAGE);
- }
- fileConnection = null;
- }
-
- private void loadRoots() {
- if (!rootsList.isEmpty()) {
- rootsList.removeAllElements();
- }
- try {
- Enumeration roots = FileSystemRegistry.listRoots();
- while (roots.hasMoreElements()) {
- rootsList.addElement("/" + (String) roots.nextElement());
- }
- } catch (Throwable e) {
- midlet.showErrorAlert(e.getMessage());
- Log.warn("Exception", e);
- }
- }
-
- private void openSelected() {
- int selectedIndex = getSelectedIndex();
- if (selectedIndex >= 0) {
- String selectedFile = getString(selectedIndex);
-
- // Directory selected
- if (selectedFile.endsWith(Constants.FILE_SEPARATOR)) {
- try {
- String tmp = selectedFile.replace(Constants.FILE_SEPARATOR.charAt(0), '/');
- if (fileConnection == null) {
- fileConnection = (FileConnection) Connector.open(Constants.SYSTEM_ROOT + tmp, Connector.READ);
- } else {
- fileConnection.setFileConnection(tmp);
- }
- displayCurrentRoot();
- } catch (IOException ioe) {
- midlet.showErrorAlert(ioe.getMessage());
- Log.warn("SecurityException", ioe);
- } catch (SecurityException se) {
- midlet.showErrorAlert(se.getMessage());
- Log.warn("SecurityException", se);
- }
- }
- // Upper directory selected
- else if (selectedFile.equals(Constants.UPPER_DIRECTORY)) {
- if (rootsList.contains(fileConnection.getPath() + fileConnection.getName())) {
- displayAllRoots();
- } else {
- try {
- fileConnection.setFileConnection(Constants.UPPER_DIRECTORY);
- displayCurrentRoot();
- } catch (IOException ioe) {
- midlet.showErrorAlert(ioe.getMessage());
- Log.warn("IOException", ioe);
- }
- }
- }
- // File selected
- else {
- midlet.getCameraView().setCategoryBarInvisible();
-
- // Get format
- int startIndex = selectedFile.indexOf(".");
- String imageFormat = selectedFile.substring(startIndex + 1, selectedFile.length());
-
- // Resize
- Image imageSelected = ImageUtils.getImage(fileConnection.getURL() + selectedFile);
-
- int sourceWidth = imageSelected.getWidth();
- int sourceHeight = imageSelected.getHeight();
-
- int newWidth = -1;
- int newHeight = -1;
-
- if (sourceWidth == sourceHeight || sourceWidth > newHeight) { // Cubic or Landscape
- if (sourceWidth > Constants.MAX_LANDSCAPE_PHOTO_WIDTH) {
- newWidth = Constants.MAX_LANDSCAPE_PHOTO_WIDTH;
- if (newHeight == -1) {
- newHeight = newWidth * sourceHeight / sourceWidth;
- }
- imageSelected = tube42.lib.imagelib.ImageUtils.resize(imageSelected, newWidth, newHeight, false, false);
- }
- } else { // Portrait
- if (sourceHeight > Constants.MAX_PORTRAIT_PHOTO_HEIGH) {
- newHeight = Constants.MAX_PORTRAIT_PHOTO_HEIGH;
- if (newWidth == -1) {
- newWidth = newHeight * sourceWidth / sourceHeight;
- }
- imageSelected = tube42.lib.imagelib.ImageUtils.resize(imageSelected, newWidth, newHeight, false, false);
- }
- }
-
- Log.debug("Resize width [" + sourceWidth + "] -> [" + imageSelected.getWidth() + "]");
- Log.debug("Resize height [" + sourceHeight + "] -> [" + imageSelected.getHeight() + "]");
-
- photoEditorView = new PhotoEditorView(midlet, imageSelected);
- photoEditorView.setImageFormat(imageFormat);
- midlet.viewForward(photoEditorView);
- }
- }
- }
-
- private void displayCurrentRoot() {
- try {
- // Remove system root before display
- ticker.setString(fileConnection.getURL().substring(Constants.SYSTEM_ROOT.length()));
-
- // Open the root
- deleteAll();
- append(Constants.UPPER_DIRECTORY, FOLDER_IMAGE);
-
- // List all directory.
- Enumeration listOfDirs = fileConnection.list("*", false);
- while (listOfDirs.hasMoreElements()) {
- String currentDir = ((String) listOfDirs.nextElement());
- if (currentDir.endsWith("/")) {
- append(currentDir, FOLDER_IMAGE);
- }
- }
-
- // List all png files and don't show hidden files.
- Enumeration listOfFiles = fileConnection.list("*.png", false);
- while (listOfFiles.hasMoreElements()) {
- String currentFile = (String) listOfFiles.nextElement();
- if (currentFile.endsWith(Constants.FILE_SEPARATOR)) {
- append(currentFile, FOLDER_IMAGE);
- } else {
- append(currentFile, FILE_IMAGE);
- }
- }
-
- listOfFiles = fileConnection.list("*.jpg", false);
- while (listOfFiles.hasMoreElements()) {
- String currentFile = (String) listOfFiles.nextElement();
- if (currentFile.endsWith(Constants.FILE_SEPARATOR)) {
- append(currentFile, FOLDER_IMAGE);
- } else {
- append(currentFile, FILE_IMAGE);
- }
- }
-
- listOfFiles = fileConnection.list("*.bmp", false);
- while (listOfFiles.hasMoreElements()) {
- String currentFile = (String) listOfFiles.nextElement();
- if (currentFile.endsWith(Constants.FILE_SEPARATOR)) {
- append(currentFile, FOLDER_IMAGE);
- } else {
- append(currentFile, FILE_IMAGE);
- }
- }
-
- // Making the top item visible.
- setSelectedIndex(0, true);
- } catch (IOException ioe) {
- midlet.showErrorAlert(ioe.getMessage());
- Log.warn("SecurityException", ioe);
- } catch (SecurityException se) {
- midlet.showErrorAlert(se.getMessage());
- Log.warn("SecurityException", se);
- }
- }
-
- /**
- * Inner class ImageViewerOperations.
- */
- private class ImageViewerOperations implements Operation {
-
- private final int operationCode;
-
- public ImageViewerOperations(int operationCode) {
- this.operationCode = operationCode;
- }
-
- public void execute() {
- switch (operationCode) {
- case OPRATION_INITIAL:
- loadRoots();
- if (Constants.GALLERY_DIRECTORY != null) {
- try {
- fileConnection = (FileConnection) Connector.open(Constants.GALLERY_DIRECTORY, Connector.READ);
- displayCurrentRoot();
- } catch (Exception ex) {
- midlet.showErrorAlert(ex.getMessage());
- Log.warn("Exception", ex);
- displayAllRoots();
- }
- } else {
- displayAllRoots();
- }
- break;
- case OPRATION_OPEN:
- openSelected();
- break;
- }
- }
-
- }
-
- }