From 0846dd2c14987454c19cf219d9d77b31268639ab Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Mon, 2 Mar 2015 17:38:05 +0100 Subject: [PATCH] make userattributeadapter superclass of useridsadapter --- .../keychain/ui/ViewKeyFragment.java | 7 +- .../ui/adapter/UserAttributesAdapter.java | 49 ++++++++ .../keychain/ui/adapter/UserIdsAdapter.java | 114 +----------------- .../ui/adapter/UserIdsSelectableAdapter.java | 88 ++++++++++++++ .../res/layout/view_key_adv_user_id_item.xml | 8 +- .../view_key_selectable_user_id_item.xml | 18 +++ 6 files changed, 162 insertions(+), 122 deletions(-) create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/UserAttributesAdapter.java create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/UserIdsSelectableAdapter.java create mode 100644 OpenKeychain/src/main/res/layout/view_key_selectable_user_id_item.xml diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyFragment.java index 453bfd499..9bcad36c5 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyFragment.java @@ -35,7 +35,6 @@ import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.compatibility.DialogFragmentWorkaround; import org.sufficientlysecure.keychain.provider.KeychainContract; 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.dialog.UserIdInfoDialogFragment; import org.sufficientlysecure.keychain.util.Log; @@ -58,8 +57,6 @@ public class ViewKeyFragment extends LoaderFragment implements private Uri mDataUri; - ProviderHelper mProviderHelper; - /** * Creates new instance of this fragment */ @@ -78,8 +75,6 @@ public class ViewKeyFragment extends LoaderFragment implements View root = super.onCreateView(inflater, superContainer, savedInstanceState); 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.setOnItemClickListener(new AdapterView.OnItemClickListener() { @@ -199,7 +194,7 @@ public class ViewKeyFragment extends LoaderFragment implements boolean isVerified = data.getInt(INDEX_VERIFIED) > 0; // 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); getLoaderManager().initLoader(LOADER_ID_USER_IDS, null, this); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/UserAttributesAdapter.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/UserAttributesAdapter.java new file mode 100644 index 000000000..457083770 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/UserAttributesAdapter.java @@ -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); + } +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/UserIdsAdapter.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/UserIdsAdapter.java index 8c0ab9165..bba59d939 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/UserIdsAdapter.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/UserIdsAdapter.java @@ -20,90 +20,38 @@ package org.sufficientlysecure.keychain.ui.adapter; import android.content.Context; import android.database.Cursor; import android.graphics.Typeface; -import android.support.v4.widget.CursorAdapter; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.AdapterView; -import android.widget.CheckBox; -import android.widget.CompoundButton; import android.widget.ImageView; import android.widget.TextView; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.pgp.KeyRing; import org.sufficientlysecure.keychain.provider.KeychainContract.Certs; -import org.sufficientlysecure.keychain.provider.KeychainContract.UserPackets; import org.sufficientlysecure.keychain.service.SaveKeyringParcel; import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils; -import java.util.ArrayList; - -public class UserIdsAdapter extends CursorAdapter implements AdapterView.OnItemClickListener { - private LayoutInflater mInflater; - private final ArrayList mCheckStates; +public class UserIdsAdapter extends UserAttributesAdapter { + protected LayoutInflater mInflater; private SaveKeyringParcel mSaveKeyringParcel; private boolean mShowStatusImages; - public static final String[] USER_IDS_PROJECTION = new String[]{ - 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, + public UserIdsAdapter(Context context, Cursor c, int flags, boolean showStatusImages, SaveKeyringParcel saveKeyringParcel) { super(context, c, flags); mInflater = LayoutInflater.from(context); - mCheckStates = showCheckBoxes ? new ArrayList() : null; mSaveKeyringParcel = saveKeyringParcel; 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) { - this(context, c, flags, false, true, saveKeyringParcel); + this(context, c, flags, true, saveKeyringParcel); } public UserIdsAdapter(Context context, Cursor c, int flags) { - this(context, c, flags, false, 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); + this(context, c, flags, true, null); } @Override @@ -206,56 +154,6 @@ public class UserIdsAdapter extends CursorAdapter implements AdapterView.OnItemC 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 getSelectedUserIds() { - ArrayList 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) { @@ -276,8 +174,6 @@ public class UserIdsAdapter extends CursorAdapter implements AdapterView.OnItemC @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { 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; } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/UserIdsSelectableAdapter.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/UserIdsSelectableAdapter.java new file mode 100644 index 000000000..947d911c3 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/UserIdsSelectableAdapter.java @@ -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 mCheckStates; + + public UserIdsSelectableAdapter(Context context, Cursor c, int flags, SaveKeyringParcel saveKeyringParcel) { + super(context, c, flags, saveKeyringParcel); + + mCheckStates = new ArrayList(); + } + + @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 getSelectedUserIds() { + ArrayList 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; + } + +} diff --git a/OpenKeychain/src/main/res/layout/view_key_adv_user_id_item.xml b/OpenKeychain/src/main/res/layout/view_key_adv_user_id_item.xml index 24e8e1a10..63c1ee70f 100644 --- a/OpenKeychain/src/main/res/layout/view_key_adv_user_id_item.xml +++ b/OpenKeychain/src/main/res/layout/view_key_adv_user_id_item.xml @@ -6,13 +6,6 @@ android:orientation="horizontal" android:singleLine="true"> - - diff --git a/OpenKeychain/src/main/res/layout/view_key_selectable_user_id_item.xml b/OpenKeychain/src/main/res/layout/view_key_selectable_user_id_item.xml new file mode 100644 index 000000000..5d0edc916 --- /dev/null +++ b/OpenKeychain/src/main/res/layout/view_key_selectable_user_id_item.xml @@ -0,0 +1,18 @@ + + + + + + + +