From 6448de8f6ac84a8dcc91908af37c8826e32aa785 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Wed, 30 Jul 2014 00:50:53 +0200 Subject: Create key with fragments --- .../keychain/ui/CreateKeyFinalFragment.java | 220 +++++++++++++++++++++ 1 file changed, 220 insertions(+) create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java new file mode 100644 index 000000000..dffdfe448 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java @@ -0,0 +1,220 @@ +/* + * 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 . + */ + +package org.sufficientlysecure.keychain.ui; + +import android.app.Activity; +import android.app.ProgressDialog; +import android.content.Intent; +import android.os.Bundle; +import android.os.Message; +import android.os.Messenger; +import android.support.v4.app.Fragment; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.CheckBox; +import android.widget.Spinner; +import android.widget.TextView; + +import com.devspark.appmsg.AppMsg; + +import org.spongycastle.bcpg.sig.KeyFlags; +import org.sufficientlysecure.keychain.Constants; +import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.service.KeychainIntentService; +import org.sufficientlysecure.keychain.service.KeychainIntentServiceHandler; +import org.sufficientlysecure.keychain.service.SaveKeyringParcel; + +public class CreateKeyFinalFragment extends Fragment { + + CreateKeyActivity mCreateKeyActivity; + + TextView mNameEdit; + TextView mEmailEdit; + CheckBox mUploadCheckbox; + View mBackButton; + View mCreateButton; + + public static final String ARG_NAME = "name"; + public static final String ARG_EMAIL = "email"; + public static final String ARG_PASSPHRASE = "passphrase"; + + String mName; + String mEmail; + String mPassphrase; + + /** + * Creates new instance of this fragment + */ + public static CreateKeyFinalFragment newInstance(String name, String email, String passphrase) { + CreateKeyFinalFragment frag = new CreateKeyFinalFragment(); + + Bundle args = new Bundle(); + args.putString(ARG_NAME, name); + args.putString(ARG_EMAIL, email); + args.putString(ARG_PASSPHRASE, passphrase); + + frag.setArguments(args); + + return frag; + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.create_key_final_fragment, container, false); + + mNameEdit = (TextView) view.findViewById(R.id.name); + mEmailEdit = (TextView) view.findViewById(R.id.email); + mUploadCheckbox = (CheckBox) view.findViewById(R.id.create_key_upload); + mBackButton = view.findViewById(R.id.create_key_back_button); + mCreateButton = view.findViewById(R.id.create_key_create_button); + + // get args + mName = getArguments().getString(ARG_NAME); + mEmail = getArguments().getString(ARG_EMAIL); + mPassphrase = getArguments().getString(ARG_PASSPHRASE); + + // set values + mNameEdit.setText(mName); + mEmailEdit.setText(mEmail); + + mCreateButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + createKey(); + } + }); + + mBackButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + CreateKeyInputFragment frag = + CreateKeyInputFragment.newInstance(mName, mEmail); + mCreateKeyActivity.loadFragment(null, frag, CreateKeyActivity.ANIM_TO_LEFT); + } + }); + + return view; + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + + mCreateKeyActivity = (CreateKeyActivity) getActivity(); + } + + private void createKey() { + Intent intent = new Intent(getActivity(), KeychainIntentService.class); + intent.setAction(KeychainIntentService.ACTION_SAVE_KEYRING); + + // Message is received after importing is done in KeychainIntentService + KeychainIntentServiceHandler saveHandler = new KeychainIntentServiceHandler( + getActivity(), + getString(R.string.progress_importing), + ProgressDialog.STYLE_HORIZONTAL) { + public void handleMessage(Message message) { + // handle messages by standard KeychainIntentServiceHandler first + super.handleMessage(message); + + // TODO +// if (mUploadCheckbox.isChecked()) { +// uploadKey(); +// } else { + if (message.arg1 == KeychainIntentServiceHandler.MESSAGE_OKAY) { + getActivity().setResult(Activity.RESULT_OK); + getActivity().finish(); + } +// } + } + }; + + // fill values for this action + Bundle data = new Bundle(); + + SaveKeyringParcel parcel = new SaveKeyringParcel(); + parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(Constants.choice.algorithm.rsa, 4096, KeyFlags.CERTIFY_OTHER, null)); + parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(Constants.choice.algorithm.rsa, 4096, KeyFlags.SIGN_DATA, null)); + parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(Constants.choice.algorithm.rsa, 4096, KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE, null)); + String userId = mName + " <" + mEmail + ">"; + parcel.mAddUserIds.add(userId); + parcel.mNewPassphrase = mPassphrase; + + // get selected key entries + data.putParcelable(KeychainIntentService.SAVE_KEYRING_PARCEL, parcel); + + intent.putExtra(KeychainIntentService.EXTRA_DATA, data); + + // Create a new Messenger for the communication back + Messenger messenger = new Messenger(saveHandler); + intent.putExtra(KeychainIntentService.EXTRA_MESSENGER, messenger); + + saveHandler.showProgressDialog(getActivity()); + + getActivity().startService(intent); + } + + private void uploadKey() { + // Send all information needed to service to upload key in other thread + Intent intent = new Intent(getActivity(), KeychainIntentService.class); + + intent.setAction(KeychainIntentService.ACTION_UPLOAD_KEYRING); + + // set data uri as path to keyring + // TODO +// Uri blobUri = KeychainContract.KeyRingData.buildPublicKeyRingUri(mDataUri); +// intent.setData(blobUri); + + // fill values for this action + Bundle data = new Bundle(); + + Spinner keyServer = (Spinner) getActivity().findViewById(R.id.upload_key_keyserver); + String server = (String) keyServer.getSelectedItem(); + data.putString(KeychainIntentService.UPLOAD_KEY_SERVER, server); + + intent.putExtra(KeychainIntentService.EXTRA_DATA, data); + + // Message is received after uploading is done in KeychainIntentService + KeychainIntentServiceHandler saveHandler = new KeychainIntentServiceHandler(getActivity(), + getString(R.string.progress_exporting), ProgressDialog.STYLE_HORIZONTAL) { + public void handleMessage(Message message) { + // handle messages by standard KeychainIntentServiceHandler first + super.handleMessage(message); + + if (message.arg1 == KeychainIntentServiceHandler.MESSAGE_OKAY) { + AppMsg.makeText(getActivity(), R.string.key_send_success, + AppMsg.STYLE_INFO).show(); + + getActivity().setResult(Activity.RESULT_OK); + getActivity().finish(); + } + } + }; + + // Create a new Messenger for the communication back + Messenger messenger = new Messenger(saveHandler); + intent.putExtra(KeychainIntentService.EXTRA_MESSENGER, messenger); + + // show progress dialog + saveHandler.showProgressDialog(getActivity()); + + // start service with intent + getActivity().startService(intent); + } + +} -- cgit v1.2.3