make userattributeadapter superclass of useridsadapter

This commit is contained in:
Vincent Breitmoser 2015-03-02 17:38:05 +01:00
parent 4e29d027af
commit 0846dd2c14
6 changed files with 162 additions and 122 deletions

View File

@ -35,7 +35,6 @@ import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.compatibility.DialogFragmentWorkaround; import org.sufficientlysecure.keychain.compatibility.DialogFragmentWorkaround;
import org.sufficientlysecure.keychain.provider.KeychainContract; import org.sufficientlysecure.keychain.provider.KeychainContract;
import org.sufficientlysecure.keychain.provider.KeychainContract.UserPackets; import org.sufficientlysecure.keychain.provider.KeychainContract.UserPackets;
import org.sufficientlysecure.keychain.provider.ProviderHelper;
import org.sufficientlysecure.keychain.ui.adapter.UserIdsAdapter; import org.sufficientlysecure.keychain.ui.adapter.UserIdsAdapter;
import org.sufficientlysecure.keychain.ui.dialog.UserIdInfoDialogFragment; import org.sufficientlysecure.keychain.ui.dialog.UserIdInfoDialogFragment;
import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.Log;
@ -58,8 +57,6 @@ public class ViewKeyFragment extends LoaderFragment implements
private Uri mDataUri; private Uri mDataUri;
ProviderHelper mProviderHelper;
/** /**
* Creates new instance of this fragment * Creates new instance of this fragment
*/ */
@ -78,8 +75,6 @@ public class ViewKeyFragment extends LoaderFragment implements
View root = super.onCreateView(inflater, superContainer, savedInstanceState); View root = super.onCreateView(inflater, superContainer, savedInstanceState);
View view = inflater.inflate(R.layout.view_key_fragment, getContainer()); View view = inflater.inflate(R.layout.view_key_fragment, getContainer());
mProviderHelper = new ProviderHelper(getActivity());
mUserIds = (ListView) view.findViewById(R.id.view_key_user_ids); mUserIds = (ListView) view.findViewById(R.id.view_key_user_ids);
mUserIds.setOnItemClickListener(new AdapterView.OnItemClickListener() { mUserIds.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@ -199,7 +194,7 @@ public class ViewKeyFragment extends LoaderFragment implements
boolean isVerified = data.getInt(INDEX_VERIFIED) > 0; boolean isVerified = data.getInt(INDEX_VERIFIED) > 0;
// load user ids after we know if it's a secret key // load user ids after we know if it's a secret key
mUserIdsAdapter = new UserIdsAdapter(getActivity(), null, 0, false, !mIsSecret, null); mUserIdsAdapter = new UserIdsAdapter(getActivity(), null, 0, !mIsSecret, null);
mUserIds.setAdapter(mUserIdsAdapter); mUserIds.setAdapter(mUserIdsAdapter);
getLoaderManager().initLoader(LOADER_ID_USER_IDS, null, this); getLoaderManager().initLoader(LOADER_ID_USER_IDS, null, this);

View File

@ -0,0 +1,49 @@
package org.sufficientlysecure.keychain.ui.adapter;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.view.View;
import org.sufficientlysecure.keychain.provider.KeychainContract.UserPackets;
public abstract class UserAttributesAdapter extends CursorAdapter {
public static final String[] USER_IDS_PROJECTION = new String[]{
UserPackets._ID,
UserPackets.TYPE,
UserPackets.USER_ID,
UserPackets.RANK,
UserPackets.VERIFIED,
UserPackets.IS_PRIMARY,
UserPackets.IS_REVOKED
};
protected static final int INDEX_ID = 0;
protected static final int INDEX_TYPE = 1;
protected static final int INDEX_USER_ID = 2;
protected static final int INDEX_RANK = 3;
protected static final int INDEX_VERIFIED = 4;
protected static final int INDEX_IS_PRIMARY = 5;
protected static final int INDEX_IS_REVOKED = 6;
public UserAttributesAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
}
@Override
public abstract void bindView(View view, Context context, Cursor cursor);
public String getUserId(int position) {
mCursor.moveToPosition(position);
return mCursor.getString(INDEX_USER_ID);
}
public boolean getIsRevoked(int position) {
mCursor.moveToPosition(position);
return mCursor.getInt(INDEX_IS_REVOKED) > 0;
}
public int getIsVerified(int position) {
mCursor.moveToPosition(position);
return mCursor.getInt(INDEX_VERIFIED);
}
}

View File

@ -20,90 +20,38 @@ package org.sufficientlysecure.keychain.ui.adapter;
import android.content.Context; import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.pgp.KeyRing; import org.sufficientlysecure.keychain.pgp.KeyRing;
import org.sufficientlysecure.keychain.provider.KeychainContract.Certs; import org.sufficientlysecure.keychain.provider.KeychainContract.Certs;
import org.sufficientlysecure.keychain.provider.KeychainContract.UserPackets;
import org.sufficientlysecure.keychain.service.SaveKeyringParcel; import org.sufficientlysecure.keychain.service.SaveKeyringParcel;
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils; import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
import java.util.ArrayList; public class UserIdsAdapter extends UserAttributesAdapter {
protected LayoutInflater mInflater;
public class UserIdsAdapter extends CursorAdapter implements AdapterView.OnItemClickListener {
private LayoutInflater mInflater;
private final ArrayList<Boolean> mCheckStates;
private SaveKeyringParcel mSaveKeyringParcel; private SaveKeyringParcel mSaveKeyringParcel;
private boolean mShowStatusImages; private boolean mShowStatusImages;
public static final String[] USER_IDS_PROJECTION = new String[]{ public UserIdsAdapter(Context context, Cursor c, int flags,
UserPackets._ID,
UserPackets.USER_ID,
UserPackets.RANK,
UserPackets.VERIFIED,
UserPackets.IS_PRIMARY,
UserPackets.IS_REVOKED
};
private static final int INDEX_ID = 0;
private static final int INDEX_USER_ID = 1;
private static final int INDEX_RANK = 2;
private static final int INDEX_VERIFIED = 3;
private static final int INDEX_IS_PRIMARY = 4;
private static final int INDEX_IS_REVOKED = 5;
public UserIdsAdapter(Context context, Cursor c, int flags, boolean showCheckBoxes,
boolean showStatusImages, SaveKeyringParcel saveKeyringParcel) { boolean showStatusImages, SaveKeyringParcel saveKeyringParcel) {
super(context, c, flags); super(context, c, flags);
mInflater = LayoutInflater.from(context); mInflater = LayoutInflater.from(context);
mCheckStates = showCheckBoxes ? new ArrayList<Boolean>() : null;
mSaveKeyringParcel = saveKeyringParcel; mSaveKeyringParcel = saveKeyringParcel;
mShowStatusImages = showStatusImages; mShowStatusImages = showStatusImages;
} }
public UserIdsAdapter(Context context, Cursor c, int flags, boolean showCheckBoxes,
SaveKeyringParcel saveKeyringParcel) {
this(context, c, flags, showCheckBoxes, true, saveKeyringParcel);
}
public UserIdsAdapter(Context context, Cursor c, int flags, boolean showCheckBoxes) {
this(context, c, flags, showCheckBoxes, true, null);
}
public UserIdsAdapter(Context context, Cursor c, int flags, SaveKeyringParcel saveKeyringParcel) { public UserIdsAdapter(Context context, Cursor c, int flags, SaveKeyringParcel saveKeyringParcel) {
this(context, c, flags, false, true, saveKeyringParcel); this(context, c, flags, true, saveKeyringParcel);
} }
public UserIdsAdapter(Context context, Cursor c, int flags) { public UserIdsAdapter(Context context, Cursor c, int flags) {
this(context, c, flags, false, true, null); this(context, c, flags, true, null);
}
@Override
public Cursor swapCursor(Cursor newCursor) {
if (mCheckStates != null) {
mCheckStates.clear();
if (newCursor != null) {
int count = newCursor.getCount();
mCheckStates.ensureCapacity(count);
// initialize to true (use case knowledge: we usually want to sign all uids)
for (int i = 0; i < count; i++) {
newCursor.moveToPosition(i);
int verified = newCursor.getInt(INDEX_VERIFIED);
mCheckStates.add(verified != Certs.VERIFIED_SECRET);
}
}
}
return super.swapCursor(newCursor);
} }
@Override @Override
@ -206,56 +154,6 @@ public class UserIdsAdapter extends CursorAdapter implements AdapterView.OnItemC
break; break;
} }
} }
// don't care further if checkboxes aren't shown
if (mCheckStates == null) {
return;
}
final CheckBox vCheckBox = (CheckBox) view.findViewById(R.id.user_id_item_check_box);
final int position = cursor.getPosition();
vCheckBox.setOnCheckedChangeListener(null);
vCheckBox.setChecked(mCheckStates.get(position));
vCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
mCheckStates.set(position, b);
}
});
vCheckBox.setClickable(false);
}
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
CheckBox box = ((CheckBox) view.findViewById(R.id.user_id_item_check_box));
if (box != null) {
box.toggle();
}
}
public ArrayList<String> getSelectedUserIds() {
ArrayList<String> result = new ArrayList<>();
for (int i = 0; i < mCheckStates.size(); i++) {
if (mCheckStates.get(i)) {
mCursor.moveToPosition(i);
result.add(mCursor.getString(INDEX_USER_ID));
}
}
return result;
}
public String getUserId(int position) {
mCursor.moveToPosition(position);
return mCursor.getString(INDEX_USER_ID);
}
public boolean getIsRevoked(int position) {
mCursor.moveToPosition(position);
return mCursor.getInt(INDEX_IS_REVOKED) > 0;
}
public int getIsVerified(int position) {
mCursor.moveToPosition(position);
return mCursor.getInt(INDEX_VERIFIED);
} }
public boolean getIsRevokedPending(int position) { public boolean getIsRevokedPending(int position) {
@ -276,8 +174,6 @@ public class UserIdsAdapter extends CursorAdapter implements AdapterView.OnItemC
@Override @Override
public View newView(Context context, Cursor cursor, ViewGroup parent) { public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = mInflater.inflate(R.layout.view_key_adv_user_id_item, null); View view = mInflater.inflate(R.layout.view_key_adv_user_id_item, null);
// only need to do this once ever, since mShowCheckBoxes is final
view.findViewById(R.id.user_id_item_check_box).setVisibility(mCheckStates != null ? View.VISIBLE : View.GONE);
return view; return view;
} }

View File

@ -0,0 +1,88 @@
package org.sufficientlysecure.keychain.ui.adapter;
import android.content.Context;
import android.database.Cursor;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.provider.KeychainContract.Certs;
import org.sufficientlysecure.keychain.service.SaveKeyringParcel;
import java.util.ArrayList;
public class UserIdsSelectableAdapter extends UserIdsAdapter implements AdapterView.OnItemClickListener {
private final ArrayList<Boolean> mCheckStates;
public UserIdsSelectableAdapter(Context context, Cursor c, int flags, SaveKeyringParcel saveKeyringParcel) {
super(context, c, flags, saveKeyringParcel);
mCheckStates = new ArrayList<Boolean>();
}
@Override
public Cursor swapCursor(Cursor newCursor) {
if (mCheckStates != null) {
mCheckStates.clear();
if (newCursor != null) {
int count = newCursor.getCount();
mCheckStates.ensureCapacity(count);
// initialize to true (use case knowledge: we usually want to sign all uids)
for (int i = 0; i < count; i++) {
newCursor.moveToPosition(i);
int verified = newCursor.getInt(INDEX_VERIFIED);
mCheckStates.add(verified != Certs.VERIFIED_SECRET);
}
}
}
return super.swapCursor(newCursor);
}
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
CheckBox box = ((CheckBox) view.findViewById(R.id.user_id_item_check_box));
if (box != null) {
box.toggle();
}
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
final CheckBox vCheckBox = (CheckBox) view.findViewById(R.id.user_id_item_check_box);
final int position = cursor.getPosition();
vCheckBox.setOnCheckedChangeListener(null);
vCheckBox.setChecked(mCheckStates.get(position));
vCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
mCheckStates.set(position, b);
}
});
vCheckBox.setClickable(false);
}
public ArrayList<String> getSelectedUserIds() {
ArrayList<String> result = new ArrayList<>();
for (int i = 0; i < mCheckStates.size(); i++) {
if (mCheckStates.get(i)) {
mCursor.moveToPosition(i);
result.add(mCursor.getString(INDEX_USER_ID));
}
}
return result;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = mInflater.inflate(R.layout.view_key_selectable_user_id_item, null);
return view;
}
}

View File

@ -6,13 +6,6 @@
android:orientation="horizontal" android:orientation="horizontal"
android:singleLine="true"> android:singleLine="true">
<CheckBox
android:id="@+id/user_id_item_check_box"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:clickable="false"
android:focusable="false" />
<LinearLayout <LinearLayout
android:id="@+id/user_id_item_certified_layout" android:id="@+id/user_id_item_certified_layout"
android:layout_width="22dp" android:layout_width="22dp"
@ -25,6 +18,7 @@
android:id="@+id/user_id_item_certified" android:id="@+id/user_id_item_certified"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:src="@drawable/status_signature_unverified_cutout_24px"
android:layout_gravity="center_horizontal" /> android:layout_gravity="center_horizontal" />
</LinearLayout> </LinearLayout>

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:orientation="horizontal"
android:singleLine="true">
<CheckBox
android:id="@+id/user_id_item_check_box"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:clickable="false"
android:focusable="false" />
<include layout="@layout/view_key_adv_user_id_item" />
</LinearLayout>