use MaterialChipsInput for encryption recipients

This commit is contained in:
Vincent Breitmoser
2018-06-27 23:51:16 +02:00
parent 387ec6ed96
commit febe9cbe92
6 changed files with 97 additions and 227 deletions

View File

@@ -23,7 +23,9 @@ import java.util.Iterator;
import java.util.List;
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.ViewModel;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
@@ -31,30 +33,30 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.ViewAnimator;
import com.tokenautocomplete.TokenCompleteTextView.TokenListener;
import com.pchmn.materialchips.ChipsInput;
import com.pchmn.materialchips.ChipsInput.ChipsListener;
import com.pchmn.materialchips.model.Chip;
import com.pchmn.materialchips.model.ChipInterface;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.livedata.GenericLiveData;
import org.sufficientlysecure.keychain.model.SubKey.UnifiedKeyInfo;
import org.sufficientlysecure.keychain.pgp.CanonicalizedPublicKeyRing;
import org.sufficientlysecure.keychain.provider.KeyRepository;
import org.sufficientlysecure.keychain.provider.KeyRepository.NotFoundException;
import org.sufficientlysecure.keychain.ui.adapter.KeyAdapter.KeyItem;
import org.sufficientlysecure.keychain.ui.keyview.GenericViewModel;
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
import org.sufficientlysecure.keychain.ui.util.Notify;
import org.sufficientlysecure.keychain.ui.util.Notify.Style;
import org.sufficientlysecure.keychain.ui.widget.EncryptKeyCompletionView;
import org.sufficientlysecure.keychain.ui.widget.KeySpinner;
import org.sufficientlysecure.keychain.util.Passphrase;
import timber.log.Timber;
public class EncryptModeAsymmetricFragment extends EncryptModeFragment {
KeyRepository mKeyRepository;
private KeySpinner mSignKeySpinner;
private EncryptKeyCompletionView mEncryptKeyView;
private ChipsInput mEncryptKeyView;
public static final String ARG_SINGATURE_KEY_ID = "signature_key_id";
public static final String ARG_ENCRYPTION_KEY_IDS = "encryption_key_ids";
@@ -80,7 +82,9 @@ public class EncryptModeAsymmetricFragment extends EncryptModeFragment {
mSignKeySpinner = view.findViewById(R.id.sign_key_spinner);
mEncryptKeyView = view.findViewById(R.id.recipient_list);
mEncryptKeyView.setThreshold(1); // Start working from first character
ViewGroup filterableListAnchor = view.findViewById(R.id.anchor_dropdown_encrypt);
mEncryptKeyView.setFilterableListLayout(filterableListAnchor);
final ViewAnimator vSignatureIcon = view.findViewById(R.id.result_signature_icon);
mSignKeySpinner.setOnKeyChangedListener(masterKeyId -> {
@@ -92,21 +96,31 @@ public class EncryptModeAsymmetricFragment extends EncryptModeFragment {
mSignKeySpinner.setShowNone(R.string.cert_none);
final ViewAnimator vEncryptionIcon = view.findViewById(R.id.result_encryption_icon);
mEncryptKeyView.setTokenListener(new TokenListener<KeyItem>() {
mEncryptKeyView.addChipsListener(new ChipsListener() {
@Override
public void onTokenAdded(KeyItem o) {
public void onChipAdded(ChipInterface chipInterface, int newSize) {
if (vEncryptionIcon.getDisplayedChild() != 1) {
vEncryptionIcon.setDisplayedChild(1);
}
}
@Override
public void onTokenRemoved(KeyItem o) {
int child = mEncryptKeyView.getObjects().isEmpty() ? 0 : 1;
public void onChipRemoved(ChipInterface chipInterface, int newSize) {
int child = newSize == 0 ? 0 : 1;
if (vEncryptionIcon.getDisplayedChild() != child) {
vEncryptionIcon.setDisplayedChild(child);
}
}
@Override
public void onTextChanged(CharSequence charSequence) {
}
@Override
public void onActionDone(CharSequence charSequence) {
}
});
return view;
@@ -115,12 +129,10 @@ public class EncryptModeAsymmetricFragment extends EncryptModeFragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mKeyRepository = KeyRepository.create(requireContext());
GenericViewModel viewModel = ViewModelProviders.of(this).get(GenericViewModel.class);
LiveData<List<UnifiedKeyInfo>> liveData = viewModel.getGenericLiveData(requireContext(),
mKeyRepository::getAllUnifiedKeyInfoWithSecret);
liveData.observe(this, mSignKeySpinner::setData);
EncryptModeViewModel viewModel = ViewModelProviders.of(this).get(EncryptModeViewModel.class);
viewModel.getSignKeyLiveData(requireContext()).observe(this, mSignKeySpinner::setData);
viewModel.getEncryptRecipientLiveData(requireContext()).observe(this, this::onLoadEncryptRecipients);
// preselect keys given, from state or arguments
if (savedInstanceState == null) {
@@ -131,7 +143,40 @@ public class EncryptModeAsymmetricFragment extends EncryptModeFragment {
long[] encryptionKeyIds = getArguments().getLongArray(ARG_ENCRYPTION_KEY_IDS);
preselectKeys(signatureKeyId, encryptionKeyIds);
}
}
private void onLoadEncryptRecipients(List<? extends ChipInterface> keyInfoChips) {
mEncryptKeyView.setFilterableList(keyInfoChips);
}
public static class EncryptModeViewModel extends ViewModel {
private LiveData<List<UnifiedKeyInfo>> signKeyLiveData;
private LiveData<List<Chip>> encryptRecipientLiveData;
LiveData<List<UnifiedKeyInfo>> getSignKeyLiveData(Context context) {
if (signKeyLiveData == null) {
signKeyLiveData = new GenericLiveData<>(context, null, () -> {
KeyRepository keyRepository = KeyRepository.create(context);
return keyRepository.getAllUnifiedKeyInfoWithSecret();
});
}
return signKeyLiveData;
}
LiveData<List<Chip>> getEncryptRecipientLiveData(Context context) {
if (encryptRecipientLiveData == null) {
encryptRecipientLiveData = new GenericLiveData<>(context, null, () -> {
KeyRepository keyRepository = KeyRepository.create(context);
List<UnifiedKeyInfo> keyInfos = keyRepository.getAllUnifiedKeyInfo();
ArrayList<Chip> result = new ArrayList<>();
for (UnifiedKeyInfo keyInfo : keyInfos) {
result.add(new Chip(keyInfo.master_key_id(), keyInfo.name(), keyInfo.email()));
}
return result;
});
}
return encryptRecipientLiveData;
}
}
/**
@@ -153,7 +198,8 @@ public class EncryptModeAsymmetricFragment extends EncryptModeFragment {
try {
CanonicalizedPublicKeyRing ring =
mKeyRepository.getCanonicalizedPublicKeyRing(preselectedId);
mEncryptKeyView.addObject(new KeyItem(ring));
Chip infooo = new Chip(ring.getMasterKeyId(), ring.getPrimaryUserIdWithFallback(), "infooo");
mEncryptKeyView.addChip(infooo);
} catch (NotFoundException e) {
Timber.e(e, "key not found for encryption!");
Notify.create(getActivity(), getString(R.string.error_preselect_encrypt_key,
@@ -180,10 +226,8 @@ public class EncryptModeAsymmetricFragment extends EncryptModeFragment {
@Override
public long[] getAsymmetricEncryptionKeyIds() {
List<Long> keyIds = new ArrayList<>();
for (Object object : mEncryptKeyView.getObjects()) {
if (object instanceof KeyItem) {
keyIds.add(((KeyItem) object).mKeyId);
}
for (ChipInterface chip : mEncryptKeyView.getSelectedChipList()) {
keyIds.add((long) chip.getId());
}
long[] keyIdsArr = new long[keyIds.size()];
@@ -197,16 +241,12 @@ public class EncryptModeAsymmetricFragment extends EncryptModeFragment {
@Override
public String[] getAsymmetricEncryptionUserIds() {
List<String> userIds = new ArrayList<>();
for (Object object : mEncryptKeyView.getObjects()) {
if (object instanceof KeyItem) {
userIds.add(((KeyItem) object).mUserIdFull);
}
for (ChipInterface chip : mEncryptKeyView.getSelectedChipList()) {
userIds.add(chip.getInfo());
}
return userIds.toArray(new String[userIds.size()]);
}
@Override

View File

@@ -1,179 +0,0 @@
/*
* Copyright (C) 2017 Schürmann & Breitmoser GbR
*
* 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 <http://www.gnu.org/licenses/>.
*/
package org.sufficientlysecure.keychain.ui.widget;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import com.tokenautocomplete.TokenCompleteTextView;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.provider.KeychainContract;
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings;
import org.sufficientlysecure.keychain.provider.KeychainDatabase.Tables;
import org.sufficientlysecure.keychain.ui.adapter.KeyAdapter;
import org.sufficientlysecure.keychain.ui.adapter.KeyAdapter.KeyItem;
import timber.log.Timber;
public class EncryptKeyCompletionView extends TokenCompleteTextView<KeyItem>
implements LoaderCallbacks<Cursor> {
public static final String ARG_QUERY = "query";
private KeyAdapter mAdapter;
private LoaderManager mLoaderManager;
public EncryptKeyCompletionView(Context context) {
super(context);
initView();
}
public EncryptKeyCompletionView(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public EncryptKeyCompletionView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView();
}
private void initView() {
allowDuplicates(false);
mAdapter = new KeyAdapter(getContext(), null, 0);
setAdapter(mAdapter);
}
@Override
protected View getViewForObject(KeyItem keyItem) {
LayoutInflater l = LayoutInflater.from(getContext());
View view = l.inflate(R.layout.recipient_box_entry, null);
((TextView) view.findViewById(android.R.id.text1)).setText(keyItem.getReadableName());
if (keyItem.mIsRevoked || !keyItem.mHasEncrypt || keyItem.mIsExpired) {
((TextView) view.findViewById(android.R.id.text1)).setTextColor(Color.RED);
}
return view;
}
@Override
protected KeyItem defaultObject(String completionText) {
return null;
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
if (getContext() instanceof FragmentActivity) {
mLoaderManager = ((FragmentActivity) getContext()).getSupportLoaderManager();
} else {
Timber.e("EncryptKeyCompletionView must be attached to a FragmentActivity, this is " +
getContext().getClass());
}
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mLoaderManager = null;
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// These are the rows that we will retrieve.
Uri baseUri = KeyRings.buildUnifiedKeyRingsUri();
String[] projection = KeyAdapter.getProjectionWith(new String[]{
KeychainContract.KeyRings.HAS_ENCRYPT,
});
String where = KeyRings.HAS_ENCRYPT + " NOT NULL AND "
+ KeyRings.IS_EXPIRED + " = 0 AND "
+ Tables.KEYS + "." + KeyRings.IS_REVOKED + " = 0";
String query = args.getString(ARG_QUERY);
mAdapter.setSearchQuery(query);
where += " AND " + KeyRings.USER_ID + " LIKE ?";
return new CursorLoader(getContext(), baseUri, projection, where,
new String[]{"%" + query + "%"}, null);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mAdapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
@Override
public void showDropDown() {
if (mAdapter == null || mAdapter.getCursor() == null || mAdapter.getCursor().isClosed()) {
return;
}
super.showDropDown();
}
@Override
public void onFocusChanged(boolean hasFocus, int direction, Rect previous) {
super.onFocusChanged(hasFocus, direction, previous);
if (hasFocus) {
((InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE))
.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT);
}
}
@Override
protected void performFiltering(@NonNull CharSequence text, int start, int end, int keyCode) {
// super.performFiltering(text, start, end, keyCode);
String query = text.subSequence(start, end).toString();
if (TextUtils.isEmpty(query) || query.length() < 2) {
mAdapter.swapCursor(null);
return;
}
Bundle args = new Bundle();
args.putString(ARG_QUERY, query);
mLoaderManager.restartLoader(0, args, this);
}
}