Permalink
Browse files

Added audio file posting capability

  • Loading branch information...
1 parent feae430 commit bebca5c3970e3ae8e1b9ddaba53fb9134cda39e4 Sam Karp committed Apr 8, 2011
@@ -58,7 +58,7 @@ public void setAuth(String username, String password) {
}
@Override
- public void createTalkMessage(GeoCamTalkMessage message)
+ public void createTalkMessage(GeoCamTalkMessage message, String filename)
throws ClientProtocolException, AuthenticationFailedException,
IOException {
// TODO Auto-generated method stub
@@ -11,6 +11,7 @@
import gov.nasa.arc.geocam.talk.service.SiteAuthInterface;
import gov.nasa.arc.geocam.talk.test.GeoCamTestCase;
+import java.io.File;
import java.util.ArrayList;
import java.util.List;
@@ -46,23 +47,23 @@ public void shouldEnsureGetTalkMessagesReturnsMessages() throws Exception {
@Test
public void shouldEnsureCreateTalkMessagePostsTalkMessage() throws Exception
{
- DjangoTalkImplementation memoImpl = new DjangoTalkImplementation();
+ DjangoTalkImplementation talkImpl = new DjangoTalkImplementation();
DjangoTalkJsonConverterInterface jsonConv =
mock(DjangoTalkJsonConverterInterface.class);
-
+
SiteAuthInterface siteauth =
mock(SiteAuthInterface.class);
- when(siteauth.post(anyString(), anyMap())).thenReturn(200);
- setHiddenField(memoImpl, "siteAuthImplementation", siteauth);
+ when(siteauth.post(anyString(), anyMap(), anyString())).thenReturn(200);
+ setHiddenField(talkImpl, "siteAuthImplementation", siteauth);
when(jsonConv.serialize((GeoCamTalkMessage)anyObject())).thenReturn("");
- setHiddenField(memoImpl, "jsonConverter", jsonConv);
+ setHiddenField(talkImpl, "jsonConverter", jsonConv);
// act
- memoImpl.createTalkMessage(new GeoCamTalkMessage());
+ talkImpl.createTalkMessage(new GeoCamTalkMessage(), null);
// assert
- verify(siteauth).post(anyString(), anyMap());
+ verify(siteauth).post(anyString(), anyMap(), anyString());
}
}
@@ -4,6 +4,7 @@
import gov.nasa.arc.geocam.talk.activity.GeoCamTalkActivity;
import android.content.Context;
import android.content.Intent;
+import android.widget.Toast;
public class UIUtils {
@@ -21,4 +22,17 @@ public static void createTalkMessage(Context context){
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
}
+
+ public static void displayException(Context context, Exception e, String additionalMessage)
+ {
+ StringBuilder sb = new StringBuilder();
+ if(additionalMessage != null)
+ {
+ sb.append(additionalMessage + ": ");
+ }
+ if(e.getLocalizedMessage() != null){
+ sb.append(e.getLocalizedMessage());
+ }
+ Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();
+ }
}
@@ -1,13 +1,20 @@
package gov.nasa.arc.geocam.talk.activity;
+import java.util.Date;
+
import com.google.inject.Inject;
+import gov.nasa.arc.geocam.talk.exception.AuthenticationFailedException;
+import gov.nasa.arc.geocam.talk.service.DjangoTalkInterface;
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.service.AudioPlayerInterface;
import gov.nasa.arc.geocam.talk.service.AudioRecorderInterface;
+import gov.nasa.arc.geocam.talk.service.DjangoTalkInterface;
import roboguice.activity.RoboActivity;
import roboguice.inject.InjectView;
+import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
@@ -19,7 +26,10 @@
@InjectView(R.id.newTalkTextInput)EditText newTalkTextView;
@Inject AudioRecorderInterface recorder;
@Inject AudioPlayerInterface player;
-
+ @Inject DjangoTalkInterface djangoTalkInterface;
+
+ private String filename = null;
+
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
@@ -54,7 +64,7 @@ public void onRecordClick(View v){
Log.i("TALKCREATE", "STOP recording now.");
try {
player.playBeepB();
- String filename = recorder.stopRecording();
+ filename = recorder.stopRecording();
Toast.makeText(this, "Recording stopped", Toast.LENGTH_SHORT).show();
player.startPlaying(filename);
//recorder.toggleRecordingStatus();
@@ -82,6 +92,21 @@ public void onRecordClick(View v){
public void onSendClick(View v){
CharSequence text = newTalkTextView.getText();
int duration = Toast.LENGTH_SHORT;
- Toast.makeText(this, text, duration).show();
+ Toast.makeText(this, text, duration).show();
+
+ GeoCamTalkMessage message = new GeoCamTalkMessage();
+ message.setContent(text.toString());
+ message.setContentTimestamp(new Date());
+
+ //filename = "this.mp4";
+
+ try {
+ djangoTalkInterface.createTalkMessage(message, filename);
+ UIUtils.goHome(this);
+ } catch (AuthenticationFailedException e) {
+ UIUtils.displayException(this, e, "Could not authenticate with the server");
+ } catch (Exception e) {
+ UIUtils.displayException(this, e, "Communication with the server failed");
+ }
}
}
@@ -51,10 +51,10 @@ public void setAuth(String username, String password) {
}
@Override
- public void createTalkMessage(GeoCamTalkMessage message) throws ClientProtocolException, AuthenticationFailedException, IOException {
+ public void createTalkMessage(GeoCamTalkMessage message, String filename) throws ClientProtocolException, AuthenticationFailedException, IOException {
HashMap<String,String>map = new HashMap<String,String>();
map.put("message", jsonConverter.serialize(message));
- int responseCode = siteAuthImplementation.post(createTalkMessageJson, map);
+ int responseCode = siteAuthImplementation.post(createTalkMessageJson, map, filename);
if(responseCode != 200)
{
throw new ClientProtocolException("Message could not be created (HTTP error "+responseCode+")");
@@ -11,7 +11,7 @@
public interface DjangoTalkInterface {
public List<GeoCamTalkMessage> getTalkMessages();
public void setAuth(String username, String password);
- void createTalkMessage(GeoCamTalkMessage message)
+ void createTalkMessage(GeoCamTalkMessage message, String filename)
throws ClientProtocolException, AuthenticationFailedException,
IOException;
}
@@ -4,6 +4,7 @@
import gov.nasa.arc.geocam.talk.R;
import java.io.BufferedReader;
+import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@@ -12,13 +13,18 @@
import java.util.List;
import java.util.Map;
+import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.cookie.Cookie;
+import org.apache.http.entity.mime.HttpMultipartMode;
+import org.apache.http.entity.mime.MultipartEntity;
+import org.apache.http.entity.mime.content.FileBody;
+import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpParams;
@@ -50,7 +56,7 @@ public void setAuth(String username, String password) {
}
@Override
- public int post(String relativePath, Map<String, String> params)
+ public int post(String relativePath, Map<String, String> params, String filename)
throws AuthenticationFailedException, IOException,
ClientProtocolException {
ensureAuthenticated();
@@ -65,13 +71,21 @@ public int post(String relativePath, Map<String, String> params)
if(params != null)
{
- List<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>();
+ MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
for(String key:params.keySet())
{
- nameValuePairs.add(new BasicNameValuePair(key, params.get(key)));
+ entity.addPart(key, new StringBody(params.get(key)));
}
+ if (filename != null)
+ {
+ File audioFile = new File(filename);
+ FileBody audioBin = new FileBody(audioFile);
+ entity.addPart("audio", audioBin);
+ }
+
+ post.setEntity(entity);
+
- post.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.ASCII));
}
httpClient.getCookieStore().addCookie(sessionIdCookie);
@@ -10,7 +10,7 @@
public interface SiteAuthInterface {
public void setRoot(String siteRoot);
public void setAuth(String username, String password);
- public int post(String relativePath, Map<String, String> params)
+ public int post(String relativePath, Map<String, String> params, String filename)
throws AuthenticationFailedException, IOException, ClientProtocolException;
public String get(String relativePath, Map<String, String> params)
throws AuthenticationFailedException, IOException, ClientProtocolException;

0 comments on commit bebca5c

Please sign in to comment.