Commits

Clayton Sims committed 9f6156d

Made the CustomChoiceGroup capable of working for implicit selections, pulled
the not-actually-part-of-the-controller media stuff out of JRFEC.

  • Participants
  • Parent commits 5d3ab84

Comments (0)

Files changed (5)

File j2me/form-entry/src/org/javarosa/formmanager/api/JrFormEntryController.java

 import org.javarosa.formmanager.api.transitions.FormEntryTransitions;
 import org.javarosa.formmanager.properties.FormManagerProperties;
 import org.javarosa.formmanager.view.IFormEntryView;
+import org.javarosa.utilities.media.MediaUtils;
 
 /**
  * Extension of {@link FormEntryController} for J2ME.
 	IFormEntryView view;
 	boolean quickEntry = true;
 	
+	private static Reference curAudRef = null;
+	private static String curAudioURI;
+	
+	protected static boolean playAudioIfAvailable = true;
+	
 	private static int POUND_KEYCODE = Canvas.KEY_POUND;
 	
 	
 	/** Causes audio player to throw runtime exceptions if there are problems instead of failing silently **/
 	private boolean audioFailFast = true;
 	
-    /////////AUDIO PLAYBACK
-	static Player audioPlayer;
-	protected static boolean playAudioIfAvailable = true;
-	protected static final int AUDIO_SUCCESS = 1;
-	protected static final int AUDIO_NO_RESOURCE = 2;
-	protected static final int AUDIO_ERROR = 3;
-	protected static final int AUDIO_DISABLED = 4;
-	protected static final int AUDIO_BUSY = 5;
-	protected static final int AUDIO_NOT_RECOGNIZED = 6;
-	private static Reference curAudRef = null;
-	private static String curAudioURI;
-	
 	String extraKeyMode;
 	
 	public JrFormEntryController(JrFormEntryModel model) {
      * @return
      */
 	public int playAudio(FormEntryPrompt fep,SelectChoice select){
-		if (!playAudioIfAvailable) return AUDIO_DISABLED;
+		if (!playAudioIfAvailable) return MediaUtils.AUDIO_DISABLED;
 		
 		String textID;
 		curAudioURI = null;
 			if (fep.getAudioText() != null) {
 				curAudioURI = fep.getAudioText();
 			} else {
-				return AUDIO_NO_RESOURCE;
+				return MediaUtils.AUDIO_NO_RESOURCE;
 			}	
 		}else{
 			textID = select.getTextID();
-			if(textID == null || textID == "") return AUDIO_NO_RESOURCE;
+			if(textID == null || textID == "") return MediaUtils.AUDIO_NO_RESOURCE;
 			
 			if (fep.getSpecialFormSelectChoiceText(select, FormEntryCaption.TEXT_FORM_AUDIO) != null) {
 				curAudioURI = fep.getSpecialFormSelectChoiceText(select, FormEntryCaption.TEXT_FORM_AUDIO);
 			} else {
-				return AUDIO_NO_RESOURCE;
+				return MediaUtils.AUDIO_NO_RESOURCE;
 			}
 		}
 		
 		//No idea why this is a member variable...
-		return playAudio(curAudioURI);
+		return MediaUtils.playAudio(curAudioURI);
 	}
 	
