2015-06-08 11:07:46 -07:00
|
|
|
package org.thoughtcrime.securesms.providers;
|
|
|
|
|
|
|
|
import android.content.ContentUris;
|
|
|
|
import android.content.Context;
|
2015-07-13 17:35:34 -07:00
|
|
|
import android.content.UriMatcher;
|
2015-06-08 11:07:46 -07:00
|
|
|
import android.net.Uri;
|
2015-07-13 17:35:34 -07:00
|
|
|
import android.os.AsyncTask;
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.v4.util.SparseArrayCompat;
|
|
|
|
import android.util.Log;
|
2015-06-08 11:07:46 -07:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.crypto.DecryptingPartInputStream;
|
|
|
|
import org.thoughtcrime.securesms.crypto.EncryptingPartOutputStream;
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2015-07-13 17:35:34 -07:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
2015-06-08 11:07:46 -07:00
|
|
|
|
2015-07-13 17:35:34 -07:00
|
|
|
import java.io.ByteArrayInputStream;
|
2015-06-08 11:07:46 -07:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
2015-07-13 17:35:34 -07:00
|
|
|
import java.util.Arrays;
|
2015-06-08 11:07:46 -07:00
|
|
|
|
|
|
|
public class CaptureProvider {
|
|
|
|
private static final String TAG = CaptureProvider.class.getSimpleName();
|
|
|
|
private static final String URI_STRING = "content://org.thoughtcrime.securesms/capture";
|
|
|
|
public static final Uri CONTENT_URI = Uri.parse(URI_STRING);
|
|
|
|
public static final String AUTHORITY = "org.thoughtcrime.securesms";
|
2015-07-13 17:35:34 -07:00
|
|
|
public static final String EXPECTED_PATH = "capture/*/#";
|
|
|
|
private static final int MATCH = 1;
|
|
|
|
public static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH) {{
|
|
|
|
addURI(AUTHORITY, EXPECTED_PATH, MATCH);
|
|
|
|
}};
|
2015-06-08 11:07:46 -07:00
|
|
|
|
|
|
|
private static volatile CaptureProvider instance;
|
2015-07-13 17:35:34 -07:00
|
|
|
|
2015-06-08 11:07:46 -07:00
|
|
|
public static CaptureProvider getInstance(Context context) {
|
|
|
|
if (instance == null) {
|
|
|
|
synchronized (CaptureProvider.class) {
|
|
|
|
if (instance == null) {
|
|
|
|
instance = new CaptureProvider(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
private final Context context;
|
2015-07-13 17:35:34 -07:00
|
|
|
private final SparseArrayCompat<byte[]> cache = new SparseArrayCompat<>();
|
2015-06-08 11:07:46 -07:00
|
|
|
|
|
|
|
private CaptureProvider(Context context) {
|
|
|
|
this.context = context.getApplicationContext();
|
|
|
|
}
|
|
|
|
|
2015-07-13 17:35:34 -07:00
|
|
|
public Uri create(@NonNull MasterSecret masterSecret,
|
|
|
|
@NonNull Recipients recipients,
|
|
|
|
@NonNull byte[] imageBytes)
|
|
|
|
{
|
|
|
|
final int id = generateId(recipients);
|
|
|
|
cache.put(id, imageBytes);
|
|
|
|
persistToDisk(masterSecret, id, imageBytes);
|
|
|
|
final Uri uniqueUri = Uri.withAppendedPath(CONTENT_URI, String.valueOf(System.currentTimeMillis()));
|
|
|
|
return ContentUris.withAppendedId(uniqueUri, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void persistToDisk(final MasterSecret masterSecret, final int id, final byte[] imageBytes) {
|
|
|
|
new AsyncTask<Void, Void, Void>() {
|
|
|
|
@Override protected Void doInBackground(Void... params) {
|
|
|
|
try {
|
|
|
|
final OutputStream output = new EncryptingPartOutputStream(getFile(id), masterSecret);
|
|
|
|
Util.copy(new ByteArrayInputStream(imageBytes), output);
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override protected void onPostExecute(Void aVoid) {
|
|
|
|
cache.remove(id);
|
|
|
|
}
|
|
|
|
}.execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Uri createForExternal(@NonNull Recipients recipients) throws IOException {
|
|
|
|
final File externalDir = context.getExternalFilesDir(null);
|
|
|
|
if (externalDir == null) throw new IOException("no external files directory");
|
|
|
|
return Uri.fromFile(new File(externalDir, String.valueOf(generateId(recipients)) + ".jpg"))
|
|
|
|
.buildUpon()
|
|
|
|
.appendQueryParameter("unique", String.valueOf(System.currentTimeMillis()))
|
|
|
|
.build();
|
2015-06-08 11:07:46 -07:00
|
|
|
}
|
|
|
|
|
2015-07-13 17:35:34 -07:00
|
|
|
public boolean delete(@NonNull Uri uri) {
|
|
|
|
switch (uriMatcher.match(uri)) {
|
|
|
|
case MATCH: return getFile(ContentUris.parseId(uri)).delete();
|
|
|
|
default: return new File(uri.getPath()).delete();
|
|
|
|
}
|
2015-06-08 11:07:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public InputStream getStream(MasterSecret masterSecret, long id) throws IOException {
|
2015-07-13 17:35:34 -07:00
|
|
|
final byte[] cached = cache.get((int)id);
|
|
|
|
return cached != null ? new ByteArrayInputStream(cached)
|
|
|
|
: new DecryptingPartInputStream(getFile(id), masterSecret);
|
|
|
|
}
|
|
|
|
|
|
|
|
private int generateId(Recipients recipients) {
|
|
|
|
return Math.abs(Arrays.hashCode(recipients.getIds()));
|
2015-06-08 11:07:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private File getFile(long id) {
|
2015-07-13 17:35:34 -07:00
|
|
|
return new File(context.getDir("captures", Context.MODE_PRIVATE), id + ".jpg");
|
2015-06-08 11:07:46 -07:00
|
|
|
}
|
|
|
|
}
|