use FlexibleAdapter for new KeyChoiceAdapter
This commit is contained in:
@@ -18,45 +18,36 @@
|
|||||||
package org.sufficientlysecure.keychain.remote.ui;
|
package org.sufficientlysecure.keychain.remote.ui;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import android.database.Cursor;
|
import android.arch.lifecycle.LiveData;
|
||||||
import android.net.Uri;
|
import android.arch.lifecycle.ViewModelProviders;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.v4.app.LoaderManager;
|
import android.support.v7.widget.LinearLayoutManager;
|
||||||
import android.support.v4.content.CursorLoader;
|
|
||||||
import android.support.v4.content.Loader;
|
|
||||||
import android.view.LayoutInflater;
|
|
||||||
import android.view.View;
|
|
||||||
import android.view.ViewGroup;
|
|
||||||
import android.widget.ListView;
|
|
||||||
|
|
||||||
import org.sufficientlysecure.keychain.R;
|
import org.sufficientlysecure.keychain.R;
|
||||||
import org.sufficientlysecure.keychain.compatibility.ListFragmentWorkaround;
|
import org.sufficientlysecure.keychain.model.SubKey.UnifiedKeyInfo;
|
||||||
import org.sufficientlysecure.keychain.provider.ApiAppDao;
|
import org.sufficientlysecure.keychain.provider.ApiAppDao;
|
||||||
import org.sufficientlysecure.keychain.provider.KeychainContract;
|
import org.sufficientlysecure.keychain.provider.KeyRepository;
|
||||||
import org.sufficientlysecure.keychain.ui.adapter.KeyAdapter;
|
import org.sufficientlysecure.keychain.ui.adapter.KeyChoiceAdapter;
|
||||||
import org.sufficientlysecure.keychain.ui.adapter.KeySelectableAdapter;
|
import org.sufficientlysecure.keychain.ui.base.RecyclerFragment;
|
||||||
import org.sufficientlysecure.keychain.ui.widget.FixedListView;
|
import org.sufficientlysecure.keychain.ui.keyview.GenericViewModel;
|
||||||
|
|
||||||
|
|
||||||
public class AppSettingsAllowedKeysListFragment extends ListFragmentWorkaround implements LoaderManager.LoaderCallbacks<Cursor> {
|
public class AppSettingsAllowedKeysListFragment extends RecyclerFragment<KeyChoiceAdapter> {
|
||||||
private static final String ARG_PACKAGE_NAME = "package_name";
|
private static final String ARG_PACKAGE_NAME = "package_name";
|
||||||
|
|
||||||
private KeySelectableAdapter mAdapter;
|
private KeyChoiceAdapter keyChoiceAdapter;
|
||||||
private ApiAppDao mApiAppDao;
|
private ApiAppDao apiAppDao;
|
||||||
|
|
||||||
private String packageName;
|
private String packageName;
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates new instance of this fragment
|
|
||||||
*/
|
|
||||||
public static AppSettingsAllowedKeysListFragment newInstance(String packageName) {
|
public static AppSettingsAllowedKeysListFragment newInstance(String packageName) {
|
||||||
AppSettingsAllowedKeysListFragment frag = new AppSettingsAllowedKeysListFragment();
|
AppSettingsAllowedKeysListFragment frag = new AppSettingsAllowedKeysListFragment();
|
||||||
|
|
||||||
Bundle args = new Bundle();
|
Bundle args = new Bundle();
|
||||||
|
|
||||||
args.putString(ARG_PACKAGE_NAME, packageName);
|
args.putString(ARG_PACKAGE_NAME, packageName);
|
||||||
|
|
||||||
frag.setArguments(args);
|
frag.setArguments(args);
|
||||||
|
|
||||||
return frag;
|
return frag;
|
||||||
@@ -66,34 +57,9 @@ public class AppSettingsAllowedKeysListFragment extends ListFragmentWorkaround i
|
|||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
mApiAppDao = ApiAppDao.getInstance(getActivity());
|
apiAppDao = ApiAppDao.getInstance(getActivity());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
|
||||||
Bundle savedInstanceState) {
|
|
||||||
View layout = super.onCreateView(inflater, container, savedInstanceState);
|
|
||||||
ListView lv = layout.findViewById(android.R.id.list);
|
|
||||||
ViewGroup parent = (ViewGroup) lv.getParent();
|
|
||||||
|
|
||||||
/*
|
|
||||||
* http://stackoverflow.com/a/15880684
|
|
||||||
* Remove ListView and add FixedListView in its place.
|
|
||||||
* This is done here programatically to be still able to use the progressBar of ListFragment.
|
|
||||||
*
|
|
||||||
* We want FixedListView to be able to put this ListFragment inside a ScrollView
|
|
||||||
*/
|
|
||||||
int lvIndex = parent.indexOfChild(lv);
|
|
||||||
parent.removeViewAt(lvIndex);
|
|
||||||
FixedListView newLv = new FixedListView(getActivity());
|
|
||||||
newLv.setId(android.R.id.list);
|
|
||||||
parent.addView(newLv, lvIndex, lv.getLayoutParams());
|
|
||||||
return layout;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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);
|
||||||
@@ -104,70 +70,36 @@ public class AppSettingsAllowedKeysListFragment extends ListFragmentWorkaround i
|
|||||||
// application this would come from a resource.
|
// application this would come from a resource.
|
||||||
setEmptyText(getString(R.string.list_empty));
|
setEmptyText(getString(R.string.list_empty));
|
||||||
|
|
||||||
Set<Long> checked = mApiAppDao.getAllowedKeyIdsForApp(packageName);
|
getRecyclerView().setLayoutManager(new LinearLayoutManager(requireContext()));
|
||||||
mAdapter = new KeySelectableAdapter(getActivity(), null, 0, checked);
|
|
||||||
setListAdapter(mAdapter);
|
|
||||||
getListView().setOnItemClickListener(mAdapter);
|
|
||||||
|
|
||||||
// Start out with a progress indicator.
|
// Start out with a progress indicator.
|
||||||
setListShown(false);
|
hideList(false);
|
||||||
|
|
||||||
// Prepare the loader. Either re-connect with an existing one,
|
|
||||||
// or start a new one.
|
|
||||||
getLoaderManager().initLoader(0, null, this);
|
|
||||||
|
|
||||||
|
KeyRepository keyRepository = KeyRepository.create(requireContext());
|
||||||
|
GenericViewModel viewModel = ViewModelProviders.of(this).get(GenericViewModel.class);
|
||||||
|
LiveData<List<UnifiedKeyInfo>> liveData =
|
||||||
|
viewModel.getGenericLiveData(requireContext(), keyRepository::getAllUnifiedKeyInfoWithSecret);
|
||||||
|
liveData.observe(this, this::onLoadUnifiedKeyData);
|
||||||
}
|
}
|
||||||
/** Returns all selected master key ids. */
|
|
||||||
public Set<Long> getSelectedMasterKeyIds() {
|
|
||||||
return mAdapter.getSelectedMasterKeyIds();
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Returns all selected user ids.
|
|
||||||
public String[] getSelectedUserIds() {
|
|
||||||
Vector<String> userIds = new Vector<>();
|
|
||||||
for (int i = 0; i < getListView().getCount(); ++i) {
|
|
||||||
if (getListView().isItemChecked(i)) {
|
|
||||||
userIds.add(spinnerAdapter.getUserId(i));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// make empty array to not return null
|
|
||||||
String userIdArray[] = new String[0];
|
|
||||||
return userIds.toArray(userIdArray);
|
|
||||||
} */
|
|
||||||
|
|
||||||
public void saveAllowedKeys() {
|
public void saveAllowedKeys() {
|
||||||
mApiAppDao.saveAllowedKeyIdsForApp(packageName, getSelectedMasterKeyIds());
|
Set<Long> longs = keyChoiceAdapter.getSelectionIds();
|
||||||
|
apiAppDao.saveAllowedKeyIdsForApp(packageName, longs);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public void onLoadUnifiedKeyData(List<UnifiedKeyInfo> data) {
|
||||||
public Loader<Cursor> onCreateLoader(int loaderId, Bundle data) {
|
if (keyChoiceAdapter == null) {
|
||||||
Uri baseUri = KeychainContract.KeyRings.buildUnifiedKeyRingsUri();
|
keyChoiceAdapter = new KeyChoiceAdapter(true, data);
|
||||||
String where = KeychainContract.KeyRings.HAS_ANY_SECRET + " = 1";
|
setAdapter(keyChoiceAdapter);
|
||||||
|
|
||||||
return new CursorLoader(getActivity(), baseUri, KeyAdapter.PROJECTION, where, null, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@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.)
|
|
||||||
mAdapter.swapCursor(data);
|
|
||||||
|
|
||||||
// The list should now be shown.
|
|
||||||
if (isResumed()) {
|
|
||||||
setListShown(true);
|
|
||||||
} else {
|
} else {
|
||||||
setListShownNoAnimation(true);
|
keyChoiceAdapter.setUnifiedKeyInfoItems(data);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
Set<Long> checkedIds = apiAppDao.getAllowedKeyIdsForApp(packageName);
|
||||||
public void onLoaderReset(Loader<Cursor> loader) {
|
keyChoiceAdapter.setSelectionByIds(checkedIds);
|
||||||
// This is called when the last Cursor provided to onLoadFinished()
|
|
||||||
// above is about to be closed. We need to make sure we are no
|
boolean animateShowList = !isResumed();
|
||||||
// longer using it.
|
showList(animateShowList);
|
||||||
mAdapter.swapCursor(null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,9 +52,6 @@ public class SelectSignKeyIdListFragment extends RecyclerFragment<SelectSignKeyA
|
|||||||
private ApiAppDao mApiAppDao;
|
private ApiAppDao mApiAppDao;
|
||||||
private String mPackageName;
|
private String mPackageName;
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates new instance of this fragment
|
|
||||||
*/
|
|
||||||
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();
|
||||||
Bundle args = new Bundle();
|
Bundle args = new Bundle();
|
||||||
|
|||||||
@@ -29,19 +29,19 @@ import android.arch.lifecycle.ViewModelProviders;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.graphics.drawable.Drawable;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
import android.support.v4.app.DialogFragment;
|
import android.support.v4.app.DialogFragment;
|
||||||
import android.support.v4.app.FragmentActivity;
|
import android.support.v4.app.FragmentActivity;
|
||||||
import android.support.v7.widget.LinearLayoutManager;
|
import android.support.v7.widget.LinearLayoutManager;
|
||||||
import android.support.v7.widget.RecyclerView;
|
import android.support.v7.widget.RecyclerView;
|
||||||
|
import android.support.v7.widget.RecyclerView.Adapter;
|
||||||
import android.view.ContextThemeWrapper;
|
import android.view.ContextThemeWrapper;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.ImageView;
|
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
import com.mikepenz.materialdrawer.util.KeyboardUtil;
|
import com.mikepenz.materialdrawer.util.KeyboardUtil;
|
||||||
import org.sufficientlysecure.keychain.R;
|
import org.sufficientlysecure.keychain.R;
|
||||||
@@ -50,11 +50,9 @@ import org.sufficientlysecure.keychain.model.SubKey.UnifiedKeyInfo;
|
|||||||
import org.sufficientlysecure.keychain.provider.KeyRepository;
|
import org.sufficientlysecure.keychain.provider.KeyRepository;
|
||||||
import org.sufficientlysecure.keychain.remote.ui.RemoteSecurityTokenOperationActivity;
|
import org.sufficientlysecure.keychain.remote.ui.RemoteSecurityTokenOperationActivity;
|
||||||
import org.sufficientlysecure.keychain.remote.ui.dialog.RemoteDeduplicatePresenter.RemoteDeduplicateView;
|
import org.sufficientlysecure.keychain.remote.ui.dialog.RemoteDeduplicatePresenter.RemoteDeduplicateView;
|
||||||
import org.sufficientlysecure.keychain.ui.adapter.KeyChoiceAdapter;
|
|
||||||
import org.sufficientlysecure.keychain.ui.dialog.CustomAlertDialogBuilder;
|
import org.sufficientlysecure.keychain.ui.dialog.CustomAlertDialogBuilder;
|
||||||
import org.sufficientlysecure.keychain.ui.util.ThemeChanger;
|
import org.sufficientlysecure.keychain.ui.util.ThemeChanger;
|
||||||
import org.sufficientlysecure.keychain.ui.util.recyclerview.DividerItemDecoration;
|
import org.sufficientlysecure.keychain.ui.util.recyclerview.DividerItemDecoration;
|
||||||
import org.sufficientlysecure.keychain.ui.util.recyclerview.RecyclerItemClickListener;
|
|
||||||
|
|
||||||
|
|
||||||
public class RemoteDeduplicateActivity extends FragmentActivity {
|
public class RemoteDeduplicateActivity extends FragmentActivity {
|
||||||
@@ -157,7 +155,7 @@ public class RemoteDeduplicateActivity extends FragmentActivity {
|
|||||||
new DividerItemDecoration(activity, DividerItemDecoration.VERTICAL_LIST, true));
|
new DividerItemDecoration(activity, DividerItemDecoration.VERTICAL_LIST, true));
|
||||||
|
|
||||||
setupListenersForPresenter();
|
setupListenersForPresenter();
|
||||||
mvpView = createMvpView(view, layoutInflater);
|
mvpView = createMvpView(view);
|
||||||
|
|
||||||
return alert.create();
|
return alert.create();
|
||||||
}
|
}
|
||||||
@@ -190,11 +188,8 @@ public class RemoteDeduplicateActivity extends FragmentActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
private RemoteDeduplicateView createMvpView(View view, LayoutInflater layoutInflater) {
|
private RemoteDeduplicateView createMvpView(View view) {
|
||||||
final ImageView iconClientApp = view.findViewById(R.id.icon_client_app);
|
|
||||||
final KeyChoiceAdapter keyChoiceAdapter = new KeyChoiceAdapter(layoutInflater, getResources());
|
|
||||||
final TextView addressText = view.findViewById(R.id.select_key_item_name);
|
final TextView addressText = view.findViewById(R.id.select_key_item_name);
|
||||||
keyChoiceList.setAdapter(keyChoiceAdapter);
|
|
||||||
|
|
||||||
return new RemoteDeduplicateView() {
|
return new RemoteDeduplicateView() {
|
||||||
@Override
|
@Override
|
||||||
@@ -222,9 +217,8 @@ public class RemoteDeduplicateActivity extends FragmentActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setTitleClientIcon(Drawable drawable) {
|
public void showNoSelectionError() {
|
||||||
iconClientApp.setImageDrawable(drawable);
|
Toast.makeText(getContext(), "No key selected!", Toast.LENGTH_SHORT).show();
|
||||||
keyChoiceAdapter.setSelectionDrawable(drawable);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -233,18 +227,8 @@ public class RemoteDeduplicateActivity extends FragmentActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setKeyListData(List<UnifiedKeyInfo> data) {
|
public void setKeyListAdapter(Adapter adapter) {
|
||||||
keyChoiceAdapter.setData(data);
|
keyChoiceList.setAdapter(adapter);
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setActiveItem(Integer position) {
|
|
||||||
keyChoiceAdapter.setActiveItem(position);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setEnableSelectButton(boolean enabled) {
|
|
||||||
buttonSelect.setEnabled(enabled);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -252,8 +236,6 @@ public class RemoteDeduplicateActivity extends FragmentActivity {
|
|||||||
private void setupListenersForPresenter() {
|
private void setupListenersForPresenter() {
|
||||||
buttonSelect.setOnClickListener(view -> presenter.onClickSelect());
|
buttonSelect.setOnClickListener(view -> presenter.onClickSelect());
|
||||||
buttonCancel.setOnClickListener(view -> presenter.onClickCancel());
|
buttonCancel.setOnClickListener(view -> presenter.onClickCancel());
|
||||||
keyChoiceList.addOnItemTouchListener(new RecyclerItemClickListener(getContext(),
|
|
||||||
(view, position) -> presenter.onKeyItemClick(position)));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,19 +22,15 @@ import java.util.List;
|
|||||||
|
|
||||||
import android.arch.lifecycle.LifecycleOwner;
|
import android.arch.lifecycle.LifecycleOwner;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.pm.ApplicationInfo;
|
import android.support.v7.widget.RecyclerView.Adapter;
|
||||||
import android.content.pm.PackageManager;
|
|
||||||
import android.content.pm.PackageManager.NameNotFoundException;
|
|
||||||
import android.graphics.drawable.Drawable;
|
|
||||||
|
|
||||||
import org.sufficientlysecure.keychain.model.SubKey.UnifiedKeyInfo;
|
import org.sufficientlysecure.keychain.model.SubKey.UnifiedKeyInfo;
|
||||||
import org.sufficientlysecure.keychain.remote.AutocryptInteractor;
|
import org.sufficientlysecure.keychain.remote.AutocryptInteractor;
|
||||||
import org.sufficientlysecure.keychain.remote.ui.dialog.RemoteDeduplicateActivity.DeduplicateViewModel;
|
import org.sufficientlysecure.keychain.remote.ui.dialog.RemoteDeduplicateActivity.DeduplicateViewModel;
|
||||||
import timber.log.Timber;
|
import org.sufficientlysecure.keychain.ui.adapter.KeyChoiceAdapter;
|
||||||
|
|
||||||
|
|
||||||
class RemoteDeduplicatePresenter {
|
class RemoteDeduplicatePresenter {
|
||||||
private final PackageManager packageManager;
|
|
||||||
private final Context context;
|
private final Context context;
|
||||||
private final LifecycleOwner lifecycleOwner;
|
private final LifecycleOwner lifecycleOwner;
|
||||||
|
|
||||||
@@ -43,15 +39,12 @@ class RemoteDeduplicatePresenter {
|
|||||||
|
|
||||||
private DeduplicateViewModel viewModel;
|
private DeduplicateViewModel viewModel;
|
||||||
private RemoteDeduplicateView view;
|
private RemoteDeduplicateView view;
|
||||||
private Integer selectedItem;
|
private KeyChoiceAdapter keyChoiceAdapter;
|
||||||
private List<UnifiedKeyInfo> keyInfoData;
|
|
||||||
|
|
||||||
|
|
||||||
RemoteDeduplicatePresenter(Context context, LifecycleOwner lifecycleOwner) {
|
RemoteDeduplicatePresenter(Context context, LifecycleOwner lifecycleOwner) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.lifecycleOwner = lifecycleOwner;
|
this.lifecycleOwner = lifecycleOwner;
|
||||||
|
|
||||||
packageManager = context.getPackageManager();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setView(RemoteDeduplicateView view) {
|
public void setView(RemoteDeduplicateView view) {
|
||||||
@@ -62,42 +55,27 @@ class RemoteDeduplicatePresenter {
|
|||||||
this.viewModel = viewModel;
|
this.viewModel = viewModel;
|
||||||
this.autocryptInteractor = AutocryptInteractor.getInstance(context, viewModel.getPackageName());
|
this.autocryptInteractor = AutocryptInteractor.getInstance(context, viewModel.getPackageName());
|
||||||
|
|
||||||
try {
|
|
||||||
setPackageInfo(viewModel.getPackageName());
|
|
||||||
} catch (NameNotFoundException e) {
|
|
||||||
Timber.e("Unable to find info of calling app!");
|
|
||||||
view.finishAsCancelled();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
view.setAddressText(viewModel.getDuplicateAddress());
|
view.setAddressText(viewModel.getDuplicateAddress());
|
||||||
|
|
||||||
viewModel.getKeyInfoLiveData(context).observe(lifecycleOwner, this::onLoadKeyInfos);
|
viewModel.getKeyInfoLiveData(context).observe(lifecycleOwner, this::onLoadKeyInfos);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setPackageInfo(String packageName) throws NameNotFoundException {
|
|
||||||
ApplicationInfo applicationInfo = packageManager.getApplicationInfo(packageName, 0);
|
|
||||||
Drawable appIcon = packageManager.getApplicationIcon(applicationInfo);
|
|
||||||
|
|
||||||
view.setTitleClientIcon(appIcon);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void onLoadKeyInfos(List<UnifiedKeyInfo> data) {
|
private void onLoadKeyInfos(List<UnifiedKeyInfo> data) {
|
||||||
this.keyInfoData = data;
|
if (keyChoiceAdapter == null) {
|
||||||
view.setKeyListData(data);
|
keyChoiceAdapter = new KeyChoiceAdapter(false, data);
|
||||||
|
view.setKeyListAdapter(keyChoiceAdapter);
|
||||||
|
} else {
|
||||||
|
keyChoiceAdapter.setUnifiedKeyInfoItems(data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void onClickSelect() {
|
void onClickSelect() {
|
||||||
if (keyInfoData == null) {
|
UnifiedKeyInfo activeItem = keyChoiceAdapter.getActiveItem();
|
||||||
Timber.e("got click on select with no data…?");
|
if (activeItem == null) {
|
||||||
|
view.showNoSelectionError();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (selectedItem == null) {
|
long masterKeyId = activeItem.master_key_id();
|
||||||
Timber.e("got click on select with no selection…?");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
long masterKeyId = keyInfoData.get(selectedItem).master_key_id();
|
|
||||||
autocryptInteractor.updateKeyGossipFromDedup(viewModel.getDuplicateAddress(), masterKeyId);
|
autocryptInteractor.updateKeyGossipFromDedup(viewModel.getDuplicateAddress(), masterKeyId);
|
||||||
|
|
||||||
view.finish();
|
view.finish();
|
||||||
@@ -111,25 +89,13 @@ class RemoteDeduplicatePresenter {
|
|||||||
view.finishAsCancelled();
|
view.finishAsCancelled();
|
||||||
}
|
}
|
||||||
|
|
||||||
void onKeyItemClick(int position) {
|
|
||||||
if (selectedItem != null && position == selectedItem) {
|
|
||||||
selectedItem = null;
|
|
||||||
} else {
|
|
||||||
selectedItem = position;
|
|
||||||
}
|
|
||||||
view.setActiveItem(selectedItem);
|
|
||||||
view.setEnableSelectButton(selectedItem != null);
|
|
||||||
}
|
|
||||||
|
|
||||||
interface RemoteDeduplicateView {
|
interface RemoteDeduplicateView {
|
||||||
|
void showNoSelectionError();
|
||||||
void finish();
|
void finish();
|
||||||
void finishAsCancelled();
|
void finishAsCancelled();
|
||||||
|
|
||||||
void setAddressText(String text);
|
void setAddressText(String text);
|
||||||
void setTitleClientIcon(Drawable drawable);
|
|
||||||
|
|
||||||
void setKeyListData(List<UnifiedKeyInfo> data);
|
void setKeyListAdapter(Adapter adapter);
|
||||||
void setActiveItem(Integer position);
|
|
||||||
void setEnableSelectButton(boolean enabled);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,81 +1,66 @@
|
|||||||
package org.sufficientlysecure.keychain.ui.adapter;
|
package org.sufficientlysecure.keychain.ui.adapter;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.res.Resources;
|
import android.support.annotation.Nullable;
|
||||||
import android.graphics.drawable.Drawable;
|
|
||||||
import android.graphics.drawable.Drawable.ConstantState;
|
|
||||||
import android.support.annotation.NonNull;
|
|
||||||
import android.support.v4.content.res.ResourcesCompat;
|
|
||||||
import android.support.v4.graphics.drawable.DrawableCompat;
|
|
||||||
import android.support.v7.widget.RecyclerView;
|
import android.support.v7.widget.RecyclerView;
|
||||||
import android.support.v7.widget.RecyclerView.Adapter;
|
|
||||||
import android.text.format.DateUtils;
|
import android.text.format.DateUtils;
|
||||||
import android.view.LayoutInflater;
|
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.widget.CheckBox;
|
||||||
import android.widget.ImageView;
|
import android.widget.RadioButton;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
|
import eu.davidea.flexibleadapter.FlexibleAdapter;
|
||||||
|
import eu.davidea.flexibleadapter.items.AbstractFlexibleItem;
|
||||||
|
import eu.davidea.flexibleadapter.items.IFlexible;
|
||||||
|
import eu.davidea.viewholders.FlexibleViewHolder;
|
||||||
import org.sufficientlysecure.keychain.R;
|
import org.sufficientlysecure.keychain.R;
|
||||||
import org.sufficientlysecure.keychain.model.SubKey.UnifiedKeyInfo;
|
import org.sufficientlysecure.keychain.model.SubKey.UnifiedKeyInfo;
|
||||||
import org.sufficientlysecure.keychain.ui.adapter.KeyChoiceAdapter.KeyChoiceViewHolder;
|
import org.sufficientlysecure.keychain.ui.adapter.KeyChoiceAdapter.KeyChoiceItem;
|
||||||
|
|
||||||
|
|
||||||
public class KeyChoiceAdapter extends Adapter<KeyChoiceViewHolder> {
|
public class KeyChoiceAdapter extends FlexibleAdapter<KeyChoiceItem> {
|
||||||
private final LayoutInflater layoutInflater;
|
|
||||||
private final Resources resources;
|
|
||||||
private List<UnifiedKeyInfo> data;
|
|
||||||
private Drawable iconUnselected;
|
|
||||||
private Drawable iconSelected;
|
|
||||||
private Integer activeItem;
|
private Integer activeItem;
|
||||||
|
|
||||||
public KeyChoiceAdapter(LayoutInflater layoutInflater, Resources resources) {
|
public KeyChoiceAdapter(boolean isMultiChoice, List<UnifiedKeyInfo> items) {
|
||||||
this.layoutInflater = layoutInflater;
|
super(getKeyChoiceItems(items));
|
||||||
this.resources = resources;
|
setMode(isMultiChoice ? Mode.MULTI : Mode.SINGLE);
|
||||||
|
addListener((OnItemClickListener) (view, position) -> onClickItem(position));
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@Nullable
|
||||||
@Override
|
private static ArrayList<KeyChoiceItem> getKeyChoiceItems(@Nullable List<UnifiedKeyInfo> items) {
|
||||||
public KeyChoiceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
if (items == null) {
|
||||||
View keyChoiceItemView = layoutInflater.inflate(R.layout.duplicate_key_item, parent, false);
|
return null;
|
||||||
return new KeyChoiceViewHolder(keyChoiceItemView);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onBindViewHolder(@NonNull KeyChoiceViewHolder holder, int position) {
|
|
||||||
UnifiedKeyInfo keyInfo = data.get(position);
|
|
||||||
Drawable icon = (activeItem != null && position == activeItem) ? iconSelected : iconUnselected;
|
|
||||||
holder.bind(keyInfo, icon);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getItemCount() {
|
|
||||||
return data != null ? data.size() : 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setData(List<UnifiedKeyInfo> data) {
|
|
||||||
this.data = data;
|
|
||||||
notifyDataSetChanged();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setSelectionDrawable(Drawable drawable) {
|
|
||||||
ConstantState constantState = drawable.getConstantState();
|
|
||||||
if (constantState == null) {
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
ArrayList<KeyChoiceItem> choiceItems = new ArrayList<>();
|
||||||
|
for (UnifiedKeyInfo keyInfo : items) {
|
||||||
|
KeyChoiceItem keyChoiceItem = new KeyChoiceItem(keyInfo);
|
||||||
|
choiceItems.add(keyChoiceItem);
|
||||||
|
}
|
||||||
|
return choiceItems;
|
||||||
|
}
|
||||||
|
|
||||||
iconSelected = constantState.newDrawable(resources);
|
private boolean onClickItem(int position) {
|
||||||
|
if (getMode() == Mode.MULTI) {
|
||||||
iconUnselected = constantState.newDrawable(resources);
|
toggleSelection(position);
|
||||||
DrawableCompat.setTint(iconUnselected.mutate(), ResourcesCompat.getColor(resources, R.color.md_grey_300, null));
|
notifyItemChanged(position);
|
||||||
|
} else {
|
||||||
notifyDataSetChanged();
|
setActiveItem(position);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setActiveItem(Integer newActiveItem) {
|
public void setActiveItem(Integer newActiveItem) {
|
||||||
|
if (getMode() != Mode.SINGLE) {
|
||||||
|
throw new IllegalStateException("Cannot get active item in single select mode!");
|
||||||
|
}
|
||||||
|
|
||||||
Integer prevActiveItem = this.activeItem;
|
Integer prevActiveItem = this.activeItem;
|
||||||
this.activeItem = newActiveItem;
|
this.activeItem = newActiveItem;
|
||||||
|
|
||||||
@@ -87,20 +72,117 @@ public class KeyChoiceAdapter extends Adapter<KeyChoiceViewHolder> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class KeyChoiceViewHolder extends RecyclerView.ViewHolder {
|
public UnifiedKeyInfo getActiveItem() {
|
||||||
private final TextView vName;
|
if (getMode() != Mode.SINGLE) {
|
||||||
private final TextView vCreation;
|
throw new IllegalStateException("Cannot get active item in single select mode!");
|
||||||
private final ImageView vIcon;
|
}
|
||||||
|
if (activeItem == null) {
|
||||||
KeyChoiceViewHolder(View itemView) {
|
return null;
|
||||||
super(itemView);
|
|
||||||
|
|
||||||
vName = itemView.findViewById(R.id.key_list_item_name);
|
|
||||||
vCreation = itemView.findViewById(R.id.key_list_item_creation);
|
|
||||||
vIcon = itemView.findViewById(R.id.key_list_item_icon);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void bind(UnifiedKeyInfo keyInfo, Drawable selectionIcon) {
|
KeyChoiceItem item = getItem(activeItem);
|
||||||
|
return item == null ? null : item.keyInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUnifiedKeyInfoItems(List<UnifiedKeyInfo> keyInfos) {
|
||||||
|
List<KeyChoiceItem> keyChoiceItems = getKeyChoiceItems(keyInfos);
|
||||||
|
updateDataSet(keyChoiceItems);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long getItemId(int position) {
|
||||||
|
KeyChoiceItem item = getItem(position);
|
||||||
|
if (item == null) {
|
||||||
|
return RecyclerView.NO_ID;
|
||||||
|
}
|
||||||
|
return item.getMasterKeyId();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSelectionByIds(Set<Long> checkedIds) {
|
||||||
|
if (getMode() != Mode.MULTI) {
|
||||||
|
throw new IllegalStateException("Cannot get active item in single select mode!");
|
||||||
|
}
|
||||||
|
|
||||||
|
clearSelection();
|
||||||
|
for (int position = 0; position < getItemCount(); position++) {
|
||||||
|
long itemId = getItemId(position);
|
||||||
|
if (checkedIds.contains(itemId)) {
|
||||||
|
addSelection(position);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<Long> getSelectionIds() {
|
||||||
|
if (getMode() != Mode.MULTI) {
|
||||||
|
throw new IllegalStateException("Cannot get active item in single select mode!");
|
||||||
|
}
|
||||||
|
|
||||||
|
Set<Long> result = new HashSet<>();
|
||||||
|
for (int position : getSelectedPositions()) {
|
||||||
|
long itemId = getItemId(position);
|
||||||
|
result.add(itemId);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class KeyChoiceItem extends AbstractFlexibleItem<KeyChoiceViewHolder> {
|
||||||
|
private UnifiedKeyInfo keyInfo;
|
||||||
|
|
||||||
|
KeyChoiceItem(UnifiedKeyInfo keyInfo) {
|
||||||
|
this.keyInfo = keyInfo;
|
||||||
|
setSelectable(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getLayoutRes() {
|
||||||
|
return R.layout.key_choice_item;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public KeyChoiceViewHolder createViewHolder(View view, FlexibleAdapter<IFlexible> adapter) {
|
||||||
|
return new KeyChoiceViewHolder(view, adapter);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void bindViewHolder(FlexibleAdapter<IFlexible> adapter, KeyChoiceViewHolder holder, int position,
|
||||||
|
List<Object> payloads) {
|
||||||
|
boolean isActive = adapter.isSelected(position);
|
||||||
|
holder.bind(keyInfo, adapter.getMode(), isActive);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
return (o instanceof KeyChoiceItem) &&
|
||||||
|
((KeyChoiceItem) o).keyInfo.master_key_id() == keyInfo.master_key_id();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
long masterKeyId = keyInfo.master_key_id();
|
||||||
|
return (int) (masterKeyId ^ (masterKeyId >>> 32));
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getMasterKeyId() {
|
||||||
|
return keyInfo.master_key_id();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class KeyChoiceViewHolder extends FlexibleViewHolder {
|
||||||
|
private final TextView vName;
|
||||||
|
private final TextView vCreation;
|
||||||
|
private final CheckBox vCheckbox;
|
||||||
|
private final RadioButton vRadio;
|
||||||
|
|
||||||
|
KeyChoiceViewHolder(View itemView, FlexibleAdapter<IFlexible> adapter) {
|
||||||
|
super(itemView, adapter);
|
||||||
|
|
||||||
|
vName = itemView.findViewById(R.id.text_keychoice_name);
|
||||||
|
vCreation = itemView.findViewById(R.id.text_keychoice_creation);
|
||||||
|
vCheckbox = itemView.findViewById(R.id.checkbox_keychoice);
|
||||||
|
vRadio = itemView.findViewById(R.id.radio_keychoice);
|
||||||
|
}
|
||||||
|
|
||||||
|
void bind(UnifiedKeyInfo keyInfo, int choiceMode, boolean isActive) {
|
||||||
vName.setText(keyInfo.name());
|
vName.setText(keyInfo.name());
|
||||||
|
|
||||||
Context context = vCreation.getContext();
|
Context context = vCreation.getContext();
|
||||||
@@ -109,7 +191,25 @@ public class KeyChoiceAdapter extends Adapter<KeyChoiceViewHolder> {
|
|||||||
DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_ABBREV_MONTH);
|
DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_ABBREV_MONTH);
|
||||||
vCreation.setText(context.getString(R.string.label_key_created, dateTime));
|
vCreation.setText(context.getString(R.string.label_key_created, dateTime));
|
||||||
|
|
||||||
vIcon.setImageDrawable(selectionIcon);
|
switch (choiceMode) {
|
||||||
|
case Mode.IDLE: {
|
||||||
|
vRadio.setVisibility(View.GONE);
|
||||||
|
vCheckbox.setVisibility(View.GONE);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Mode.SINGLE: {
|
||||||
|
vRadio.setVisibility(View.VISIBLE);
|
||||||
|
vRadio.setChecked(isActive);
|
||||||
|
vCheckbox.setVisibility(View.GONE);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Mode.MULTI: {
|
||||||
|
vCheckbox.setVisibility(View.VISIBLE);
|
||||||
|
vCheckbox.setChecked(isActive);
|
||||||
|
vRadio.setVisibility(View.GONE);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -101,7 +101,7 @@
|
|||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:id="@+id/duplicate_key_list"
|
android:id="@+id/duplicate_key_list"
|
||||||
tools:listitem="@layout/duplicate_key_item"
|
tools:listitem="@layout/key_choice_item"
|
||||||
tools:layout_height="100dp"
|
tools:layout_height="100dp"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
|||||||
@@ -8,30 +8,42 @@
|
|||||||
android:gravity="center_vertical"
|
android:gravity="center_vertical"
|
||||||
android:orientation="horizontal">
|
android:orientation="horizontal">
|
||||||
|
|
||||||
<ImageView
|
<RadioButton
|
||||||
android:layout_width="24dp"
|
android:id="@+id/radio_keychoice"
|
||||||
android:layout_height="24dp"
|
android:layout_width="wrap_content"
|
||||||
android:layout_marginRight="12dp"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginEnd="12dp"
|
android:layout_marginStart="12dp"
|
||||||
android:id="@+id/key_list_item_icon"
|
android:layout_marginLeft="12dp"
|
||||||
tools:tint="@color/md_grey_600"
|
android:clickable="false"
|
||||||
tools:src="@drawable/apps_k9"
|
/>
|
||||||
|
|
||||||
|
<CheckBox
|
||||||
|
android:id="@+id/checkbox_keychoice"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginStart="12dp"
|
||||||
|
android:layout_marginLeft="12dp"
|
||||||
|
android:clickable="false"
|
||||||
|
tools:visibility="gone"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<LinearLayout
|
<LinearLayout
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:orientation="vertical">
|
android:orientation="vertical"
|
||||||
|
android:layout_marginStart="12dp"
|
||||||
|
android:layout_marginLeft="12dp"
|
||||||
|
>
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/key_list_item_name"
|
android:id="@+id/text_keychoice_name"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
tools:text="@string/label_main_user_id"
|
tools:text="@string/label_main_user_id"
|
||||||
android:textAppearance="?android:attr/textAppearanceMedium" />
|
android:textAppearance="?android:attr/textAppearanceMedium" />
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/key_list_item_creation"
|
android:id="@+id/text_keychoice_creation"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:maxLines="1"
|
android:maxLines="1"
|
||||||
Reference in New Issue
Block a user