From 1beb85acf5d4900a9cc558d077b0df4ed6c683c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Fri, 6 Sep 2013 11:55:08 +0200 Subject: Register works basically --- .../keychain/helper/OtherHelper.java | 2 +- .../keychain/provider/ProviderHelper.java | 11 +- .../keychain/remote_api/CryptoService.java | 39 +-- .../keychain/remote_api/CryptoServiceActivity.java | 266 +++++++++++++++++++++ .../keychain/remote_api/ServiceActivity.java | 263 -------------------- 5 files changed, 298 insertions(+), 283 deletions(-) create mode 100644 OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/CryptoServiceActivity.java delete mode 100644 OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/ServiceActivity.java (limited to 'OpenPGP-Keychain/src/org') diff --git a/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/helper/OtherHelper.java b/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/helper/OtherHelper.java index e38b1b726..5dc561923 100644 --- a/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/helper/OtherHelper.java +++ b/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/helper/OtherHelper.java @@ -120,7 +120,7 @@ public class OtherHelper { public static void checkPackagePermissionForActions(Activity activity, String pkgName, String permName, String action, String[] restrictedActions) { if (action != null) { - PackageManager pkgManager = activity.getPackageManager(); +// PackageManager pkgManager = activity.getPackageManager(); // for (int i = 0; i < restrictedActions.length; i++) { // if (restrictedActions[i].equals(action)) { diff --git a/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/provider/ProviderHelper.java b/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/provider/ProviderHelper.java index c8f9baeff..60aa0542e 100644 --- a/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/provider/ProviderHelper.java +++ b/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/provider/ProviderHelper.java @@ -718,9 +718,9 @@ public class ProviderHelper { return cursor; } - public static ArrayList getCryptoConsumers(Context context) { - Cursor cursor = context.getContentResolver().query(ApiApps.CONTENT_URI, null, null, - null, null); + public static ArrayList getRegisteredApiApps(Context context) { + Cursor cursor = context.getContentResolver().query(ApiApps.CONTENT_URI, null, null, null, + null); ArrayList packageNames = new ArrayList(); if (cursor != null) { @@ -739,9 +739,12 @@ public class ProviderHelper { return packageNames; } - public static void addCryptoConsumer(Context context, String packageName) { + public static void addCryptoConsumer(Context context, String packageName, long keyId, + boolean asciiArmor) { ContentValues values = new ContentValues(); values.put(ApiApps.PACKAGE_NAME, packageName); + values.put(ApiApps.KEY_ID, keyId); + values.put(ApiApps.ASCII_ARMOR, asciiArmor); context.getContentResolver().insert(ApiApps.CONTENT_URI, values); } } diff --git a/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/CryptoService.java b/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/CryptoService.java index 71b78ee01..2e6ab4263 100644 --- a/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/CryptoService.java +++ b/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/CryptoService.java @@ -76,10 +76,9 @@ public class CryptoService extends Service { public IBinder onBind(Intent intent) { // return different binder for connections from internal service activity if (ACTION_SERVICE_ACTIVITY.equals(intent.getAction())) { - String callingPackageName = intent.getPackage(); // this binder can only be used from OpenPGP Keychain - if (callingPackageName.equals(Constants.PACKAGE_NAME)) { + if (isCallerAllowed(true)) { return mBinderServiceActivity; } else { Log.e(Constants.TAG, "This binder can only be used from " + Constants.PACKAGE_NAME); @@ -150,8 +149,8 @@ public class CryptoService extends Service { // start passphrase dialog Bundle extras = new Bundle(); - extras.putLong(ServiceActivity.EXTRA_SECRET_KEY_ID, secretKeyId); - pauseQueueAndStartServiceActivity(ServiceActivity.ACTION_CACHE_PASSPHRASE, extras); + extras.putLong(CryptoServiceActivity.EXTRA_SECRET_KEY_ID, secretKeyId); + pauseQueueAndStartServiceActivity(CryptoServiceActivity.ACTION_CACHE_PASSPHRASE, extras); } // if (signedOnly) { @@ -255,7 +254,7 @@ public class CryptoService extends Service { public void setup(boolean asciiArmor, boolean newKeyring, String newKeyringUserId) throws RemoteException { // TODO Auto-generated method stub - + } }; @@ -267,7 +266,7 @@ public class CryptoService extends Service { if (success) { // resume threads - if (isPackageAllowed(packageName)) { + if (isPackageAllowed(packageName, false)) { mThreadPool.resume(); } else { // TODO: should not happen? @@ -287,7 +286,7 @@ public class CryptoService extends Service { }; private void checkAndEnqueue(Runnable r) { - if (isCallerAllowed()) { + if (isCallerAllowed(false)) { mThreadPool.execute(r); Log.d(Constants.TAG, "Enqueued runnable…"); @@ -298,8 +297,8 @@ public class CryptoService extends Service { Log.e(Constants.TAG, "Not allowed to use service! Starting activity for registration!"); Bundle extras = new Bundle(); // TODO: currently simply uses first entry - extras.putString(ServiceActivity.EXTRA_PACKAGE_NAME, callingPackages[0]); - pauseQueueAndStartServiceActivity(ServiceActivity.ACTION_REGISTER, extras); + extras.putString(CryptoServiceActivity.EXTRA_PACKAGE_NAME, callingPackages[0]); + pauseQueueAndStartServiceActivity(CryptoServiceActivity.ACTION_REGISTER, extras); mThreadPool.execute(r); @@ -311,16 +310,18 @@ public class CryptoService extends Service { * Checks if process that binds to this service (i.e. the package name corresponding to the * process) is in the list of allowed package names. * + * @param allowOnlySelf + * allow only Keychain app itself * @return true if process is allowed to use this service */ - private boolean isCallerAllowed() { + private boolean isCallerAllowed(boolean allowOnlySelf) { String[] callingPackages = getPackageManager().getPackagesForUid(Binder.getCallingUid()); // is calling package allowed to use this service? for (int i = 0; i < callingPackages.length; i++) { String currentPkg = callingPackages[i]; - if (isPackageAllowed(currentPkg)) { + if (isPackageAllowed(currentPkg, allowOnlySelf)) { return true; } } @@ -329,14 +330,22 @@ public class CryptoService extends Service { return false; } - private boolean isPackageAllowed(String packageName) { + /** + * Checks if packageName is a registered app for the API. + * + * @param packageName + * @param allowOnlySelf + * allow only Keychain app itself + * @return + */ + private boolean isPackageAllowed(String packageName, boolean allowOnlySelf) { Log.d(Constants.TAG, "packageName: " + packageName); - ArrayList allowedPkgs = ProviderHelper.getCryptoConsumers(mContext); + ArrayList allowedPkgs = ProviderHelper.getRegisteredApiApps(mContext); Log.d(Constants.TAG, "allowed: " + allowedPkgs); // check if package is allowed to use our service - if (allowedPkgs.contains(packageName)) { + if (allowedPkgs.contains(packageName) && (!allowOnlySelf)) { Log.d(Constants.TAG, "Package is allowed! packageName: " + packageName); return true; @@ -353,7 +362,7 @@ public class CryptoService extends Service { mThreadPool.pause(); Log.d(Constants.TAG, "starting activity..."); - Intent intent = new Intent(getBaseContext(), ServiceActivity.class); + Intent intent = new Intent(getBaseContext(), CryptoServiceActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(action); if (extras != null) { diff --git a/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/CryptoServiceActivity.java b/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/CryptoServiceActivity.java new file mode 100644 index 000000000..39ff79f16 --- /dev/null +++ b/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/CryptoServiceActivity.java @@ -0,0 +1,266 @@ +/* + * Copyright (C) 2013 Dominik Schürmann + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.sufficientlysecure.keychain.remote_api; + +import org.sufficientlysecure.keychain.Constants; +import org.sufficientlysecure.keychain.Id; +import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.helper.PgpMain; +import org.sufficientlysecure.keychain.provider.ProviderHelper; +import org.sufficientlysecure.keychain.ui.dialog.PassphraseDialogFragment; +import org.sufficientlysecure.keychain.util.Log; + +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.ServiceConnection; +import android.os.Bundle; +import android.os.Handler; +import android.os.IBinder; +import android.os.Message; +import android.os.Messenger; +import android.os.RemoteException; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; +import android.widget.Toast; + +import com.actionbarsherlock.app.ActionBar; +import com.actionbarsherlock.app.SherlockFragmentActivity; + +public class CryptoServiceActivity extends SherlockFragmentActivity { + + public static final String ACTION_REGISTER = "org.sufficientlysecure.keychain.remote_api.REGISTER"; + public static final String ACTION_CACHE_PASSPHRASE = "org.sufficientlysecure.keychain.remote_api.CRYPTO_CACHE_PASSPHRASE"; + + public static final String EXTRA_SECRET_KEY_ID = "secretKeyId"; + public static final String EXTRA_PACKAGE_NAME = "packageName"; + + private IServiceActivityCallback mServiceCallback; + private boolean mServiceBound; + + // view + AppSettingsFragment settingsFragment; + + private ServiceConnection mServiceActivityConnection = new ServiceConnection() { + public void onServiceConnected(ComponentName name, IBinder service) { + mServiceCallback = IServiceActivityCallback.Stub.asInterface(service); + Log.d(Constants.TAG, "connected to ICryptoServiceActivity"); + mServiceBound = true; + } + + public void onServiceDisconnected(ComponentName name) { + mServiceCallback = null; + Log.d(Constants.TAG, "disconnected from ICryptoServiceActivity"); + mServiceBound = false; + } + }; + + /** + * If not already bound, bind! + * + * @return + */ + public boolean bindToService() { + if (mServiceCallback == null && !mServiceBound) { // if not already connected + try { + Log.d(Constants.TAG, "not bound yet"); + + Intent serviceIntent = new Intent(); + serviceIntent + .setAction("org.sufficientlysecure.keychain.crypto_provider.IServiceActivityCallback"); + bindService(serviceIntent, mServiceActivityConnection, Context.BIND_AUTO_CREATE); + + return true; + } catch (Exception e) { + Log.d(Constants.TAG, "Exception", e); + return false; + } + } else { // already connected + Log.d(Constants.TAG, "already bound... "); + return true; + } + } + + public void unbindFromService() { + unbindService(mServiceActivityConnection); + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + Log.d(Constants.TAG, "onCreate…"); + + // bind to our own crypto service + bindToService(); + + handleActions(getIntent()); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + + // unbind from our crypto service + if (mServiceActivityConnection != null) { + unbindFromService(); + } + } + + protected void handleActions(Intent intent) { + String action = intent.getAction(); + Bundle extras = intent.getExtras(); + + if (extras == null) { + extras = new Bundle(); + } + + /** + * com.android.crypto actions + */ + if (ACTION_REGISTER.equals(action)) { + final String packageName = extras.getString(EXTRA_PACKAGE_NAME); + + // Inflate a "Done"/"Cancel" custom action bar view + final LayoutInflater inflater = (LayoutInflater) getSupportActionBar() + .getThemedContext().getSystemService(LAYOUT_INFLATER_SERVICE); + final View customActionBarView = inflater.inflate( + R.layout.actionbar_custom_view_done_cancel, null); + + ((TextView) customActionBarView.findViewById(R.id.actionbar_done_text)) + .setText(R.string.api_register_allow); + customActionBarView.findViewById(R.id.actionbar_done).setOnClickListener( + new View.OnClickListener() { + @Override + public void onClick(View v) { + // Allow + + if (settingsFragment.getSecretKeyId() == Id.key.none) { + Toast.makeText(CryptoServiceActivity.this, + R.string.api_register_error_select_key, Toast.LENGTH_LONG) + .show(); + } else { + ProviderHelper.addCryptoConsumer(CryptoServiceActivity.this, + packageName, settingsFragment.getSecretKeyId(), + settingsFragment.isAsciiArmor()); + // Intent data = new Intent(); + + try { + mServiceCallback.onRegistered(true, packageName); + } catch (RemoteException e) { + Log.e(Constants.TAG, "ServiceActivity"); + } + finish(); + } + } + }); + ((TextView) customActionBarView.findViewById(R.id.actionbar_cancel_text)) + .setText(R.string.api_register_disallow); + customActionBarView.findViewById(R.id.actionbar_cancel).setOnClickListener( + new View.OnClickListener() { + @Override + public void onClick(View v) { + // Disallow + try { + mServiceCallback.onRegistered(false, packageName); + } catch (RemoteException e) { + Log.e(Constants.TAG, "ServiceActivity"); + } + finish(); + } + }); + + // Show the custom action bar view and hide the normal Home icon and title. + final ActionBar actionBar = getSupportActionBar(); + actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM, + ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME + | ActionBar.DISPLAY_SHOW_TITLE); + actionBar.setCustomView(customActionBarView, new ActionBar.LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); + + setContentView(R.layout.api_app_register_activity); + + settingsFragment = (AppSettingsFragment) getSupportFragmentManager().findFragmentById( + R.id.api_app_settings_fragment); + + settingsFragment.setPackage(packageName); + + // TODO: handle if app is already registered + // LinearLayout layoutRegister = (LinearLayout) + // findViewById(R.id.register_crypto_consumer_register_layout); + // LinearLayout layoutEdit = (LinearLayout) + // findViewById(R.id.register_crypto_consumer_edit_layout); + // + // // if already registered show edit buttons + // ArrayList allowedPkgs = ProviderHelper.getCryptoConsumers(this); + // if (allowedPkgs.contains(packageName)) { + // Log.d(Constants.TAG, "Package is allowed! packageName: " + packageName); + // layoutRegister.setVisibility(View.GONE); + // layoutEdit.setVisibility(View.VISIBLE); + // } else { + // layoutRegister.setVisibility(View.VISIBLE); + // layoutEdit.setVisibility(View.GONE); + // } + + } else if (ACTION_CACHE_PASSPHRASE.equals(action)) { + long secretKeyId = extras.getLong(EXTRA_SECRET_KEY_ID); + + showPassphraseDialog(secretKeyId); + } else { + Log.e(Constants.TAG, "Wrong action!"); + finish(); + } + } + + /** + * Shows passphrase dialog to cache a new passphrase the user enters for using it later for + * encryption. Based on mSecretKeyId it asks for a passphrase to open a private key or it asks + * for a symmetric passphrase + */ + private void showPassphraseDialog(long secretKeyId) { + // Message is received after passphrase is cached + Handler returnHandler = new Handler() { + @Override + public void handleMessage(Message message) { + if (message.what == PassphraseDialogFragment.MESSAGE_OKAY) { + try { + mServiceCallback.onCachedPassphrase(true); + } catch (RemoteException e) { + Log.e(Constants.TAG, "ServiceActivity"); + } + finish(); + } + } + }; + + // Create a new Messenger for the communication back + Messenger messenger = new Messenger(returnHandler); + + try { + PassphraseDialogFragment passphraseDialog = PassphraseDialogFragment.newInstance(this, + messenger, secretKeyId); + + passphraseDialog.show(getSupportFragmentManager(), "passphraseDialog"); + } catch (PgpMain.PgpGeneralException e) { + Log.d(Constants.TAG, "No passphrase for this secret key, encrypt directly!"); + // send message to handler to start encryption directly + returnHandler.sendEmptyMessage(PassphraseDialogFragment.MESSAGE_OKAY); + } + } +} diff --git a/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/ServiceActivity.java b/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/ServiceActivity.java deleted file mode 100644 index 422026a19..000000000 --- a/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/ServiceActivity.java +++ /dev/null @@ -1,263 +0,0 @@ -/* - * Copyright (C) 2013 Dominik Schürmann - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.sufficientlysecure.keychain.remote_api; - -import org.sufficientlysecure.keychain.Constants; -import org.sufficientlysecure.keychain.Id; -import org.sufficientlysecure.keychain.R; -import org.sufficientlysecure.keychain.helper.PgpMain; -import org.sufficientlysecure.keychain.provider.ProviderHelper; -import org.sufficientlysecure.keychain.ui.dialog.PassphraseDialogFragment; -import org.sufficientlysecure.keychain.util.Log; - -import android.content.ComponentName; -import android.content.Context; -import android.content.Intent; -import android.content.ServiceConnection; -import android.os.Bundle; -import android.os.Handler; -import android.os.IBinder; -import android.os.Message; -import android.os.Messenger; -import android.os.RemoteException; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.TextView; -import android.widget.Toast; - -import com.actionbarsherlock.app.ActionBar; -import com.actionbarsherlock.app.SherlockFragmentActivity; - -public class ServiceActivity extends SherlockFragmentActivity { - - public static final String ACTION_REGISTER = "org.sufficientlysecure.keychain.remote_api.REGISTER"; - public static final String ACTION_CACHE_PASSPHRASE = "org.sufficientlysecure.keychain.remote_api.CRYPTO_CACHE_PASSPHRASE"; - - public static final String EXTRA_SECRET_KEY_ID = "secretKeyId"; - public static final String EXTRA_PACKAGE_NAME = "packageName"; - - private IServiceActivityCallback mServiceCallback; - private boolean mServiceBound; - - // view - AppSettingsFragment settingsFragment; - - private ServiceConnection mServiceActivityConnection = new ServiceConnection() { - public void onServiceConnected(ComponentName name, IBinder service) { - mServiceCallback = IServiceActivityCallback.Stub.asInterface(service); - Log.d(Constants.TAG, "connected to ICryptoServiceActivity"); - mServiceBound = true; - } - - public void onServiceDisconnected(ComponentName name) { - mServiceCallback = null; - Log.d(Constants.TAG, "disconnected from ICryptoServiceActivity"); - mServiceBound = false; - } - }; - - /** - * If not already bound, bind! - * - * @return - */ - public boolean bindToService() { - if (mServiceCallback == null && !mServiceBound) { // if not already connected - try { - Log.d(Constants.TAG, "not bound yet"); - - Intent serviceIntent = new Intent(); - serviceIntent.setAction("org.openintents.crypto.ICryptoService"); - bindService(serviceIntent, mServiceActivityConnection, Context.BIND_AUTO_CREATE); - - return true; - } catch (Exception e) { - Log.d(Constants.TAG, "Exception", e); - return false; - } - } else { // already connected - Log.d(Constants.TAG, "already bound... "); - return true; - } - } - - public void unbindFromService() { - unbindService(mServiceActivityConnection); - } - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - Log.d(Constants.TAG, "onCreate…"); - - // bind to our own crypto service - bindToService(); - - handleActions(getIntent()); - } - - @Override - protected void onDestroy() { - super.onDestroy(); - - // unbind from our crypto service - if (mServiceActivityConnection != null) { - unbindFromService(); - } - } - - protected void handleActions(Intent intent) { - String action = intent.getAction(); - Bundle extras = intent.getExtras(); - - if (extras == null) { - extras = new Bundle(); - } - - /** - * com.android.crypto actions - */ - if (ACTION_REGISTER.equals(action)) { - final String packageName = extras.getString(EXTRA_PACKAGE_NAME); - - // Inflate a "Done"/"Cancel" custom action bar view - final LayoutInflater inflater = (LayoutInflater) getSupportActionBar() - .getThemedContext().getSystemService(LAYOUT_INFLATER_SERVICE); - final View customActionBarView = inflater.inflate( - R.layout.actionbar_custom_view_done_cancel, null); - - ((TextView) customActionBarView.findViewById(R.id.actionbar_done_text)) - .setText(R.string.api_register_allow); - customActionBarView.findViewById(R.id.actionbar_done).setOnClickListener( - new View.OnClickListener() { - @Override - public void onClick(View v) { - // Allow - - if (settingsFragment.getSecretKeyId() == Id.key.none) { - Toast.makeText(ServiceActivity.this, - R.string.api_register_error_select_key, Toast.LENGTH_LONG) - .show(); - } else { - ProviderHelper.addCryptoConsumer(ServiceActivity.this, packageName); - // Intent data = new Intent(); - - try { - mServiceCallback.onRegistered(true, packageName); - } catch (RemoteException e) { - Log.e(Constants.TAG, "ServiceActivity"); - } - finish(); - } - } - }); - ((TextView) customActionBarView.findViewById(R.id.actionbar_cancel_text)) - .setText(R.string.api_register_disallow); - customActionBarView.findViewById(R.id.actionbar_cancel).setOnClickListener( - new View.OnClickListener() { - @Override - public void onClick(View v) { - // Disallow - try { - mServiceCallback.onRegistered(false, packageName); - } catch (RemoteException e) { - Log.e(Constants.TAG, "ServiceActivity"); - } - finish(); - } - }); - - // Show the custom action bar view and hide the normal Home icon and title. - final ActionBar actionBar = getSupportActionBar(); - actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM, - ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME - | ActionBar.DISPLAY_SHOW_TITLE); - actionBar.setCustomView(customActionBarView, new ActionBar.LayoutParams( - ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); - - setContentView(R.layout.api_app_register_activity); - - settingsFragment = (AppSettingsFragment) getSupportFragmentManager().findFragmentById( - R.id.api_app_settings_fragment); - - settingsFragment.setPackage(packageName); - - // TODO: handle if app is already registered - // LinearLayout layoutRegister = (LinearLayout) - // findViewById(R.id.register_crypto_consumer_register_layout); - // LinearLayout layoutEdit = (LinearLayout) - // findViewById(R.id.register_crypto_consumer_edit_layout); - // - // // if already registered show edit buttons - // ArrayList allowedPkgs = ProviderHelper.getCryptoConsumers(this); - // if (allowedPkgs.contains(packageName)) { - // Log.d(Constants.TAG, "Package is allowed! packageName: " + packageName); - // layoutRegister.setVisibility(View.GONE); - // layoutEdit.setVisibility(View.VISIBLE); - // } else { - // layoutRegister.setVisibility(View.VISIBLE); - // layoutEdit.setVisibility(View.GONE); - // } - - } else if (ACTION_CACHE_PASSPHRASE.equals(action)) { - long secretKeyId = extras.getLong(EXTRA_SECRET_KEY_ID); - - showPassphraseDialog(secretKeyId); - } else { - Log.e(Constants.TAG, "Wrong action!"); - finish(); - } - } - - /** - * Shows passphrase dialog to cache a new passphrase the user enters for using it later for - * encryption. Based on mSecretKeyId it asks for a passphrase to open a private key or it asks - * for a symmetric passphrase - */ - private void showPassphraseDialog(long secretKeyId) { - // Message is received after passphrase is cached - Handler returnHandler = new Handler() { - @Override - public void handleMessage(Message message) { - if (message.what == PassphraseDialogFragment.MESSAGE_OKAY) { - try { - mServiceCallback.onCachedPassphrase(true); - } catch (RemoteException e) { - Log.e(Constants.TAG, "ServiceActivity"); - } - finish(); - } - } - }; - - // Create a new Messenger for the communication back - Messenger messenger = new Messenger(returnHandler); - - try { - PassphraseDialogFragment passphraseDialog = PassphraseDialogFragment.newInstance(this, - messenger, secretKeyId); - - passphraseDialog.show(getSupportFragmentManager(), "passphraseDialog"); - } catch (PgpMain.PgpGeneralException e) { - Log.d(Constants.TAG, "No passphrase for this secret key, encrypt directly!"); - // send message to handler to start encryption directly - returnHandler.sendEmptyMessage(PassphraseDialogFragment.MESSAGE_OKAY); - } - } -} -- cgit v1.2.3