Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Can now hear audio messages on phone

  • Loading branch information...
commit 81c7dc84e4f9ccb9444253e7d11628bf3b64d220 1 parent 42a0a64
Sam Karp authored
2  android/res/layout/list_item.xml
View
@@ -2,7 +2,7 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" android:padding="2dip"
- android:onClick="onPlaybackClick" style="@style/default_style">
+ style="@style/default_style">
<TableLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/headerTable"
62 android/src/gov/nasa/arc/geocam/talk/activity/GeoCamTalkActivity.java
View
@@ -3,12 +3,22 @@
import gov.nasa.arc.geocam.talk.R;
import gov.nasa.arc.geocam.talk.UIUtils;
import gov.nasa.arc.geocam.talk.bean.GeoCamTalkMessage;
+import gov.nasa.arc.geocam.talk.exception.AuthenticationFailedException;
+import gov.nasa.arc.geocam.talk.service.IAudioPlayer;
import gov.nasa.arc.geocam.talk.service.IDjangoTalk;
import gov.nasa.arc.geocam.talk.service.IMessageStore;
+import gov.nasa.arc.geocam.talk.service.ISiteAuth;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.sql.SQLException;
import java.util.List;
+import org.apache.http.client.ClientProtocolException;
+
import roboguice.activity.RoboActivity;
+import roboguice.inject.InjectResource;
import roboguice.inject.InjectView;
import android.content.Intent;
import android.os.Bundle;
@@ -17,6 +27,7 @@
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
+import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
@@ -28,10 +39,16 @@
IDjangoTalk djangoTalk;
@InjectView(R.id.TalkListView)
ListView talkListView;
+ @InjectResource(R.string.url_server_root)
+ String serverRootUrl;
@Inject
GeoCamTalkMessageArrayAdapter adapter;
@Inject
IMessageStore messageStore;
+ @Inject
+ ISiteAuth siteAuth;
+ @Inject
+ IAudioPlayer player;
/** Called when the activity is first created. */
@Override
@@ -40,6 +57,47 @@ public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main);
List<GeoCamTalkMessage> talkMessages = null;
+
+ talkListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
+ @Override
+ public void onItemClick (AdapterView<?> parentView, View childView, int position, long id) {
+ GeoCamTalkMessage msg = adapter.getTalkMessage(position);
+
+ try
+ {
+ String audioUrl = msg.getAudioUrl();
+ //No audio recorded with message
+ if (msg.getAudio() == null && audioUrl.equals("")) {
+ Toast.makeText(getApplicationContext(), "This message has no audio",
+ Toast.LENGTH_SHORT).show();
+ }
+ //We have audio, but not locally
+ else if (msg.getAudio() == null && !audioUrl.equals(""))
+ {
+ String localFileName = siteAuth.getAudioFile(audioUrl, null);
+ player.startPlaying(localFileName);
+ File audioFile = new File(localFileName);
+ int length = (int) audioFile.length();
+ byte[] audioBytes = new byte[(int) length];
+
+ FileInputStream fis = new FileInputStream(audioFile);
+ fis.read(audioBytes, 0, length);
+ msg.setAudio(audioBytes);
+ }
+ // We have audio locally
+ else
+ {
+ Toast.makeText(getApplicationContext(), "This message LOCAL audio",
+ Toast.LENGTH_SHORT).show();
+ player.startPlaying(msg.getAudio());
+ }
+ } catch (Exception e)
+ {
+ e.printStackTrace();
+ }
+ }
+ });
+
try {
talkMessages = messageStore.getAllMessages();
@@ -89,8 +147,4 @@ public void onCreateTalkClick(View v) {
UIUtils.createTalkMessage(this);
}
- public void onPlaybackClick(View v) {
- Toast.makeText(this.getApplicationContext(), "Clicked to listen for audio",
- Toast.LENGTH_SHORT).show();
- }
}
5 android/src/gov/nasa/arc/geocam/talk/activity/GeoCamTalkCreateActivity.java
View
@@ -111,6 +111,11 @@ public void onSendClick(View v) {
if (filename != null) {
message.setAudio(createByteArray());
+ } else
+ {
+ //TODO: Is this the best way to prevent pulling null AudioUrl on click
+ // before django update? (still local)
+ message.setAudioUrl("");
}
try {
5 android/src/gov/nasa/arc/geocam/talk/activity/GeoCamTalkMessageArrayAdapter.java
View
@@ -35,6 +35,11 @@ public void setTalkMessages(List<GeoCamTalkMessage> talkMessages)
add(m);
}
}
+
+ public GeoCamTalkMessage getTalkMessage(int position){
+ GeoCamTalkMessage msg = getItem(position);
+ return msg;
+ }
@Override
public View getView(int position, View convertView, ViewGroup parent) {
10 android/src/gov/nasa/arc/geocam/talk/bean/GeoCamTalkMessage.java
View
@@ -52,6 +52,9 @@
@DatabaseField(dataType = DataType.BYTE_ARRAY)
private byte[] audio;
+ @DatabaseField
+ private String audioUrl;
+
public int getMessageId() {
if (this.messageId == null) {
return -1;
@@ -122,6 +125,13 @@ public void setAudio(byte[] audio) {
this.audio = audio;
}
+ public String getAudioUrl() {
+ return audioUrl;
+ }
+ public void setAudioUrl(String audioUrl) {
+ this.audioUrl = audioUrl;
+ }
+
public void setSynchronized(boolean isSynchronized) {
this.isSynchronized = isSynchronized;
}
15 android/src/gov/nasa/arc/geocam/talk/service/AudioPlayer.java
View
@@ -3,11 +3,14 @@
import gov.nasa.arc.geocam.talk.R;
+import java.io.File;
import java.io.FileInputStream;
+import java.io.FileOutputStream;
import java.io.IOException;
import android.content.Context;
import android.media.MediaPlayer;
+import android.os.Environment;
import com.google.inject.Inject;
@@ -51,4 +54,16 @@ public void playBeepB() {
MediaPlayer mp = MediaPlayer.create(context, R.raw.beep_b);
mp.start();
}
+
+ @Override
+ public void startPlaying(byte[] soundByte) throws IllegalArgumentException, IllegalStateException, IOException {
+
+ File tempAudio = File.createTempFile("tempAudioMessage", ".mp4", context.getFilesDir());
+ //tempAudio.deleteOnExit();
+ FileOutputStream fos = new FileOutputStream(tempAudio);
+ fos.write(soundByte);
+ fos.close();
+
+ startPlaying(tempAudio.getAbsolutePath());
+ }
}
1  android/src/gov/nasa/arc/geocam/talk/service/IAudioPlayer.java
View
@@ -3,6 +3,7 @@
import java.io.IOException;
public interface IAudioPlayer {
+ public void startPlaying(byte[] soundByte) throws IllegalArgumentException, IllegalStateException, IOException;
public void startPlaying(String filename) throws IllegalArgumentException, IllegalStateException, IOException;
void stopPlaying();
void playBeepA();
3  android/src/gov/nasa/arc/geocam/talk/service/ISiteAuth.java
View
@@ -21,6 +21,9 @@ public int post(String relativePath, Map<String, String> params, byte[] audioByt
public String get(String relativePath, Map<String, String> params)
throws AuthenticationFailedException, IOException, ClientProtocolException;
+
+ public String getAudioFile(String relativePath, Map<String, String> params)
+ throws AuthenticationFailedException, IOException, ClientProtocolException;
}
class AuthorizationFailedException extends Exception {
28 android/src/gov/nasa/arc/geocam/talk/service/SiteAuthCookie.java
View
@@ -4,6 +4,9 @@
import gov.nasa.arc.geocam.talk.exception.AuthenticationFailedException;
import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@@ -33,7 +36,9 @@
import roboguice.inject.InjectResource;
import android.content.Context;
import android.content.SharedPreferences;
+import android.media.MediaPlayer;
import android.preference.PreferenceManager;
+import android.util.Log;
import com.google.inject.Inject;
@@ -146,6 +151,29 @@ public String get(String relativePath, Map<String, String> params)
br.close();
return sb.toString();
}
+
+ @Override
+ public String getAudioFile(String relativePath, Map<String, String> params)
+ throws AuthenticationFailedException, IOException, ClientProtocolException {
+ ensureAuthenticated();
+
+ httpClient = new DefaultHttpClient();
+
+ HttpPost post = new HttpPost(this.serverRootUrl + relativePath);
+ // TODO: add param parsing and query string construction as necessary
+
+ httpClient.getCookieStore().addCookie(sessionIdCookie);
+
+ HttpResponse r = httpClient.execute(post);
+
+ if(r.getFirstHeader("Content-Type").getValue().contains("mp4"))
+ {
+ FileOutputStream ostream = new FileOutputStream(context.getFilesDir().toString() + "/tempfile.mp4");
+ r.getEntity().writeTo(ostream);
+ }
+ return (context.getFilesDir().toString() + "/tempfile.mp4");
+
+ }
private void ensureAuthenticated() throws AuthenticationFailedException,
ClientProtocolException, IOException {
Please sign in to comment.
Something went wrong with that request. Please try again.