mirror of
https://github.com/oxen-io/session-android.git
synced 2025-10-24 01:21:09 +00:00
Add camera preview to message composition
This commit is contained in:
committed by
Moxie Marlinspike
parent
13eed3baa7
commit
c4a37e38ab
@@ -24,6 +24,7 @@ import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.provider.ContactsContract;
|
||||
import android.provider.MediaStore;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.view.animation.AlphaAnimation;
|
||||
@@ -36,6 +37,7 @@ import android.widget.Toast;
|
||||
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.components.ThumbnailView;
|
||||
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
||||
import org.thoughtcrime.securesms.util.BitmapDecodingException;
|
||||
|
||||
import java.io.File;
|
||||
@@ -106,11 +108,24 @@ public class AttachmentManager {
|
||||
setMedia(new AudioSlide(context, audio));
|
||||
}
|
||||
|
||||
public void setEncryptedImage(Uri uri, MasterSecret masterSecret) throws IOException, BitmapDecodingException {
|
||||
setMedia(new ImageSlide(context, masterSecret, uri), masterSecret);
|
||||
}
|
||||
|
||||
public void setMedia(final Slide slide) {
|
||||
setMedia(slide, null);
|
||||
}
|
||||
|
||||
public void setMedia(final Slide slide, @Nullable MasterSecret masterSecret) {
|
||||
Slide thumbnailSlide = slideDeck.getThumbnailSlide(context);
|
||||
if (thumbnailSlide != null && thumbnailSlide.isEncrypted()) {
|
||||
Uri dataUri = slideDeck.getThumbnailSlide(context).getPart().getDataUri();
|
||||
new File(dataUri.getPath()).delete();
|
||||
}
|
||||
slideDeck.clear();
|
||||
slideDeck.addSlide(slide);
|
||||
attachmentView.setVisibility(View.VISIBLE);
|
||||
thumbnail.setImageResource(slide);
|
||||
thumbnail.setImageResource(slide, masterSecret);
|
||||
attachmentListener.onAttachmentChanged();
|
||||
}
|
||||
|
||||
|
||||
@@ -20,25 +20,36 @@ import android.content.Context;
|
||||
import android.content.res.Resources.Theme;
|
||||
import android.net.Uri;
|
||||
import android.support.annotation.DrawableRes;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.crypto.DecryptingPartInputStream;
|
||||
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
||||
import org.thoughtcrime.securesms.util.BitmapDecodingException;
|
||||
import org.thoughtcrime.securesms.util.Util;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import ws.com.google.android.mms.ContentType;
|
||||
import ws.com.google.android.mms.pdu.PduPart;
|
||||
|
||||
public class ImageSlide extends Slide {
|
||||
private static final String TAG = ImageSlide.class.getSimpleName();
|
||||
private boolean encrypted = false;
|
||||
|
||||
public ImageSlide(Context context, MasterSecret masterSecret, PduPart part) {
|
||||
super(context, masterSecret, part);
|
||||
}
|
||||
|
||||
public ImageSlide(Context context, Uri uri) throws IOException, BitmapDecodingException {
|
||||
super(context, constructPartFromUri(uri));
|
||||
this(context, null, uri);
|
||||
}
|
||||
|
||||
public ImageSlide(Context context, MasterSecret masterSecret, Uri uri) throws IOException, BitmapDecodingException {
|
||||
super(context, masterSecret, constructPartFromByteArrayAndUri(uri, decryptContent(uri, masterSecret), masterSecret != null));
|
||||
encrypted = masterSecret != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -62,12 +73,32 @@ public class ImageSlide extends Slide {
|
||||
return true;
|
||||
}
|
||||
|
||||
private static PduPart constructPartFromUri(Uri uri)
|
||||
@Override
|
||||
public boolean isEncrypted() {
|
||||
return encrypted;
|
||||
}
|
||||
|
||||
private static byte[] decryptContent(Uri uri, MasterSecret masterSecret) {
|
||||
try {
|
||||
if (masterSecret != null) {
|
||||
InputStream inputStream = new DecryptingPartInputStream(new File(uri.getPath()), masterSecret);
|
||||
return Util.readFully(inputStream);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static PduPart constructPartFromByteArrayAndUri(Uri uri, @Nullable byte[] data, boolean encrypted)
|
||||
throws IOException, BitmapDecodingException
|
||||
{
|
||||
PduPart part = new PduPart();
|
||||
|
||||
part.setDataUri(uri);
|
||||
if (data != null)
|
||||
part.setData(data);
|
||||
part.setEncrypted(encrypted);
|
||||
part.setContentType(ContentType.IMAGE_JPEG.getBytes());
|
||||
part.setContentId((System.currentTimeMillis()+"").getBytes());
|
||||
part.setName(("Image" + System.currentTimeMillis()).getBytes());
|
||||
|
||||
@@ -5,11 +5,13 @@ import android.content.Context;
|
||||
import android.content.UriMatcher;
|
||||
import android.net.Uri;
|
||||
|
||||
import org.thoughtcrime.securesms.crypto.DecryptingPartInputStream;
|
||||
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
||||
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
||||
import org.thoughtcrime.securesms.database.PartDatabase;
|
||||
import org.thoughtcrime.securesms.providers.PartProvider;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
@@ -46,7 +48,11 @@ public class PartAuthority {
|
||||
partUri = new PartUriParser(uri);
|
||||
return partDatabase.getThumbnailStream(masterSecret, partUri.getPartId());
|
||||
default:
|
||||
return context.getContentResolver().openInputStream(uri);
|
||||
String tempMediaDir = context.getDir("media", Context.MODE_PRIVATE).getPath();
|
||||
if (uri.getPath().startsWith(tempMediaDir))
|
||||
return new DecryptingPartInputStream(new File(uri.getPath()), masterSecret);
|
||||
else
|
||||
return context.getContentResolver().openInputStream(uri);
|
||||
}
|
||||
} catch (SecurityException se) {
|
||||
throw new IOException(se);
|
||||
|
||||
@@ -66,6 +66,10 @@ public abstract class Slide {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isEncrypted() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public PduPart getPart() {
|
||||
return part;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user