-	public int playAudio(String jrRefURI) {
-		curAudioURI = jrRefURI;
-		int retcode = AUDIO_SUCCESS;
-		try {
-			curAudRef = ReferenceManager._().DeriveReference(curAudioURI);
-			String format = getFileFormat(curAudioURI);
-
-			if(format == null) return AUDIO_NOT_RECOGNIZED;
-			if(audioPlayer == null){
-				audioPlayer = Manager.createPlayer(curAudRef.getLocalURI());
-				audioPlayer.start();
-			}else{
-				audioPlayer.deallocate();
-				audioPlayer.close();
-				audioPlayer = Manager.createPlayer(curAudRef.getLocalURI());
-				audioPlayer.start();
-			}
-			
-		} catch (InvalidReferenceException ire) {
-			retcode = AUDIO_ERROR;
-			if(audioFailFast)throw new RuntimeException("Invalid Reference Exception when attempting to play audio at URI:"+ curAudioURI + "Exception msg:"+ire.getMessage());
-			System.err.println("Invalid Reference Exception when attempting to play audio at URI:"+ curAudioURI + "Exception msg:"+ire.getMessage());
-		} catch (IOException ioe) {
-			retcode = AUDIO_ERROR;
-			if(audioFailFast) throw new RuntimeException("IO Exception (input cannot be read) when attempting to play audio stream with URI:"+ curAudioURI + "Exception msg:"+ioe.getMessage());
-			System.err.println("IO Exception (input cannot be read) when attempting to play audio stream with URI:"+ curAudioURI + "Exception msg:"+ioe.getMessage());
-		} catch (MediaException e) {
-			retcode = AUDIO_ERROR;
-			if(audioFailFast) throw new RuntimeException("Media format not supported! Uri: "+ curAudioURI + "Exception msg:"+e.getMessage());
-			System.err.println("Media format not supported! Uri: "+ curAudioURI + "Exception msg:"+e.getMessage());
-		}
-		return retcode;
-
-	}
-	
-	private static String getFileFormat(String fpath){
-//		Wave audio files: audio/x-wav
-//		AU audio files: audio/basic
-//		MP3 audio files: audio/mpeg
-//		MIDI files: audio/midi
-//		Tone sequences: audio/x-tone-seq
-//		MPEG video files: video/mpeg
-//		Audio 3GPP files (.3gp) audio/3gpp
-//		Audio AMR files (.amr) audio/amr
-//		Audio AMR (wideband) files (.awb) audio/amr-wb
-//		Audio MIDI files (.mid or .midi) audio/midi
-//		Audio MP3 files (.mp3) audio/mpeg
-//		Audio MP4 files (.mp4) audio/mp4
-//		Audio WAV files (.wav) audio/wav audio/x-wav
-		
-		if(fpath.indexOf(".mp3") > -1) return "audio/mp3";
-		if(fpath.indexOf(".wav") > -1) return "audio/x-wav";
-		if(fpath.indexOf(".amr") > -1) return "audio/amr";
-		if(fpath.indexOf(".awb") > -1) return "audio/amr-wb";
-		if(fpath.indexOf(".mp4") > -1) return "audio/mp4";
-		if(fpath.indexOf(".aac") > -1) return "audio/aac";
-		if(fpath.indexOf(".3gp") > -1) return "audio/3gpp";
-		if(fpath.indexOf(".au") > -1) return "audio/basic";
-		throw new RuntimeException("COULDN'T FIND FILE FORMAT");
-	}
 
 	public boolean isEntryOptimized() {
 		return quickEntry;

File j2me/form-entry/src/org/javarosa/formmanager/view/CustomChoiceGroup.java

 package org.javarosa.formmanager.view;
 
 import javax.microedition.lcdui.Canvas;
+import javax.microedition.lcdui.Image;
 
 import de.enough.polish.ui.ChoiceGroup;
 import de.enough.polish.ui.ChoiceItem;
 import de.enough.polish.ui.Container;
 import de.enough.polish.ui.Item;
 import de.enough.polish.ui.MoreUIAccess;
+import de.enough.polish.ui.Screen;
 import de.enough.polish.ui.Style;
 import de.enough.polish.ui.StyleSheet;
-import de.enough.polish.ui.UiAccess;
 
 /**
  * @author ctsims
 	
 	//Do this better
 	public CustomChoiceGroup(String s, int choiceType, boolean autoSelect, boolean numericNavigation) {
-		super(s,choiceType);
-		this.choiceType = choiceType;
-		this.autoSelect = autoSelect;
-		this.numericNavigation = numericNavigation;
-		this.touched = !numericNavigation;
+		this(s,choiceType,autoSelect,numericNavigation,null);
 	}
 
 	public CustomChoiceGroup(String s, int choiceType, boolean autoSelect, boolean numericNavigation, Style style) {
-		super(s,choiceType, style);
+		super(s,choiceType, new String[0], null, style, true);
 		this.choiceType = choiceType;
 		this.autoSelect = autoSelect;
 		this.numericNavigation = numericNavigation;
 		public void setLastSelected(int index) {
 			this.lastSelected = index;
 		}
-		
+
 		public void touch() {
 			if(!touched) {
 				this.itemStyle = StyleSheet.getStyle("listitem");
 				touched = true;
 			}
 		}
+		
+		public void setImplicit(Screen screen) {
+			this.autoFocusEnabled = true;
+			this.screen = screen;
+		}
+		
+		private void disableNumericNav() {
+			numericNavigation = false;
+			touch();
+		}
+		
+		
+		//TODO: Hate this duplication, not sure if there's a cleaner way to get around
+		//how polish does this, though
+		public int append(String stringPart, Image imagePart) {
+			if(numericNavigation) {
+				//#style uninitializedListItem
+				int index =  super.append(stringPart, imagePart);
+				if(index >= 9) {
+					disableNumericNav();
+				}
+				return index;
+			} else {
+				//#style listitem
+				return super.append(stringPart, imagePart);
+			}
+		}
+
+		public int append(ChoiceItem item) {
+			if(numericNavigation) {
+				//#style uninitializedListItem
+				int index =  super.append(item);
+				if(index >= 9) {
+					disableNumericNav();
+				}
+				return index;
+			} else {
+				//#style listitem
+				return super.append(item);
+			}
+		}
 }

File j2me/form-entry/src/org/javarosa/formmanager/view/chatterbox/Chatterbox.java

 import org.javarosa.j2me.log.HandledPCommandListener;
 import org.javarosa.j2me.log.HandledThread;
 import org.javarosa.j2me.view.J2MEDisplay;
+import org.javarosa.utilities.media.MediaUtils;
 
 import de.enough.polish.ui.Command;
 import de.enough.polish.ui.Container;
 					while(new Date().getTime() < time + 300);
 					J2MEDisplay.showError(at, m, alIm);
 					if(aURI != null) {
-						Chatterbox.this.controller.playAudio(aURI);
+						MediaUtils.playAudio(aURI);
 					}
 				}
 				

File j2me/form-entry/src/org/javarosa/formmanager/view/singlequestionscreen/SingleQuestionView.java

 import org.javarosa.j2me.log.CrashHandler;
 import org.javarosa.j2me.log.HandledPCommandListener;
 import org.javarosa.j2me.view.J2MEDisplay;
+import org.javarosa.utilities.media.MediaUtils;
 
 import de.enough.polish.ui.Command;
 import de.enough.polish.ui.Displayable;
 			}
 			J2MEDisplay.showError("Validation failure", constraintMsg, image);
 			if(constraintAudio != null) {
-				this.controller.playAudio(constraintAudio);
+				MediaUtils.playAudio(constraintAudio);
 			}
 		} else if (result == FormEntryController.ANSWER_REQUIRED_BUT_EMPTY) {
 			String txt = Localization.get("view.sending.RequiredQuestion");

File j2me/form-entry/src/org/javarosa/utilities/media/MediaUtils.java

 
 import javax.microedition.lcdui.Graphics;
 import javax.microedition.lcdui.Image;
+import javax.microedition.media.Manager;
+import javax.microedition.media.MediaException;
+import javax.microedition.media.Player;
 
 import org.javarosa.core.reference.InvalidReferenceException;
 import org.javarosa.core.reference.Reference;
 import org.javarosa.core.reference.ReferenceManager;
 
 public class MediaUtils {
+	
+    /////////AUDIO PLAYBACK
+	
+	//We only really every want to play audio through one centralized player, so we'll keep a static
+	//instance
+	private static Player audioPlayer;
+	
+	public static final int AUDIO_SUCCESS = 1;
+	public static final int AUDIO_NO_RESOURCE = 2;
+	public static final int AUDIO_ERROR = 3;
+	public static final int AUDIO_DISABLED = 4;
+	public static final int AUDIO_BUSY = 5;
+	public static final int AUDIO_NOT_RECOGNIZED = 6;
+	
+
+	
 	private static boolean IMAGE_DEBUG_MODE = false;
 	public static Image getImage(String URI){
 		if(URI != null && !URI.equals("")){
 			int[] dim = {h,w};
 			return dim;
 	  }
+	  
+	  
+	  
+		public static int playAudio(String jrRefURI) {
+			String curAudioURI = jrRefURI;
+			int retcode = AUDIO_SUCCESS;
+			try {
+				Reference curAudRef = ReferenceManager._().DeriveReference(curAudioURI);
+				String format = getFileFormat(curAudioURI);
+
+				if(format == null) return AUDIO_NOT_RECOGNIZED;
+				if(audioPlayer == null){
+					audioPlayer = Manager.createPlayer(curAudRef.getLocalURI());
+					audioPlayer.start();
+				}else{
+					audioPlayer.deallocate();
+					audioPlayer.close();
+					audioPlayer = Manager.createPlayer(curAudRef.getLocalURI());
+					audioPlayer.start();
+				}
+				
+			} catch (InvalidReferenceException ire) {
+				retcode = AUDIO_ERROR;
+				System.err.println("Invalid Reference Exception when attempting to play audio at URI:"+ curAudioURI + "Exception msg:"+ire.getMessage());
+			} catch (IOException ioe) {
+				retcode = AUDIO_ERROR;
+				System.err.println("IO Exception (input cannot be read) when attempting to play audio stream with URI:"+ curAudioURI + "Exception msg:"+ioe.getMessage());
+			} catch (MediaException e) {
+				retcode = AUDIO_ERROR;
+				System.err.println("Media format not supported! Uri: "+ curAudioURI + "Exception msg:"+e.getMessage());
+			}
+			return retcode;
+		}
+		
+		private static String getFileFormat(String fpath){
+//			Wave audio files: audio/x-wav
+//			AU audio files: audio/basic
+//			MP3 audio files: audio/mpeg
+//			MIDI files: audio/midi
+//			Tone sequences: audio/x-tone-seq
+//			MPEG video files: video/mpeg
+//			Audio 3GPP files (.3gp) audio/3gpp
+//			Audio AMR files (.amr) audio/amr
+//			Audio AMR (wideband) files (.awb) audio/amr-wb
+//			Audio MIDI files (.mid or .midi) audio/midi
+//			Audio MP3 files (.mp3) audio/mpeg
+//			Audio MP4 files (.mp4) audio/mp4
+//			Audio WAV files (.wav) audio/wav audio/x-wav
+			
+			if(fpath.indexOf(".mp3") > -1) return "audio/mp3";
+			if(fpath.indexOf(".wav") > -1) return "audio/x-wav";
+			if(fpath.indexOf(".amr") > -1) return "audio/amr";
+			if(fpath.indexOf(".awb") > -1) return "audio/amr-wb";
+			if(fpath.indexOf(".mp4") > -1) return "audio/mp4";
+			if(fpath.indexOf(".aac") > -1) return "audio/aac";
+			if(fpath.indexOf(".3gp") > -1) return "audio/3gpp";
+			if(fpath.indexOf(".au") > -1) return "audio/basic";
+			throw new RuntimeException("COULDN'T FIND FILE FORMAT");
+		}
+
+		
 }