Cleanup and simplification

This commit is contained in:
Dominik Schürmann 2014-03-13 18:27:08 +01:00
parent fb1b0deaf5
commit 471cefa71f
2 changed files with 24 additions and 61 deletions

View File

@ -414,7 +414,7 @@ public class KeychainProvider extends ContentProvider {
* Builds default query for keyRings: KeyRings table is joined with UserIds and Keys
*/
private SQLiteQueryBuilder buildKeyRingQuery(SQLiteQueryBuilder qb, int match) {
if(match != UNIFIED_KEY_RING) {
if (match != UNIFIED_KEY_RING) {
// public or secret keyring
qb.appendWhere(Tables.KEY_RINGS + "." + KeyRingsColumns.TYPE + " = ");
qb.appendWhereEscapeString(Integer.toString(getKeyType(match)));

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2013 Dominik Schürmann <dominik@dominikschuermann.de>
* Copyright (C) 2013-2014 Dominik Schürmann <dominik@dominikschuermann.de>
*
* 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
@ -19,8 +19,6 @@ package org.sufficientlysecure.keychain.ui;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.Id;
@ -32,7 +30,6 @@ import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings;
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyTypes;
import org.sufficientlysecure.keychain.provider.KeychainContract.UserIds;
import org.sufficientlysecure.keychain.provider.KeychainDatabase;
import org.sufficientlysecure.keychain.provider.ProviderHelper;
import org.sufficientlysecure.keychain.ui.adapter.HighlightQueryCursorAdapter;
import org.sufficientlysecure.keychain.ui.dialog.DeleteKeyDialogFragment;
import org.sufficientlysecure.keychain.util.Log;
@ -57,13 +54,10 @@ 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.widget.CursorAdapter;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.SearchView;
import android.text.Spannable;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.Menu;
@ -270,7 +264,10 @@ public class KeyListFragment extends Fragment implements SearchView.OnQueryTextL
};
static final int INDEX_TYPE = 1;
static final int INDEX_UID = 3;
static final int INDEX_MASTER_KEY_ID = 2;
static final int INDEX_USER_ID = 3;
static final int INDEX_IS_REVOKED = 4;
static final String SORT_ORDER =
// show secret before public key
KeychainDatabase.Tables.KEY_RINGS + "." + KeyRings.TYPE + " DESC, "
@ -472,43 +469,20 @@ public class KeyListFragment extends Fragment implements SearchView.OnQueryTextL
*/
private class KeyListAdapter extends HighlightQueryCursorAdapter implements StickyListHeadersAdapter {
private LayoutInflater mInflater;
private int mIndexUserId;
private int mIndexIsRevoked;
private int mMasterKeyId;
private String mCurQuery;
@SuppressLint("UseSparseArrays")
private HashMap<Integer, Boolean> mSelection = new HashMap<Integer, Boolean>();
public KeyListAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
mInflater = LayoutInflater.from(context);
initIndex(c);
}
@Override
public Cursor swapCursor(Cursor newCursor) {
initIndex(newCursor);
return super.swapCursor(newCursor);
}
/**
* Get column indexes for performance reasons just once in constructor and swapCursor. For a
* performance comparison see http://stackoverflow.com/a/17999582
*
* @param cursor
*/
private void initIndex(Cursor cursor) {
if (cursor != null) {
mIndexUserId = cursor.getColumnIndexOrThrow(KeychainContract.UserIds.USER_ID);
mIndexIsRevoked = cursor.getColumnIndexOrThrow(KeychainContract.Keys.IS_REVOKED);
mMasterKeyId = cursor.getColumnIndexOrThrow(KeychainContract.KeyRings.MASTER_KEY_ID);
}
}
/**
* Bind cursor data to the item list view
* <p/>
@ -522,7 +496,7 @@ public class KeyListFragment extends Fragment implements SearchView.OnQueryTextL
TextView mainUserId = (TextView) view.findViewById(R.id.mainUserId);
TextView mainUserIdRest = (TextView) view.findViewById(R.id.mainUserIdRest);
String userId = cursor.getString(mIndexUserId);
String userId = cursor.getString(INDEX_USER_ID);
String[] userIdSplit = PgpKeyHelper.splitUserId(userId);
if (userIdSplit[0] != null) {
mainUserId.setText(highlightSearchQuery(userIdSplit[0]));
@ -541,12 +515,12 @@ public class KeyListFragment extends Fragment implements SearchView.OnQueryTextL
Button button = (Button) view.findViewById(R.id.edit);
TextView revoked = (TextView) view.findViewById(R.id.revoked);
if(cursor.getInt(KeyListFragment.INDEX_TYPE) == KeyTypes.SECRET) {
if (cursor.getInt(KeyListFragment.INDEX_TYPE) == KeyTypes.SECRET) {
// this is a secret key - show the edit button
revoked.setVisibility(View.GONE);
button.setVisibility(View.VISIBLE);
final long id = cursor.getLong(mMasterKeyId);
final long id = cursor.getLong(INDEX_MASTER_KEY_ID);
button.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Intent editIntent = new Intent(getActivity(), EditKeyActivity.class);
@ -559,7 +533,7 @@ public class KeyListFragment extends Fragment implements SearchView.OnQueryTextL
// this is a public key - hide the edit button, show if it's revoked
button.setVisibility(View.GONE);
boolean isRevoked = cursor.getInt(mIndexIsRevoked) > 0;
boolean isRevoked = cursor.getInt(INDEX_IS_REVOKED) > 0;
revoked.setVisibility(isRevoked ? View.VISIBLE : View.GONE);
}
}
@ -567,23 +541,11 @@ public class KeyListFragment extends Fragment implements SearchView.OnQueryTextL
}
public long getMasterKeyId(int id) {
if (!mCursor.moveToPosition(id)) {
throw new IllegalStateException("couldn't move cursor to position " + id);
}
return mCursor.getLong(mMasterKeyId);
}
public int getKeyType(int position) {
if (!mCursor.moveToPosition(position)) {
throw new IllegalStateException("couldn't move cursor to position " + position);
}
return mCursor.getInt(KeyListFragment.INDEX_TYPE);
return mCursor.getLong(INDEX_MASTER_KEY_ID);
}
@Override
@ -621,7 +583,7 @@ public class KeyListFragment extends Fragment implements SearchView.OnQueryTextL
throw new IllegalStateException("couldn't move cursor to position " + position);
}
if(mCursor.getInt(KeyListFragment.INDEX_TYPE) == KeyTypes.SECRET) {
if (mCursor.getInt(KeyListFragment.INDEX_TYPE) == KeyTypes.SECRET) {
{ // set contact count
int num = mCursor.getCount();
String contactsTotal = getResources().getQuantityString(R.plurals.n_contacts, num, num);
@ -634,10 +596,10 @@ public class KeyListFragment extends Fragment implements SearchView.OnQueryTextL
}
// set header text as first char in user id
String userId = mCursor.getString(KeyListFragment.INDEX_UID);
String userId = mCursor.getString(KeyListFragment.INDEX_USER_ID);
String headerText = convertView.getResources().getString(R.string.user_id_no_name);
if (userId != null && userId.length() > 0) {
headerText = "" + mCursor.getString(KeyListFragment.INDEX_UID).subSequence(0, 1).charAt(0);
headerText = "" + mCursor.getString(KeyListFragment.INDEX_USER_ID).subSequence(0, 1).charAt(0);
}
holder.text.setText(headerText);
holder.count.setVisibility(View.GONE);
@ -660,11 +622,11 @@ public class KeyListFragment extends Fragment implements SearchView.OnQueryTextL
}
// early breakout: all secret keys are assigned id 0
if(mCursor.getInt(KeyListFragment.INDEX_TYPE) == KeyTypes.SECRET)
if (mCursor.getInt(KeyListFragment.INDEX_TYPE) == KeyTypes.SECRET)
return 1L;
// otherwise, return the first character of the name as ID
String userId = mCursor.getString(KeyListFragment.INDEX_UID);
String userId = mCursor.getString(KeyListFragment.INDEX_USER_ID);
if (userId != null && userId.length() > 0) {
return userId.charAt(0);
} else {
@ -689,8 +651,9 @@ public class KeyListFragment extends Fragment implements SearchView.OnQueryTextL
long[] ids = new long[mSelection.size()];
int i = 0;
// get master key ids
for (int pos : mSelection.keySet())
for (int pos : mSelection.keySet()) {
ids[i++] = mAdapter.getMasterKeyId(pos);
}
return ids;
}
@ -712,11 +675,11 @@ public class KeyListFragment extends Fragment implements SearchView.OnQueryTextL
/**
* Change color for multi-selection
*/
// default color
if (mSelection.get(position) != null) {
// this is a selected position, change color!
// selected position color
v.setBackgroundColor(parent.getResources().getColor(R.color.emphasis));
} else {
// default color
v.setBackgroundColor(Color.TRANSPARENT);
}