aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptActivity.java
diff options
context:
space:
mode:
authorDominik Schürmann <dominik@dominikschuermann.de>2014-08-04 14:42:03 +0200
committerDominik Schürmann <dominik@dominikschuermann.de>2014-08-04 14:42:03 +0200
commit839294d27c4f8b3ac22573a1d429bc230f641bd2 (patch)
tree602985e170c040e801ddef45e519f354cf1e9b69 /OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptActivity.java
parentb42afcd32cc082d45daf64e37d89ba88f0c3eb56 (diff)
downloadopen-keychain-839294d27c4f8b3ac22573a1d429bc230f641bd2.tar.gz
open-keychain-839294d27c4f8b3ac22573a1d429bc230f641bd2.tar.bz2
open-keychain-839294d27c4f8b3ac22573a1d429bc230f641bd2.zip
Cleanup, prevent encrypt Intent inception
Diffstat (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptActivity.java')
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptActivity.java76
1 files changed, 74 insertions, 2 deletions
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptActivity.java
index 211802717..f9dbb2b6b 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptActivity.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptActivity.java
@@ -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;
}