2012-10-01 02:56:29 +00:00
|
|
|
/**
|
2011-12-20 18:20:44 +00:00
|
|
|
* Copyright (C) 2011 Whisper Systems
|
2012-10-01 02:56:29 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00: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 02:56:29 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00: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.BroadcastReceiver;
|
|
|
|
import android.content.ComponentName;
|
|
|
|
import android.content.ContentProvider;
|
|
|
|
import android.content.ContentValues;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.IntentFilter;
|
|
|
|
import android.content.ServiceConnection;
|
|
|
|
import android.content.UriMatcher;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.IBinder;
|
|
|
|
import android.os.ParcelFileDescriptor;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2014-11-03 23:16:04 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2012-10-01 02:56:29 +00:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
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 18:20:44 +00:00
|
|
|
public class PartProvider extends ContentProvider {
|
|
|
|
|
|
|
|
private static final String CONTENT_URI_STRING = "content://org.thoughtcrime.provider.securesms/part";
|
|
|
|
public static final Uri CONTENT_URI = Uri.parse(CONTENT_URI_STRING);
|
|
|
|
private static final int SINGLE_ROW = 1;
|
|
|
|
|
|
|
|
private static final UriMatcher uriMatcher;
|
2012-10-01 02:56:29 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
static {
|
|
|
|
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
|
|
|
|
uriMatcher.addURI("org.thoughtcrime.provider.securesms", "part/#", SINGLE_ROW);
|
|
|
|
}
|
|
|
|
|
|
|
|
private MasterSecret masterSecret;
|
|
|
|
private NewKeyReceiver receiver;
|
|
|
|
|
|
|
|
@Override
|
2012-10-01 02:56:29 +00:00
|
|
|
public boolean onCreate() {
|
2011-12-20 18:20:44 +00:00
|
|
|
initializeMasterSecret();
|
|
|
|
return true;
|
|
|
|
}
|
2012-10-01 02:56:29 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public static boolean isAuthority(Uri uri) {
|
|
|
|
return uriMatcher.match(uri) != -1;
|
|
|
|
}
|
2012-10-01 02:56:29 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private File copyPartToTemporaryFile(MasterSecret masterSecret, long partId) throws IOException {
|
|
|
|
InputStream in = DatabaseFactory.getEncryptingPartDatabase(getContext(), masterSecret).getPartStream(partId);
|
|
|
|
File tmpDir = getContext().getDir("tmp", 0);
|
|
|
|
File tmpFile = File.createTempFile("test", ".jpg", tmpDir);
|
|
|
|
FileOutputStream fout = new FileOutputStream(tmpFile);
|
2012-10-01 02:56:29 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
byte[] buffer = new byte[512];
|
|
|
|
int read;
|
2012-10-01 02:56:29 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
while ((read = in.read(buffer)) != -1)
|
|
|
|
fout.write(buffer, 0, read);
|
2012-10-01 02:56:29 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
in.close();
|
|
|
|
|
|
|
|
return tmpFile;
|
|
|
|
}
|
2012-10-01 02:56:29 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
@Override
|
|
|
|
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
|
|
|
|
Log.w("PartProvider", "openFile() called!");
|
2012-10-01 02:56:29 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
if (this.masterSecret == null)
|
|
|
|
return null;
|
2012-10-01 02:56:29 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
switch (uriMatcher.match(uri)) {
|
|
|
|
case SINGLE_ROW:
|
|
|
|
Log.w("PartProvider", "Parting out a single row...");
|
|
|
|
try {
|
2012-10-01 02:56:29 +00:00
|
|
|
int partId = Integer.parseInt(uri.getPathSegments().get(1));
|
|
|
|
File tmpFile = copyPartToTemporaryFile(masterSecret, partId);
|
|
|
|
ParcelFileDescriptor pdf = ParcelFileDescriptor.open(tmpFile, ParcelFileDescriptor.MODE_READ_ONLY);
|
|
|
|
tmpFile.delete();
|
|
|
|
return pdf;
|
2011-12-20 18:20:44 +00:00
|
|
|
} catch (IOException ioe) {
|
2012-10-01 02:56:29 +00:00
|
|
|
Log.w("PartProvider", ioe);
|
|
|
|
throw new FileNotFoundException("Error opening file");
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
2012-10-01 02:56:29 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
throw new FileNotFoundException("Request for bad part.");
|
|
|
|
}
|
2012-10-01 02:56:29 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
@Override
|
2012-10-01 02:56:29 +00:00
|
|
|
public int delete(Uri arg0, String arg1, String[] arg2) {
|
2011-12-20 18:20:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-10-01 02:56:29 +00:00
|
|
|
public String getType(Uri arg0) {
|
2011-12-20 18:20:44 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-10-01 02:56:29 +00:00
|
|
|
public Uri insert(Uri arg0, ContentValues arg1) {
|
2011-12-20 18:20:44 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-10-01 02:56:29 +00:00
|
|
|
public Cursor query(Uri arg0, String[] arg1, String arg2, String[] arg3, String arg4) {
|
2011-12-20 18:20:44 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-10-01 02:56:29 +00:00
|
|
|
public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
|
2011-12-20 18:20:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initializeWithMasterSecret(MasterSecret masterSecret) {
|
2012-10-01 02:56:29 +00:00
|
|
|
Log.w("PartProvider", "Got master secret: " + masterSecret);
|
2011-12-20 18:20:44 +00:00
|
|
|
this.masterSecret = masterSecret;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initializeMasterSecret() {
|
|
|
|
receiver = new NewKeyReceiver();
|
|
|
|
IntentFilter filter = new IntentFilter(KeyCachingService.NEW_KEY_EVENT);
|
|
|
|
getContext().registerReceiver(receiver, filter, KeyCachingService.KEY_PERMISSION, null);
|
|
|
|
|
2014-11-03 23:16:04 +00:00
|
|
|
initializeWithMasterSecret(KeyCachingService.getMasterSecret(getContext()));
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-10-01 02:56:29 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private class NewKeyReceiver extends BroadcastReceiver {
|
|
|
|
@Override
|
2012-10-01 02:56:29 +00:00
|
|
|
public void onReceive(Context context, Intent intent) {
|
2011-12-20 18:20:44 +00:00
|
|
|
Log.w("SendReceiveService", "Got a MasterSecret broadcast...");
|
|
|
|
initializeWithMasterSecret((MasterSecret)intent.getParcelableExtra("master_secret"));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|