From bce0fe62217e1f174559354b1a0dc06fd9663385 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Tue, 10 Mar 2015 01:44:07 +0100 Subject: Add additional email addresses in create key --- .../keychain/ui/CreateKeyEmailFragment.java | 180 ++++++++++++++++- .../keychain/ui/CreateKeyFinalFragment.java | 33 +++- .../keychain/ui/CreateKeyPassphraseFragment.java | 10 +- .../keychain/ui/EditKeyFragment.java | 1 - .../keychain/ui/dialog/AddEmailDialogFragment.java | 215 +++++++++++++++++++++ 5 files changed, 433 insertions(+), 6 deletions(-) create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddEmailDialogFragment.java (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyEmailFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyEmailFragment.java index d9f391149..66424e012 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyEmailFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyEmailFragment.java @@ -20,17 +20,30 @@ package org.sufficientlysecure.keychain.ui; import android.app.Activity; import android.content.Context; import android.os.Bundle; +import android.os.Handler; +import android.os.Message; +import android.os.Messenger; import android.support.v4.app.Fragment; +import android.support.v7.widget.DefaultItemAnimator; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.view.inputmethod.InputMethodManager; +import android.widget.Button; import android.widget.EditText; +import android.widget.ImageButton; +import android.widget.TextView; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.ui.CreateKeyActivity.FragAction; +import org.sufficientlysecure.keychain.ui.dialog.AddEmailDialogFragment; +import org.sufficientlysecure.keychain.ui.dialog.SetPassphraseDialogFragment; import org.sufficientlysecure.keychain.ui.widget.EmailEditText; +import java.util.ArrayList; +import java.util.List; + public class CreateKeyEmailFragment extends Fragment { public static final String ARG_NAME = "name"; @@ -38,10 +51,14 @@ public class CreateKeyEmailFragment extends Fragment { CreateKeyActivity mCreateKeyActivity; EmailEditText mEmailEdit; + RecyclerView mEmailsRecyclerView; View mBackButton; View mNextButton; String mName; + ArrayList mAdditionalEmailModels; + + EmailAdapter mEmailAdapter; /** * Creates new instance of this fragment @@ -86,6 +103,7 @@ public class CreateKeyEmailFragment extends Fragment { mEmailEdit = (EmailEditText) view.findViewById(R.id.create_key_email); mBackButton = view.findViewById(R.id.create_key_back_button); mNextButton = view.findViewById(R.id.create_key_next_button); + mEmailsRecyclerView = (RecyclerView) view.findViewById(R.id.create_key_emails); // initial values mName = getArguments().getString(ARG_NAME); @@ -108,10 +126,45 @@ public class CreateKeyEmailFragment extends Fragment { createKeyCheck(); } }); + mEmailsRecyclerView.setHasFixedSize(true); + mEmailsRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); + mEmailsRecyclerView.setItemAnimator(new DefaultItemAnimator()); + + mAdditionalEmailModels = new ArrayList<>(); + mEmailAdapter = new EmailAdapter(mAdditionalEmailModels, new View.OnClickListener() { + @Override + public void onClick(View v) { + addEmail(); + } + }); + mEmailsRecyclerView.setAdapter(mEmailAdapter); return view; } + private void addEmail() { + Handler returnHandler = new Handler() { + @Override + public void handleMessage(Message message) { + if (message.what == SetPassphraseDialogFragment.MESSAGE_OKAY) { + Bundle data = message.getData(); + + // add new user id + mEmailAdapter.add( + data.getString(AddEmailDialogFragment.MESSAGE_DATA_EMAIL) + ); + } + } + }; + + // Create a new Messenger for the communication back + Messenger messenger = new Messenger(returnHandler); + + AddEmailDialogFragment addEmailDialog = AddEmailDialogFragment.newInstance(messenger); + + addEmailDialog.show(getActivity().getSupportFragmentManager(), "addEmailDialog"); + } + @Override public void onAttach(Activity activity) { super.onAttach(activity); @@ -121,14 +174,137 @@ public class CreateKeyEmailFragment extends Fragment { private void createKeyCheck() { if (isEditTextNotEmpty(getActivity(), mEmailEdit)) { + ArrayList emails = new ArrayList<>(); + for (EmailAdapter.ViewModel holder : mAdditionalEmailModels) { + emails.add(holder.toString()); + } + CreateKeyPassphraseFragment frag = CreateKeyPassphraseFragment.newInstance( mName, - mEmailEdit.getText().toString() + mEmailEdit.getText().toString(), + emails ); mCreateKeyActivity.loadFragment(null, frag, FragAction.TO_RIGHT); } } + public static class EmailAdapter extends RecyclerView.Adapter { + private List mDataset; + private View.OnClickListener mFooterOnClickListener; + private static final int TYPE_FOOTER = 0; + private static final int TYPE_ITEM = 1; + + public static class ViewModel { + String email; + + ViewModel(String email) { + this.email = email; + } + + @Override + public String toString() { + return email; + } + } + + // Provide a reference to the views for each data item + // Complex data items may need more than one view per item, and + // you provide access to all the views for a data item in a view holder + public static class ViewHolder extends RecyclerView.ViewHolder { + // each data item is just a string in this case + public TextView mTextView; + public ImageButton mDeleteButton; + + public ViewHolder(View itemView) { + super(itemView); + mTextView = (TextView) itemView.findViewById(R.id.create_key_email_item_email); + mDeleteButton = (ImageButton) itemView.findViewById(R.id.create_key_email_item_delete_button); + } + } + + class FooterHolder extends RecyclerView.ViewHolder { + public Button mAddButton; + + public FooterHolder(View itemView) { + super(itemView); + mAddButton = (Button) itemView.findViewById(R.id.create_key_add_email); + } + } + + // Provide a suitable constructor (depends on the kind of dataset) + public EmailAdapter(List myDataset, View.OnClickListener onFooterClickListener) { + mDataset = myDataset; + mFooterOnClickListener = onFooterClickListener; + } + + // Create new views (invoked by the layout manager) + @Override + public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + if (viewType == TYPE_FOOTER) { + View v = LayoutInflater.from(parent.getContext()) + .inflate(R.layout.create_key_email_list_footer, parent, false); + return new FooterHolder(v); + } else { + //inflate your layout and pass it to view holder + View v = LayoutInflater.from(parent.getContext()) + .inflate(R.layout.create_key_email_list_item, parent, false); + return new ViewHolder(v); + } + } + + // Replace the contents of a view (invoked by the layout manager) + @Override + public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) { + if (holder instanceof ViewHolder) { + ViewHolder thisHolder = (ViewHolder) holder; + // - get element from your dataset at this position + // - replace the contents of the view with that element + final ViewModel model = mDataset.get(position); + + thisHolder.mTextView.setText(model.email); + thisHolder.mDeleteButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + remove(model); + } + }); + } else if (holder instanceof FooterHolder) { + FooterHolder thisHolder = (FooterHolder) holder; + thisHolder.mAddButton.setOnClickListener(mFooterOnClickListener); + } + } + + // Return the size of your dataset (invoked by the layout manager) + @Override + public int getItemCount() { + return mDataset.size() + 1; + } + + @Override + public int getItemViewType(int position) { + if (isPositionFooter(position)) { + return TYPE_FOOTER; + } else { + return TYPE_ITEM; + } + } + + private boolean isPositionFooter(int position) { + return position == mDataset.size(); + } + + public void add(String email) { + mDataset.add(new ViewModel(email)); + notifyItemInserted(mDataset.size() - 1); + } + + public void remove(ViewModel model) { + int position = mDataset.indexOf(model); + mDataset.remove(position); + notifyItemRemoved(position); + } + } + } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java index dc9f3d4ad..4853c61c5 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java @@ -47,6 +47,9 @@ import org.sufficientlysecure.keychain.ui.CreateKeyActivity.FragAction; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.Preferences; +import java.util.ArrayList; +import java.util.Iterator; + public class CreateKeyFinalFragment extends Fragment { public static final int REQUEST_EDIT_KEY = 0x00008007; @@ -63,10 +66,12 @@ public class CreateKeyFinalFragment extends Fragment { public static final String ARG_NAME = "name"; public static final String ARG_EMAIL = "email"; + public static final String ARG_ADDITIONAL_EMAILS = "emails"; public static final String ARG_PASSPHRASE = "passphrase"; String mName; String mEmail; + ArrayList mAdditionalEmails; String mPassphrase; SaveKeyringParcel mSaveKeyringParcel; @@ -74,12 +79,15 @@ public class CreateKeyFinalFragment extends Fragment { /** * Creates new instance of this fragment */ - public static CreateKeyFinalFragment newInstance(String name, String email, String passphrase) { + public static CreateKeyFinalFragment newInstance(String name, String email, + ArrayList additionalEmails, + String passphrase) { CreateKeyFinalFragment frag = new CreateKeyFinalFragment(); Bundle args = new Bundle(); args.putString(ARG_NAME, name); args.putString(ARG_EMAIL, email); + args.putStringArrayList(ARG_ADDITIONAL_EMAILS, additionalEmails); args.putString(ARG_PASSPHRASE, passphrase); frag.setArguments(args); @@ -102,11 +110,25 @@ public class CreateKeyFinalFragment extends Fragment { // get args mName = getArguments().getString(ARG_NAME); mEmail = getArguments().getString(ARG_EMAIL); + mAdditionalEmails = getArguments().getStringArrayList(ARG_ADDITIONAL_EMAILS); mPassphrase = getArguments().getString(ARG_PASSPHRASE); // set values mNameEdit.setText(mName); - mEmailEdit.setText(mEmail); + if (mAdditionalEmails != null && mAdditionalEmails.size() > 0) { + String emailText = mEmail + ", "; + Iterator it = mAdditionalEmails.iterator(); + while (it.hasNext()) { + Object next = it.next(); + emailText += next; + if (it.hasNext()) { + emailText += ", "; + } + } + mEmailEdit.setText(emailText); + } else { + mEmailEdit.setText(mEmail); + } mCreateButton.setOnClickListener(new View.OnClickListener() { @Override @@ -167,12 +189,19 @@ public class CreateKeyFinalFragment extends Fragment { String userId = KeyRing.createUserId(mName, mEmail, null); mSaveKeyringParcel.mAddUserIds.add(userId); mSaveKeyringParcel.mChangePrimaryUserId = userId; + if (mAdditionalEmails != null && mAdditionalEmails.size() > 0) { + for (String email : mAdditionalEmails) { + String thisUserId = KeyRing.createUserId(mName, email, null); + mSaveKeyringParcel.mAddUserIds.add(thisUserId); + } + } mSaveKeyringParcel.mNewUnlock = mPassphrase != null ? new ChangeUnlockParcel(mPassphrase, null) : null; } } + private void createKey() { Intent intent = new Intent(getActivity(), KeychainIntentService.class); intent.setAction(KeychainIntentService.ACTION_EDIT_KEYRING); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyPassphraseFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyPassphraseFragment.java index 00ac00ff4..055ea608b 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyPassphraseFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyPassphraseFragment.java @@ -35,14 +35,18 @@ import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.ui.CreateKeyActivity.FragAction; import org.sufficientlysecure.keychain.ui.widget.PassphraseEditText; +import java.util.ArrayList; + public class CreateKeyPassphraseFragment extends Fragment { public static final String ARG_NAME = "name"; public static final String ARG_EMAIL = "email"; + public static final String ARG_ADDITIONAL_EMAILS = "emails"; // model String mName; String mEmail; + ArrayList mAdditionalEmails; // view CreateKeyActivity mCreateKeyActivity; @@ -55,12 +59,14 @@ public class CreateKeyPassphraseFragment extends Fragment { /** * Creates new instance of this fragment */ - public static CreateKeyPassphraseFragment newInstance(String name, String email) { + public static CreateKeyPassphraseFragment newInstance(String name, String email, + ArrayList additionalEmails) { CreateKeyPassphraseFragment frag = new CreateKeyPassphraseFragment(); Bundle args = new Bundle(); args.putString(ARG_NAME, name); args.putString(ARG_EMAIL, email); + args.putStringArrayList(ARG_ADDITIONAL_EMAILS, additionalEmails); frag.setArguments(args); @@ -114,6 +120,7 @@ public class CreateKeyPassphraseFragment extends Fragment { // initial values mName = getArguments().getString(ARG_NAME); mEmail = getArguments().getString(ARG_EMAIL); + mAdditionalEmails = getArguments().getStringArrayList(ARG_ADDITIONAL_EMAILS); mPassphraseEdit.requestFocus(); mBackButton.setOnClickListener(new View.OnClickListener() { @Override @@ -163,6 +170,7 @@ public class CreateKeyPassphraseFragment extends Fragment { CreateKeyFinalFragment.newInstance( mName, mEmail, + mAdditionalEmails, mPassphraseEdit.getText().toString() ); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java index 25ca6e8fd..1e9a90fc8 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java @@ -528,7 +528,6 @@ public class EditKeyFragment extends LoaderFragment implements } private void addUserId() { - // Message is received after passphrase is cached Handler returnHandler = new Handler() { @Override public void handleMessage(Message message) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddEmailDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddEmailDialogFragment.java new file mode 100644 index 000000000..5d5ca533e --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddEmailDialogFragment.java @@ -0,0 +1,215 @@ +/* + * Copyright (C) 2015 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.dialog; + +import android.app.Activity; +import android.app.AlertDialog; +import android.app.Dialog; +import android.content.Context; +import android.content.DialogInterface; +import android.content.DialogInterface.OnClickListener; +import android.os.Bundle; +import android.os.Message; +import android.os.Messenger; +import android.os.RemoteException; +import android.support.v4.app.DialogFragment; +import android.view.KeyEvent; +import android.view.LayoutInflater; +import android.view.View; +import android.view.inputmethod.EditorInfo; +import android.view.inputmethod.InputMethodManager; +import android.widget.Button; +import android.widget.TextView; +import android.widget.TextView.OnEditorActionListener; + +import org.sufficientlysecure.keychain.Constants; +import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.ui.widget.EmailEditText; +import org.sufficientlysecure.keychain.util.Log; + +public class AddEmailDialogFragment extends DialogFragment implements OnEditorActionListener { + private static final String ARG_MESSENGER = "messenger"; + + public static final int MESSAGE_OKAY = 1; + public static final int MESSAGE_CANCEL = 2; + + public static final String MESSAGE_DATA_EMAIL = "email"; + + private Messenger mMessenger; + private EmailEditText mEmail; + + public static AddEmailDialogFragment newInstance(Messenger messenger) { + + AddEmailDialogFragment frag = new AddEmailDialogFragment(); + Bundle args = new Bundle(); + args.putParcelable(ARG_MESSENGER, messenger); + frag.setArguments(args); + + return frag; + } + + /** + * Creates dialog + */ + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + final Activity activity = getActivity(); + mMessenger = getArguments().getParcelable(ARG_MESSENGER); + + CustomAlertDialogBuilder alert = new CustomAlertDialogBuilder(activity); + + alert.setTitle(R.string.create_key_add_email); + + LayoutInflater inflater = activity.getLayoutInflater(); + View view = inflater.inflate(R.layout.add_email_dialog, null); + alert.setView(view); + + mEmail = (EmailEditText) view.findViewById(R.id.add_email_address); + + alert.setPositiveButton(android.R.string.ok, new OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int id) { + dismiss(); + + // return new user id back to activity + Bundle data = new Bundle(); + String email = mEmail.getText().toString(); + data.putString(MESSAGE_DATA_EMAIL, email); + sendMessageToHandler(MESSAGE_OKAY, data); + } + }); + + alert.setNegativeButton(android.R.string.cancel, new OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int id) { + dialog.cancel(); + } + }); + + // Hack to open keyboard. + // This is the only method that I found to work across all Android versions + // http://turbomanage.wordpress.com/2012/05/02/show-soft-keyboard-automatically-when-edittext-receives-focus/ + // Notes: * onCreateView can't be used because we want to add buttons to the dialog + // * opening in onActivityCreated does not work on Android 4.4 + mEmail.setOnFocusChangeListener(new View.OnFocusChangeListener() { + @Override + public void onFocusChange(View v, boolean hasFocus) { + mEmail.post(new Runnable() { + @Override + public void run() { + InputMethodManager imm = (InputMethodManager) getActivity() + .getSystemService(Context.INPUT_METHOD_SERVICE); + imm.showSoftInput(mEmail, InputMethodManager.SHOW_IMPLICIT); + } + }); + } + }); + mEmail.requestFocus(); + + mEmail.setImeActionLabel(getString(android.R.string.ok), EditorInfo.IME_ACTION_DONE); + mEmail.setOnEditorActionListener(this); + + return alert.show(); + } + + @Override + public void onCancel(DialogInterface dialog) { + super.onCancel(dialog); + + dismiss(); + sendMessageToHandler(MESSAGE_CANCEL); + } + + @Override + public void onDismiss(DialogInterface dialog) { + super.onDismiss(dialog); + + // hide keyboard on dismiss + hideKeyboard(); + } + + private void hideKeyboard() { + if (getActivity() == null) { + return; + } + InputMethodManager inputManager = (InputMethodManager) getActivity() + .getSystemService(Context.INPUT_METHOD_SERVICE); + + // check if no view has focus: + View v = getActivity().getCurrentFocus(); + if (v == null) + return; + + inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0); + } + + /** + * Associate the "done" button on the soft keyboard with the okay button in the view + */ + @Override + public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { + if (EditorInfo.IME_ACTION_DONE == actionId) { + AlertDialog dialog = ((AlertDialog) getDialog()); + Button bt = dialog.getButton(AlertDialog.BUTTON_POSITIVE); + + bt.performClick(); + return true; + } + return false; + } + + /** + * Send message back to handler which is initialized in a activity + * + * @param what Message integer you want to send + */ + private void sendMessageToHandler(Integer what) { + Message msg = Message.obtain(); + msg.what = what; + + try { + mMessenger.send(msg); + } catch (RemoteException e) { + Log.w(Constants.TAG, "Exception sending message, Is handler present?", e); + } catch (NullPointerException e) { + Log.w(Constants.TAG, "Messenger is null!", e); + } + } + + /** + * Send message back to handler which is initialized in a activity + * + * @param what Message integer you want to send + */ + private void sendMessageToHandler(Integer what, Bundle data) { + Message msg = Message.obtain(); + msg.what = what; + if (data != null) { + msg.setData(data); + } + + try { + mMessenger.send(msg); + } catch (RemoteException e) { + Log.w(Constants.TAG, "Exception sending message, Is handler present?", e); + } catch (NullPointerException e) { + Log.w(Constants.TAG, "Messenger is null!", e); + } + } + +} -- cgit v1.2.3