/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

  1. package com.nokia.cameraframe.views;
  2. import java.io.IOException;
  3. import java.util.Enumeration;
  4. import java.util.Vector;
  5. import javax.microedition.io.Connector;
  6. import javax.microedition.io.file.FileConnection;
  7. import javax.microedition.io.file.FileSystemRegistry;
  8. import javax.microedition.lcdui.Command;
  9. import javax.microedition.lcdui.CommandListener;
  10. import javax.microedition.lcdui.Displayable;
  11. import javax.microedition.lcdui.Image;
  12. import javax.microedition.lcdui.List;
  13. import javax.microedition.lcdui.Ticker;
  14. import com.nokia.cameraframe.MainMIDlet;
  15. import com.nokia.cameraframe.utils.Commands;
  16. import com.nokia.cameraframe.utils.Constants;
  17. import com.nokia.cameraframe.utils.ImageUtils;
  18. import com.nokia.cameraframe.utils.Log;
  19. import com.nokia.cameraframe.views.component.Operation;
  20. import com.nokia.cameraframe.views.component.OperationsQueue;
  21. class GalleryView extends List implements CommandListener {
  22. private final MainMIDlet midlet;
  23. private PhotoEditorView photoEditorView;
  24. private Ticker ticker;
  25. // Gallery
  26. private static final Image ROOT_IMAGE = ImageUtils.loadImage("/root.png");
  27. private static final Image FOLDER_IMAGE = ImageUtils.loadImage("/folder.png");
  28. private static final Image FILE_IMAGE = ImageUtils.loadImage("/file.png");
  29. private final OperationsQueue queue;
  30. private FileConnection fileConnection;
  31. private Vector rootsList;
  32. // Operation value
  33. private final static int OPRATION_INITIAL = 1;
  34. private final static int OPRATION_OPEN = 2;
  35. public GalleryView(MainMIDlet midlet) {
  36. super("Photos", List.IMPLICIT);
  37. this.midlet = midlet;
  38. ticker = new Ticker("");
  39. setTicker(ticker);
  40. // Add command
  41. setSelectCommand(Commands.OPEN_ITEM);
  42. addCommand(Commands.OPEN_ITEM);
  43. addCommand(Commands.MENU_ABOUT);
  44. addCommand(Commands.MENU_HELP);
  45. addCommand(Commands.MENU_EXIT);
  46. addCommand(Commands.EXIT);
  47. setCommandListener(this);
  48. // Initialize
  49. queue = new OperationsQueue();
  50. rootsList = new Vector();
  51. }
  52. public void initialize() {
  53. queue.enqueueOperation(new ImageViewerOperations(OPRATION_INITIAL));
  54. }
  55. public void commandAction(Command command, Displayable displayable) {
  56. if (command == Commands.OPEN_ITEM) {
  57. queue.enqueueOperation(new ImageViewerOperations(OPRATION_OPEN));
  58. } else if (command == Commands.MENU_ABOUT) {
  59. midlet.viewForward(midlet.getAboutView());
  60. midlet.getCameraView().setCategoryBarInvisible();
  61. } else if (command == Commands.MENU_HELP) {
  62. midlet.viewForward(midlet.getHelpView());
  63. midlet.getCameraView().setCategoryBarInvisible();
  64. } else if (command == Commands.MENU_EXIT || command == Commands.EXIT) {
  65. midlet.destroyApp(true);
  66. }
  67. }
  68. private void displayAllRoots() {
  69. ticker.setString("Roots");
  70. deleteAll();
  71. Enumeration roots = rootsList.elements();
  72. while (roots.hasMoreElements()) {
  73. String root = (String) roots.nextElement();
  74. root = root.replace('/', Constants.FILE_SEPARATOR.charAt(0));
  75. append(root.substring(1), ROOT_IMAGE);
  76. }
  77. fileConnection = null;
  78. }
  79. private void loadRoots() {
  80. if (!rootsList.isEmpty()) {
  81. rootsList.removeAllElements();
  82. }
  83. try {
  84. Enumeration roots = FileSystemRegistry.listRoots();
  85. while (roots.hasMoreElements()) {
  86. rootsList.addElement("/" + (String) roots.nextElement());
  87. }
  88. } catch (Throwable e) {
  89. midlet.showErrorAlert(e.getMessage());
  90. Log.warn("Exception", e);
  91. }
  92. }
  93. private void openSelected() {
  94. int selectedIndex = getSelectedIndex();
  95. if (selectedIndex >= 0) {
  96. String selectedFile = getString(selectedIndex);
  97. // Directory selected
  98. if (selectedFile.endsWith(Constants.FILE_SEPARATOR)) {
  99. try {
  100. String tmp = selectedFile.replace(Constants.FILE_SEPARATOR.charAt(0), '/');
  101. if (fileConnection == null) {
  102. fileConnection = (FileConnection) Connector.open(Constants.SYSTEM_ROOT + tmp, Connector.READ);
  103. } else {
  104. fileConnection.setFileConnection(tmp);
  105. }
  106. displayCurrentRoot();
  107. } catch (IOException ioe) {
  108. midlet.showErrorAlert(ioe.getMessage());
  109. Log.warn("SecurityException", ioe);
  110. } catch (SecurityException se) {
  111. midlet.showErrorAlert(se.getMessage());
  112. Log.warn("SecurityException", se);
  113. }
  114. }
  115. // Upper directory selected
  116. else if (selectedFile.equals(Constants.UPPER_DIRECTORY)) {
  117. if (rootsList.contains(fileConnection.getPath() + fileConnection.getName())) {
  118. displayAllRoots();
  119. } else {
  120. try {
  121. fileConnection.setFileConnection(Constants.UPPER_DIRECTORY);
  122. displayCurrentRoot();
  123. } catch (IOException ioe) {
  124. midlet.showErrorAlert(ioe.getMessage());
  125. Log.warn("IOException", ioe);
  126. }
  127. }
  128. }
  129. // File selected
  130. else {
  131. midlet.getCameraView().setCategoryBarInvisible();
  132. // Get format
  133. int startIndex = selectedFile.indexOf(".");
  134. String imageFormat = selectedFile.substring(startIndex + 1, selectedFile.length());
  135. // Resize
  136. Image imageSelected = ImageUtils.getImage(fileConnection.getURL() + selectedFile);
  137. int sourceWidth = imageSelected.getWidth();
  138. int sourceHeight = imageSelected.getHeight();
  139. int newWidth = -1;
  140. int newHeight = -1;
  141. if (sourceWidth == sourceHeight || sourceWidth > newHeight) { // Cubic or Landscape
  142. if (sourceWidth > Constants.MAX_LANDSCAPE_PHOTO_WIDTH) {
  143. newWidth = Constants.MAX_LANDSCAPE_PHOTO_WIDTH;
  144. if (newHeight == -1) {
  145. newHeight = newWidth * sourceHeight / sourceWidth;
  146. }
  147. imageSelected = tube42.lib.imagelib.ImageUtils.resize(imageSelected, newWidth, newHeight, false, false);
  148. }
  149. } else { // Portrait
  150. if (sourceHeight > Constants.MAX_PORTRAIT_PHOTO_HEIGH) {
  151. newHeight = Constants.MAX_PORTRAIT_PHOTO_HEIGH;
  152. if (newWidth == -1) {
  153. newWidth = newHeight * sourceWidth / sourceHeight;
  154. }
  155. imageSelected = tube42.lib.imagelib.ImageUtils.resize(imageSelected, newWidth, newHeight, false, false);
  156. }
  157. }
  158. Log.debug("Resize width [" + sourceWidth + "] -> [" + imageSelected.getWidth() + "]");
  159. Log.debug("Resize height [" + sourceHeight + "] -> [" + imageSelected.getHeight() + "]");
  160. photoEditorView = new PhotoEditorView(midlet, imageSelected);
  161. photoEditorView.setImageFormat(imageFormat);
  162. midlet.viewForward(photoEditorView);
  163. }
  164. }
  165. }
  166. private void displayCurrentRoot() {
  167. try {
  168. // Remove system root before display
  169. ticker.setString(fileConnection.getURL().substring(Constants.SYSTEM_ROOT.length()));
  170. // Open the root
  171. deleteAll();
  172. append(Constants.UPPER_DIRECTORY, FOLDER_IMAGE);
  173. // List all directory.
  174. Enumeration listOfDirs = fileConnection.list("*", false);
  175. while (listOfDirs.hasMoreElements()) {
  176. String currentDir = ((String) listOfDirs.nextElement());
  177. if (currentDir.endsWith("/")) {
  178. append(currentDir, FOLDER_IMAGE);
  179. }
  180. }
  181. // List all png files and don't show hidden files.
  182. Enumeration listOfFiles = fileConnection.list("*.png", false);
  183. while (listOfFiles.hasMoreElements()) {
  184. String currentFile = (String) listOfFiles.nextElement();
  185. if (currentFile.endsWith(Constants.FILE_SEPARATOR)) {
  186. append(currentFile, FOLDER_IMAGE);
  187. } else {
  188. append(currentFile, FILE_IMAGE);
  189. }
  190. }
  191. listOfFiles = fileConnection.list("*.jpg", false);
  192. while (listOfFiles.hasMoreElements()) {
  193. String currentFile = (String) listOfFiles.nextElement();
  194. if (currentFile.endsWith(Constants.FILE_SEPARATOR)) {
  195. append(currentFile, FOLDER_IMAGE);
  196. } else {
  197. append(currentFile, FILE_IMAGE);
  198. }
  199. }
  200. listOfFiles = fileConnection.list("*.bmp", false);
  201. while (listOfFiles.hasMoreElements()) {
  202. String currentFile = (String) listOfFiles.nextElement();
  203. if (currentFile.endsWith(Constants.FILE_SEPARATOR)) {
  204. append(currentFile, FOLDER_IMAGE);
  205. } else {
  206. append(currentFile, FILE_IMAGE);
  207. }
  208. }
  209. // Making the top item visible.
  210. setSelectedIndex(0, true);
  211. } catch (IOException ioe) {
  212. midlet.showErrorAlert(ioe.getMessage());
  213. Log.warn("SecurityException", ioe);
  214. } catch (SecurityException se) {
  215. midlet.showErrorAlert(se.getMessage());
  216. Log.warn("SecurityException", se);
  217. }
  218. }
  219. /**
  220. * Inner class ImageViewerOperations.
  221. */
  222. private class ImageViewerOperations implements Operation {
  223. private final int operationCode;
  224. public ImageViewerOperations(int operationCode) {
  225. this.operationCode = operationCode;
  226. }
  227. public void execute() {
  228. switch (operationCode) {
  229. case OPRATION_INITIAL:
  230. loadRoots();
  231. if (Constants.GALLERY_DIRECTORY != null) {
  232. try {
  233. fileConnection = (FileConnection) Connector.open(Constants.GALLERY_DIRECTORY, Connector.READ);
  234. displayCurrentRoot();
  235. } catch (Exception ex) {
  236. midlet.showErrorAlert(ex.getMessage());
  237. Log.warn("Exception", ex);
  238. displayAllRoots();
  239. }
  240. } else {
  241. displayAllRoots();
  242. }
  243. break;
  244. case OPRATION_OPEN:
  245. openSelected();
  246. break;
  247. }
  248. }
  249. }
  250. }