Cleanup, prevent encrypt Intent inception
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user