From 7ad30dfc63338611ee738197ccdb6bdb78ec439d Mon Sep 17 00:00:00 2001 From: Joe Bowser Date: Tue, 8 Jun 2010 16:30:20 -0700 Subject: [PATCH] Removed PhoneGap.java, renaming to device and audio --- framework/src/com/phonegap/AudioHandler.java | 241 +++++------------- framework/src/com/phonegap/AudioPlayer.java | 193 ++++++++++++++ framework/src/com/phonegap/BrowserKey.java | 46 ++++ .../phonegap/{PhoneGap.java => Device.java} | 84 +----- framework/src/com/phonegap/DroidGap.java | 7 +- 5 files changed, 314 insertions(+), 257 deletions(-) create mode 100644 framework/src/com/phonegap/AudioPlayer.java create mode 100644 framework/src/com/phonegap/BrowserKey.java rename framework/src/com/phonegap/{PhoneGap.java => Device.java} (67%) diff --git a/framework/src/com/phonegap/AudioHandler.java b/framework/src/com/phonegap/AudioHandler.java index b971873a..349dea2b 100644 --- a/framework/src/com/phonegap/AudioHandler.java +++ b/framework/src/com/phonegap/AudioHandler.java @@ -1,193 +1,68 @@ package com.phonegap; -import java.io.File; -import java.io.IOException; - import android.content.Context; -import android.media.AudioManager; -import android.media.MediaPlayer; -import android.media.MediaPlayer.OnErrorListener; -import android.media.MediaRecorder; -import android.media.MediaPlayer.OnBufferingUpdateListener; -import android.media.MediaPlayer.OnCompletionListener; -import android.media.MediaPlayer.OnPreparedListener; -import android.util.Log; +import android.webkit.WebView; -public class AudioHandler implements OnCompletionListener, OnPreparedListener, OnErrorListener { - private MediaRecorder recorder; - private boolean isRecording = false; - MediaPlayer mPlayer; - private boolean isPlaying = false; - private String recording; - private String saveFile; - private Context mCtx; +public class AudioHandler { - public AudioHandler(String file, Context ctx) { - this.recording = file; - this.mCtx = ctx; - } + AudioPlayer audio; + WebView mAppView; + Context mCtx; - protected void startRecording(String file){ - if (!isRecording){ - saveFile=file; - recorder = new MediaRecorder(); - recorder.setAudioSource(MediaRecorder.AudioSource.MIC); - recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); - recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); - recorder.setOutputFile(this.recording); - try { - recorder.prepare(); - } catch (IllegalStateException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - isRecording = true; - recorder.start(); - } - } - - private void moveFile(String file) { - /* this is a hack to save the file as the specified name */ - File f = new File (this.recording); - f.renameTo(new File("/sdcard" + file)); - } - - protected void stopRecording(){ - try{ - if((recorder != null)&&(isRecording)) - { - isRecording = false; - recorder.stop(); - recorder.release(); - } - moveFile(saveFile); - } - catch (Exception e) - { - e.printStackTrace(); - } - } - - protected void startPlaying(String file) { - if (isPlaying==false) { - try { - mPlayer = new MediaPlayer(); - isPlaying=true; - Log.d("Audio startPlaying", "audio: " + file); - if (isStreaming(file)) - { - Log.d("AudioStartPlaying", "Streaming"); - // Streaming prepare async - mPlayer.setDataSource(file); - mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); - mPlayer.prepareAsync(); - } else { - Log.d("AudioStartPlaying", "File"); - // Not streaming prepare synchronous, abstract base directory - mPlayer.setDataSource("/sdcard/" + file); - mPlayer.prepare(); - } - mPlayer.setOnPreparedListener(this); - } catch (Exception e) - { - e.printStackTrace(); - } - } - } - - public void stopPlaying() { - if (isPlaying) { - mPlayer.stop(); - mPlayer.release(); - isPlaying=false; - } - } - - public void onCompletion(MediaPlayer mPlayer) { - mPlayer.stop(); - mPlayer.release(); - isPlaying=false; - } - - protected long getCurrentPosition() { - if (isPlaying) - { - return(mPlayer.getCurrentPosition()); - } else { return(-1); } - } - - private boolean isStreaming(String file) + AudioHandler(WebView view, Context ctx) { - if (file.contains("http://")) { - return true; - } else { - return false; - } - } - - protected long getDuration(String file) { - long duration = -2; - if (!isPlaying & !isStreaming(file)) { - try { - mPlayer = new MediaPlayer(); - mPlayer.setDataSource("/sdcard/" + file); - mPlayer.prepare(); - duration = mPlayer.getDuration(); - mPlayer.release(); - } catch (Exception e) { e.printStackTrace(); return(-3); } - } else - if (isPlaying & !isStreaming(file)) { - duration = mPlayer.getDuration(); - } else - if (isPlaying & isStreaming(file)) { - try { - duration = mPlayer.getDuration(); - } catch (Exception e) { e.printStackTrace(); return(-4); } - }else { return -1; } - return duration; + mAppView = view; + mCtx = ctx; + // YES, I know this is bad, but I can't do it the right way because Google didn't have the + // foresight to add android.os.environment.getExternalDataDirectory until Android 2.2 + audio = new AudioPlayer("/sdcard/tmprecording.mp3", mCtx); } - public void onPrepared(MediaPlayer mPlayer) { - if (isPlaying) { - mPlayer.setOnCompletionListener(this); - mPlayer.setOnBufferingUpdateListener(new OnBufferingUpdateListener() - { - public void onBufferingUpdate(MediaPlayer mPlayer, int percent) - { - /* TODO: call back, e.g. update outer progress bar */ - Log.d("AudioOnBufferingUpdate", "percent: " + percent); - } - }); - mPlayer.start(); - } - } - - public boolean onError(MediaPlayer mPlayer, int arg1, int arg2) { - Log.e("AUDIO onError", "error " + arg1 + " " + arg2); - return false; - } - - protected void setAudioOutputDevice(int output){ - // Changes the default audio output device to speaker or earpiece - AudioManager audiMgr = (AudioManager) mCtx.getSystemService(Context.AUDIO_SERVICE); - if (output == (2)) - audiMgr.setRouting(AudioManager.MODE_NORMAL, AudioManager.ROUTE_SPEAKER, AudioManager.ROUTE_ALL); - else if (output == (1)){ - audiMgr.setRouting(AudioManager.MODE_NORMAL, AudioManager.ROUTE_EARPIECE, AudioManager.ROUTE_ALL); - }else - Log.e("AudioHandler setAudioOutputDevice", " unknown output device"); - } - - protected int getAudioOutputDevice(){ - AudioManager audiMgr = (AudioManager) mCtx.getSystemService(Context.AUDIO_SERVICE); - if (audiMgr.getRouting(AudioManager.MODE_NORMAL) == AudioManager.ROUTE_EARPIECE) - return 1; - else if (audiMgr.getRouting(AudioManager.MODE_NORMAL) == AudioManager.ROUTE_SPEAKER) - return 2; - else - return -1; - } + /** + * AUDIO + * TODO: Basic functions done but needs more work on error handling and call backs, remove record hack + */ + + public void startRecordingAudio(String file) + { + /* for this to work the recording needs to be specified in the constructor, + * a hack to get around this, I'm moving the recording after it's complete + */ + audio.startRecording(file); + } + + public void stopRecordingAudio() + { + audio.stopRecording(); + } + + public void startPlayingAudio(String file) + { + audio.startPlaying(file); + } + + public void stopPlayingAudio() + { + audio.stopPlaying(); + } + + public long getCurrentPositionAudio() + { + System.out.println(audio.getCurrentPosition()); + return(audio.getCurrentPosition()); + } + + public long getDurationAudio(String file) + { + System.out.println(audio.getDuration(file)); + return(audio.getDuration(file)); + } + + public void setAudioOutputDevice(int output){ + audio.setAudioOutputDevice(output); + } + + public int getAudioOutputDevice(){ + return audio.getAudioOutputDevice(); + } } diff --git a/framework/src/com/phonegap/AudioPlayer.java b/framework/src/com/phonegap/AudioPlayer.java new file mode 100644 index 00000000..a52262b7 --- /dev/null +++ b/framework/src/com/phonegap/AudioPlayer.java @@ -0,0 +1,193 @@ +package com.phonegap; + +import java.io.File; +import java.io.IOException; + +import android.content.Context; +import android.media.AudioManager; +import android.media.MediaPlayer; +import android.media.MediaPlayer.OnErrorListener; +import android.media.MediaRecorder; +import android.media.MediaPlayer.OnBufferingUpdateListener; +import android.media.MediaPlayer.OnCompletionListener; +import android.media.MediaPlayer.OnPreparedListener; +import android.util.Log; + +public class AudioPlayer implements OnCompletionListener, OnPreparedListener, OnErrorListener { + private MediaRecorder recorder; + private boolean isRecording = false; + MediaPlayer mPlayer; + private boolean isPlaying = false; + private String recording; + private String saveFile; + private Context mCtx; + + public AudioPlayer(String file, Context ctx) { + this.recording = file; + this.mCtx = ctx; + } + + protected void startRecording(String file){ + if (!isRecording){ + saveFile=file; + recorder = new MediaRecorder(); + recorder.setAudioSource(MediaRecorder.AudioSource.MIC); + recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); + recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); + recorder.setOutputFile(this.recording); + try { + recorder.prepare(); + } catch (IllegalStateException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + isRecording = true; + recorder.start(); + } + } + + private void moveFile(String file) { + /* this is a hack to save the file as the specified name */ + File f = new File (this.recording); + f.renameTo(new File("/sdcard" + file)); + } + + protected void stopRecording(){ + try{ + if((recorder != null)&&(isRecording)) + { + isRecording = false; + recorder.stop(); + recorder.release(); + } + moveFile(saveFile); + } + catch (Exception e) + { + e.printStackTrace(); + } + } + + protected void startPlaying(String file) { + if (isPlaying==false) { + try { + mPlayer = new MediaPlayer(); + isPlaying=true; + Log.d("Audio startPlaying", "audio: " + file); + if (isStreaming(file)) + { + Log.d("AudioStartPlaying", "Streaming"); + // Streaming prepare async + mPlayer.setDataSource(file); + mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); + mPlayer.prepareAsync(); + } else { + Log.d("AudioStartPlaying", "File"); + // Not streaming prepare synchronous, abstract base directory + mPlayer.setDataSource("/sdcard/" + file); + mPlayer.prepare(); + } + mPlayer.setOnPreparedListener(this); + } catch (Exception e) + { + e.printStackTrace(); + } + } + } + + public void stopPlaying() { + if (isPlaying) { + mPlayer.stop(); + mPlayer.release(); + isPlaying=false; + } + } + + public void onCompletion(MediaPlayer mPlayer) { + mPlayer.stop(); + mPlayer.release(); + isPlaying=false; + } + + protected long getCurrentPosition() { + if (isPlaying) + { + return(mPlayer.getCurrentPosition()); + } else { return(-1); } + } + + private boolean isStreaming(String file) + { + if (file.contains("http://")) { + return true; + } else { + return false; + } + } + + protected long getDuration(String file) { + long duration = -2; + if (!isPlaying & !isStreaming(file)) { + try { + mPlayer = new MediaPlayer(); + mPlayer.setDataSource("/sdcard/" + file); + mPlayer.prepare(); + duration = mPlayer.getDuration(); + mPlayer.release(); + } catch (Exception e) { e.printStackTrace(); return(-3); } + } else + if (isPlaying & !isStreaming(file)) { + duration = mPlayer.getDuration(); + } else + if (isPlaying & isStreaming(file)) { + try { + duration = mPlayer.getDuration(); + } catch (Exception e) { e.printStackTrace(); return(-4); } + }else { return -1; } + return duration; + } + + public void onPrepared(MediaPlayer mPlayer) { + if (isPlaying) { + mPlayer.setOnCompletionListener(this); + mPlayer.setOnBufferingUpdateListener(new OnBufferingUpdateListener() + { + public void onBufferingUpdate(MediaPlayer mPlayer, int percent) + { + /* TODO: call back, e.g. update outer progress bar */ + Log.d("AudioOnBufferingUpdate", "percent: " + percent); + } + }); + mPlayer.start(); + } + } + + public boolean onError(MediaPlayer mPlayer, int arg1, int arg2) { + Log.e("AUDIO onError", "error " + arg1 + " " + arg2); + return false; + } + + protected void setAudioOutputDevice(int output){ + // Changes the default audio output device to speaker or earpiece + AudioManager audiMgr = (AudioManager) mCtx.getSystemService(Context.AUDIO_SERVICE); + if (output == (2)) + audiMgr.setRouting(AudioManager.MODE_NORMAL, AudioManager.ROUTE_SPEAKER, AudioManager.ROUTE_ALL); + else if (output == (1)){ + audiMgr.setRouting(AudioManager.MODE_NORMAL, AudioManager.ROUTE_EARPIECE, AudioManager.ROUTE_ALL); + }else + Log.e("AudioHandler setAudioOutputDevice", " unknown output device"); + } + + protected int getAudioOutputDevice(){ + AudioManager audiMgr = (AudioManager) mCtx.getSystemService(Context.AUDIO_SERVICE); + if (audiMgr.getRouting(AudioManager.MODE_NORMAL) == AudioManager.ROUTE_EARPIECE) + return 1; + else if (audiMgr.getRouting(AudioManager.MODE_NORMAL) == AudioManager.ROUTE_SPEAKER) + return 2; + else + return -1; + } +} diff --git a/framework/src/com/phonegap/BrowserKey.java b/framework/src/com/phonegap/BrowserKey.java new file mode 100644 index 00000000..2079e869 --- /dev/null +++ b/framework/src/com/phonegap/BrowserKey.java @@ -0,0 +1,46 @@ +package com.phonegap; + +import android.app.Activity; +import android.util.Log; +import android.webkit.WebView; + +/* + * This class literally exists to protect DroidGap from Javascript directly. + * + * + */ + + +public class BrowserKey { + + DroidGap mAction; + boolean bound; + + WebView mView; + + BrowserKey(WebView view, DroidGap action) + { + bound = false; + } + + public void override() + { + Log.d("PhoneGap", "WARNING: Back Button Default Behaviour will be overridden. The backKeyDown event will be fired!"); + bound = true; + } + + public boolean isBound() + { + return bound; + } + + public void reset() + { + bound = false; + } + + public void exitApp() + { + mAction.finish(); + } +} diff --git a/framework/src/com/phonegap/PhoneGap.java b/framework/src/com/phonegap/Device.java similarity index 67% rename from framework/src/com/phonegap/PhoneGap.java rename to framework/src/com/phonegap/Device.java index 1141e708..074f72f5 100644 --- a/framework/src/com/phonegap/PhoneGap.java +++ b/framework/src/com/phonegap/Device.java @@ -25,8 +25,6 @@ package com.phonegap; import java.util.TimeZone; import android.content.Context; -import android.content.IntentFilter; -import android.hardware.SensorManager; import android.net.Uri; import android.os.Vibrator; import android.provider.Settings; @@ -35,24 +33,23 @@ import android.webkit.WebView; import android.media.Ringtone; import android.media.RingtoneManager; -public class PhoneGap{ +public class Device{ private static final String LOG_TAG = "PhoneGap"; /* * UUID, version and availability */ public boolean droid = true; - public static String version = "0.9.99999"; + public static String version = "0.91"; public static String platform = "Android"; public static String uuid; private Context mCtx; private WebView mAppView; - AudioHandler audio; + AudioPlayer audio; - public PhoneGap(WebView appView, Context ctx) { + public Device(WebView appView, Context ctx) { this.mCtx = ctx; this.mAppView = appView; - audio = new AudioHandler("/sdcard/tmprecording.mp3", ctx); uuid = getUuid(); } @@ -87,22 +84,25 @@ public class PhoneGap{ return uuid; } - public String getLine1Number(){ + public String getLine1Number(){ TelephonyManager operator = (TelephonyManager)mCtx.getSystemService(Context.TELEPHONY_SERVICE); return operator.getLine1Number(); - } + } + public String getDeviceId(){ TelephonyManager operator = (TelephonyManager)mCtx.getSystemService(Context.TELEPHONY_SERVICE); return operator.getDeviceId(); - } + } + public String getSimSerialNumber(){ TelephonyManager operator = (TelephonyManager)mCtx.getSystemService(Context.TELEPHONY_SERVICE); return operator.getSimSerialNumber(); } - public String getSubscriberId(){ + + public String getSubscriberId(){ TelephonyManager operator = (TelephonyManager)mCtx.getSystemService(Context.TELEPHONY_SERVICE); return operator.getSubscriberId(); - } + } public String getModel() { @@ -129,66 +129,6 @@ public class PhoneGap{ { return version; } - - - public void httpGet(String url, String file) - /** - * grabs a file from specified url and saves it to a name and location - * the base directory /sdcard is abstracted so that paths may be the same from one mobile OS to another - * TODO: JavaScript call backs and error handling - */ - { - HttpHandler http = new HttpHandler(); - http.get(url, file); - } - - /** - * AUDIO - * TODO: Basic functions done but needs more work on error handling and call backs, remove record hack - */ - - public void startRecordingAudio(String file) - { - /* for this to work the recording needs to be specified in the constructor, - * a hack to get around this, I'm moving the recording after it's complete - */ - audio.startRecording(file); - } - - public void stopRecordingAudio() - { - audio.stopRecording(); - } - - public void startPlayingAudio(String file) - { - audio.startPlaying(file); - } - - public void stopPlayingAudio() - { - audio.stopPlaying(); - } - - public long getCurrentPositionAudio() - { - System.out.println(audio.getCurrentPosition()); - return(audio.getCurrentPosition()); - } - - public long getDurationAudio(String file) - { - System.out.println(audio.getDuration(file)); - return(audio.getDuration(file)); - } - - public void setAudioOutputDevice(int output){ - audio.setAudioOutputDevice(output); - } - - public int getAudioOutputDevice(){ - return audio.getAudioOutputDevice(); - } public String getTimeZoneID() { TimeZone tz = TimeZone.getDefault(); diff --git a/framework/src/com/phonegap/DroidGap.java b/framework/src/com/phonegap/DroidGap.java index 6a746e72..299710bc 100644 --- a/framework/src/com/phonegap/DroidGap.java +++ b/framework/src/com/phonegap/DroidGap.java @@ -53,7 +53,7 @@ public class DroidGap extends Activity { protected WebView appView; private LinearLayout root; - private PhoneGap gap; + private Device gap; private GeoBroker geo; private AccelBroker accel; private CameraLauncher launcher; @@ -64,6 +64,7 @@ public class DroidGap extends Activity { private Storage cupcakeStorage; private CryptoHandler crypto; private BrowserKey mKey; + private AudioHandler audio; /** Called when the activity is first created. */ @Override @@ -134,7 +135,7 @@ public class DroidGap extends Activity { private void bindBrowser(WebView appView) { - gap = new PhoneGap(appView, this); + gap = new Device(appView, this); geo = new GeoBroker(appView, this); accel = new AccelBroker(appView, this); launcher = new CameraLauncher(appView, this); @@ -144,6 +145,7 @@ public class DroidGap extends Activity { mCompass = new CompassListener(appView, this); crypto = new CryptoHandler(appView); mKey = new BrowserKey(appView, this); + audio = new AudioHandler(appView, this); // This creates the new javascript interfaces for PhoneGap appView.addJavascriptInterface(gap, "DroidGap"); @@ -156,6 +158,7 @@ public class DroidGap extends Activity { appView.addJavascriptInterface(mCompass, "CompassHook"); appView.addJavascriptInterface(crypto, "GapCrypto"); appView.addJavascriptInterface(mKey, "BackButton"); + appView.addJavascriptInterface(audio, "GapAudio"); if (android.os.Build.VERSION.RELEASE.startsWith("1."))