session-android/src/org/thoughtcrime/securesms/providers/PartProvider.java

141 lines
4.5 KiB
Java
Raw Normal View History

2012-10-01 04:56:29 +02:00
/**
2011-12-20 19:20:44 +01:00
* Copyright (C) 2011 Whisper Systems
2012-10-01 04:56:29 +02:00
*
2011-12-20 19:20:44 +01:00
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
2012-10-01 04:56:29 +02:00
*
2011-12-20 19:20:44 +01:00
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.thoughtcrime.securesms.providers;
import android.content.ContentProvider;
import android.content.ContentUris;
2011-12-20 19:20:44 +01:00
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.support.annotation.NonNull;
2011-12-20 19:20:44 +01:00
import android.util.Log;
import org.thoughtcrime.securesms.attachments.AttachmentId;
import org.thoughtcrime.securesms.crypto.MasterSecret;
2012-10-01 04:56:29 +02:00
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.mms.PartUriParser;
2012-10-01 04:56:29 +02:00
import org.thoughtcrime.securesms.service.KeyCachingService;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
2011-12-20 19:20:44 +01:00
public class PartProvider extends ContentProvider {
private static final String TAG = PartProvider.class.getSimpleName();
2011-12-20 19:20:44 +01:00
private static final String CONTENT_URI_STRING = "content://org.thoughtcrime.provider.securesms/part";
private static final Uri CONTENT_URI = Uri.parse(CONTENT_URI_STRING);
2011-12-20 19:20:44 +01:00
private static final int SINGLE_ROW = 1;
private static final UriMatcher uriMatcher;
2012-10-01 04:56:29 +02:00
2011-12-20 19:20:44 +01:00
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI("org.thoughtcrime.provider.securesms", "part/*/#", SINGLE_ROW);
2011-12-20 19:20:44 +01:00
}
@Override
2012-10-01 04:56:29 +02:00
public boolean onCreate() {
Log.w(TAG, "onCreate()");
2011-12-20 19:20:44 +01:00
return true;
}
2012-10-01 04:56:29 +02:00
public static Uri getContentUri(AttachmentId attachmentId) {
Uri uri = Uri.withAppendedPath(CONTENT_URI, String.valueOf(attachmentId.getUniqueId()));
return ContentUris.withAppendedId(uri, attachmentId.getRowId());
}
@SuppressWarnings("ConstantConditions")
private File copyPartToTemporaryFile(MasterSecret masterSecret, AttachmentId attachmentId) throws IOException {
InputStream in = DatabaseFactory.getAttachmentDatabase(getContext()).getAttachmentStream(masterSecret, attachmentId);
2011-12-20 19:20:44 +01:00
File tmpDir = getContext().getDir("tmp", 0);
File tmpFile = File.createTempFile("test", ".jpg", tmpDir);
FileOutputStream fout = new FileOutputStream(tmpFile);
2012-10-01 04:56:29 +02:00
2011-12-20 19:20:44 +01:00
byte[] buffer = new byte[512];
int read;
2012-10-01 04:56:29 +02:00
2011-12-20 19:20:44 +01:00
while ((read = in.read(buffer)) != -1)
fout.write(buffer, 0, read);
2012-10-01 04:56:29 +02:00
2011-12-20 19:20:44 +01:00
in.close();
return tmpFile;
}
2012-10-01 04:56:29 +02:00
2011-12-20 19:20:44 +01:00
@Override
public ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode) throws FileNotFoundException {
MasterSecret masterSecret = KeyCachingService.getMasterSecret(getContext());
Log.w(TAG, "openFile() called!");
2012-10-01 04:56:29 +02:00
if (masterSecret == null) {
Log.w(TAG, "masterSecret was null, abandoning.");
2011-12-20 19:20:44 +01:00
return null;
}
2012-10-01 04:56:29 +02:00
2011-12-20 19:20:44 +01:00
switch (uriMatcher.match(uri)) {
case SINGLE_ROW:
Log.w(TAG, "Parting out a single row...");
2011-12-20 19:20:44 +01:00
try {
PartUriParser partUri = new PartUriParser(uri);
File tmpFile = copyPartToTemporaryFile(masterSecret, partUri.getPartId());
ParcelFileDescriptor pdf = ParcelFileDescriptor.open(tmpFile, ParcelFileDescriptor.MODE_READ_ONLY);
if (!tmpFile.delete()) {
Log.w(TAG, "Failed to delete temp file.");
}
2012-10-01 04:56:29 +02:00
return pdf;
2011-12-20 19:20:44 +01:00
} catch (IOException ioe) {
Log.w(TAG, ioe);
2012-10-01 04:56:29 +02:00
throw new FileNotFoundException("Error opening file");
2011-12-20 19:20:44 +01:00
}
}
2012-10-01 04:56:29 +02:00
2011-12-20 19:20:44 +01:00
throw new FileNotFoundException("Request for bad part.");
}
2012-10-01 04:56:29 +02:00
2011-12-20 19:20:44 +01:00
@Override
public int delete(@NonNull Uri arg0, String arg1, String[] arg2) {
2011-12-20 19:20:44 +01:00
return 0;
}
@Override
public String getType(@NonNull Uri arg0) {
2011-12-20 19:20:44 +01:00
return null;
}
@Override
public Uri insert(@NonNull Uri arg0, ContentValues arg1) {
2011-12-20 19:20:44 +01:00
return null;
}
@Override
public Cursor query(@NonNull Uri arg0, String[] arg1, String arg2, String[] arg3, String arg4) {
2011-12-20 19:20:44 +01:00
return null;
}
@Override
public int update(@NonNull Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
2011-12-20 19:20:44 +01:00
return 0;
}
}