Cleanup, prevent encrypt Intent inception

This commit is contained in:
Dominik Schürmann 2014-08-04 14:42:03 +02:00
parent b42afcd32c
commit 839294d27c
7 changed files with 98 additions and 25 deletions

View File

@ -51,8 +51,6 @@ public final class Constants {
public static final String CUSTOM_CONTACT_DATA_MIME_TYPE = "vnd.android.cursor.item/vnd.org.sufficientlysecure.keychain.key";
public static boolean KITKAT = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
public static int TEMPFILE_TTL = 24*60*60*1000; // 1 day
public static final class Path {

View File

@ -32,6 +32,7 @@ import android.provider.OpenableColumns;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.widget.Toast;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.compatibility.DialogFragmentWorkaround;
@ -136,9 +137,10 @@ public class FileHelper {
/**
* Opens the storage browser on Android 4.4 or later for opening a file
*
* @param fragment
* @param mimeType can be text/plain for example
* @param multiple allow file chooser to return multiple files
* @param mimeType can be text/plain for example
* @param multiple allow file chooser to return multiple files
* @param requestCode used to identify the result coming back from storage browser onActivityResult() in your
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
@ -153,10 +155,11 @@ public class FileHelper {
/**
* Opens the storage browser on Android 4.4 or later for saving a file
*
* @param fragment
* @param mimeType can be text/plain for example
* @param mimeType can be text/plain for example
* @param suggestedName a filename desirable for the file to be saved
* @param requestCode used to identify the result coming back from storage browser onActivityResult() in your
* @param requestCode used to identify the result coming back from storage browser onActivityResult() in your
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
public static void saveDocument(Fragment fragment, String mimeType, String suggestedName, int requestCode) {
@ -210,7 +213,7 @@ public class FileHelper {
* Retrieve thumbnail of file, document api feature and thus KitKat only
*/
public static Bitmap getThumbnail(Context context, Uri uri, Point size) {
if (Constants.KITKAT) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
return DocumentsContract.getDocumentThumbnail(context.getContentResolver(), uri, size, null);
} else {
return null;
@ -218,10 +221,10 @@ public class FileHelper {
}
public static String readableFileSize(long size) {
if(size <= 0) return "0";
final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" };
int digitGroups = (int) (Math.log10(size)/Math.log10(1024));
return new DecimalFormat("#,##0.#").format(size/Math.pow(1024, digitGroups)) + " " + units[digitGroups];
if (size <= 0) return "0";
final String[] units = new String[]{"B", "KB", "MB", "GB", "TB"};
int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
}
public static interface FileDialogCallback {

View File

@ -165,8 +165,8 @@ public class KeychainDatabase extends SQLiteOpenHelper {
// make sure this is only done once, on the first instance!
boolean iAmIt = false;
synchronized(KeychainDatabase.class) {
if(!KeychainDatabase.apgHack) {
synchronized (KeychainDatabase.class) {
if (!KeychainDatabase.apgHack) {
iAmIt = true;
KeychainDatabase.apgHack = true;
}
@ -334,9 +334,9 @@ public class KeychainDatabase extends SQLiteOpenHelper {
File out;
if (restore) {
in = context.getDatabasePath("debug_backup.db");
out = context.getDatabasePath("openkeychain.db");
out = context.getDatabasePath(DATABASE_NAME);
} else {
in = context.getDatabasePath("openkeychain.db");
in = context.getDatabasePath(DATABASE_NAME);
out = context.getDatabasePath("debug_backup.db");
out.createNewFile();
}

View File

@ -21,6 +21,7 @@ import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.os.Messenger;
@ -72,7 +73,7 @@ public class DecryptFileFragment extends DecryptFragment {
mDecryptButton = view.findViewById(R.id.decrypt_file_action_decrypt);
view.findViewById(R.id.decrypt_file_browse).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (Constants.KITKAT) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
FileHelper.openDocument(DecryptFileFragment.this, "*/*", REQUEST_CODE_INPUT);
} else {
FileHelper.openFile(DecryptFileFragment.this, mInputUri, "*/*",
@ -126,7 +127,7 @@ public class DecryptFileFragment extends DecryptFragment {
private void askForOutputFilename() {
String targetName = removeEncryptedAppend(FileHelper.getFilename(getActivity(), mInputUri));
if (!Constants.KITKAT) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
File file = new File(mInputUri.getPath());
File parentDir = file.exists() ? file.getParentFile() : Constants.Path.APP_DIR;
File targetFile = new File(parentDir, targetName);

View File

@ -20,16 +20,20 @@ package org.sufficientlysecure.keychain.ui;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.LabeledIntent;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Messenger;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.compatibility.ClipboardReflection;
@ -45,7 +49,11 @@ import org.sufficientlysecure.keychain.util.Log;
import org.sufficientlysecure.keychain.util.Notify;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class EncryptActivity extends DrawerActivity implements EncryptActivityInterface {
@ -228,7 +236,7 @@ public class EncryptActivity extends DrawerActivity implements EncryptActivityIn
if (mShareAfterEncrypt) {
// Share encrypted file
startActivity(Intent.createChooser(createSendIntent(message), getString(R.string.title_share_file)));
startActivity(sendCreateChooserExcludingOpenKeychain(message));
} else if (isContentMessage()) {
// Copy to clipboard
copyToClipboard(message);
@ -289,6 +297,69 @@ public class EncryptActivity extends DrawerActivity implements EncryptActivityIn
ClipboardReflection.copyToClipboard(this, new String(message.getData().getByteArray(KeychainIntentService.RESULT_BYTES)));
}
/**
* Create Intent Chooser but exclude OK's EncryptActivity.
* <p/>
* Put together from some stackoverflow posts...
*
* @param message
* @return
*/
private Intent sendCreateChooserExcludingOpenKeychain(Message message) {
Intent prototype = createSendIntent(message);
String[] blacklist = new String[]{Constants.PACKAGE_NAME + ".ui.EncryptActivity"};
List<LabeledIntent> targetedShareIntents = new ArrayList<LabeledIntent>();
List<ResolveInfo> resInfoList = getPackageManager().queryIntentActivities(prototype, 0);
List<ResolveInfo> resInfoListFiltered = new ArrayList<ResolveInfo>();
if (!resInfoList.isEmpty()) {
for (ResolveInfo resolveInfo : resInfoList) {
// do not add blacklisted ones
if (resolveInfo.activityInfo == null || Arrays.asList(blacklist).contains(resolveInfo.activityInfo.name))
continue;
resInfoListFiltered.add(resolveInfo);
}
if (!resInfoListFiltered.isEmpty()) {
// sorting for nice readability
Collections.sort(resInfoListFiltered, new Comparator<ResolveInfo>() {
@Override
public int compare(ResolveInfo first, ResolveInfo second) {
String firstName = first.loadLabel(getPackageManager()).toString();
String secondName = second.loadLabel(getPackageManager()).toString();
return firstName.compareToIgnoreCase(secondName);
}
});
// create the custom intent list
for (ResolveInfo resolveInfo : resInfoListFiltered) {
Intent targetedShareIntent = (Intent) prototype.clone();
targetedShareIntent.setPackage(resolveInfo.activityInfo.packageName);
targetedShareIntent.setClassName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name);
LabeledIntent lIntent = new LabeledIntent(targetedShareIntent,
resolveInfo.activityInfo.packageName,
resolveInfo.loadLabel(getPackageManager()),
resolveInfo.activityInfo.icon);
targetedShareIntents.add(lIntent);
}
// Create chooser with only one Intent in it
Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(targetedShareIntents.size() - 1), getString(R.string.title_share_file));
// append all other Intents
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
return chooserIntent;
}
}
// fallback to Android's default chooser
return Intent.createChooser(prototype, getString(R.string.title_share_file));
}
private Intent createSendIntent(Message message) {
Intent sendIntent;
if (isContentMessage()) {
@ -380,7 +451,8 @@ public class EncryptActivity extends DrawerActivity implements EncryptActivityIn
startEncrypt();
}
}
});
}
);
return false;
}

View File

@ -27,7 +27,6 @@ import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
@ -39,7 +38,6 @@ import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.helper.FileHelper;
import org.sufficientlysecure.keychain.helper.OtherHelper;
import org.sufficientlysecure.keychain.provider.TemporaryStorageProvider;
import org.sufficientlysecure.keychain.util.Log;
import java.io.File;
import java.util.HashMap;
@ -117,7 +115,7 @@ public class EncryptFileFragment extends Fragment implements EncryptActivityInte
}
private void addInputUri() {
if (Constants.KITKAT) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
FileHelper.openDocument(EncryptFileFragment.this, "*/*", true, REQUEST_CODE_INPUT);
} else {
FileHelper.openFile(EncryptFileFragment.this, mEncryptInterface.getInputUris().isEmpty() ?
@ -174,7 +172,7 @@ public class EncryptFileFragment extends Fragment implements EncryptActivityInte
throw new IllegalStateException();
}
Uri inputUri = mEncryptInterface.getInputUris().get(0);
if (!Constants.KITKAT) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
File file = new File(inputUri.getPath());
File parentDir = file.exists() ? file.getParentFile() : Constants.Path.APP_DIR;
String targetName = FileHelper.getFilename(getActivity(), inputUri) +
@ -219,7 +217,7 @@ public class EncryptFileFragment extends Fragment implements EncryptActivityInte
switch (requestCode) {
case REQUEST_CODE_INPUT: {
if (resultCode == Activity.RESULT_OK && data != null) {
if (!Constants.KITKAT || !handleClipData(data)) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT || !handleClipData(data)) {
addInputUri(data.getData());
}
}

View File

@ -20,6 +20,7 @@ package org.sufficientlysecure.keychain.ui.dialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.DocumentsContract;
import android.support.v4.app.DialogFragment;
@ -71,7 +72,7 @@ public class DeleteFileDialogFragment extends DialogFragment {
dismiss();
// We can not securely delete Uris, so just use usual delete on them
if (Constants.KITKAT) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (DocumentsContract.deleteDocument(getActivity().getContentResolver(), deleteUri)) {
Toast.makeText(getActivity(), R.string.file_delete_successful, Toast.LENGTH_SHORT).show();
return;