Extract remaining user id loading from KeychainProvider
This commit is contained in:
@@ -17,67 +17,47 @@
|
||||
|
||||
package org.sufficientlysecure.keychain.ui;
|
||||
|
||||
import android.database.Cursor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import android.arch.lifecycle.LiveData;
|
||||
import android.database.MatrixCursor;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.Parcel;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.LoaderManager;
|
||||
import android.support.v4.content.CursorLoader;
|
||||
import android.support.v4.content.Loader;
|
||||
import android.support.v4.app.FragmentActivity;
|
||||
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.provider.KeychainContract;
|
||||
import org.sufficientlysecure.keychain.provider.KeychainDatabase;
|
||||
import org.sufficientlysecure.keychain.livedata.GenericLiveData;
|
||||
import org.sufficientlysecure.keychain.model.UserPacket.UserId;
|
||||
import org.sufficientlysecure.keychain.provider.KeyRepository;
|
||||
import org.sufficientlysecure.keychain.service.CertifyActionsParcel;
|
||||
import org.sufficientlysecure.keychain.ui.adapter.MultiUserIdsAdapter;
|
||||
import timber.log.Timber;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class MultiUserIdsFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>{
|
||||
public class MultiUserIdsFragment extends Fragment {
|
||||
public static final String ARG_CHECK_STATES = "check_states";
|
||||
public static final String EXTRA_KEY_IDS = "extra_key_ids";
|
||||
private boolean checkboxVisibility = true;
|
||||
|
||||
ListView mUserIds;
|
||||
private MultiUserIdsAdapter mUserIdsAdapter;
|
||||
ListView userIds;
|
||||
private MultiUserIdsAdapter userIdsAdapter;
|
||||
|
||||
private long[] mPubMasterKeyIds;
|
||||
|
||||
public static final String[] USER_IDS_PROJECTION = new String[]{
|
||||
KeychainContract.UserPackets._ID,
|
||||
KeychainContract.UserPackets.MASTER_KEY_ID,
|
||||
KeychainContract.UserPackets.USER_ID,
|
||||
KeychainContract.UserPackets.IS_PRIMARY,
|
||||
KeychainContract.UserPackets.IS_REVOKED,
|
||||
KeychainContract.UserPackets.NAME,
|
||||
KeychainContract.UserPackets.EMAIL,
|
||||
KeychainContract.UserPackets.COMMENT,
|
||||
};
|
||||
private static final int INDEX_MASTER_KEY_ID = 1;
|
||||
private static final int INDEX_USER_ID = 2;
|
||||
@SuppressWarnings("unused")
|
||||
private static final int INDEX_IS_PRIMARY = 3;
|
||||
@SuppressWarnings("unused")
|
||||
private static final int INDEX_IS_REVOKED = 4;
|
||||
private static final int INDEX_NAME = 5;
|
||||
private static final int INDEX_EMAIL = 6;
|
||||
private static final int INDEX_COMMENT = 7;
|
||||
private long[] pubMasterKeyIds;
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
View view = inflater.inflate(R.layout.multi_user_ids_fragment, null);
|
||||
|
||||
mUserIds = view.findViewById(R.id.view_key_user_ids);
|
||||
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
View view = inflater.inflate(R.layout.multi_user_ids_fragment, container, false);
|
||||
userIds = view.findViewById(R.id.view_key_user_ids);
|
||||
return view;
|
||||
}
|
||||
|
||||
@@ -85,10 +65,11 @@ public class MultiUserIdsFragment extends Fragment implements LoaderManager.Load
|
||||
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
|
||||
super.onActivityCreated(savedInstanceState);
|
||||
|
||||
mPubMasterKeyIds = getActivity().getIntent().getLongArrayExtra(EXTRA_KEY_IDS);
|
||||
if (mPubMasterKeyIds == null) {
|
||||
FragmentActivity activity = requireActivity();
|
||||
pubMasterKeyIds = activity.getIntent().getLongArrayExtra(EXTRA_KEY_IDS);
|
||||
if (pubMasterKeyIds == null) {
|
||||
Timber.e("List of key ids to certify missing!");
|
||||
getActivity().finish();
|
||||
activity.finish();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -97,18 +78,22 @@ public class MultiUserIdsFragment extends Fragment implements LoaderManager.Load
|
||||
checkedStates = (ArrayList<Boolean>) savedInstanceState.getSerializable(ARG_CHECK_STATES);
|
||||
}
|
||||
|
||||
mUserIdsAdapter = new MultiUserIdsAdapter(getActivity(), null, 0, checkedStates, checkboxVisibility);
|
||||
mUserIds.setAdapter(mUserIdsAdapter);
|
||||
mUserIds.setDividerHeight(0);
|
||||
userIdsAdapter = new MultiUserIdsAdapter(activity, null, 0, checkedStates, checkboxVisibility);
|
||||
userIds.setAdapter(userIdsAdapter);
|
||||
userIds.setDividerHeight(0);
|
||||
|
||||
KeyRepository keyRepository = KeyRepository.create(activity);
|
||||
LiveData<List<UserId>> userIdLiveData =
|
||||
new GenericLiveData<>(getContext(), null, () -> keyRepository.getUserIds(pubMasterKeyIds));
|
||||
userIdLiveData.observe(this, this::onUserIdsLoaded);
|
||||
|
||||
getLoaderManager().initLoader(0, null, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSaveInstanceState(Bundle outState) {
|
||||
public void onSaveInstanceState(@NonNull Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
|
||||
ArrayList<Boolean> states = mUserIdsAdapter.getCheckStates();
|
||||
ArrayList<Boolean> states = userIdsAdapter.getCheckStates();
|
||||
// no proper parceling method available :(
|
||||
outState.putSerializable(ARG_CHECK_STATES, states);
|
||||
}
|
||||
@@ -118,40 +103,10 @@ public class MultiUserIdsFragment extends Fragment implements LoaderManager.Load
|
||||
throw new AssertionError("Item selection not allowed");
|
||||
}
|
||||
|
||||
return mUserIdsAdapter.getSelectedCertifyActions();
|
||||
return userIdsAdapter.getSelectedCertifyActions();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
|
||||
Uri uri = KeychainContract.UserPackets.buildUserIdsUri();
|
||||
|
||||
String selection, ids[];
|
||||
{
|
||||
// generate placeholders and string selection args
|
||||
ids = new String[mPubMasterKeyIds.length];
|
||||
StringBuilder placeholders = new StringBuilder("?");
|
||||
for (int i = 0; i < mPubMasterKeyIds.length; i++) {
|
||||
ids[i] = Long.toString(mPubMasterKeyIds[i]);
|
||||
if (i != 0) {
|
||||
placeholders.append(",?");
|
||||
}
|
||||
}
|
||||
// put together selection string
|
||||
selection = KeychainContract.UserPackets.IS_REVOKED + " = 0" + " AND "
|
||||
+ KeychainDatabase.Tables.USER_PACKETS + "." + KeychainContract.UserPackets.MASTER_KEY_ID
|
||||
+ " IN (" + placeholders + ")";
|
||||
}
|
||||
|
||||
return new CursorLoader(getActivity(), uri,
|
||||
USER_IDS_PROJECTION, selection, ids,
|
||||
KeychainDatabase.Tables.USER_PACKETS + "." + KeychainContract.UserPackets.MASTER_KEY_ID + " ASC"
|
||||
+ ", " + KeychainDatabase.Tables.USER_PACKETS + "." + KeychainContract.UserPackets.USER_ID + " ASC"
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
|
||||
|
||||
private void onUserIdsLoaded(List<UserId> userIds) {
|
||||
MatrixCursor matrix = new MatrixCursor(new String[]{
|
||||
"_id", "user_data", "grouped"
|
||||
}) {
|
||||
@@ -160,28 +115,25 @@ public class MultiUserIdsFragment extends Fragment implements LoaderManager.Load
|
||||
return super.getBlob(column);
|
||||
}
|
||||
};
|
||||
data.moveToFirst();
|
||||
|
||||
long lastMasterKeyId = 0;
|
||||
String lastName = "";
|
||||
ArrayList<String> uids = new ArrayList<>();
|
||||
|
||||
boolean header = true;
|
||||
boolean isFirst = true;
|
||||
|
||||
// Iterate over all rows
|
||||
while (!data.isAfterLast()) {
|
||||
long masterKeyId = data.getLong(INDEX_MASTER_KEY_ID);
|
||||
String userId = data.getString(INDEX_USER_ID);
|
||||
String name = data.getString(INDEX_NAME);
|
||||
|
||||
for (UserId userId : userIds) {
|
||||
// Two cases:
|
||||
|
||||
boolean grouped = masterKeyId == lastMasterKeyId;
|
||||
boolean subGrouped = data.isFirst() || grouped && lastName != null && lastName.equals(name);
|
||||
boolean grouped = userId.master_key_id() == lastMasterKeyId;
|
||||
boolean subGrouped = isFirst || grouped && lastName != null && lastName.equals(userId.name());
|
||||
isFirst = false;
|
||||
// Remember for next loop
|
||||
lastName = name;
|
||||
lastName = userId.name();
|
||||
|
||||
Timber.d(Long.toString(masterKeyId, 16) + (grouped ? "grouped" : "not grouped"));
|
||||
Timber.d(Long.toString(userId.master_key_id(), 16) + (grouped ? "grouped" : "not grouped"));
|
||||
|
||||
if (!subGrouped) {
|
||||
// 1. This name should NOT be grouped with the previous, so we flush the buffer
|
||||
@@ -203,17 +155,13 @@ public class MultiUserIdsFragment extends Fragment implements LoaderManager.Load
|
||||
}
|
||||
|
||||
// 2. This name should be grouped with the previous, just add to buffer
|
||||
uids.add(userId);
|
||||
lastMasterKeyId = masterKeyId;
|
||||
uids.add(userId.user_id());
|
||||
lastMasterKeyId = userId.master_key_id();
|
||||
|
||||
// If this one wasn't grouped, the next one's gotta be a header
|
||||
if (!grouped) {
|
||||
header = true;
|
||||
}
|
||||
|
||||
// Regardless of the outcome, move to next entry
|
||||
data.moveToNext();
|
||||
|
||||
}
|
||||
|
||||
// If there is anything left in the buffer, flush it one last time
|
||||
@@ -230,12 +178,7 @@ public class MultiUserIdsFragment extends Fragment implements LoaderManager.Load
|
||||
|
||||
}
|
||||
|
||||
mUserIdsAdapter.swapCursor(matrix);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoaderReset(Loader<Cursor> loader) {
|
||||
mUserIdsAdapter.swapCursor(null);
|
||||
userIdsAdapter.swapCursor(matrix);
|
||||
}
|
||||
|
||||
public void setCheckboxVisibility(boolean checkboxVisibility) {
|
||||
|
||||
@@ -172,7 +172,7 @@ public class IdentityDao {
|
||||
return null;
|
||||
}
|
||||
|
||||
private void loadUserIds(ArrayList<IdentityInfo> identities, long masterKeyId) {
|
||||
private void loadUserIds(ArrayList<IdentityInfo> identities, long... masterKeyId) {
|
||||
SqlDelightQuery query = UserPacket.FACTORY.selectUserIdsByMasterKeyId(masterKeyId);
|
||||
try (Cursor cursor = db.query(query)) {
|
||||
while (cursor.moveToNext()) {
|
||||
|
||||
Reference in New Issue
Block a user