Changeset 1455

Show
Ignore:
Timestamp:
11/07/08 17:03:58 (2 months ago)
Author:
czue
Message:

updating the flow and image chooser activity to actually play with its commands and display a list of thumbnails. these can currently only be added from the camera control.

Location:
branches/dev/j2merosa/org.javarosa.media.image/src/org/javarosa/media/image
Files:
1 added
5 modified
1 moved

Legend:

Unmodified
Added
Removed
  • branches/dev/j2merosa/org.javarosa.media.image/src/org/javarosa/media/image/activity/FileBrowseActivity.java

    r1445 r1455  
    1 package org.javarosa.media.image.utilities; 
     1package org.javarosa.media.image.activity; 
    22 
    33import java.io.IOException; 
     
    66 
    77import javax.microedition.io.Connector; 
    8 import javax.microedition.io.file.FileConnection; 
    9 import javax.microedition.io.file.FileSystemRegistry; 
    108import javax.microedition.lcdui.Alert; 
    119import javax.microedition.lcdui.AlertType; 
     
    2018import org.javarosa.core.Context; 
    2119import org.javarosa.core.JavaRosaServiceProvider; 
     20import org.javarosa.core.api.Constants; 
    2221import org.javarosa.core.api.IActivity; 
    2322import org.javarosa.core.api.IDisplay; 
    2423import org.javarosa.core.api.IShell; 
    2524import org.javarosa.j2me.view.DisplayViewFactory; 
     25import org.javarosa.media.image.utilities.FileUtility; 
    2626 
    2727public class FileBrowseActivity implements IActivity, CommandListener { 
     
    2929        private String currDirName; 
    3030 
    31         private Command view = new Command("View", Command.ITEM, 1); 
     31        private Command selectCommand = new Command("Select", Command.OK, 1); 
    3232        private Command back = new Command("Back", Command.BACK, 2); 
    33         private Command exit = new Command("Exit", Command.EXIT, 3); 
     33        private Command cancel = new Command("Cancel", Command.CANCEL, 3); 
    3434 
    3535        private IShell shell; 
     
    5454 
    5555        public void destroy() { 
    56                 shell.returnFromActivity(this, "Success!", null); 
    57  
     56                 
    5857        } 
    5958 
     
    101100        public void commandAction(Command c, Displayable d) { 
    102101                System.out.println("updir:" + UP_DIRECTORY); 
    103                 if (c == view) { 
     102                if (c == selectCommand ) { 
    104103                        List curr = (List) d; 
    105104                        final String currFile = curr.getString(curr.getSelectedIndex()); 
    106                         System.out.println("currFile:" + currFile); 
    107  
     105                         
    108106                        new Thread(new Runnable() { 
    109107                                public void run() { 
    110108                                        if (currFile.endsWith(SEP_STR) 
    111109                                                        || currFile.equals(UP_DIRECTORY)) { 
    112                                                 System.out.println("h7"); 
    113  
    114110                                                traverseDirectory(currFile); 
    115111                                        } else { 
    116                                                 showFile(currFile); 
     112                                                returnFile(currFile); 
    117113                                        } 
    118114                                } 
     
    120116                } else if (c == back) { 
    121117                        showCurrDir(); 
    122                 } else if (c == exit) { 
    123                         destroy(); 
     118                } else if (c == cancel) { 
     119                        shell.returnFromActivity(this, Constants.ACTIVITY_CANCEL, null);  
    124120                } 
    125121        } 
     
    127123        void showCurrDir() { 
    128124                Enumeration e; 
    129                 FileConnection currDir = null; 
    130125                List browser; 
    131                 try { 
    132                         System.out.println("In showCurrDir"); 
    133                         System.out.println("mega_root:" + MEGA_ROOT + "cur_dir:" 
    134                                         + currDirName); 
    135                         if (MEGA_ROOT.equals(currDirName)) { 
    136                                 e = FileSystemRegistry.listRoots(); 
    137                                 browser = new List(currDirName, List.IMPLICIT); 
    138                                 System.out.println("here"); 
    139                         } else { 
    140                                 System.out.println("connector"); 
    141                                 currDir = (FileConnection) Connector.open("file://localhost/" 
    142                                                 + currDirName); 
    143                                 System.out.println("curr_dir:" + currDir); 
    144                                 // currDir = 
    145                                 // (FileConnection)Connector.open("http://localhost:8080/" + 
    146                                 // currDirName); 
    147                                 e = currDir.list(); 
    148                                 browser = new List(currDirName, List.IMPLICIT); 
    149                                 browser.append(UP_DIRECTORY, null); 
     126                System.out.println("In showCurrDir"); 
     127                System.out.println("mega_root:" + MEGA_ROOT + "cur_dir:" 
     128                                + currDirName); 
     129                if (MEGA_ROOT.equals(currDirName)) { 
     130                        e = FileUtility.getRootNames(); 
     131                        browser = new List(currDirName, List.IMPLICIT); 
     132                        System.out.println("here"); 
     133                } else { 
     134                        String fullPath =  "file://localhost/" + currDirName; 
     135                        e = FileUtility.listDirectory(fullPath); 
     136                        browser = new List(currDirName, List.IMPLICIT); 
     137                        browser.append(UP_DIRECTORY, null); 
     138                } 
     139                while (e.hasMoreElements()) { 
     140                        String fileName = (String) e.nextElement(); 
     141                        if (fileName.charAt(fileName.length() - 1) == SEP) { 
     142                                browser.append(fileName, null); 
    150143                        } 
    151                         while (e.hasMoreElements()) { 
    152                                 System.out.println("h2"); 
    153                                 String fileName = (String) e.nextElement(); 
    154                                 System.out.println("fileName:" + fileName + " char_at:" 
    155                                                 + fileName.charAt(fileName.length() - 1)); 
     144                        // if((fileName.charAt(fileName.length()-1))).equals("g"))){} 
     145                        else { 
     146                                System.out.println("h4"); 
     147                                // Image image = Image.createImage(fileName); 
     148                                browser.append(fileName, null); 
     149                                // Form form = new Form("Image here"); 
    156150 
    157                                 if (fileName.charAt(fileName.length() - 1) == SEP) { 
    158                                         browser.append(fileName, null); 
    159  
    160                                 } 
    161                                 // if((fileName.charAt(fileName.length()-1))).equals("g"))){} 
    162                                 else { 
    163                                         System.out.println("h4"); 
    164                                         // Image image = Image.createImage(fileName); 
    165                                         browser.append(fileName, null); 
    166                                         // Form form = new Form("Image here"); 
    167  
    168                                         // form.append(image); 
    169                                 } 
     151                                // form.append(image); 
    170152                        } 
    171                         browser.setSelectCommand(view); 
    172                         browser.addCommand(exit); 
    173                         browser.setCommandListener(this); 
    174                         if (currDir != null) { 
    175                                 currDir.close(); 
    176                         } 
    177                         display.setView(DisplayViewFactory.createView(browser)); 
    178                 } catch (IOException ioe) { 
    179                         //System.out.println(ioe); 
    180153                } 
     154                browser.setSelectCommand(selectCommand ); 
     155                browser.addCommand(cancel); 
     156                browser.setCommandListener(this); 
     157                display.setView(DisplayViewFactory.createView(browser)); 
    181158        } 
    182159 
     
    206183        } 
    207184 
    208         void showFile(String fileName) { 
     185        void returnFile(String fileName) { 
    209186                try { 
    210                         FileConnection fc = (FileConnection) Connector 
    211                                         .open("file://localhost/" + currDirName + fileName); 
    212                         if (!fc.exists()) { 
    213                                 throw new IOException("File does not exists"); 
    214                         } 
    215                         InputStream fis = fc.openInputStream(); 
    216                         byte[] b = new byte[1024]; 
    217                         int length = fis.read(b, 0, 1024); 
    218                         fis.close(); 
    219                         fc.close(); 
    220  
    221                         TextBox tb = new TextBox("View File: " + fileName, null, 1024, 
    222                                         TextField.ANY | TextField.UNEDITABLE); 
    223  
    224                         tb.addCommand(back); 
    225                         tb.addCommand(exit); 
    226                         tb.setCommandListener(this); 
    227  
    228                         if (length > 0) { 
    229                                 tb.setString(new String(b, 0, length)); 
    230                         } 
    231                         display.setView(DisplayViewFactory.createView(tb)); 
     187                        // todo 
    232188                } catch (Exception e) { 
    233189                } 
  • branches/dev/j2merosa/org.javarosa.media.image/src/org/javarosa/media/image/activity/ImageCaptureActivity.java

    r1445 r1455  
    4444{ 
    4545 
     46        public static final String IMAGE_KEY = "IMAGE_KEY"; 
    4647        private Context context; 
    4748        private IShell shell; 
     
    5657        private IDisplay display; 
    5758        private ImageRMSUtility dataModel; 
    58  
     59        private Image image; 
     60         
    5961        public ImageCaptureActivity(IShell shell) { 
    6062                this.shell = shell; 
     
    6971 
    7072        public void destroy() { 
    71                 shell.returnFromActivity(this, "Success!", null); 
     73                mPlayer.close(); 
     74                mPlayer = null; 
     75                mVideoControl = null; 
    7276        } 
    7377 
    7478        public Context getActivityContext() { 
    75                 // TODO Auto-generated method stub 
    76                 return null; 
     79                return context; 
    7780        } 
    7881 
     
    9699                this.context = context; 
    97100                showCamera(); 
    98                  
    99101        } 
    100102         
     
    112114         * Other images are deleted? 
    113115         */ 
    114         private void finish() { 
     116        private void doFinish() { 
    115117                Hashtable args = buildReturnArgs(); 
    116                 shell.returnFromActivity(this, "Success!", args); 
    117  
     118                shell.returnFromActivity(this, Constants.ACTIVITY_COMPLETE, args); 
     119        } 
     120 
     121        private void doError() { 
     122                shell.returnFromActivity(this, Constants.ACTIVITY_ERROR, null); 
    118123        } 
    119124 
    120125        private Hashtable buildReturnArgs() { 
    121126                // stick the picture in here.  
    122                 return null; 
     127                Hashtable table = new Hashtable(); 
     128                table.put(IMAGE_KEY, image); 
     129                return table; 
    123130        } 
    124131        private void showCamera() { 
     
    129136                        mVideoControl = (VideoControl) mPlayer.getControl("VideoControl"); 
    130137 
    131 //                      Command mExitCommand = new Command("Exit", Command.EXIT, 0); 
    132 //                      Command mCameraCommand = new Command("Camera", Command.SCREEN, 0); 
     138                        //      Command mExitCommand = new Command("Exit", Command.EXIT, 0); 
     139                        //      Command mCameraCommand = new Command("Camera", Command.SCREEN, 0); 
    133140                        mBackCommand = new Command("Back", Command.BACK, 0); 
    134141                        mCaptureCommand = new Command("Capture", Command.SCREEN, 0); 
     
    153160//              JavaRosaServiceProvider.instance().getDisplay().setCurrent(a); 
    154161//              throw new RuntimeException(e.getMessage()); 
     162                System.out.println(e.getMessage()); 
     163                e.printStackTrace(); 
    155164                String toLog = e.getMessage(); 
    156165                toLog += e.toString(); 
    157166                saveFile("log" + System.currentTimeMillis() + ".txt", toLog.getBytes()); 
    158167                 
    159         } 
    160  
     168                doError(); 
     169        } 
     170 
     171         
    161172        public void commandAction(Command cmd, Displayable display) { 
    162                 // TODO Auto-generated method stub 
    163173                if (cmd.equals(this.mBackCommand)) { 
    164174                        goBack(); 
     
    166176                else if (cmd.equals(this.mCaptureCommand)) { 
    167177                        doCapture(); 
    168                         System.out.println("Click!"); 
     178                        //doCaptureLoop(); 
    169179                } 
    170180        } 
    171181 
    172182        private void goBack() { 
    173                 // TODO 
    174                 System.out.println("Back Again!"); 
    175183                this.shell.returnFromActivity(this, Constants.ACTIVITY_CANCEL, null); 
    176184                 
     
    178186 
    179187        private void doCapture() { 
    180                 byte[] jpg; 
    181                 int width = 1360; 
    182                 int height = 1020; 
     188                byte[] jpg;      
     189                int width = 640; 
     190                int height = 480; 
    183191                try { 
    184192                        // Get the image. 
    185193                        jpg = mVideoControl.getSnapshot("encoding=jpeg&quality=100&width=" + width + "&height=" + height); 
    186                         // Save to file 
    187                         String fileName = "test" + System.currentTimeMillis(); 
    188                         boolean saved = saveFile(fileName + ".jpg", jpg); 
    189                 } catch (MediaException me) { 
     194                        image = Image.createImage(jpg, 0, jpg.length); 
     195                        doFinish(); 
     196                        // Save to file no longer 
     197                        //String fileName = "test" + System.currentTimeMillis(); 
     198                        //boolean saved = saveFile(fileName + ".jpg", jpg); 
     199                } catch (Exception me) { 
    190200                        handleException(me); 
    191201                } 
  • branches/dev/j2merosa/org.javarosa.media.image/src/org/javarosa/media/image/activity/ImageChooserActivity.java

    r1445 r1455  
    33import java.util.Hashtable; 
    44 
     5import javax.microedition.lcdui.Command; 
     6import javax.microedition.lcdui.CommandListener; 
    57import javax.microedition.lcdui.Display; 
     8import javax.microedition.lcdui.Displayable; 
     9import javax.microedition.lcdui.Form; 
     10import javax.microedition.lcdui.Image; 
    611 
    712import org.javarosa.core.Context; 
    813import org.javarosa.core.JavaRosaServiceProvider; 
     14import org.javarosa.core.api.Constants; 
    915import org.javarosa.core.api.IActivity; 
    1016import org.javarosa.core.api.IDisplay; 
    1117import org.javarosa.core.api.IShell; 
     18import org.javarosa.j2me.view.DisplayViewFactory; 
    1219import org.javarosa.media.image.model.FileDataPointer; 
    1320import org.javarosa.media.image.storage.ImageRMSUtility; 
     21import org.javarosa.media.image.utilities.ImageUtility; 
    1422 
    1523/** 
     
    2129 * 
    2230 */ 
    23 public class ImageChooserActivity implements IActivity 
     31public class ImageChooserActivity implements IActivity, CommandListener 
    2432{ 
     33        public static final String ACTIVITY_KEY = "ACTIVITY_KEY"; 
    2534        // this should map images (IDataPointers) to bool true/false whether selected or not 
    2635        private Hashtable allImages; 
     
    2938        private IDisplay display; 
    3039        private ImageRMSUtility dataModel; 
     40        private Form mainForm; 
     41        private Command cancelCommand; 
     42        private Command cameraCommand; 
     43        private Command browseCommand; 
     44        private Command returnCommand; 
     45        private Command viewCommand; 
     46        private Command markCommand; 
     47        private String currentKey; 
     48           
    3149 
    3250        public ImageChooserActivity(IShell shell) { 
     
    3452                display = JavaRosaServiceProvider.instance().getDisplay(); 
    3553                dataModel = new ImageRMSUtility("image_store"); 
     54                 
     55                cancelCommand = new Command("Cancel", Command.CANCEL, 0); 
     56                returnCommand = new Command("Return", Command.OK, 0); 
     57            cameraCommand = new Command("Camera", Command.SCREEN, 0); 
     58            browseCommand = new Command("Browse", Command.SCREEN, 0); 
     59            viewCommand = new Command("View", Command.SCREEN, 0); 
     60            markCommand = new Command("Mark/Unmark", Command.SCREEN, 0); 
    3661        } 
    3762 
     
    4772 
    4873        public Context getActivityContext() { 
    49                 // TODO Auto-generated method stub 
    50                 return null; 
     74                return context; 
    5175        } 
    5276 
     
    5781 
    5882        public void resume(Context globalContext) { 
    59                 // TODO Auto-generated method stub 
     83                Image img = (Image) globalContext.getElement(currentKey); 
     84                if (img != null) { 
     85                        Image thumbnail = ImageUtility.createThumbnail(img); 
     86                        mainForm.append(thumbnail); 
     87                } 
     88                display.setView(DisplayViewFactory.createView(mainForm)); 
    6089                 
    6190        } 
    6291 
    6392        public void setShell(IShell shell) { 
    64                 // TODO Auto-generated method stub 
     93                this.shell = shell; 
    6594                 
    6695        } 
    6796 
    6897        public void start(Context context) { 
    69 //              The start method should complete all initialization that was not performed in the  
    70 //              constructor, including all GUI initialization. The context object parameter is  
    71 //              likely a subclass of the global context object. The context should contain required  
    72 //              and optional parameters for running the Activity. It is preferred to pass all this  
    73 //              information through the context rather than call separate initialize(...) methods,  
    74 //              because this way the activity's current 'configuration' can be accessed and passed  
    75 //              around generically. 
    76  
    77 //              When start() is called, the Activity should initialize itself and then take control  
    78 //              of the application, for instance by setting the Display. Activities shouldn't touch  
    79 //              the device's Display object directly, but rather call the shell's setDisplay() method.  
    80 //              This allows the Shell to mediate requests for the Display, thus preventing confused  
    81 //              Activities from breaking the Application's workflow. 
    82  
    83 //              In this method you should generally save off a reference to the activity's parent Shell.  
    84  
     98                this.context = context; 
     99                mainForm = new Form("Image Chooser"); 
     100        mainForm.addCommand(cancelCommand); 
     101                mainForm.addCommand(cameraCommand); 
     102                mainForm.addCommand(browseCommand); 
     103                mainForm.addCommand(returnCommand); 
     104                mainForm.addCommand(viewCommand); 
     105                mainForm.addCommand(markCommand); 
     106            mainForm.setCommandListener(this); 
     107             
     108            display.setView(DisplayViewFactory.createView(mainForm)); 
    85109                 
    86110        } 
     
    118142        } 
    119143 
     144        public void commandAction(Command command, Displayable display) { 
     145                if (command.equals(cameraCommand)) { 
     146                        processCamera(); 
     147                } else if (command.equals(browseCommand)) { 
     148                        processBrowser(); 
     149                } else if (command.equals(viewCommand)) { 
     150                        processView(); 
     151                } else if (command.equals(cancelCommand)) { 
     152                        processCancel(); 
     153                } else if (command.equals(returnCommand)) { 
     154                        processReturn(); 
     155                } else if (command.equals(markCommand)) { 
     156                        processMark(); 
     157                }   
     158        } 
     159 
     160        private void processReturn() { 
     161                shell.returnFromActivity(this, Constants.ACTIVITY_COMPLETE, null); 
     162        } 
     163 
     164        private void processMark() { 
     165                // TODO Auto-generated method stub 
     166                 
     167        } 
     168 
     169        private void processCancel() { 
     170                shell.returnFromActivity(this, Constants.ACTIVITY_CANCEL, null);                                 
     171        } 
     172 
     173        private void processView() { 
     174                // TODO Auto-generated method stub 
     175                 
     176        } 
     177 
     178        private void processBrowser() { 
     179                returnFromActivity(new FileBrowseActivity(shell)); 
     180        } 
     181 
     182        private void processCamera()  
     183        { 
     184                currentKey = ImageCaptureActivity.IMAGE_KEY; 
     185                returnFromActivity(new ImageCaptureActivity(shell)); 
     186        } 
     187 
     188        private void returnFromActivity(IActivity activity) { 
     189                Hashtable returnArgs = buildReturnArgsFromActivity(activity); 
     190                shell.returnFromActivity(this, Constants.ACTIVITY_NEEDS_RESOLUTION, returnArgs); 
     191        } 
     192 
     193        private Hashtable buildReturnArgsFromActivity(IActivity activity) { 
     194                Hashtable table = new Hashtable(); 
     195                table.put(ACTIVITY_KEY, activity); 
     196                return table; 
     197        } 
     198 
    120199         
    121200 
  • branches/dev/j2merosa/org.javarosa.media.image/src/org/javarosa/media/image/midlet/SnapperMIDlet.java

    r1434 r1455  
    11/* License 
    2 * Modifications by Andres Monroy-Hernandez, 2008 
    32 *  
    43 * Copyright 1994-2004 Sun Microsystems, Inc. All Rights Reserved. 
     
    3837package org.javarosa.media.image.midlet; 
    3938 
    40 import java.io.DataInputStream; 
     39 
    4140import java.io.IOException; 
    42 import java.io.InputStream; 
    43 import java.io.OutputStream; 
    44 import java.util.Calendar; 
    45 import java.util.Date; 
    46 import java.util.Enumeration; 
    4741 
    48 import javax.microedition.io.Connector; 
    49 import javax.microedition.io.HttpConnection; 
    50 import javax.microedition.io.file.FileConnection; 
    51 import javax.microedition.io.file.FileSystemRegistry; 
    52 import javax.microedition.lcdui.Alert; 
    53 import javax.microedition.lcdui.AlertType; 
    54 import javax.microedition.lcdui.Canvas; 
    55 import javax.microedition.lcdui.Choice; 
    56 import javax.microedition.lcdui.ChoiceGroup; 
    57 import javax.microedition.lcdui.Command; 
    58 import javax.microedition.lcdui.CommandListener; 
    59 import javax.microedition.lcdui.DateField; 
    60 import javax.microedition.lcdui.Display; 
    61