record voice and attachment refactor

Daniel Gultsch created

Change summary

src/main/java/eu/siacs/conversations/entities/DownloadableFile.java |  16 
src/main/java/eu/siacs/conversations/ui/ConversationActivity.java   | 112 
src/main/java/eu/siacs/conversations/ui/ShareWithActivity.java      |  13 
src/main/res/menu/attachment_choices.xml                            |   5 
src/main/res/values/strings.xml                                     |   1 
5 files changed, 73 insertions(+), 74 deletions(-)

Detailed changes

src/main/java/eu/siacs/conversations/entities/DownloadableFile.java 🔗

@@ -56,12 +56,16 @@ public class DownloadableFile extends File {
 
 	public String getMimeType() {
 		String path = this.getAbsolutePath();
-		String mime = URLConnection.guessContentTypeFromName(path);
-		if (mime != null) {
-			return mime;
-		} else if (mime == null && path.endsWith(".webp")) {
-			return "image/webp";
-		} else {
+		try {
+			String mime = URLConnection.guessContentTypeFromName(path.replace("#",""));
+			if (mime != null) {
+				return mime;
+			} else if (mime == null && path.endsWith(".webp")) {
+				return "image/webp";
+			} else {
+				return "";
+			}
+		} catch (final StringIndexOutOfBoundsException e) {
 			return "";
 		}
 	}

src/main/java/eu/siacs/conversations/ui/ConversationActivity.java 🔗

@@ -9,6 +9,7 @@ import android.content.DialogInterface;
 import android.content.DialogInterface.OnClickListener;
 import android.content.Intent;
 import android.content.IntentSender.SendIntentException;
+import android.media.MediaActionSound;
 import android.net.Uri;
 import android.os.Bundle;
 import android.os.SystemClock;
@@ -52,19 +53,14 @@ public class ConversationActivity extends XmppActivity
 	public static final String CONVERSATION = "conversationUuid";
 	public static final String TEXT = "text";
 	public static final String NICK = "nick";
-	public static final String PRESENCE = "eu.siacs.conversations.presence";
 
 	public static final int REQUEST_SEND_MESSAGE = 0x0201;
 	public static final int REQUEST_DECRYPT_PGP = 0x0202;
 	public static final int REQUEST_ENCRYPT_MESSAGE = 0x0207;
-	private static final int REQUEST_ATTACH_IMAGE_DIALOG = 0x0203;
-	private static final int REQUEST_IMAGE_CAPTURE = 0x0204;
-	private static final int REQUEST_RECORD_AUDIO = 0x0205;
-	private static final int REQUEST_SEND_PGP_IMAGE = 0x0206;
-	private static final int REQUEST_ATTACH_FILE_DIALOG = 0x0208;
 	private static final int ATTACHMENT_CHOICE_CHOOSE_IMAGE = 0x0301;
 	private static final int ATTACHMENT_CHOICE_TAKE_PHOTO = 0x0302;
 	private static final int ATTACHMENT_CHOICE_CHOOSE_FILE = 0x0303;
+	private static final int ATTACHMENT_CHOICE_RECORD_VOICE = 0x0304;
 	private static final String STATE_OPEN_CONVERSATION = "state_open_conversation";
 	private static final String STATE_PANEL_OPEN = "state_panel_open";
 	private static final String STATE_PENDING_URI = "state_pending_uri";
@@ -100,10 +96,6 @@ public class ConversationActivity extends XmppActivity
 		this.mSelectedConversation = conversation;
 	}
 
-	public ListView getConversationListView() {
-		return this.listView;
-	}
-
 	public void showConversationsOverview() {
 		if (mContentView instanceof SlidingPaneLayout) {
 			SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
@@ -346,33 +338,37 @@ public class ConversationActivity extends XmppActivity
 
 			@Override
 			public void onPresenceSelected() {
-				if (attachmentChoice == ATTACHMENT_CHOICE_TAKE_PHOTO) {
-					mPendingImageUri = xmppConnectionService.getFileBackend()
-						.getTakePhotoUri();
-					Intent takePictureIntent = new Intent(
-							MediaStore.ACTION_IMAGE_CAPTURE);
-					takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
-							mPendingImageUri);
-					if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
-						startActivityForResult(takePictureIntent,
-								REQUEST_IMAGE_CAPTURE);
+				Intent intent = new Intent();
+				boolean chooser = false;
+				switch (attachmentChoice) {
+					case ATTACHMENT_CHOICE_CHOOSE_IMAGE:
+						intent.setAction(Intent.ACTION_GET_CONTENT);
+						intent.setType("image/*");
+						chooser = true;
+						break;
+					case ATTACHMENT_CHOICE_TAKE_PHOTO:
+						mPendingImageUri = xmppConnectionService.getFileBackend().getTakePhotoUri();
+						intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
+						intent.putExtra(MediaStore.EXTRA_OUTPUT,mPendingImageUri);
+						break;
+					case ATTACHMENT_CHOICE_CHOOSE_FILE:
+						chooser = true;
+						intent.setType("*/*");
+						intent.addCategory(Intent.CATEGORY_OPENABLE);
+						intent.setAction(Intent.ACTION_GET_CONTENT);
+						break;
+					case ATTACHMENT_CHOICE_RECORD_VOICE:
+						intent.setAction(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
+						break;
+				}
+				if (intent.resolveActivity(getPackageManager()) != null) {
+					if (chooser) {
+						startActivityForResult(
+								Intent.createChooser(intent,getString(R.string.perform_action_with)),
+								attachmentChoice);
+					} else {
+						startActivityForResult(intent, attachmentChoice);
 					}
-				} else if (attachmentChoice == ATTACHMENT_CHOICE_CHOOSE_IMAGE) {
-					Intent attachFileIntent = new Intent();
-					attachFileIntent.setType("image/*");
-					attachFileIntent.setAction(Intent.ACTION_GET_CONTENT);
-					Intent chooser = Intent.createChooser(attachFileIntent,
-							getString(R.string.attach_file));
-					startActivityForResult(chooser, REQUEST_ATTACH_IMAGE_DIALOG);
-				} else if (attachmentChoice == ATTACHMENT_CHOICE_CHOOSE_FILE) {
-					Intent attachFileIntent = new Intent();
-					//attachFileIntent.setType("file/*");
-					attachFileIntent.setType("*/*");
-					attachFileIntent.addCategory(Intent.CATEGORY_OPENABLE);
-					attachFileIntent.setAction(Intent.ACTION_GET_CONTENT);
-					Intent chooser = Intent.createChooser(attachFileIntent,
-							getString(R.string.attach_file));
-					startActivityForResult(chooser, REQUEST_ATTACH_FILE_DIALOG);
 				}
 			}
 		});
@@ -541,8 +537,10 @@ public class ConversationActivity extends XmppActivity
 		}
 		PopupMenu attachFilePopup = new PopupMenu(this, menuAttachFile);
 		attachFilePopup.inflate(R.menu.attachment_choices);
-		attachFilePopup
-			.setOnMenuItemClickListener(new OnMenuItemClickListener() {
+		if (new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION).resolveActivity(getPackageManager()) == null) {
+			attachFilePopup.getMenu().findItem(R.id.attach_record_voice).setVisible(false);
+		}
+		attachFilePopup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
 
 				@Override
 				public boolean onMenuItemClick(MenuItem item) {
@@ -553,9 +551,12 @@ public class ConversationActivity extends XmppActivity
 						case R.id.attach_take_picture:
 							attachFile(ATTACHMENT_CHOICE_TAKE_PHOTO);
 							break;
-						case R.id.attach_record_voice:
+						case R.id.attach_choose_file:
 							attachFile(ATTACHMENT_CHOICE_CHOOSE_FILE);
 							break;
+						case R.id.attach_record_voice:
+							attachFile(ATTACHMENT_CHOICE_RECORD_VOICE);
+							break;
 					}
 					return false;
 				}
@@ -864,44 +865,29 @@ public class ConversationActivity extends XmppActivity
 			if (requestCode == REQUEST_DECRYPT_PGP) {
 				mConversationFragment.hideSnackbar();
 				mConversationFragment.updateMessages();
-			} else if (requestCode == REQUEST_ATTACH_IMAGE_DIALOG) {
+			} else if (requestCode == ATTACHMENT_CHOICE_CHOOSE_IMAGE) {
 				mPendingImageUri = data.getData();
 				if (xmppConnectionServiceBound) {
-					attachImageToConversation(getSelectedConversation(),
-							mPendingImageUri);
+					attachImageToConversation(getSelectedConversation(),mPendingImageUri);
 					mPendingImageUri = null;
 				}
-			} else if (requestCode == REQUEST_ATTACH_FILE_DIALOG) {
+			} else if (requestCode == ATTACHMENT_CHOICE_CHOOSE_FILE || requestCode == ATTACHMENT_CHOICE_RECORD_VOICE) {
 				mPendingFileUri = data.getData();
 				if (xmppConnectionServiceBound) {
-					attachFileToConversation(getSelectedConversation(),
-							mPendingFileUri);
+					attachFileToConversation(getSelectedConversation(),mPendingFileUri);
 					mPendingFileUri = null;
 				}
-			} else if (requestCode == REQUEST_SEND_PGP_IMAGE) {
-
-			} else if (requestCode == ATTACHMENT_CHOICE_CHOOSE_IMAGE) {
-				attachFile(ATTACHMENT_CHOICE_CHOOSE_IMAGE);
-			} else if (requestCode == ATTACHMENT_CHOICE_TAKE_PHOTO) {
-				attachFile(ATTACHMENT_CHOICE_TAKE_PHOTO);
-			} else if (requestCode == REQUEST_ANNOUNCE_PGP) {
-				announcePgp(getSelectedConversation().getAccount(),
-						getSelectedConversation());
-			} else if (requestCode == REQUEST_ENCRYPT_MESSAGE) {
-				// encryptTextMessage();
-			} else if (requestCode == REQUEST_IMAGE_CAPTURE && mPendingImageUri != null) {
+			} else if (requestCode == ATTACHMENT_CHOICE_TAKE_PHOTO && mPendingImageUri != null) {
 				if (xmppConnectionServiceBound) {
-					attachImageToConversation(getSelectedConversation(),
-							mPendingImageUri);
+					attachImageToConversation(getSelectedConversation(),mPendingImageUri);
 					mPendingImageUri = null;
 				}
-				Intent intent = new Intent(
-						Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
+				Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
 				intent.setData(mPendingImageUri);
 				sendBroadcast(intent);
 			}
 		} else {
-			if (requestCode == REQUEST_IMAGE_CAPTURE) {
+			if (requestCode == ATTACHMENT_CHOICE_TAKE_PHOTO) {
 				mPendingImageUri = null;
 			}
 		}
@@ -941,8 +927,6 @@ public class ConversationActivity extends XmppActivity
 					public void userInputRequried(PendingIntent pi,
 							Message object) {
 						hidePrepareFileToast();
-						ConversationActivity.this.runIntent(pi,
-								ConversationActivity.REQUEST_SEND_PGP_IMAGE);
 					}
 
 					@Override

src/main/java/eu/siacs/conversations/ui/ShareWithActivity.java 🔗

@@ -13,7 +13,10 @@ import android.widget.AdapterView.OnItemClickListener;
 import android.widget.ListView;
 import android.widget.Toast;
 
+import java.io.UnsupportedEncodingException;
 import java.net.URLConnection;
+import java.net.URLDecoder;
+import java.nio.charset.UnsupportedCharsetException;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -133,13 +136,17 @@ public class ShareWithActivity extends XmppActivity {
 		final String type = getIntent().getType();
 		if (type != null && !type.startsWith("text/")) {
 			this.share.uri = (Uri) getIntent().getParcelableExtra(Intent.EXTRA_STREAM);
-			this.share.image = type.startsWith("image/") || URLConnection.guessContentTypeFromName(share.uri.getPath()).startsWith("image/");
+			try {
+				this.share.image = type.startsWith("image/")
+						|| URLConnection.guessContentTypeFromName(this.share.uri.toString()).startsWith("image/");
+			} catch (final StringIndexOutOfBoundsException ignored) {
+				this.share.image = false;
+			}
 		} else {
 			this.share.text = getIntent().getStringExtra(Intent.EXTRA_TEXT);
 		}
 		if (xmppConnectionServiceBound) {
-			xmppConnectionService.populateWithOrderedConversations(
-					mConversations, this.share.uri == null);
+			xmppConnectionService.populateWithOrderedConversations(mConversations, this.share.uri == null);
 		}
 		super.onStart();
 	}

src/main/res/menu/attachment_choices.xml 🔗

@@ -7,8 +7,11 @@
     <item
         android:id="@+id/attach_take_picture"
         android:title="@string/attach_take_picture"/>
+	<item
+		android:id="@+id/attach_record_voice"
+		android:title="@string/attach_record_voice"/>
     <item
-        android:id="@+id/attach_record_voice"
+        android:id="@+id/attach_choose_file"
         android:title="@string/choose_file"/>
 
 </menu>

src/main/res/values/strings.xml 🔗

@@ -405,4 +405,5 @@
 	<string name="password_should_not_be_empty">Password should not be empty</string>
 	<string name="enable_all_accounts">Enable all accounts</string>
 	<string name="disable_all_accounts">Disable all accounts</string>
+	<string name="perform_action_with">Perform action with</string>
 </resources>