From f2f1a9516d2c877eda994b3debdaa462319f4941 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Sat, 6 Sep 2014 17:39:00 +0200 Subject: Share helper --- .../keychain/helper/ShareHelper.java | 101 +++++++++++++++++++++ .../keychain/ui/EncryptFileActivity.java | 72 ++------------- .../keychain/ui/EncryptTextActivity.java | 69 +------------- 3 files changed, 113 insertions(+), 129 deletions(-) create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/ShareHelper.java (limited to 'OpenKeychain') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/ShareHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/ShareHelper.java new file mode 100644 index 000000000..58178e296 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/ShareHelper.java @@ -0,0 +1,101 @@ +package org.sufficientlysecure.keychain.helper; + +import android.content.Context; +import android.content.Intent; +import android.content.pm.LabeledIntent; +import android.content.pm.ResolveInfo; +import android.os.Build; +/* + * Copyright (C) 2014 Dominik Schürmann + * + * 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. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +import android.os.Parcelable; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +public class ShareHelper { + Context mContext; + + public ShareHelper(Context context) { + mContext = context; + } + + /** + * Create Intent Chooser but exclude OK's EncryptActivity. + *

+ * Put together from some stackoverflow posts... + */ + public Intent createChooserExcluding(Intent prototype, String title, String[] activityBlacklist) { + // Produced an empty list on Huawei U8860 with Android Version 4.0.3 and weird results on 2.3 + // TODO: test on 4.1, 4.2, 4.3, only tested on 4.4 + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { + return Intent.createChooser(prototype, title); + } + + List targetedShareIntents = new ArrayList(); + + List resInfoList = mContext.getPackageManager().queryIntentActivities(prototype, 0); + List resInfoListFiltered = new ArrayList(); + if (!resInfoList.isEmpty()) { + for (ResolveInfo resolveInfo : resInfoList) { + // do not add blacklisted ones + if (resolveInfo.activityInfo == null || Arrays.asList(activityBlacklist).contains(resolveInfo.activityInfo.name)) + continue; + + resInfoListFiltered.add(resolveInfo); + } + + if (!resInfoListFiltered.isEmpty()) { + // sorting for nice readability + Collections.sort(resInfoListFiltered, new Comparator() { + @Override + public int compare(ResolveInfo first, ResolveInfo second) { + String firstName = first.loadLabel(mContext.getPackageManager()).toString(); + String secondName = second.loadLabel(mContext.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(mContext.getPackageManager()), + resolveInfo.activityInfo.icon); + targetedShareIntents.add(lIntent); + } + + // Create chooser with only one Intent in it + Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(targetedShareIntents.size() - 1), title); + // 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, title); + } +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptFileActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptFileActivity.java index 8fc72ca89..0f4503652 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptFileActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptFileActivity.java @@ -33,6 +33,7 @@ import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.compatibility.ClipboardReflection; import org.sufficientlysecure.keychain.helper.Preferences; +import org.sufficientlysecure.keychain.helper.ShareHelper; import org.sufficientlysecure.keychain.pgp.KeyRing; import org.sufficientlysecure.keychain.service.KeychainIntentService; import org.sufficientlysecure.keychain.service.KeychainIntentServiceHandler; @@ -262,74 +263,13 @@ public class EncryptFileActivity extends DrawerActivity implements EncryptActivi /** * Create Intent Chooser but exclude OK's EncryptActivity. - *

- * Put together from some stackoverflow posts... - * - * @param message - * @return */ private Intent sendWithChooserExcludingEncrypt(Message message) { Intent prototype = createSendIntent(message); - String title = getString(R.string.title_share_file); + String[] blacklist = new String[]{Constants.PACKAGE_NAME + ".ui.EncryptActivity"}; - // Disabled, produced an empty list on Huawei U8860 with Android Version 4.0.3 -// // fallback on Android 2.3, otherwise we would get weird results -// if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) { -// return Intent.createChooser(prototype, title); -// } -// -// // prevent recursion aka Inception :P -// String[] blacklist = new String[]{Constants.PACKAGE_NAME + ".ui.EncryptActivity"}; -// -// List targetedShareIntents = new ArrayList(); -// -// List resInfoList = getPackageManager().queryIntentActivities(prototype, 0); -// List resInfoListFiltered = new ArrayList(); -// 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() { -// @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), title); -// // 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, title); + return new ShareHelper(this).createChooserExcluding(prototype, title, blacklist); } private Intent createSendIntent(Message message) { @@ -452,7 +392,8 @@ public class EncryptFileActivity extends DrawerActivity implements EncryptActivi .replace(R.id.encrypt_pager_mode, mCurrentMode == MODE_SYMMETRIC ? new EncryptSymmetricFragment() - : new EncryptAsymmetricFragment()) + : new EncryptAsymmetricFragment() + ) .commitAllowingStateLoss(); getSupportFragmentManager().executePendingTransactions(); } @@ -520,7 +461,8 @@ public class EncryptFileActivity extends DrawerActivity implements EncryptActivi action = ACTION_ENCRYPT_FILE; } - } else */ { + } else */ + { // Files via content provider, override uri and action uris.clear(); uris.add(intent.getParcelableExtra(Intent.EXTRA_STREAM)); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptTextActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptTextActivity.java index d1bdfe14b..5386178f0 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptTextActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptTextActivity.java @@ -33,6 +33,7 @@ import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.compatibility.ClipboardReflection; import org.sufficientlysecure.keychain.helper.Preferences; +import org.sufficientlysecure.keychain.helper.ShareHelper; import org.sufficientlysecure.keychain.pgp.KeyRing; import org.sufficientlysecure.keychain.service.KeychainIntentService; import org.sufficientlysecure.keychain.service.KeychainIntentServiceHandler; @@ -245,74 +246,13 @@ public class EncryptTextActivity extends DrawerActivity implements EncryptActivi /** * Create Intent Chooser but exclude OK's EncryptActivity. - *

- * Put together from some stackoverflow posts... - * - * @param message - * @return */ private Intent sendWithChooserExcludingEncrypt(Message message) { Intent prototype = createSendIntent(message); - String title = getString(R.string.title_share_message); + String[] blacklist = new String[]{Constants.PACKAGE_NAME + ".ui.EncryptActivity"}; - // Disabled, produced an empty list on Huawei U8860 with Android Version 4.0.3 -// // fallback on Android 2.3, otherwise we would get weird results -// if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) { -// return Intent.createChooser(prototype, title); -// } -// -// // prevent recursion aka Inception :P -// String[] blacklist = new String[]{Constants.PACKAGE_NAME + ".ui.EncryptActivity"}; -// -// List targetedShareIntents = new ArrayList(); -// -// List resInfoList = getPackageManager().queryIntentActivities(prototype, 0); -// List resInfoListFiltered = new ArrayList(); -// 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() { -// @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), title); -// // 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, title); + return new ShareHelper(this).createChooserExcluding(prototype, title, blacklist); } private Intent createSendIntent(Message message) { @@ -418,7 +358,8 @@ public class EncryptTextActivity extends DrawerActivity implements EncryptActivi .replace(R.id.encrypt_pager_mode, mCurrentMode == MODE_SYMMETRIC ? new EncryptSymmetricFragment() - : new EncryptAsymmetricFragment()) + : new EncryptAsymmetricFragment() + ) .commitAllowingStateLoss(); getSupportFragmentManager().executePendingTransactions(); } -- cgit v1.2.3