gui for images

This commit is contained in:
Daniel Gultsch 2014-04-06 15:34:08 +02:00
parent 9fc7a1b980
commit c30bf75a5d
8 changed files with 105 additions and 42 deletions

View file

@ -21,6 +21,14 @@
android:orientation="vertical"
android:padding="5dp" >
<ImageView
android:id="@+id/message_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxHeight="300dp"
/>
<TextView
android:id="@+id/message_body"
android:layout_width="wrap_content"

View file

@ -20,6 +20,14 @@
android:orientation="vertical"
android:padding="5dp" >
<ImageView
android:id="@+id/message_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxHeight="300dp"
/>
<TextView
android:id="@+id/message_body"
android:layout_width="wrap_content"

View file

@ -19,12 +19,16 @@ public class Message extends AbstractEntity {
public static final int ENCRYPTION_OTR = 2;
public static final int ENCRYPTION_DECRYPTED = 3;
public static final int TYPE_TEXT = 0;
public static final int TYPE_IMAGE = 1;
public static String CONVERSATION = "conversationUuid";
public static String COUNTERPART = "counterpart";
public static String BODY = "body";
public static String TIME_SENT = "timeSent";
public static String ENCRYPTION = "encryption";
public static String STATUS = "status";
public static String TYPE = "type";
protected String conversationUuid;
protected String counterpart;
@ -33,6 +37,7 @@ public class Message extends AbstractEntity {
protected long timeSent;
protected int encryption;
protected int status;
protected int type;
protected boolean read = true;
protected transient Conversation conversation = null;
@ -40,17 +45,17 @@ public class Message extends AbstractEntity {
public Message(Conversation conversation, String body, int encryption) {
this(java.util.UUID.randomUUID().toString(), conversation.getUuid(),
conversation.getContactJid(), body, System.currentTimeMillis(), encryption,
Message.STATUS_UNSEND);
Message.STATUS_UNSEND,TYPE_TEXT);
this.conversation = conversation;
}
public Message(Conversation conversation, String counterpart, String body, int encryption, int status) {
this(java.util.UUID.randomUUID().toString(), conversation.getUuid(),counterpart, body, System.currentTimeMillis(), encryption,status);
this(java.util.UUID.randomUUID().toString(), conversation.getUuid(),counterpart, body, System.currentTimeMillis(), encryption,status,TYPE_TEXT);
this.conversation = conversation;
}
public Message(String uuid, String conversationUUid, String counterpart,
String body, long timeSent, int encryption, int status) {
String body, long timeSent, int encryption, int status, int type) {
this.uuid = uuid;
this.conversationUuid = conversationUUid;
this.counterpart = counterpart;
@ -58,6 +63,7 @@ public class Message extends AbstractEntity {
this.timeSent = timeSent;
this.encryption = encryption;
this.status = status;
this.type = type;
}
@Override
@ -70,6 +76,7 @@ public class Message extends AbstractEntity {
values.put(TIME_SENT, timeSent);
values.put(ENCRYPTION, encryption);
values.put(STATUS, status);
values.put(TYPE, type);
return values;
}
@ -108,7 +115,8 @@ public class Message extends AbstractEntity {
cursor.getString(cursor.getColumnIndex(BODY)),
cursor.getLong(cursor.getColumnIndex(TIME_SENT)),
cursor.getInt(cursor.getColumnIndex(ENCRYPTION)),
cursor.getInt(cursor.getColumnIndex(STATUS)));
cursor.getInt(cursor.getColumnIndex(STATUS)),
cursor.getInt(cursor.getColumnIndex(TYPE)));
}
public void setConversation(Conversation conv) {
@ -150,4 +158,12 @@ public class Message extends AbstractEntity {
public void setEncryptedBody(String body) {
this.encryptedBody = body;
}
public void setType(int type) {
this.type = type;
}
public int getType() {
return this.type;
}
}

View file

@ -23,7 +23,7 @@ public class DatabaseBackend extends SQLiteOpenHelper {
private static DatabaseBackend instance = null;
private static final String DATABASE_NAME = "history";
private static final int DATABASE_VERSION = 2;
private static final int DATABASE_VERSION = 3;
public DatabaseBackend(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
@ -50,7 +50,7 @@ public class DatabaseBackend extends SQLiteOpenHelper {
+ " TEXT PRIMARY KEY, " + Message.CONVERSATION + " TEXT, "
+ Message.TIME_SENT + " NUMBER, " + Message.COUNTERPART
+ " TEXT, " + Message.BODY + " TEXT, " + Message.ENCRYPTION
+ " NUMBER, " + Message.STATUS + " NUMBER," + "FOREIGN KEY("
+ " NUMBER, " + Message.STATUS + " NUMBER," +Message.TYPE +" NUMBER, FOREIGN KEY("
+ Message.CONVERSATION + ") REFERENCES "
+ Conversation.TABLENAME + "(" + Conversation.UUID
+ ") ON DELETE CASCADE);");
@ -72,6 +72,10 @@ public class DatabaseBackend extends SQLiteOpenHelper {
db.execSQL("update " + Account.TABLENAME
+ " set " + Account.OPTIONS + " = " + Account.OPTIONS + " | 8");
}
if (oldVersion < 3 && newVersion >= 3) {
//add field type to message
db.execSQL("ALTER TABLE "+Message.TABLENAME+" ADD COLUMN "+Message.TYPE+" NUMBER");;
}
}
public static synchronized DatabaseBackend getInstance(Context context) {

View file

@ -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());
}
}

View file

@ -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) {

View file

@ -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());
}
}
}

View file

@ -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;