use KeyChoiceAdapter in SelectSignKeyIdListFragment
This commit is contained in:
@@ -89,7 +89,7 @@ public class AppSettingsAllowedKeysListFragment extends RecyclerFragment<KeyChoi
|
|||||||
|
|
||||||
public void onLoadUnifiedKeyData(List<UnifiedKeyInfo> data) {
|
public void onLoadUnifiedKeyData(List<UnifiedKeyInfo> data) {
|
||||||
if (keyChoiceAdapter == null) {
|
if (keyChoiceAdapter == null) {
|
||||||
keyChoiceAdapter = new KeyChoiceAdapter(true, data);
|
keyChoiceAdapter = KeyChoiceAdapter.createMultiChoiceAdapter(data);
|
||||||
setAdapter(keyChoiceAdapter);
|
setAdapter(keyChoiceAdapter);
|
||||||
Set<Long> checkedIds = apiAppDao.getAllowedKeyIdsForApp(packageName);
|
Set<Long> checkedIds = apiAppDao.getAllowedKeyIdsForApp(packageName);
|
||||||
keyChoiceAdapter.setSelectionByIds(checkedIds);
|
keyChoiceAdapter.setSelectionByIds(checkedIds);
|
||||||
|
|||||||
@@ -123,7 +123,7 @@ public class SelectPublicKeyFragment extends RecyclerFragment<KeyChoiceAdapter>
|
|||||||
|
|
||||||
public void onLoadUnifiedKeyData(List<UnifiedKeyInfo> data) {
|
public void onLoadUnifiedKeyData(List<UnifiedKeyInfo> data) {
|
||||||
if (keyChoiceAdapter == null) {
|
if (keyChoiceAdapter == null) {
|
||||||
keyChoiceAdapter = new KeyChoiceAdapter(true, data);
|
keyChoiceAdapter = KeyChoiceAdapter.createMultiChoiceAdapter(data);
|
||||||
setAdapter(keyChoiceAdapter);
|
setAdapter(keyChoiceAdapter);
|
||||||
keyChoiceAdapter.setSelectionByIds(selectedMasterKeyIds);
|
keyChoiceAdapter.setSelectionByIds(selectedMasterKeyIds);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -18,39 +18,45 @@
|
|||||||
package org.sufficientlysecure.keychain.remote.ui;
|
package org.sufficientlysecure.keychain.remote.ui;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
|
import android.arch.lifecycle.LiveData;
|
||||||
|
import android.arch.lifecycle.ViewModelProviders;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.database.Cursor;
|
|
||||||
import android.net.Uri;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.v4.app.LoaderManager;
|
|
||||||
import android.support.v4.content.CursorLoader;
|
|
||||||
import android.support.v4.content.Loader;
|
|
||||||
import android.support.v7.widget.LinearLayoutManager;
|
import android.support.v7.widget.LinearLayoutManager;
|
||||||
|
import android.view.LayoutInflater;
|
||||||
|
import android.view.View;
|
||||||
|
import android.view.ViewGroup;
|
||||||
|
import android.widget.LinearLayout;
|
||||||
|
|
||||||
import org.openintents.openpgp.util.OpenPgpApi;
|
import org.openintents.openpgp.util.OpenPgpApi;
|
||||||
import org.openintents.openpgp.util.OpenPgpUtils;
|
import org.openintents.openpgp.util.OpenPgpUtils;
|
||||||
import org.sufficientlysecure.keychain.R;
|
import org.sufficientlysecure.keychain.R;
|
||||||
|
import org.sufficientlysecure.keychain.model.SubKey.UnifiedKeyInfo;
|
||||||
import org.sufficientlysecure.keychain.pgp.KeyRing;
|
import org.sufficientlysecure.keychain.pgp.KeyRing;
|
||||||
import org.sufficientlysecure.keychain.provider.ApiAppDao;
|
import org.sufficientlysecure.keychain.provider.ApiAppDao;
|
||||||
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings;
|
import org.sufficientlysecure.keychain.provider.KeyRepository;
|
||||||
import org.sufficientlysecure.keychain.remote.ui.adapter.SelectSignKeyAdapter;
|
|
||||||
import org.sufficientlysecure.keychain.ui.CreateKeyActivity;
|
import org.sufficientlysecure.keychain.ui.CreateKeyActivity;
|
||||||
|
import org.sufficientlysecure.keychain.ui.adapter.KeyChoiceAdapter;
|
||||||
import org.sufficientlysecure.keychain.ui.base.RecyclerFragment;
|
import org.sufficientlysecure.keychain.ui.base.RecyclerFragment;
|
||||||
import org.sufficientlysecure.keychain.ui.util.adapter.CursorAdapter;
|
import org.sufficientlysecure.keychain.ui.keyview.GenericViewModel;
|
||||||
|
|
||||||
|
|
||||||
public class SelectSignKeyIdListFragment extends RecyclerFragment<SelectSignKeyAdapter>
|
public class SelectSignKeyIdListFragment extends RecyclerFragment<KeyChoiceAdapter> {
|
||||||
implements SelectSignKeyAdapter.SelectSignKeyListener, LoaderManager.LoaderCallbacks<Cursor> {
|
|
||||||
|
|
||||||
private static final String ARG_PACKAGE_NAME = "package_name";
|
private static final String ARG_PACKAGE_NAME = "package_name";
|
||||||
private static final String ARG_PREF_UID = "pref_uid";
|
private static final String ARG_PREF_UID = "pref_uid";
|
||||||
public static final String ARG_DATA = "data";
|
public static final String ARG_DATA = "data";
|
||||||
|
|
||||||
private Intent mResult;
|
private ApiAppDao apiAppDao;
|
||||||
private String mPrefUid;
|
private KeyRepository keyRepository;
|
||||||
private ApiAppDao mApiAppDao;
|
|
||||||
private String mPackageName;
|
private KeyChoiceAdapter keyChoiceAdapter;
|
||||||
|
|
||||||
|
private Intent resultIntent;
|
||||||
|
private String prefUid;
|
||||||
|
private String packageName;
|
||||||
|
|
||||||
public static SelectSignKeyIdListFragment newInstance(String packageName, Intent data, String preferredUserId) {
|
public static SelectSignKeyIdListFragment newInstance(String packageName, Intent data, String preferredUserId) {
|
||||||
SelectSignKeyIdListFragment frag = new SelectSignKeyIdListFragment();
|
SelectSignKeyIdListFragment frag = new SelectSignKeyIdListFragment();
|
||||||
@@ -68,112 +74,74 @@ public class SelectSignKeyIdListFragment extends RecyclerFragment<SelectSignKeyA
|
|||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
mApiAppDao = ApiAppDao.getInstance(getActivity());
|
apiAppDao = ApiAppDao.getInstance(requireContext());
|
||||||
|
keyRepository = KeyRepository.create(requireContext());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
|
||||||
|
LinearLayout linearLayout = new LinearLayout(requireContext());
|
||||||
|
linearLayout.setOrientation(LinearLayout.VERTICAL);
|
||||||
|
|
||||||
|
View dummyItemView = inflater.inflate(R.layout.key_list_dummy, linearLayout, false);
|
||||||
|
linearLayout.addView(dummyItemView, new LinearLayout.LayoutParams(
|
||||||
|
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
|
||||||
|
|
||||||
|
View recyclerView = super.onCreateView(inflater, parent, savedInstanceState);
|
||||||
|
linearLayout.addView(recyclerView, new LinearLayout.LayoutParams(
|
||||||
|
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
|
||||||
|
|
||||||
|
dummyItemView.setOnClickListener((v) -> onCreateKeyDummyClicked());
|
||||||
|
|
||||||
|
return linearLayout;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Define Adapter and Loader on create of Activity
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public void onActivityCreated(Bundle savedInstanceState) {
|
public void onActivityCreated(Bundle savedInstanceState) {
|
||||||
super.onActivityCreated(savedInstanceState);
|
super.onActivityCreated(savedInstanceState);
|
||||||
|
|
||||||
mResult = getArguments().getParcelable(ARG_DATA);
|
resultIntent = getArguments().getParcelable(ARG_DATA);
|
||||||
mPrefUid = getArguments().getString(ARG_PREF_UID);
|
prefUid = getArguments().getString(ARG_PREF_UID);
|
||||||
mPackageName = getArguments().getString(ARG_PACKAGE_NAME);
|
packageName = getArguments().getString(ARG_PACKAGE_NAME);
|
||||||
|
|
||||||
// Give some text to display if there is no data. In a real
|
|
||||||
// application this would come from a resource.
|
|
||||||
setEmptyText(getString(R.string.list_empty));
|
setEmptyText(getString(R.string.list_empty));
|
||||||
|
|
||||||
SelectSignKeyAdapter adapter = new SelectSignKeyAdapter(getContext(), null);
|
|
||||||
adapter.setListener(this);
|
|
||||||
|
|
||||||
setAdapter(adapter);
|
|
||||||
setLayoutManager(new LinearLayoutManager(getContext()));
|
setLayoutManager(new LinearLayoutManager(getContext()));
|
||||||
|
|
||||||
// Start out with a progress indicator.
|
|
||||||
hideList(false);
|
hideList(false);
|
||||||
|
|
||||||
// Prepare the loader. Either re-connect with an existing one,
|
GenericViewModel viewModel = ViewModelProviders.of(this).get(GenericViewModel.class);
|
||||||
// or start a new one.
|
LiveData<List<UnifiedKeyInfo>> liveData = viewModel.getGenericLiveData(
|
||||||
getLoaderManager().initLoader(0, null, this);
|
requireContext(), keyRepository::getAllUnifiedKeyInfoWithSecret);
|
||||||
|
liveData.observe(this, this::onLoadUnifiedKeyData);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public void onLoadUnifiedKeyData(List<UnifiedKeyInfo> data) {
|
||||||
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
|
if (keyChoiceAdapter == null) {
|
||||||
Uri baseUri = KeyRings.buildUnifiedKeyRingsUri();
|
keyChoiceAdapter = KeyChoiceAdapter.createSingleClickableAdapter(data, this::onSelectKeyItemClicked);
|
||||||
|
setAdapter(keyChoiceAdapter);
|
||||||
// These are the rows that we will retrieve.
|
|
||||||
String[] projection = new String[]{
|
|
||||||
KeyRings._ID,
|
|
||||||
KeyRings.MASTER_KEY_ID,
|
|
||||||
KeyRings.USER_ID,
|
|
||||||
KeyRings.IS_EXPIRED,
|
|
||||||
KeyRings.IS_REVOKED,
|
|
||||||
KeyRings.HAS_ENCRYPT,
|
|
||||||
KeyRings.VERIFIED,
|
|
||||||
KeyRings.HAS_ANY_SECRET,
|
|
||||||
KeyRings.HAS_DUPLICATE_USER_ID,
|
|
||||||
KeyRings.CREATION,
|
|
||||||
KeyRings.NAME,
|
|
||||||
KeyRings.EMAIL,
|
|
||||||
KeyRings.COMMENT
|
|
||||||
};
|
|
||||||
|
|
||||||
String selection = KeyRings.HAS_ANY_SECRET + " != 0";
|
|
||||||
|
|
||||||
String orderBy = KeyRings.USER_ID + " ASC";
|
|
||||||
// Now create and return a CursorLoader that will take care of
|
|
||||||
// creating a Cursor for the data being displayed.
|
|
||||||
return new CursorLoader(getActivity(), baseUri, projection, selection, null, orderBy);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
|
|
||||||
// Swap the new cursor in. (The framework will take care of closing the
|
|
||||||
// old cursor once we return.)
|
|
||||||
getAdapter().swapCursor(CursorAdapter.KeyCursor.wrap(data));
|
|
||||||
|
|
||||||
// The list should now be shown.
|
|
||||||
if (isResumed()) {
|
|
||||||
showList(true);
|
|
||||||
} else {
|
} else {
|
||||||
showList(false);
|
keyChoiceAdapter.setUnifiedKeyInfoItems(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
boolean animateShowList = !isResumed();
|
||||||
|
showList(animateShowList);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onLoaderReset(Loader<Cursor> loader) {
|
|
||||||
// This is called when the last Cursor provided to onLoadFinished()
|
|
||||||
// above is about to be closed. We need to make sure we are no
|
|
||||||
// longer using it.
|
|
||||||
getAdapter().swapCursor(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDestroy() {
|
|
||||||
getAdapter().setListener(null);
|
|
||||||
super.onDestroy();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onCreateKeyDummyClicked() {
|
public void onCreateKeyDummyClicked() {
|
||||||
OpenPgpUtils.UserId userIdSplit = KeyRing.splitUserId(mPrefUid);
|
OpenPgpUtils.UserId userIdSplit = KeyRing.splitUserId(prefUid);
|
||||||
|
|
||||||
Intent intent = new Intent(getActivity(), CreateKeyActivity.class);
|
Intent intent = new Intent(getActivity(), CreateKeyActivity.class);
|
||||||
intent.putExtra(CreateKeyActivity.EXTRA_NAME, userIdSplit.name);
|
intent.putExtra(CreateKeyActivity.EXTRA_NAME, userIdSplit.name);
|
||||||
intent.putExtra(CreateKeyActivity.EXTRA_EMAIL, userIdSplit.email);
|
intent.putExtra(CreateKeyActivity.EXTRA_EMAIL, userIdSplit.email);
|
||||||
getActivity().startActivityForResult(intent, SelectSignKeyIdActivity.REQUEST_CODE_CREATE_KEY);
|
|
||||||
|
requireActivity().startActivityForResult(intent, SelectSignKeyIdActivity.REQUEST_CODE_CREATE_KEY);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private void onSelectKeyItemClicked(UnifiedKeyInfo keyInfo) {
|
||||||
public void onSelectKeyItemClicked(long masterKeyId) {
|
apiAppDao.addAllowedKeyIdForApp(packageName, keyInfo.master_key_id());
|
||||||
mApiAppDao.addAllowedKeyIdForApp(mPackageName, masterKeyId);
|
resultIntent.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, keyInfo.master_key_id());
|
||||||
mResult.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, masterKeyId);
|
|
||||||
|
|
||||||
getActivity().setResult(Activity.RESULT_OK, mResult);
|
Activity activity = requireActivity();
|
||||||
getActivity().finish();
|
activity.setResult(Activity.RESULT_OK, resultIntent);
|
||||||
|
activity.finish();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ class RemoteDeduplicatePresenter {
|
|||||||
|
|
||||||
private void onLoadKeyInfos(List<UnifiedKeyInfo> data) {
|
private void onLoadKeyInfos(List<UnifiedKeyInfo> data) {
|
||||||
if (keyChoiceAdapter == null) {
|
if (keyChoiceAdapter == null) {
|
||||||
keyChoiceAdapter = new KeyChoiceAdapter(false, data);
|
keyChoiceAdapter = KeyChoiceAdapter.createSingleChoiceAdapter(data);
|
||||||
view.setKeyListAdapter(keyChoiceAdapter);
|
view.setKeyListAdapter(keyChoiceAdapter);
|
||||||
} else {
|
} else {
|
||||||
keyChoiceAdapter.setUnifiedKeyInfoItems(data);
|
keyChoiceAdapter.setUnifiedKeyInfoItems(data);
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ package org.sufficientlysecure.keychain.ui.adapter;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
@@ -25,12 +26,27 @@ import org.sufficientlysecure.keychain.ui.adapter.KeyChoiceAdapter.KeyChoiceItem
|
|||||||
|
|
||||||
|
|
||||||
public class KeyChoiceAdapter extends FlexibleAdapter<KeyChoiceItem> {
|
public class KeyChoiceAdapter extends FlexibleAdapter<KeyChoiceItem> {
|
||||||
|
private final OnKeyClickListener onKeyClickListener;
|
||||||
private Integer activeItem;
|
private Integer activeItem;
|
||||||
|
|
||||||
public KeyChoiceAdapter(boolean isMultiChoice, List<UnifiedKeyInfo> items) {
|
public static KeyChoiceAdapter createSingleClickableAdapter(List<UnifiedKeyInfo> items,
|
||||||
|
OnKeyClickListener onKeyClickListener) {
|
||||||
|
return new KeyChoiceAdapter(items, Objects.requireNonNull(onKeyClickListener), Mode.IDLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static KeyChoiceAdapter createSingleChoiceAdapter(List<UnifiedKeyInfo> items) {
|
||||||
|
return new KeyChoiceAdapter(items, null, Mode.SINGLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static KeyChoiceAdapter createMultiChoiceAdapter(List<UnifiedKeyInfo> items) {
|
||||||
|
return new KeyChoiceAdapter(items, null, Mode.MULTI);
|
||||||
|
}
|
||||||
|
|
||||||
|
private KeyChoiceAdapter(List<UnifiedKeyInfo> items, OnKeyClickListener onKeyClickListener, int idle) {
|
||||||
super(getKeyChoiceItems(items));
|
super(getKeyChoiceItems(items));
|
||||||
setMode(isMultiChoice ? Mode.MULTI : Mode.SINGLE);
|
setMode(idle);
|
||||||
addListener((OnItemClickListener) (view, position) -> onClickItem(position));
|
addListener((OnItemClickListener) (view, position) -> onClickItem(position));
|
||||||
|
this.onKeyClickListener = onKeyClickListener;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@@ -50,10 +66,15 @@ public class KeyChoiceAdapter extends FlexibleAdapter<KeyChoiceItem> {
|
|||||||
if (getMode() == Mode.MULTI) {
|
if (getMode() == Mode.MULTI) {
|
||||||
toggleSelection(position);
|
toggleSelection(position);
|
||||||
notifyItemChanged(position);
|
notifyItemChanged(position);
|
||||||
} else {
|
return true;
|
||||||
|
} else if (getMode() == Mode.SINGLE) {
|
||||||
setActiveItem(position);
|
setActiveItem(position);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
|
KeyChoiceItem item = getItem(position);
|
||||||
|
onKeyClickListener.onKeyClick(item.keyInfo);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setActiveItem(Integer newActiveItem) {
|
public void setActiveItem(Integer newActiveItem) {
|
||||||
@@ -212,4 +233,8 @@ public class KeyChoiceAdapter extends FlexibleAdapter<KeyChoiceItem> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public interface OnKeyClickListener {
|
||||||
|
void onKeyClick(UnifiedKeyInfo keyInfo);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,10 +16,10 @@
|
|||||||
|
|
||||||
package org.sufficientlysecure.keychain.ui.base;
|
package org.sufficientlysecure.keychain.ui.base;
|
||||||
|
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.support.annotation.LayoutRes;
|
|
||||||
import android.support.v4.app.Fragment;
|
import android.support.v4.app.Fragment;
|
||||||
import android.support.v7.widget.RecyclerView;
|
import android.support.v7.widget.RecyclerView;
|
||||||
import android.view.Gravity;
|
import android.view.Gravity;
|
||||||
@@ -32,7 +32,7 @@ import android.widget.LinearLayout;
|
|||||||
import android.widget.ProgressBar;
|
import android.widget.ProgressBar;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import org.sufficientlysecure.keychain.ui.util.FormattingUtils;
|
import org.sufficientlysecure.keychain.R;
|
||||||
import timber.log.Timber;
|
import timber.log.Timber;
|
||||||
|
|
||||||
|
|
||||||
@@ -124,9 +124,16 @@ public class RecyclerFragment<A extends RecyclerView.Adapter> extends Fragment {
|
|||||||
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
|
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
|
||||||
|
|
||||||
|
|
||||||
FrameLayout listContainer = new FrameLayout(context);
|
LinearLayout listContainer = new LinearLayout(context);
|
||||||
|
listContainer.setOrientation(LinearLayout.VERTICAL);
|
||||||
listContainer.setId(INTERNAL_LIST_CONTAINER_ID);
|
listContainer.setId(INTERNAL_LIST_CONTAINER_ID);
|
||||||
|
|
||||||
|
LinearLayout headerLayout = new LinearLayout(context);
|
||||||
|
headerLayout.setId(R.id.headerlayout);
|
||||||
|
|
||||||
|
listContainer.addView(headerLayout, new LinearLayout.LayoutParams(
|
||||||
|
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
|
||||||
|
|
||||||
TextView textView = new TextView(context);
|
TextView textView = new TextView(context);
|
||||||
textView.setId(INTERNAL_EMPTY_VIEW_ID);
|
textView.setId(INTERNAL_EMPTY_VIEW_ID);
|
||||||
textView.setGravity(Gravity.CENTER);
|
textView.setGravity(Gravity.CENTER);
|
||||||
|
|||||||
Reference in New Issue
Block a user