From 44232afe944ecb1dad0492f30283b71f7be48bd2 Mon Sep 17 00:00:00 2001 From: mar-v-in Date: Wed, 26 Feb 2014 18:58:52 +0100 Subject: [PATCH] Show link to Market if no openpgp provider installed installed Issue #295 --- .../res/values/strings.xml | 1 + .../openpgp/util/OpenPgpListPreference.java | 52 +++++++++++++++++-- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/res/values/strings.xml b/OpenPGP-Keychain-API/libraries/keychain-api-library/res/values/strings.xml index a198d0b5e..44d6145d4 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/res/values/strings.xml +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/res/values/strings.xml @@ -2,5 +2,6 @@ None + Install OpenKeychain via %s \ No newline at end of file diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpListPreference.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpListPreference.java index 034186a3a..6a9d85b5f 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpListPreference.java +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpListPreference.java @@ -23,6 +23,7 @@ import android.content.Intent; import android.content.pm.ResolveInfo; import android.content.res.TypedArray; import android.graphics.drawable.Drawable; +import android.net.Uri; import android.preference.DialogPreference; import android.util.AttributeSet; import android.view.View; @@ -30,17 +31,21 @@ import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ListAdapter; import android.widget.TextView; +import org.sufficientlysecure.keychain.api.R; import java.util.ArrayList; import java.util.List; -import org.sufficientlysecure.keychain.api.R; - /** * Does not extend ListPreference, but is very similar to it! * http://grepcode.com/file_/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/android/preference/ListPreference.java/?v=source */ public class OpenPgpListPreference extends DialogPreference { + private static final String OPENKEYCHAIN_PACKAGE = "org.sufficientlysecure.keychain"; + private static final String MARKET_INTENT_URI_BASE = "market://details?id=%s"; + private static final Intent MARKET_INTENT = new Intent(Intent.ACTION_VIEW, Uri.parse( + String.format(MARKET_INTENT_URI_BASE, OPENKEYCHAIN_PACKAGE))); + private ArrayList mProviderList = new ArrayList(); private String mSelectedPackage; @@ -84,6 +89,26 @@ public class OpenPgpListPreference extends DialogPreference { } } + + // add install links if empty + if (mProviderList.isEmpty()) { + resInfo = getContext().getPackageManager().queryIntentActivities + (MARKET_INTENT, 0); + if (!resInfo.isEmpty()) { + for (ResolveInfo resolveInfo : resInfo) { + Intent marketIntent = new Intent(MARKET_INTENT); + intent.setPackage(resolveInfo.activityInfo.packageName); + Drawable icon = resolveInfo.activityInfo.loadIcon(getContext().getPackageManager()); + String marketName = String.valueOf(resolveInfo.activityInfo.applicationInfo + .loadLabel(getContext().getPackageManager())); + String simpleName = String.format(getContext().getString(R.string + .install_via), marketName); + mProviderList.add(new OpenPgpProviderEntry(OPENKEYCHAIN_PACKAGE, simpleName, + icon, marketIntent)); + } + } + } + // add "none"-entry mProviderList.add(0, new OpenPgpProviderEntry("", getContext().getString(R.string.openpgp_list_preference_none), @@ -114,7 +139,22 @@ public class OpenPgpListPreference extends DialogPreference { @Override public void onClick(DialogInterface dialog, int which) { - mSelectedPackage = mProviderList.get(which).packageName; + OpenPgpProviderEntry entry = mProviderList.get(which); + + if (entry.intent != null) { + /* + * Intents are called as activity + * + * Current approach is to assume the user installed the app. + * If he does not, the selected package is not valid. + * + * However applications should always consider this could happen, + * as the user might remove the currently used OpenPGP app. + */ + getContext().startActivity(entry.intent); + } + + mSelectedPackage = entry.packageName; /* * Clicking on an item simulates the positive button click, and dismisses @@ -190,6 +230,7 @@ public class OpenPgpListPreference extends DialogPreference { private String packageName; private String simpleName; private Drawable icon; + private Intent intent; public OpenPgpProviderEntry(String packageName, String simpleName, Drawable icon) { this.packageName = packageName; @@ -197,6 +238,11 @@ public class OpenPgpListPreference extends DialogPreference { this.icon = icon; } + public OpenPgpProviderEntry(String packageName, String simpleName, Drawable icon, Intent intent) { + this(packageName, simpleName, icon); + this.intent = intent; + } + @Override public String toString() { return simpleName;