Rework encrypt for files with intents

This commit is contained in:
Dominik 2012-11-16 16:33:31 +01:00
parent bbddc4c56d
commit 56cfb6bc5a
7 changed files with 726 additions and 657 deletions

View File

@ -374,31 +374,6 @@
android:name=".provider.ApgServiceBlobProvider"
android:authorities="org.thialfihar.android.apg.provider.apgserviceblobprovider"
android:permission="org.thialfihar.android.apg.permission.ACCESS_API" />
<!-- DEPRECATED: -->
<!-- <provider -->
<!-- android:name=".deprecated.DataProvider" -->
<!-- android:authorities="org.thialfihar.android.apg.provider" -->
<!-- android:readPermission="org.thialfihar.android.apg.permission.READ_KEY_DETAILS" /> -->
<!-- TODO: need to be moved into new service model -->
<!-- <service -->
<!-- android:name=".deprecated.ApgService2" -->
<!-- android:enabled="true" -->
<!-- android:exported="true" -->
<!-- android:permission="org.thialfihar.android.apg.permission.READ_KEY_DETAILS" -->
<!-- android:process=":remote" > -->
<!-- <intent-filter> -->
<!-- <action android:name="org.thialfihar.android.apg.service.IApgService2" /> -->
<!-- </intent-filter> -->
<!-- <meta-data -->
<!-- android:name="api_version" -->
<!-- android:value="2" /> -->
<!-- </service> -->
</application>
</manifest>

View File

@ -260,7 +260,8 @@
<string name="error_wrongPassPhrase">wrong passphrase</string>
<string name="error_savingKeys">error saving some key(s)</string>
<string name="error_couldNotExtractPrivateKey">could not extract private key</string>
<string name="error_onlyFilesAreSupported">Direct binary data without actual file in filesystem is not supported</string>
<!-- progress_lowerCase: lowercase, phrases, usually ending in '…' -->
<string name="progress_done">done.</string>
<string name="progress_initializing">initializing…</string>

View File

@ -16,11 +16,17 @@
package org.thialfihar.android.apg.helper;
import java.net.URISyntaxException;
import org.thialfihar.android.apg.Constants;
import org.thialfihar.android.apg.R;
import org.thialfihar.android.apg.util.Log;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.widget.Toast;
@ -70,4 +76,46 @@ public class FileHelper {
Toast.makeText(activity, R.string.noFilemanagerInstalled, Toast.LENGTH_SHORT).show();
}
}
/**
* Get a file path from a Uri.
*
* from https://github.com/iPaulPro/aFileChooser/blob/master/aFileChooser/src/com/ipaulpro/
* afilechooser/utils/FileUtils.java
*
* @param context
* @param uri
* @return
*
* @author paulburke
*/
public static String getPath(Context context, Uri uri) {
Log.d(Constants.TAG + " File -",
"Authority: " + uri.getAuthority() + ", Fragment: " + uri.getFragment()
+ ", Port: " + uri.getPort() + ", Query: " + uri.getQuery() + ", Scheme: "
+ uri.getScheme() + ", Host: " + uri.getHost() + ", Segments: "
+ uri.getPathSegments().toString());
if ("content".equalsIgnoreCase(uri.getScheme())) {
String[] projection = { "_data" };
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow("_data");
if (cursor.moveToFirst()) {
return cursor.getString(column_index);
}
} catch (Exception e) {
// Eat it
}
}
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
}

View File

@ -1746,12 +1746,15 @@ public class PGPMain {
return "APG v" + getVersion(context);
}
public static String generateRandomString(int length) {
/**
* Generate a random filename
*
* @param length
* @return
*/
public static String generateRandomFilename(int length) {
SecureRandom random = new SecureRandom();
/*
* try { random = SecureRandom.getInstance("SHA1PRNG", new BouncyCastleProvider()); } catch
* (NoSuchAlgorithmException e) { // TODO: need to handle this case somehow return null; }
*/
byte bytes[] = new byte[length];
random.nextBytes(bytes);
String result = "";
@ -1772,6 +1775,14 @@ public class PGPMain {
return result;
}
/**
* Go once through stream to get length of stream. The length is later used to display progress
* when encrypting/decrypting
*
* @param in
* @return
* @throws IOException
*/
public static long getLengthOfStream(InputStream in) throws IOException {
long size = 0;
long n = 0;
@ -1782,6 +1793,17 @@ public class PGPMain {
return size;
}
/**
* Deletes file securely by overwriting it with random data before deleting it.
*
* TODO: Does this really help on flash storage?
*
* @param context
* @param progress
* @param file
* @throws FileNotFoundException
* @throws IOException
*/
public static void deleteFileSecurely(Context context, ProgressDialogUpdater progress, File file)
throws FileNotFoundException, IOException {
long length = file.length();

View File

@ -295,7 +295,7 @@ public class ApgIntentService extends IntentService implements ProgressDialogUpd
// OutputStream
try {
while (true) {
streamFilename = PGPMain.generateRandomString(32);
streamFilename = PGPMain.generateRandomFilename(32);
if (streamFilename == null) {
throw new PGPMain.ApgGeneralException(
"couldn't generate random file name");
@ -448,7 +448,7 @@ public class ApgIntentService extends IntentService implements ProgressDialogUpd
// OutputStream
try {
while (true) {
streamFilename = PGPMain.generateRandomString(32);
streamFilename = PGPMain.generateRandomFilename(32);
if (streamFilename == null) {
throw new PGPMain.ApgGeneralException(
"couldn't generate random file name");

View File

@ -254,11 +254,12 @@ public class DecryptActivity extends SherlockFragmentActivity {
String action = intent.getAction();
String type = intent.getType();
mContentUri = intent.getData();
if (Intent.ACTION_VIEW.equals(action)) {
// Android's Action when opening file associated to APG (see AndroidManifest.xml)
// This gets the Uri, where an inputStream can be opened from
mContentUri = intent.getData();
// TODO: old implementation of ACTION_VIEW. Is this used in K9?
// Uri uri = mIntent.getData();
// try {
@ -893,7 +894,7 @@ public class DecryptActivity extends SherlockFragmentActivity {
case Id.request.filename: {
if (resultCode == RESULT_OK && data != null) {
try {
String path = data.getData().getPath();
String path = FileHelper.getPath(this, data.getData());
Log.d(Constants.TAG, "path=" + path);
mFilename.setText(path);

File diff suppressed because it is too large Load Diff