diff options
author | Dominik <dominik@dominikschuermann.de> | 2012-04-25 15:10:12 +0200 |
---|---|---|
committer | Dominik <dominik@dominikschuermann.de> | 2012-06-13 19:28:23 +0300 |
commit | f06fcd989b2eb1416eea521c88944ff0c1482347 (patch) | |
tree | 2e19e98c5152d1dc2614241c39095f5eab8f33e0 /org_apg/src/org/thialfihar/android/apg/service | |
parent | da96aacf55d113c9a91b8e760b3ffef88b11f786 (diff) | |
download | open-keychain-f06fcd989b2eb1416eea521c88944ff0c1482347.tar.gz open-keychain-f06fcd989b2eb1416eea521c88944ff0c1482347.tar.bz2 open-keychain-f06fcd989b2eb1416eea521c88944ff0c1482347.zip |
externalizing code into service
Diffstat (limited to 'org_apg/src/org/thialfihar/android/apg/service')
-rw-r--r-- | org_apg/src/org/thialfihar/android/apg/service/ApgHandler.java | 103 | ||||
-rw-r--r-- | org_apg/src/org/thialfihar/android/apg/service/ApgService.java | 256 |
2 files changed, 240 insertions, 119 deletions
diff --git a/org_apg/src/org/thialfihar/android/apg/service/ApgHandler.java b/org_apg/src/org/thialfihar/android/apg/service/ApgHandler.java new file mode 100644 index 000000000..fe848fb68 --- /dev/null +++ b/org_apg/src/org/thialfihar/android/apg/service/ApgHandler.java @@ -0,0 +1,103 @@ +/* + * Copyright (C) 2012 Dominik Schürmann <dominik@dominikschuermann.de> + * + * 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.thialfihar.android.apg.service; + +import org.thialfihar.android.apg.R; +import org.thialfihar.android.apg.ui.ProgressDialogFragment; + +import android.app.Activity; +import android.os.Bundle; +import android.os.Handler; +import android.os.Message; +import android.widget.Toast; + +public class ApgHandler extends Handler { + + // possible messages send from this service to handler on ui + public static final int MESSAGE_OKAY = 1; + public static final int MESSAGE_EXCEPTION = 2; + public static final int MESSAGE_UPDATE_PROGRESS = 3; + + // possible data keys for messages + public static final String ERROR = "error"; + public static final String PROGRESS = "progress"; + public static final String PROGRESS_MAX = "max"; + public static final String MESSAGE = "message"; + public static final String MESSAGE_ID = "message_id"; + + // generate key results + public static final String NEW_KEY = "new_key"; + + + Activity mActivity; + + ProgressDialogFragment mProgressDialogFragment; + + public ApgHandler(Activity activity) { + this.mActivity = activity; + } + + public ApgHandler(Activity activity, ProgressDialogFragment progressDialogFragment) { + this.mActivity = activity; + this.mProgressDialogFragment = progressDialogFragment; + } + + @Override + public void handleMessage(Message message) { + Bundle data = message.getData(); + + switch (message.arg1) { + case MESSAGE_OKAY: + mProgressDialogFragment.dismiss(); + + break; + + case MESSAGE_EXCEPTION: + mProgressDialogFragment.dismiss(); + + if (data.containsKey(ERROR)) { + Toast.makeText(mActivity, + mActivity.getString(R.string.errorMessage, data.getString(ERROR)), + Toast.LENGTH_SHORT).show(); + } + + break; + + case MESSAGE_UPDATE_PROGRESS: + if (data.containsKey(PROGRESS) && data.containsKey(PROGRESS_MAX)) { + + if (data.containsKey(MESSAGE)) { + mProgressDialogFragment.setProgress(data.getString(MESSAGE), + data.getInt(PROGRESS), data.getInt(PROGRESS_MAX)); + } else if (data.containsKey(MESSAGE_ID)) { + mProgressDialogFragment.setProgress(data.getInt(MESSAGE_ID), + data.getInt(PROGRESS), data.getInt(PROGRESS_MAX)); + + } else { + mProgressDialogFragment.setProgress(data.getInt(PROGRESS), + data.getInt(PROGRESS_MAX)); + } + + } + + break; + + default: + break; + } + } +} diff --git a/org_apg/src/org/thialfihar/android/apg/service/ApgService.java b/org_apg/src/org/thialfihar/android/apg/service/ApgService.java index 7a77e4891..6a27ba45d 100644 --- a/org_apg/src/org/thialfihar/android/apg/service/ApgService.java +++ b/org_apg/src/org/thialfihar/android/apg/service/ApgService.java @@ -1,24 +1,28 @@ -/** - * TODO: - * - Reimplement all the threads in the activitys as intents in this intentService - * - This IntentService stopps itself after an action is executed +/* + * Copyright (C) 2012 Dominik Schürmann <dominik@dominikschuermann.de> + * + * 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.thialfihar.android.apg.service; import java.util.ArrayList; -import java.util.Iterator; -import java.util.Vector; -import org.spongycastle.openpgp.PGPObjectFactory; import org.spongycastle.openpgp.PGPSecretKey; -import org.spongycastle.openpgp.PGPSecretKeyRing; import org.thialfihar.android.apg.Apg; import org.thialfihar.android.apg.Constants; -import org.thialfihar.android.apg.Id; import org.thialfihar.android.apg.ProgressDialogUpdater; -import org.thialfihar.android.apg.ui.widget.KeyEditor; -import org.thialfihar.android.apg.ui.widget.SectionView; +import org.thialfihar.android.apg.util.Utils; import android.app.IntentService; import android.content.Intent; @@ -28,7 +32,12 @@ import android.os.Messenger; import android.os.RemoteException; import android.util.Log; -//TODO: ProgressDialogUpdater rework??? +/** + * This Service contains all important long lasting operations for APG. It receives Intents with + * data from the activities or other apps, queues these intents, executes them, and stops itself + * doing it. + */ +// TODO: ProgressDialogUpdater rework??? public class ApgService extends IntentService implements ProgressDialogUpdater { // extras that can be given by intent @@ -38,22 +47,22 @@ public class ApgService extends IntentService implements ProgressDialogUpdater { // keys for data bundle // edit keys - public static final String DATA_NEW_PASSPHRASE = "new_passphrase"; - public static final String DATA_CURRENT_PASSPHRASE = "current_passphrase"; - public static final String DATA_USER_IDS = "user_ids"; - public static final String DATA_KEYS = "keys"; - public static final String DATA_KEYS_USAGES = "keys_usages"; - public static final String DATA_MASTER_KEY_ID = "master_key_id"; + public static final String NEW_PASSPHRASE = "new_passphrase"; + public static final String CURRENT_PASSPHRASE = "current_passphrase"; + public static final String USER_IDS = "user_ids"; + public static final String KEYS = "keys"; + public static final String KEYS_USAGES = "keys_usages"; + public static final String MASTER_KEY_ID = "master_key_id"; + + // generate key + public static final String ALGORITHM = "algorithm"; + public static final String KEY_SIZE = "key_size"; + public static final String PASSPHRASE = "passphrase"; + public static final String MASTER_KEY = "master_key"; // possible ints for EXTRA_ACTION public static final int ACTION_SAVE_KEYRING = 1; - - // possible messages send from this service to handler on ui - public static final int MESSAGE_OKAY = 1; - public static final int MESSAGE_EXCEPTION = 2; - - // possible data keys for messages - public static final String MESSAGE_DATA_ERROR = "error"; + public static final int ACTION_GENERATE_KEY = 2; Messenger mMessenger; @@ -69,89 +78,104 @@ public class ApgService extends IntentService implements ProgressDialogUpdater { @Override protected void onHandleIntent(Intent intent) { Bundle extras = intent.getExtras(); - if (extras != null) { + if (extras == null) { + Log.e(Constants.TAG, "Extra bundle is null!"); + return; + } - if (!extras.containsKey(EXTRA_ACTION)) { - Log.e(Constants.TAG, "Extra bundle must contain a action!"); - return; - } + if (!extras.containsKey(EXTRA_MESSENGER)) { + Log.e(Constants.TAG, "Extra bundle must contain a messenger!"); + return; + } + mMessenger = (Messenger) extras.get(EXTRA_MESSENGER); - if (!extras.containsKey(EXTRA_MESSENGER)) { - Log.e(Constants.TAG, "Extra bundle must contain a messenger!"); - return; - } - mMessenger = (Messenger) extras.get(EXTRA_MESSENGER); + if (!extras.containsKey(EXTRA_DATA)) { + Log.e(Constants.TAG, "Extra bundle must contain data bundle!"); + return; + } + Bundle data = extras.getBundle(EXTRA_DATA); - Bundle data = null; - if (extras.containsKey(EXTRA_DATA)) { - data = extras.getBundle(EXTRA_DATA); + if (!extras.containsKey(EXTRA_ACTION)) { + Log.e(Constants.TAG, "Extra bundle must contain a action!"); + return; + } + int action = extras.getInt(EXTRA_ACTION); + + // execute action from extra bundle + switch (action) { + case ACTION_SAVE_KEYRING: + + try { + // Input + String oldPassPhrase = data.getString(CURRENT_PASSPHRASE); + String newPassPhrase = data.getString(NEW_PASSPHRASE); + if (newPassPhrase == null) { + newPassPhrase = oldPassPhrase; + } + @SuppressWarnings("unchecked") + ArrayList<String> userIds = (ArrayList<String>) data.getSerializable(USER_IDS); + ArrayList<PGPSecretKey> keys = Utils.BytesToPGPSecretKeyList(data + .getByteArray(KEYS)); + @SuppressWarnings("unchecked") + ArrayList<Integer> keysUsages = (ArrayList<Integer>) data + .getSerializable(KEYS_USAGES); + long masterKeyId = data.getLong(MASTER_KEY_ID); + + // Operation + Apg.buildSecretKey(this, userIds, keys, keysUsages, masterKeyId, oldPassPhrase, + newPassPhrase, this); + Apg.setCachedPassPhrase(masterKeyId, newPassPhrase); + + // Output + sendMessageToHandler(ApgHandler.MESSAGE_OKAY); + } catch (Exception e) { + sendErrorToHandler(e); } - int action = extras.getInt(EXTRA_ACTION); - - // will be filled if error occurs - String error = ""; - - // execute action from extra bundle - switch (action) { - case ACTION_SAVE_KEYRING: - - try { - String oldPassPhrase = data.getString(DATA_CURRENT_PASSPHRASE); - String newPassPhrase = data.getString(DATA_NEW_PASSPHRASE); - if (newPassPhrase == null) { - newPassPhrase = oldPassPhrase; - } - ArrayList<String> userIds = (ArrayList<String>) data - .getSerializable(DATA_USER_IDS); - - byte[] keysBytes = data.getByteArray(DATA_KEYS); - - // convert back from byte[] to ArrayList<PGPSecretKey> - PGPObjectFactory factory = new PGPObjectFactory(keysBytes); - PGPSecretKeyRing keyRing = null; - if ((keyRing = (PGPSecretKeyRing) factory.nextObject()) == null) { - Log.e(Constants.TAG, "No keys given!"); - } - ArrayList<PGPSecretKey> keys = new ArrayList<PGPSecretKey>(); - - Iterator<PGPSecretKey> itr = keyRing.getSecretKeys(); - while (itr.hasNext()) { - keys.add(itr.next()); - Log.d(Constants.TAG, "added..."); - } - - ArrayList<Integer> keysUsages = (ArrayList<Integer>) data - .getSerializable(DATA_KEYS_USAGES); - long masterKeyId = data.getLong(DATA_MASTER_KEY_ID); - - Apg.buildSecretKey(this, userIds, keys, keysUsages, masterKeyId, oldPassPhrase, - newPassPhrase, this); - Apg.setCachedPassPhrase(masterKeyId, newPassPhrase); - } catch (Exception e) { - error = e.getMessage(); - Log.e(Constants.TAG, "Exception: " + error); - e.printStackTrace(); - sendErrorToUi(error); + break; + + case ACTION_GENERATE_KEY: + + try { + // Input + int algorithm = data.getInt(ALGORITHM); + String passphrase = data.getString(PASSPHRASE); + int keysize = data.getInt(KEY_SIZE); + PGPSecretKey masterKey = null; + if (data.containsKey(MASTER_KEY)) { + masterKey = Utils.BytesToPGPSecretKey(data.getByteArray(MASTER_KEY)); } - sendMessageToUi(MESSAGE_OKAY, null, null); - break; + // Operation + PGPSecretKey newKey = Apg + .createKey(this, algorithm, keysize, passphrase, masterKey); - default: - break; + // Output + Bundle resultData = new Bundle(); + resultData.putByteArray(ApgHandler.NEW_KEY, Utils.PGPSecretKeyToBytes(newKey)); + sendMessageToHandler(ApgHandler.MESSAGE_OKAY, null, resultData); + } catch (Exception e) { + sendErrorToHandler(e); } + break; + + default: + break; } + } - private void sendErrorToUi(String error) { + private void sendErrorToHandler(Exception e) { + Log.e(Constants.TAG, "ApgService Exception: ", e); + e.printStackTrace(); + Bundle data = new Bundle(); - data.putString(MESSAGE_DATA_ERROR, error); - sendMessageToUi(MESSAGE_EXCEPTION, null, data); + data.putString(ApgHandler.ERROR, e.getMessage()); + sendMessageToHandler(ApgHandler.MESSAGE_EXCEPTION, null, data); } - private void sendMessageToUi(Integer arg1, Integer arg2, Bundle data) { + private void sendMessageToHandler(Integer arg1, Integer arg2, Bundle data) { Message msg = Message.obtain(); msg.arg1 = arg1; if (arg2 != null) { @@ -164,43 +188,37 @@ public class ApgService extends IntentService implements ProgressDialogUpdater { try { mMessenger.send(msg); } catch (RemoteException e) { - Log.w(Constants.TAG, "Exception sending message", e); + Log.w(Constants.TAG, "Exception sending message, Is handler present?", e); } catch (NullPointerException e) { Log.w(Constants.TAG, "Messenger is null!", e); } } - public void setProgress(int resourceId, int progress, int max) { - setProgress(getString(resourceId), progress, max); + private void sendMessageToHandler(Integer arg1) { + sendMessageToHandler(arg1, null, null); } - public void setProgress(int progress, int max) { - Message msg = new Message(); + /** + * Set progress of ProgressDialog by sending message to handler on UI thread + */ + public void setProgress(String message, int progress, int max) { + Log.d(Constants.TAG, "Send message by setProgress"); + Bundle data = new Bundle(); - data.putInt(Constants.extras.STATUS, Id.message.progress_update); - data.putInt(Constants.extras.PROGRESS, progress); - data.putInt(Constants.extras.PROGRESS_MAX, max); - msg.setData(data); - try { - mMessenger.send(msg); - } catch (RemoteException e) { - e.printStackTrace(); + if (message != null) { + data.putString(ApgHandler.MESSAGE, message); } + data.putInt(ApgHandler.PROGRESS, progress); + data.putInt(ApgHandler.PROGRESS_MAX, max); + + sendMessageToHandler(ApgHandler.MESSAGE_UPDATE_PROGRESS, null, data); } - public void setProgress(String message, int progress, int max) { - Message msg = new Message(); - Bundle data = new Bundle(); - data.putInt(Constants.extras.STATUS, Id.message.progress_update); - data.putString(Constants.extras.MESSAGE, message); - data.putInt(Constants.extras.PROGRESS, progress); - data.putInt(Constants.extras.PROGRESS_MAX, max); - msg.setData(data); - try { - mMessenger.send(msg); - } catch (RemoteException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + public void setProgress(int resourceId, int progress, int max) { + setProgress(getString(resourceId), progress, max); + } + + public void setProgress(int progress, int max) { + setProgress(null, progress, max); } } |