From c30bf75a5d77fab21480e04ce18b46d6b2826295 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sun, 6 Apr 2014 15:34:08 +0200 Subject: [PATCH] gui for images --- res/layout/message_recieved.xml | 8 +++ res/layout/message_sent.xml | 8 +++ .../siacs/conversations/entities/Message.java | 24 +++++-- .../persistance/DatabaseBackend.java | 8 ++- .../persistance/FileBackend.java | 20 ++++-- .../services/XmppConnectionService.java | 11 ++++ .../ui/ConversationActivity.java | 5 +- .../ui/ConversationFragment.java | 63 +++++++++++-------- 8 files changed, 105 insertions(+), 42 deletions(-) diff --git a/res/layout/message_recieved.xml b/res/layout/message_recieved.xml index 65a9cd29a..62f4f00ac 100644 --- a/res/layout/message_recieved.xml +++ b/res/layout/message_recieved.xml @@ -21,6 +21,14 @@ android:orientation="vertical" android:padding="5dp" > + + + + = 3) { + //add field type to message + db.execSQL("ALTER TABLE "+Message.TABLENAME+" ADD COLUMN "+Message.TYPE+" NUMBER");; + } } public static synchronized DatabaseBackend getInstance(Context context) { diff --git a/src/eu/siacs/conversations/persistance/FileBackend.java b/src/eu/siacs/conversations/persistance/FileBackend.java index 05c505308..9ae406452 100644 --- a/src/eu/siacs/conversations/persistance/FileBackend.java +++ b/src/eu/siacs/conversations/persistance/FileBackend.java @@ -15,6 +15,7 @@ import android.net.Uri; import android.util.Log; import eu.siacs.conversations.entities.Conversation; +import eu.siacs.conversations.entities.Message; public class FileBackend { @@ -27,14 +28,18 @@ public class FileBackend { this.context = context; } + private File getImageFile(Message message) { + Conversation conversation = message.getConversation(); + String prefix = context.getFilesDir().getAbsolutePath(); + String path = prefix+"/"+conversation.getAccount().getJid()+"/"+conversation.getContactJid(); + String filename = message.getUuid() + ".webp"; + return new File(path+"/"+filename); + } - public File copyImageToPrivateStorage(Conversation conversation, Uri image) { + public File copyImageToPrivateStorage(Message message, Uri image) { try { InputStream is = context.getContentResolver().openInputStream(image); - String prefix = context.getFilesDir().getAbsolutePath(); - String path = prefix+"/"+conversation.getAccount().getJid()+"/"+conversation.getContactJid(); - String filename =new BigInteger(""+System.currentTimeMillis()).toString(32) + ".webp"; - File file = new File(path+"/"+filename); + File file = getImageFile(message); file.getParentFile().mkdirs(); file.createNewFile(); OutputStream os = new FileOutputStream(file); @@ -73,4 +78,9 @@ public class FileBackend { return null; } + + + public Bitmap getImageFromMessage(Message message) { + return BitmapFactory.decodeFile(getImageFile(message).getAbsolutePath()); + } } diff --git a/src/eu/siacs/conversations/services/XmppConnectionService.java b/src/eu/siacs/conversations/services/XmppConnectionService.java index 76e7d7289..e8ec6f7f1 100644 --- a/src/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/eu/siacs/conversations/services/XmppConnectionService.java @@ -1,5 +1,6 @@ package eu.siacs.conversations.services; +import java.io.File; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Collections; @@ -60,6 +61,7 @@ import android.database.ContentObserver; import android.database.DatabaseUtils; import android.net.ConnectivityManager; import android.net.NetworkInfo; +import android.net.Uri; import android.os.Binder; import android.os.Bundle; import android.os.IBinder; @@ -387,6 +389,15 @@ public class XmppConnectionService extends Service { return this.fileBackend; } + public void attachImageToConversation(Conversation conversation, Uri uri) { + Message message = new Message(conversation, "", Message.ENCRYPTION_NONE); + message.setType(Message.TYPE_IMAGE); + File file = this.fileBackend.copyImageToPrivateStorage(message, uri); + Log.d(LOGTAG,"new file"+file.getAbsolutePath()); + conversation.getMessages().add(message); + databaseBackend.createMessage(message); + } + protected Conversation findMuc(String name, Account account) { for (Conversation conversation : this.conversations) { diff --git a/src/eu/siacs/conversations/ui/ConversationActivity.java b/src/eu/siacs/conversations/ui/ConversationActivity.java index 82b334990..85186e9bd 100644 --- a/src/eu/siacs/conversations/ui/ConversationActivity.java +++ b/src/eu/siacs/conversations/ui/ConversationActivity.java @@ -494,10 +494,7 @@ public class ConversationActivity extends XmppActivity { selectedFragment.hidePgpPassphraseBox(); } } else if (requestCode == ATTACH_FILE) { - FileBackend backend = xmppConnectionService.getFileBackend(); - File file = backend.copyImageToPrivateStorage(getSelectedConversation(), data.getData()); - Log.d(LOGTAG,"new file"+file.getAbsolutePath()); - + xmppConnectionService.attachImageToConversation(getSelectedConversation(), data.getData()); } } } diff --git a/src/eu/siacs/conversations/ui/ConversationFragment.java b/src/eu/siacs/conversations/ui/ConversationFragment.java index 51caafbd5..a9bde53d0 100644 --- a/src/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/eu/siacs/conversations/ui/ConversationFragment.java @@ -89,7 +89,6 @@ public class ConversationFragment extends Fragment { @Override public void onClick(View v) { - Log.d("gultsch", "clicked to decrypt"); if (askForPassphraseIntent != null) { try { getActivity().startIntentSenderForResult( @@ -97,7 +96,7 @@ public class ConversationFragment extends Fragment { ConversationActivity.REQUEST_DECRYPT_PGP, null, 0, 0, 0); } catch (SendIntentException e) { - Log.d("gultsch", "couldnt fire intent"); + Log.d("xmppService", "couldnt fire intent"); } } } @@ -210,6 +209,7 @@ public class ConversationFragment extends Fragment { .findViewById(R.id.message_photo); viewHolder.imageView.setImageBitmap(selfBitmap); viewHolder.indicator = (ImageView) view.findViewById(R.id.security_indicator); + viewHolder.image = (ImageView) view.findViewById(R.id.message_image); break; case RECIEVED: view = (View) inflater.inflate( @@ -262,32 +262,40 @@ public class ConversationFragment extends Fragment { } } } - String body = item.getBody(); - if (body != null) { - if (item.getEncryption() == Message.ENCRYPTION_PGP) { - viewHolder.messageBody - .setText(getString(R.string.encrypted_message)); - viewHolder.messageBody.setTextColor(0xff33B5E5); - viewHolder.messageBody.setTypeface(null, - Typeface.ITALIC); - viewHolder.indicator.setVisibility(View.VISIBLE); - } else if ((item.getEncryption() == Message.ENCRYPTION_OTR)||(item.getEncryption() == Message.ENCRYPTION_DECRYPTED)) { - viewHolder.messageBody.setText(body.trim()); - viewHolder.messageBody.setTextColor(0xff000000); - viewHolder.messageBody.setTypeface(null, - Typeface.NORMAL); - viewHolder.indicator.setVisibility(View.VISIBLE); - } else { - viewHolder.messageBody.setText(body.trim()); - viewHolder.messageBody.setTextColor(0xff000000); - viewHolder.messageBody.setTypeface(null, - Typeface.NORMAL); - if (item.getStatus() != Message.STATUS_ERROR) { - viewHolder.indicator.setVisibility(View.GONE); - } - } + if (item.getType() == Message.TYPE_IMAGE) { + viewHolder.image.setVisibility(View.VISIBLE); + viewHolder.image.setImageBitmap(activity.xmppConnectionService.getFileBackend().getImageFromMessage(item)); + viewHolder.messageBody.setVisibility(View.GONE); } else { - viewHolder.indicator.setVisibility(View.GONE); + if (viewHolder.image != null) viewHolder.image.setVisibility(View.GONE); + viewHolder.messageBody.setVisibility(View.VISIBLE); + String body = item.getBody(); + if (body != null) { + if (item.getEncryption() == Message.ENCRYPTION_PGP) { + viewHolder.messageBody + .setText(getString(R.string.encrypted_message)); + viewHolder.messageBody.setTextColor(0xff33B5E5); + viewHolder.messageBody.setTypeface(null, + Typeface.ITALIC); + viewHolder.indicator.setVisibility(View.VISIBLE); + } else if ((item.getEncryption() == Message.ENCRYPTION_OTR)||(item.getEncryption() == Message.ENCRYPTION_DECRYPTED)) { + viewHolder.messageBody.setText(body.trim()); + viewHolder.messageBody.setTextColor(0xff000000); + viewHolder.messageBody.setTypeface(null, + Typeface.NORMAL); + viewHolder.indicator.setVisibility(View.VISIBLE); + } else { + viewHolder.messageBody.setText(body.trim()); + viewHolder.messageBody.setTextColor(0xff000000); + viewHolder.messageBody.setTypeface(null, + Typeface.NORMAL); + if (item.getStatus() != Message.STATUS_ERROR) { + viewHolder.indicator.setVisibility(View.GONE); + } + } + } else { + viewHolder.indicator.setVisibility(View.GONE); + } } if (item.getStatus() == Message.STATUS_UNSEND) { viewHolder.time.setTypeface(null, Typeface.ITALIC); @@ -585,6 +593,7 @@ public class ConversationFragment extends Fragment { private static class ViewHolder { + protected ImageView image; protected ImageView indicator; protected TextView time; protected TextView messageBody;