From ea361f4bf50fca2dea96e83a186fff7bb01bccd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Sat, 15 Feb 2014 18:51:58 +0100 Subject: [PATCH] select provider with preference --- .../demo/OpenPgpProviderActivity.java | 96 +++---------------- .../openpgp/util/OpenPgpListPreference.java | 4 + .../openpgp/util/OpenPgpListPreference.java | 4 + 3 files changed, 22 insertions(+), 82 deletions(-) diff --git a/OpenPGP-Keychain-API/example-app/src/main/java/org/sufficientlysecure/keychain/demo/OpenPgpProviderActivity.java b/OpenPGP-Keychain-API/example-app/src/main/java/org/sufficientlysecure/keychain/demo/OpenPgpProviderActivity.java index 7b1aecae9..33a94de61 100644 --- a/OpenPGP-Keychain-API/example-app/src/main/java/org/sufficientlysecure/keychain/demo/OpenPgpProviderActivity.java +++ b/OpenPGP-Keychain-API/example-app/src/main/java/org/sufficientlysecure/keychain/demo/OpenPgpProviderActivity.java @@ -22,9 +22,12 @@ import android.app.PendingIntent; import android.content.DialogInterface; import android.content.Intent; import android.content.IntentSender; +import android.content.SharedPreferences; import android.content.pm.ResolveInfo; import android.graphics.drawable.Drawable; import android.os.Bundle; +import android.preference.PreferenceManager; +import android.text.TextUtils; import android.util.Log; import android.view.View; import android.view.ViewGroup; @@ -101,7 +104,17 @@ public class OpenPgpProviderActivity extends Activity { } }); - selectCryptoProvider(); + SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); + String providerPackageName = settings.getString("openpgp_provider_list", ""); + if (TextUtils.isEmpty(providerPackageName)) { + Toast.makeText(this, "No OpenPGP Provider selected!", Toast.LENGTH_LONG).show(); + finish(); + } else { + // bind to service + mCryptoServiceConnection = new OpenPgpServiceConnection( + OpenPgpProviderActivity.this, providerPackageName); + mCryptoServiceConnection.bindToService(); + } } private void handleError(final OpenPgpError error) { @@ -283,85 +296,4 @@ public class OpenPgpProviderActivity extends Activity { } } - private void selectCryptoProvider() { - Intent intent = new Intent(OpenPgpConstants.SERVICE_INTENT); - - final ArrayList providerList = new ArrayList(); - - List resInfo = getPackageManager().queryIntentServices(intent, 0); - if (!resInfo.isEmpty()) { - for (ResolveInfo resolveInfo : resInfo) { - if (resolveInfo.serviceInfo == null) - continue; - - String packageName = resolveInfo.serviceInfo.packageName; - String simpleName = String.valueOf(resolveInfo.serviceInfo - .loadLabel(getPackageManager())); - Drawable icon = resolveInfo.serviceInfo.loadIcon(getPackageManager()); - providerList.add(new OpenPgpProviderElement(packageName, simpleName, icon)); - } - } - - AlertDialog.Builder alert = new AlertDialog.Builder(this); - alert.setTitle("Select OpenPGP Provider!"); - alert.setCancelable(false); - - if (!providerList.isEmpty()) { - // add "disable OpenPGP provider" - providerList.add(0, new OpenPgpProviderElement(null, "Disable OpenPGP Provider", - getResources().getDrawable(android.R.drawable.ic_menu_close_clear_cancel))); - - // Init ArrayAdapter with OpenPGP Providers - ListAdapter adapter = new ArrayAdapter(this, - android.R.layout.select_dialog_item, android.R.id.text1, providerList) { - public View getView(int position, View convertView, ViewGroup parent) { - // User super class to create the View - View v = super.getView(position, convertView, parent); - TextView tv = (TextView) v.findViewById(android.R.id.text1); - - // Put the image on the TextView - tv.setCompoundDrawablesWithIntrinsicBounds(providerList.get(position).icon, - null, null, null); - - // Add margin between image and text (support various screen densities) - int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f); - tv.setCompoundDrawablePadding(dp5); - - return v; - } - }; - - alert.setSingleChoiceItems(adapter, -1, new DialogInterface.OnClickListener() { - - public void onClick(DialogInterface dialog, int position) { - String packageName = providerList.get(position).packageName; - - if (packageName == null) { - dialog.cancel(); - finish(); - } - - // bind to service - mCryptoServiceConnection = new OpenPgpServiceConnection( - OpenPgpProviderActivity.this, packageName); - mCryptoServiceConnection.bindToService(); - - dialog.dismiss(); - } - }); - } else { - alert.setMessage("No OpenPGP Provider installed!"); - } - - alert.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { - - public void onClick(DialogInterface dialog, int id) { - dialog.cancel(); - finish(); - } - }); - - AlertDialog ad = alert.create(); - ad.show(); - } } 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 f1f326fca..034186a3a 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 @@ -36,6 +36,10 @@ 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 ArrayList mProviderList = new ArrayList(); private String mSelectedPackage; diff --git a/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpListPreference.java b/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpListPreference.java index f1f326fca..034186a3a 100644 --- a/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpListPreference.java +++ b/libraries/keychain-api-library/src/org/openintents/openpgp/util/OpenPgpListPreference.java @@ -36,6 +36,10 @@ 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 ArrayList mProviderList = new ArrayList(); private String mSelectedPackage;