use FlexibleAdapter for new KeyChoiceAdapter
This commit is contained in:
@@ -18,45 +18,36 @@
|
||||
package org.sufficientlysecure.keychain.remote.ui;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
import android.arch.lifecycle.LiveData;
|
||||
import android.arch.lifecycle.ViewModelProviders;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.LoaderManager;
|
||||
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 android.support.v7.widget.LinearLayoutManager;
|
||||
|
||||
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.KeychainContract;
|
||||
import org.sufficientlysecure.keychain.ui.adapter.KeyAdapter;
|
||||
import org.sufficientlysecure.keychain.ui.adapter.KeySelectableAdapter;
|
||||
import org.sufficientlysecure.keychain.ui.widget.FixedListView;
|
||||
import org.sufficientlysecure.keychain.provider.KeyRepository;
|
||||
import org.sufficientlysecure.keychain.ui.adapter.KeyChoiceAdapter;
|
||||
import org.sufficientlysecure.keychain.ui.base.RecyclerFragment;
|
||||
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 KeySelectableAdapter mAdapter;
|
||||
private ApiAppDao mApiAppDao;
|
||||
private KeyChoiceAdapter keyChoiceAdapter;
|
||||
private ApiAppDao apiAppDao;
|
||||
|
||||
private String packageName;
|
||||
|
||||
/**
|
||||
* Creates new instance of this fragment
|
||||
*/
|
||||
public static AppSettingsAllowedKeysListFragment newInstance(String packageName) {
|
||||
AppSettingsAllowedKeysListFragment frag = new AppSettingsAllowedKeysListFragment();
|
||||
|
||||
Bundle args = new Bundle();
|
||||
|
||||
args.putString(ARG_PACKAGE_NAME, packageName);
|
||||
|
||||
frag.setArguments(args);
|
||||
|
||||
return frag;
|
||||
@@ -66,34 +57,9 @@ public class AppSettingsAllowedKeysListFragment extends ListFragmentWorkaround i
|
||||
public void onCreate(Bundle 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
|
||||
public void onActivityCreated(Bundle savedInstanceState) {
|
||||
super.onActivityCreated(savedInstanceState);
|
||||
@@ -104,70 +70,36 @@ public class AppSettingsAllowedKeysListFragment extends ListFragmentWorkaround i
|
||||
// application this would come from a resource.
|
||||
setEmptyText(getString(R.string.list_empty));
|
||||
|
||||
Set<Long> checked = mApiAppDao.getAllowedKeyIdsForApp(packageName);
|
||||
mAdapter = new KeySelectableAdapter(getActivity(), null, 0, checked);
|
||||
setListAdapter(mAdapter);
|
||||
getListView().setOnItemClickListener(mAdapter);
|
||||
getRecyclerView().setLayoutManager(new LinearLayoutManager(requireContext()));
|
||||
|
||||
// Start out with a progress indicator.
|
||||
setListShown(false);
|
||||
|
||||
// Prepare the loader. Either re-connect with an existing one,
|
||||
// or start a new one.
|
||||
getLoaderManager().initLoader(0, null, this);
|
||||
hideList(false);
|
||||
|
||||
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() {
|
||||
mApiAppDao.saveAllowedKeyIdsForApp(packageName, getSelectedMasterKeyIds());
|
||||
Set<Long> longs = keyChoiceAdapter.getSelectionIds();
|
||||
apiAppDao.saveAllowedKeyIdsForApp(packageName, longs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Loader<Cursor> onCreateLoader(int loaderId, Bundle data) {
|
||||
Uri baseUri = KeychainContract.KeyRings.buildUnifiedKeyRingsUri();
|
||||
String where = KeychainContract.KeyRings.HAS_ANY_SECRET + " = 1";
|
||||
|
||||
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);
|
||||
public void onLoadUnifiedKeyData(List<UnifiedKeyInfo> data) {
|
||||
if (keyChoiceAdapter == null) {
|
||||
keyChoiceAdapter = new KeyChoiceAdapter(true, data);
|
||||
setAdapter(keyChoiceAdapter);
|
||||
} else {
|
||||
setListShownNoAnimation(true);
|
||||
keyChoiceAdapter.setUnifiedKeyInfoItems(data);
|
||||
}
|
||||
}
|
||||
|
||||
@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.
|
||||
mAdapter.swapCursor(null);
|
||||
Set<Long> checkedIds = apiAppDao.getAllowedKeyIdsForApp(packageName);
|
||||
keyChoiceAdapter.setSelectionByIds(checkedIds);
|
||||
|
||||
boolean animateShowList = !isResumed();
|
||||
showList(animateShowList);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -52,9 +52,6 @@ public class SelectSignKeyIdListFragment extends RecyclerFragment<SelectSignKeyA
|
||||
private ApiAppDao mApiAppDao;
|
||||
private String mPackageName;
|
||||
|
||||
/**
|
||||
* Creates new instance of this fragment
|
||||
*/
|
||||
public static SelectSignKeyIdListFragment newInstance(String packageName, Intent data, String preferredUserId) {
|
||||
SelectSignKeyIdListFragment frag = new SelectSignKeyIdListFragment();
|
||||
Bundle args = new Bundle();
|
||||
|
||||
@@ -29,19 +29,19 @@ import android.arch.lifecycle.ViewModelProviders;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v4.app.DialogFragment;
|
||||
import android.support.v4.app.FragmentActivity;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.support.v7.widget.RecyclerView.Adapter;
|
||||
import android.view.ContextThemeWrapper;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.mikepenz.materialdrawer.util.KeyboardUtil;
|
||||
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.remote.ui.RemoteSecurityTokenOperationActivity;
|
||||
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.util.ThemeChanger;
|
||||
import org.sufficientlysecure.keychain.ui.util.recyclerview.DividerItemDecoration;
|
||||
import org.sufficientlysecure.keychain.ui.util.recyclerview.RecyclerItemClickListener;
|
||||
|
||||
|
||||
public class RemoteDeduplicateActivity extends FragmentActivity {
|
||||
@@ -157,7 +155,7 @@ public class RemoteDeduplicateActivity extends FragmentActivity {
|
||||
new DividerItemDecoration(activity, DividerItemDecoration.VERTICAL_LIST, true));
|
||||
|
||||
setupListenersForPresenter();
|
||||
mvpView = createMvpView(view, layoutInflater);
|
||||
mvpView = createMvpView(view);
|
||||
|
||||
return alert.create();
|
||||
}
|
||||
@@ -190,11 +188,8 @@ public class RemoteDeduplicateActivity extends FragmentActivity {
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private RemoteDeduplicateView createMvpView(View view, LayoutInflater layoutInflater) {
|
||||
final ImageView iconClientApp = view.findViewById(R.id.icon_client_app);
|
||||
final KeyChoiceAdapter keyChoiceAdapter = new KeyChoiceAdapter(layoutInflater, getResources());
|
||||
private RemoteDeduplicateView createMvpView(View view) {
|
||||
final TextView addressText = view.findViewById(R.id.select_key_item_name);
|
||||
keyChoiceList.setAdapter(keyChoiceAdapter);
|
||||
|
||||
return new RemoteDeduplicateView() {
|
||||
@Override
|
||||
@@ -222,9 +217,8 @@ public class RemoteDeduplicateActivity extends FragmentActivity {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTitleClientIcon(Drawable drawable) {
|
||||
iconClientApp.setImageDrawable(drawable);
|
||||
keyChoiceAdapter.setSelectionDrawable(drawable);
|
||||
public void showNoSelectionError() {
|
||||
Toast.makeText(getContext(), "No key selected!", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -233,18 +227,8 @@ public class RemoteDeduplicateActivity extends FragmentActivity {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setKeyListData(List<UnifiedKeyInfo> data) {
|
||||
keyChoiceAdapter.setData(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setActiveItem(Integer position) {
|
||||
keyChoiceAdapter.setActiveItem(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setEnableSelectButton(boolean enabled) {
|
||||
buttonSelect.setEnabled(enabled);
|
||||
public void setKeyListAdapter(Adapter adapter) {
|
||||
keyChoiceList.setAdapter(adapter);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -252,8 +236,6 @@ public class RemoteDeduplicateActivity extends FragmentActivity {
|
||||
private void setupListenersForPresenter() {
|
||||
buttonSelect.setOnClickListener(view -> presenter.onClickSelect());
|
||||
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.content.Context;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.PackageManager.NameNotFoundException;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.support.v7.widget.RecyclerView.Adapter;
|
||||
|
||||
import org.sufficientlysecure.keychain.model.SubKey.UnifiedKeyInfo;
|
||||
import org.sufficientlysecure.keychain.remote.AutocryptInteractor;
|
||||
import org.sufficientlysecure.keychain.remote.ui.dialog.RemoteDeduplicateActivity.DeduplicateViewModel;
|
||||
import timber.log.Timber;
|
||||
import org.sufficientlysecure.keychain.ui.adapter.KeyChoiceAdapter;
|
||||
|
||||
|
||||
class RemoteDeduplicatePresenter {
|
||||
private final PackageManager packageManager;
|
||||
private final Context context;
|
||||
private final LifecycleOwner lifecycleOwner;
|
||||
|
||||
@@ -43,15 +39,12 @@ class RemoteDeduplicatePresenter {
|
||||
|
||||
private DeduplicateViewModel viewModel;
|
||||
private RemoteDeduplicateView view;
|
||||
private Integer selectedItem;
|
||||
private List<UnifiedKeyInfo> keyInfoData;
|
||||
private KeyChoiceAdapter keyChoiceAdapter;
|
||||
|
||||
|
||||
RemoteDeduplicatePresenter(Context context, LifecycleOwner lifecycleOwner) {
|
||||
this.context = context;
|
||||
this.lifecycleOwner = lifecycleOwner;
|
||||
|
||||
packageManager = context.getPackageManager();
|
||||
}
|
||||
|
||||
public void setView(RemoteDeduplicateView view) {
|
||||
@@ -62,42 +55,27 @@ class RemoteDeduplicatePresenter {
|
||||
this.viewModel = viewModel;
|
||||
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());
|
||||
|
||||
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) {
|
||||
this.keyInfoData = data;
|
||||
view.setKeyListData(data);
|
||||
if (keyChoiceAdapter == null) {
|
||||
keyChoiceAdapter = new KeyChoiceAdapter(false, data);
|
||||
view.setKeyListAdapter(keyChoiceAdapter);
|
||||
} else {
|
||||
keyChoiceAdapter.setUnifiedKeyInfoItems(data);
|
||||
}
|
||||
}
|
||||
|
||||
void onClickSelect() {
|
||||
if (keyInfoData == null) {
|
||||
Timber.e("got click on select with no data…?");
|
||||
UnifiedKeyInfo activeItem = keyChoiceAdapter.getActiveItem();
|
||||
if (activeItem == null) {
|
||||
view.showNoSelectionError();
|
||||
return;
|
||||
}
|
||||
if (selectedItem == null) {
|
||||
Timber.e("got click on select with no selection…?");
|
||||
return;
|
||||
}
|
||||
|
||||
long masterKeyId = keyInfoData.get(selectedItem).master_key_id();
|
||||
long masterKeyId = activeItem.master_key_id();
|
||||
autocryptInteractor.updateKeyGossipFromDedup(viewModel.getDuplicateAddress(), masterKeyId);
|
||||
|
||||
view.finish();
|
||||
@@ -111,25 +89,13 @@ class RemoteDeduplicatePresenter {
|
||||
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 {
|
||||
void showNoSelectionError();
|
||||
void finish();
|
||||
void finishAsCancelled();
|
||||
|
||||
void setAddressText(String text);
|
||||
void setTitleClientIcon(Drawable drawable);
|
||||
|
||||
void setKeyListData(List<UnifiedKeyInfo> data);
|
||||
void setActiveItem(Integer position);
|
||||
void setEnableSelectButton(boolean enabled);
|
||||
void setKeyListAdapter(Adapter adapter);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user