Merge tobsbot RecyclerView branch

This commit is contained in:
Dominik Schürmann
2016-11-30 15:51:32 +01:00
25 changed files with 1795 additions and 991 deletions

View File

@@ -38,37 +38,35 @@ import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.tonicartos.superslim.LayoutManager;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.operations.results.OperationResult;
import org.sufficientlysecure.keychain.operations.results.OperationResult.LogEntryParcel;
import org.sufficientlysecure.keychain.operations.results.OperationResult.LogLevel;
import org.sufficientlysecure.keychain.operations.results.OperationResult.SubLogEntryParcel;
import org.sufficientlysecure.keychain.provider.TemporaryFileProvider;
import org.sufficientlysecure.keychain.ui.adapter.NestedLogAdapter;
import org.sufficientlysecure.keychain.ui.dialog.ShareLogDialogFragment;
import org.sufficientlysecure.keychain.ui.util.FormattingUtils;
import org.sufficientlysecure.keychain.ui.util.Notify;
import org.sufficientlysecure.keychain.ui.util.Notify.Style;
import org.sufficientlysecure.keychain.ui.util.recyclerview.RecyclerFragment;
import java.io.IOException;
import java.io.OutputStream;
public class LogDisplayFragment extends ListFragment implements OnItemClickListener {
LogAdapter mAdapter;
OperationResult mResult;
public class LogDisplayFragment extends RecyclerFragment<NestedLogAdapter>
implements NestedLogAdapter.LogActionListener {
private OperationResult mResult;
public static final String EXTRA_RESULT = "log";
protected int mTextColor;
private Uri mLogTempFile;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mTextColor = FormattingUtils.getColorFromAttr(getActivity(), R.attr.colorText);
setHasOptionsMenu(true);
}
@@ -93,14 +91,11 @@ public class LogDisplayFragment extends ListFragment implements OnItemClickListe
return;
}
mAdapter = new LogAdapter(getActivity(), mResult.getLog());
setListAdapter(mAdapter);
getListView().setOnItemClickListener(this);
getListView().setFastScrollEnabled(true);
getListView().setDividerHeight(0);
NestedLogAdapter adapter = new NestedLogAdapter(getContext(), mResult.getLog());
adapter.setListener(this);
setAdapter(adapter);
setLayoutManager(new LayoutManager(getContext()));
}
@Override
@@ -130,7 +125,6 @@ public class LogDisplayFragment extends ListFragment implements OnItemClickListe
}
private void shareLog() {
Activity activity = getActivity();
if (activity == null) {
return;
@@ -144,7 +138,7 @@ public class LogDisplayFragment extends ListFragment implements OnItemClickListe
try {
OutputStream outputStream = activity.getContentResolver().openOutputStream(mLogTempFile);
outputStream.write(log.getBytes());
} catch (IOException e) {
} catch (IOException | NullPointerException e) {
Notify.create(activity, R.string.error_log_share_internal, Style.ERROR).show();
return;
}
@@ -153,129 +147,12 @@ public class LogDisplayFragment extends ListFragment implements OnItemClickListe
ShareLogDialogFragment shareLogDialog = ShareLogDialogFragment.newInstance(mLogTempFile);
shareLogDialog.show(getActivity().getSupportFragmentManager(), "shareLogDialog");
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
LogEntryParcel parcel = mAdapter.getItem(position);
if ( ! (parcel instanceof SubLogEntryParcel)) {
return;
}
Intent intent = new Intent(
getActivity(), LogDisplayActivity.class);
intent.putExtra(LogDisplayFragment.EXTRA_RESULT, ((SubLogEntryParcel) parcel).getSubResult());
public void onSubEntryClicked(SubLogEntryParcel subLogEntryParcel) {
Intent intent = new Intent(getActivity(), LogDisplayActivity.class);
intent.putExtra(LogDisplayFragment.EXTRA_RESULT, subLogEntryParcel.getSubResult());
startActivity(intent);
}
private class LogAdapter extends ArrayAdapter<LogEntryParcel> {
private LayoutInflater mInflater;
private int dipFactor;
public LogAdapter(Context context, OperationResult.OperationLog log) {
super(context, R.layout.log_display_item, log.toList());
mInflater = LayoutInflater.from(getContext());
dipFactor = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
(float) 8, getResources().getDisplayMetrics());
}
private class ItemHolder {
final View mSecond;
final TextView mText, mSecondText;
final ImageView mImg, mSecondImg, mSub;
public ItemHolder(TextView text, ImageView image, ImageView sub, View second, TextView secondText, ImageView secondImg) {
mText = text;
mImg = image;
mSub = sub;
mSecond = second;
mSecondText = secondText;
mSecondImg = secondImg;
}
}
// Check if convertView.setPadding is redundant
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LogEntryParcel entry = getItem(position);
ItemHolder ih;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.log_display_item, parent, false);
ih = new ItemHolder(
(TextView) convertView.findViewById(R.id.log_text),
(ImageView) convertView.findViewById(R.id.log_img),
(ImageView) convertView.findViewById(R.id.log_sub),
convertView.findViewById(R.id.log_second),
(TextView) convertView.findViewById(R.id.log_second_text),
(ImageView) convertView.findViewById(R.id.log_second_img)
);
convertView.setTag(ih);
} else {
ih = (ItemHolder) convertView.getTag();
}
if (entry instanceof SubLogEntryParcel) {
ih.mSub.setVisibility(View.VISIBLE);
convertView.setClickable(false);
convertView.setPadding((entry.mIndent) * dipFactor, 0, 0, 0);
OperationResult result = ((SubLogEntryParcel) entry).getSubResult();
LogEntryParcel subEntry = result.getLog().getLast();
if (subEntry != null) {
ih.mSecond.setVisibility(View.VISIBLE);
// special case: first parameter may be a quantity
if (subEntry.mParameters != null && subEntry.mParameters.length > 0
&& subEntry.mParameters[0] instanceof Integer) {
ih.mSecondText.setText(getResources().getQuantityString(subEntry.mType.getMsgId(),
(Integer) subEntry.mParameters[0],
subEntry.mParameters));
} else {
ih.mSecondText.setText(getResources().getString(subEntry.mType.getMsgId(),
subEntry.mParameters));
}
ih.mSecondText.setTextColor(subEntry.mType.mLevel == LogLevel.DEBUG ? Color.GRAY : mTextColor);
switch (subEntry.mType.mLevel) {
case DEBUG: ih.mSecondImg.setBackgroundColor(Color.GRAY); break;
case INFO: ih.mSecondImg.setBackgroundColor(mTextColor); break;
case WARN: ih.mSecondImg.setBackgroundColor(getResources().getColor(R.color.android_orange_light)); break;
case ERROR: ih.mSecondImg.setBackgroundColor(getResources().getColor(R.color.android_red_light)); break;
case START: ih.mSecondImg.setBackgroundColor(mTextColor); break;
case OK: ih.mSecondImg.setBackgroundColor(getResources().getColor(R.color.android_green_light)); break;
case CANCELLED: ih.mSecondImg.setBackgroundColor(getResources().getColor(R.color.android_red_light)); break;
}
} else {
ih.mSecond.setVisibility(View.GONE);
}
} else {
ih.mSub.setVisibility(View.GONE);
ih.mSecond.setVisibility(View.GONE);
convertView.setClickable(true);
}
// special case: first parameter may be a quantity
if (entry.mParameters != null && entry.mParameters.length > 0
&& entry.mParameters[0] instanceof Integer) {
ih.mText.setText(getResources().getQuantityString(entry.mType.getMsgId(),
(Integer) entry.mParameters[0],
entry.mParameters));
} else {
ih.mText.setText(getResources().getString(entry.mType.getMsgId(),
entry.mParameters));
}
convertView.setPadding((entry.mIndent) * dipFactor, 0, 0, 0);
ih.mText.setTextColor(entry.mType.mLevel == LogLevel.DEBUG ? Color.GRAY : mTextColor);
switch (entry.mType.mLevel) {
case DEBUG: ih.mImg.setBackgroundColor(Color.GRAY); break;
case INFO: ih.mImg.setBackgroundColor(mTextColor); break;
case WARN: ih.mImg.setBackgroundColor(getResources().getColor(R.color.android_orange_light)); break;
case ERROR: ih.mImg.setBackgroundColor(getResources().getColor(R.color.android_red_light)); break;
case START: ih.mImg.setBackgroundColor(mTextColor); break;
case OK: ih.mImg.setBackgroundColor(getResources().getColor(R.color.android_green_light)); break;
case CANCELLED: ih.mImg.setBackgroundColor(getResources().getColor(R.color.android_red_light)); break;
}
return convertView;
}
}
}

View File

@@ -1,400 +0,0 @@
/*
* Copyright (C) 2012-2014 Dominik Schürmann <dominik@dominikschuermann.de>
* Copyright (C) 2010-2014 Thialfihar <thi@thialfihar.org>
*
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.sufficientlysecure.keychain.ui;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.compatibility.ListFragmentWorkaround;
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings;
import org.sufficientlysecure.keychain.provider.KeychainDatabase.Tables;
import org.sufficientlysecure.keychain.ui.adapter.SelectKeyCursorAdapter;
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils.State;
import java.util.Vector;
public class SelectPublicKeyFragment extends ListFragmentWorkaround implements TextWatcher,
LoaderManager.LoaderCallbacks<Cursor> {
public static final String ARG_PRESELECTED_KEY_IDS = "preselected_key_ids";
private SelectKeyCursorAdapter mAdapter;
private EditText mSearchView;
private long mSelectedMasterKeyIds[];
private String mQuery;
// copied from ListFragment
static final int INTERNAL_EMPTY_ID = 0x00ff0001;
static final int INTERNAL_PROGRESS_CONTAINER_ID = 0x00ff0002;
static final int INTERNAL_LIST_CONTAINER_ID = 0x00ff0003;
// added for search view
static final int SEARCH_ID = 0x00ff0004;
/**
* Creates new instance of this fragment
*/
public static SelectPublicKeyFragment newInstance(long[] preselectedKeyIds) {
SelectPublicKeyFragment frag = new SelectPublicKeyFragment();
Bundle args = new Bundle();
args.putLongArray(ARG_PRESELECTED_KEY_IDS, preselectedKeyIds);
frag.setArguments(args);
return frag;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSelectedMasterKeyIds = getArguments().getLongArray(ARG_PRESELECTED_KEY_IDS);
}
/**
* Copied from ListFragment and added EditText for search on top of list.
* We do not use a custom layout here, because this breaks the progress bar functionality
* of ListFragment.
*
* @param inflater
* @param container
* @param savedInstanceState
* @return
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final Context context = getActivity();
FrameLayout root = new FrameLayout(context);
// ------------------------------------------------------------------
LinearLayout pframe = new LinearLayout(context);
pframe.setId(INTERNAL_PROGRESS_CONTAINER_ID);
pframe.setOrientation(LinearLayout.VERTICAL);
pframe.setVisibility(View.GONE);
pframe.setGravity(Gravity.CENTER);
ProgressBar progress = new ProgressBar(context, null,
android.R.attr.progressBarStyleLarge);
pframe.addView(progress, new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
root.addView(pframe, new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
// ------------------------------------------------------------------
FrameLayout lframe = new FrameLayout(context);
lframe.setId(INTERNAL_LIST_CONTAINER_ID);
TextView tv = new TextView(getActivity());
tv.setId(INTERNAL_EMPTY_ID);
tv.setGravity(Gravity.CENTER);
lframe.addView(tv, new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
// Added for search view: linearLayout, mSearchView
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setOrientation(LinearLayout.VERTICAL);
mSearchView = new EditText(context);
mSearchView.setId(SEARCH_ID);
mSearchView.setHint(R.string.menu_search);
mSearchView.setCompoundDrawablesWithIntrinsicBounds(
getResources().getDrawable(R.drawable.ic_search_grey_24dp), null, null, null);
linearLayout.addView(mSearchView, new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
ListView lv = new ListView(getActivity());
lv.setId(android.R.id.list);
lv.setDrawSelectorOnTop(false);
linearLayout.addView(lv, new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
lframe.addView(linearLayout, new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
root.addView(lframe, new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
// ------------------------------------------------------------------
root.setLayoutParams(new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
return root;
}
/**
* Define Adapter and Loader on create of Activity
*/
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
// Give some text to display if there is no data. In a real
// application this would come from a resource.
setEmptyText(getString(R.string.list_empty));
mSearchView.addTextChangedListener(this);
mAdapter = new SelectPublicKeyCursorAdapter(getActivity(), null, 0, getListView());
setListAdapter(mAdapter);
// 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);
}
/**
* Selects items based on master key ids in list view
*
* @param masterKeyIds
*/
private void preselectMasterKeyIds(long[] masterKeyIds) {
if (masterKeyIds != null) {
for (int i = 0; i < getListView().getCount(); ++i) {
long keyId = mAdapter.getMasterKeyId(i);
for (long masterKeyId : masterKeyIds) {
if (keyId == masterKeyId) {
getListView().setItemChecked(i, true);
break;
}
}
}
}
}
/**
* Returns all selected master key ids
*
* @return
*/
public long[] getSelectedMasterKeyIds() {
// mListView.getCheckedItemIds() would give the row ids of the KeyRings not the master key
// ids!
Vector<Long> vector = new Vector<>();
for (int i = 0; i < getListView().getCount(); ++i) {
if (getListView().isItemChecked(i)) {
vector.add(mAdapter.getMasterKeyId(i));
}
}
// convert to long array
long[] selectedMasterKeyIds = new long[vector.size()];
for (int i = 0; i < vector.size(); ++i) {
selectedMasterKeyIds[i] = vector.get(i);
}
return selectedMasterKeyIds;
}
/**
* Returns all selected user ids
*
* @return
*/
public String[] getSelectedUserIds() {
Vector<String> userIds = new Vector<>();
for (int i = 0; i < getListView().getCount(); ++i) {
if (getListView().isItemChecked(i)) {
userIds.add(mAdapter.getUserId(i));
}
}
// make empty array to not return null
String userIdArray[] = new String[0];
return userIds.toArray(userIdArray);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
Uri baseUri = KeyRings.buildUnifiedKeyRingsUri();
// These are the rows that we will retrieve.
String[] projection = new String[]{
KeyRings._ID,
KeyRings.MASTER_KEY_ID,
KeyRings.USER_ID,
KeyRings.IS_EXPIRED,
KeyRings.IS_REVOKED,
KeyRings.HAS_ENCRYPT,
KeyRings.VERIFIED,
KeyRings.HAS_DUPLICATE_USER_ID,
KeyRings.CREATION,
};
String inMasterKeyList = null;
if (mSelectedMasterKeyIds != null && mSelectedMasterKeyIds.length > 0) {
inMasterKeyList = Tables.KEYS + "." + KeyRings.MASTER_KEY_ID + " IN (";
for (int i = 0; i < mSelectedMasterKeyIds.length; ++i) {
if (i != 0) {
inMasterKeyList += ", ";
}
inMasterKeyList += DatabaseUtils.sqlEscapeString("" + mSelectedMasterKeyIds[i]);
}
inMasterKeyList += ")";
}
String orderBy = KeyRings.USER_ID + " ASC";
if (inMasterKeyList != null) {
// sort by selected master keys
orderBy = inMasterKeyList + " DESC, " + orderBy;
}
String where = null;
String whereArgs[] = null;
if (mQuery != null) {
String[] words = mQuery.trim().split("\\s+");
whereArgs = new String[words.length];
for (int i = 0; i < words.length; ++i) {
if (where == null) {
where = "";
} else {
where += " AND ";
}
where += KeyRings.USER_ID + " LIKE ?";
whereArgs[i] = "%" + words[i] + "%";
}
}
// Now create and return a CursorLoader that will take care of
// creating a Cursor for the data being displayed.
return new CursorLoader(getActivity(), baseUri, projection, where, whereArgs, orderBy);
}
@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.setSearchQuery(mQuery);
mAdapter.swapCursor(data);
// The list should now be shown.
if (isResumed()) {
setListShown(true);
} else {
setListShownNoAnimation(true);
}
// preselect given master keys
preselectMasterKeyIds(mSelectedMasterKeyIds);
}
@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);
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override
public void afterTextChanged(Editable editable) {
mQuery = !TextUtils.isEmpty(editable.toString()) ? editable.toString() : null;
getLoaderManager().restartLoader(0, null, this);
}
private class SelectPublicKeyCursorAdapter extends SelectKeyCursorAdapter {
private int mIndexHasEncrypt, mIndexIsVerified;
public SelectPublicKeyCursorAdapter(Context context, Cursor c, int flags, ListView listView) {
super(context, c, flags, listView);
}
@Override
protected void initIndex(Cursor cursor) {
super.initIndex(cursor);
if (cursor != null) {
mIndexHasEncrypt = cursor.getColumnIndexOrThrow(KeyRings.HAS_ENCRYPT);
mIndexIsVerified = cursor.getColumnIndexOrThrow(KeyRings.VERIFIED);
}
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
ViewHolderItem h = (SelectKeyCursorAdapter.ViewHolderItem) view.getTag();
// We care about the checkbox
h.selected.setVisibility(View.VISIBLE);
// the getListView works because this is not a static subclass!
h.selected.setChecked(getListView().isItemChecked(cursor.getPosition()));
boolean enabled = false;
if((Boolean) h.statusIcon.getTag()) {
// Check if key is viable for our purposes
if (cursor.getInt(mIndexHasEncrypt) == 0) {
h.statusIcon.setVisibility(View.VISIBLE);
KeyFormattingUtils.setStatusImage(mContext, h.statusIcon, State.UNAVAILABLE);
enabled = false;
} else if (cursor.getInt(mIndexIsVerified) != 0) {
h.statusIcon.setVisibility(View.VISIBLE);
KeyFormattingUtils.setStatusImage(mContext, h.statusIcon, State.VERIFIED);
enabled = true;
} else {
h.statusIcon.setVisibility(View.VISIBLE);
KeyFormattingUtils.setStatusImage(mContext, h.statusIcon, State.UNVERIFIED);
enabled = true;
}
}
h.setEnabled(enabled);
}
}
}

View File

@@ -17,7 +17,6 @@
package org.sufficientlysecure.keychain.ui;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
@@ -25,56 +24,25 @@ import android.os.Bundle;
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.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;
import android.view.LayoutInflater;
import com.tonicartos.superslim.LayoutManager;
import org.openintents.openpgp.util.OpenPgpUtils;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.pgp.KeyRing;
import org.sufficientlysecure.keychain.pgp.WrappedSignature;
import org.sufficientlysecure.keychain.provider.KeychainContract;
import org.sufficientlysecure.keychain.provider.KeychainDatabase;
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
import org.sufficientlysecure.keychain.ui.adapter.CertSectionedListAdapter;
import org.sufficientlysecure.keychain.ui.util.recyclerview.RecyclerFragment;
import org.sufficientlysecure.keychain.util.Log;
import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter;
import se.emilsjolander.stickylistheaders.StickyListHeadersListView;
public class ViewKeyAdvCertsFragment extends LoaderFragment implements
LoaderManager.LoaderCallbacks<Cursor>, AdapterView.OnItemClickListener {
public class ViewKeyAdvCertsFragment extends RecyclerFragment<CertSectionedListAdapter>
implements LoaderManager.LoaderCallbacks<Cursor>, CertSectionedListAdapter.CertListListener {
public static final String ARG_DATA_URI = "data_uri";
private StickyListHeadersListView mStickyList;
private CertListAdapter mCertsAdapter;
private Uri mDataUriCerts;
// These are the rows that we will retrieve.
static final String[] CERTS_PROJECTION = new String[]{
KeychainContract.Certs._ID,
KeychainContract.Certs.MASTER_KEY_ID,
KeychainContract.Certs.VERIFIED,
KeychainContract.Certs.TYPE,
KeychainContract.Certs.RANK,
KeychainContract.Certs.KEY_ID_CERTIFIER,
KeychainContract.Certs.USER_ID,
KeychainContract.Certs.SIGNER_UID
};
// sort by our user id,
static final String CERTS_SORT_ORDER =
KeychainDatabase.Tables.CERTS + "." + KeychainContract.Certs.RANK + " ASC, "
+ KeychainContract.Certs.VERIFIED + " DESC, "
+ KeychainDatabase.Tables.CERTS + "." + KeychainContract.Certs.TYPE + " DESC, "
+ KeychainContract.Certs.SIGNER_UID + " ASC";
/**
* Creates new instance of this fragment
*/
@@ -89,56 +57,39 @@ public class ViewKeyAdvCertsFragment extends LoaderFragment implements
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup superContainer, Bundle savedInstanceState) {
View root = super.onCreateView(inflater, superContainer, savedInstanceState);
View view = inflater.inflate(R.layout.view_key_adv_certs_fragment, getContainer());
mStickyList = (StickyListHeadersListView) view.findViewById(R.id.certs_list);
return root;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.view_key_adv_certs_fragment, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
hideList(false);
Uri dataUri = getArguments().getParcelable(ARG_DATA_URI);
if (dataUri == null) {
Log.e(Constants.TAG, "Data missing. Should be Uri of key!");
getActivity().finish();
return;
} else {
mDataUriCerts = KeychainContract.Certs.buildCertsUri(dataUri);
}
loadData(dataUri);
}
CertSectionedListAdapter adapter = new CertSectionedListAdapter(getActivity(), null);
adapter.setCertListListener(this);
private void loadData(Uri dataUri) {
mDataUriCerts = KeychainContract.Certs.buildCertsUri(dataUri);
setAdapter(adapter);
setLayoutManager(new LayoutManager(getActivity()));
mStickyList.setAreHeadersSticky(true);
mStickyList.setDrawingListUnderStickyHeader(false);
mStickyList.setOnItemClickListener(this);
mStickyList.setEmptyView(getActivity().findViewById(R.id.empty));
// Create an empty adapter we will use to display the loaded data.
mCertsAdapter = new CertListAdapter(getActivity(), null);
mStickyList.setAdapter(mCertsAdapter);
// Prepare the loaders. Either re-connect with an existing ones,
// or start new ones.
getLoaderManager().initLoader(0, null, this);
}
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
setContentShown(false);
// Now create and return a CursorLoader that will take care of
// creating a Cursor for the data being displayed.
return new CursorLoader(getActivity(), mDataUriCerts,
CERTS_PROJECTION, null, null, CERTS_SORT_ORDER);
CertSectionedListAdapter.CertCursor.CERTS_PROJECTION, null, null,
CertSectionedListAdapter.CertCursor.CERTS_SORT_ORDER);
}
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
@@ -149,11 +100,13 @@ public class ViewKeyAdvCertsFragment extends LoaderFragment implements
// Swap the new cursor in. (The framework will take care of closing the
// old cursor once we return.)
mCertsAdapter.swapCursor(data);
mStickyList.setAdapter(mCertsAdapter);
getAdapter().swapCursor(CertSectionedListAdapter.CertCursor.wrap(data));
// TODO: maybe show not before both are loaded!
setContentShown(true);
if (isResumed()) {
showList(true);
} else {
showList(false);
}
}
/**
@@ -161,184 +114,180 @@ public class ViewKeyAdvCertsFragment extends LoaderFragment implements
* We need to make sure we are no longer using it.
*/
public void onLoaderReset(Loader<Cursor> loader) {
mCertsAdapter.swapCursor(null);
getAdapter().swapCursor(null);
}
/**
* On click on item, start key view activity
*/
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
if (view.getTag(R.id.tag_mki) != null) {
long masterKeyId = (Long) view.getTag(R.id.tag_mki);
long rank = (Long) view.getTag(R.id.tag_rank);
long certifierId = (Long) view.getTag(R.id.tag_certifierId);
public void onClick(long masterKeyId, long signerKeyId, long rank) {
if(masterKeyId != 0L) {
Intent viewIntent = new Intent(getActivity(), ViewCertActivity.class);
viewIntent.setData(KeychainContract.Certs.buildCertsSpecificUri(
masterKeyId, rank, certifierId));
masterKeyId, rank, signerKeyId));
startActivity(viewIntent);
}
}
/**
* Implements StickyListHeadersAdapter from library
*/
private class CertListAdapter extends CursorAdapter implements StickyListHeadersAdapter {
private LayoutInflater mInflater;
private int mIndexMasterKeyId, mIndexUserId, mIndexRank;
private int mIndexSignerKeyId, mIndexSignerUserId;
private int mIndexVerified, mIndexType;
public CertListAdapter(Context context, Cursor c) {
super(context, c, 0);
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) {
mIndexMasterKeyId = cursor.getColumnIndexOrThrow(KeychainContract.Certs.MASTER_KEY_ID);
mIndexUserId = cursor.getColumnIndexOrThrow(KeychainContract.Certs.USER_ID);
mIndexRank = cursor.getColumnIndexOrThrow(KeychainContract.Certs.RANK);
mIndexType = cursor.getColumnIndexOrThrow(KeychainContract.Certs.TYPE);
mIndexVerified = cursor.getColumnIndexOrThrow(KeychainContract.Certs.VERIFIED);
mIndexSignerKeyId = cursor.getColumnIndexOrThrow(KeychainContract.Certs.KEY_ID_CERTIFIER);
mIndexSignerUserId = cursor.getColumnIndexOrThrow(KeychainContract.Certs.SIGNER_UID);
}
}
/**
* Bind cursor data to the item list view
* <p/>
* NOTE: CursorAdapter already implements the ViewHolder pattern in its getView() method.
* Thus no ViewHolder is required here.
*/
@Override
public void bindView(View view, Context context, Cursor cursor) {
// set name and stuff, common to both key types
TextView wSignerKeyId = (TextView) view.findViewById(R.id.signerKeyId);
TextView wSignerName = (TextView) view.findViewById(R.id.signerName);
TextView wSignStatus = (TextView) view.findViewById(R.id.signStatus);
String signerKeyId = KeyFormattingUtils.beautifyKeyIdWithPrefix(
cursor.getLong(mIndexSignerKeyId));
OpenPgpUtils.UserId userId = KeyRing.splitUserId(cursor.getString(mIndexSignerUserId));
if (userId.name != null) {
wSignerName.setText(userId.name);
} else {
wSignerName.setText(R.string.user_id_no_name);
}
wSignerKeyId.setText(signerKeyId);
switch (cursor.getInt(mIndexType)) {
case WrappedSignature.DEFAULT_CERTIFICATION: // 0x10
wSignStatus.setText(R.string.cert_default);
break;
case WrappedSignature.NO_CERTIFICATION: // 0x11
wSignStatus.setText(R.string.cert_none);
break;
case WrappedSignature.CASUAL_CERTIFICATION: // 0x12
wSignStatus.setText(R.string.cert_casual);
break;
case WrappedSignature.POSITIVE_CERTIFICATION: // 0x13
wSignStatus.setText(R.string.cert_positive);
break;
case WrappedSignature.CERTIFICATION_REVOCATION: // 0x30
wSignStatus.setText(R.string.cert_revoke);
break;
}
view.setTag(R.id.tag_mki, cursor.getLong(mIndexMasterKeyId));
view.setTag(R.id.tag_rank, cursor.getLong(mIndexRank));
view.setTag(R.id.tag_certifierId, cursor.getLong(mIndexSignerKeyId));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return mInflater.inflate(R.layout.view_key_adv_certs_item, parent, false);
}
/**
* Creates a new header view and binds the section headers to it. It uses the ViewHolder
* pattern. Most functionality is similar to getView() from Android's CursorAdapter.
* <p/>
* NOTE: The variables mDataValid and mCursor are available due to the super class
* CursorAdapter.
*/
@Override
public View getHeaderView(int position, View convertView, ViewGroup parent) {
HeaderViewHolder holder;
if (convertView == null) {
holder = new HeaderViewHolder();
convertView = mInflater.inflate(R.layout.view_key_adv_certs_header, parent, false);
holder.text = (TextView) convertView.findViewById(R.id.stickylist_header_text);
holder.count = (TextView) convertView.findViewById(R.id.certs_num);
convertView.setTag(holder);
} else {
holder = (HeaderViewHolder) convertView.getTag();
}
if (!mDataValid) {
// no data available at this point
Log.d(Constants.TAG, "getHeaderView: No data available at this point!");
return convertView;
}
if (!mCursor.moveToPosition(position)) {
throw new IllegalStateException("couldn't move cursor to position " + position);
}
// set header text as first char in user id
String userId = mCursor.getString(mIndexUserId);
holder.text.setText(userId);
holder.count.setVisibility(View.GONE);
return convertView;
}
/**
* Header IDs should be static, position=1 should always return the same Id that is.
*/
@Override
public long getHeaderId(int position) {
if (!mDataValid) {
// no data available at this point
Log.d(Constants.TAG, "getHeaderView: No data available at this point!");
return -1;
}
if (!mCursor.moveToPosition(position)) {
throw new IllegalStateException("couldn't move cursor to position " + position);
}
// otherwise, return the first character of the name as ID
return mCursor.getInt(mIndexRank);
// sort by the first four characters (should be enough I guess?)
// return ByteBuffer.wrap(userId.getBytes()).asLongBuffer().get(0);
}
class HeaderViewHolder {
TextView text;
TextView count;
}
}
//<<<<<<< HEAD
//
//
// /**
// * Implements StickyListHeadersAdapter from library
// */
// private class CertListAdapter extends CursorAdapter implements StickyListHeadersAdapter {
// private LayoutInflater mInflater;
// private int mIndexMasterKeyId, mIndexUserId, mIndexRank;
// private int mIndexSignerKeyId, mIndexSignerUserId;
// private int mIndexVerified, mIndexType;
//
// public CertListAdapter(Context context, Cursor c) {
// super(context, c, 0);
//
// 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) {
// mIndexMasterKeyId = cursor.getColumnIndexOrThrow(KeychainContract.Certs.MASTER_KEY_ID);
// mIndexUserId = cursor.getColumnIndexOrThrow(KeychainContract.Certs.USER_ID);
// mIndexRank = cursor.getColumnIndexOrThrow(KeychainContract.Certs.RANK);
// mIndexType = cursor.getColumnIndexOrThrow(KeychainContract.Certs.TYPE);
// mIndexVerified = cursor.getColumnIndexOrThrow(KeychainContract.Certs.VERIFIED);
// mIndexSignerKeyId = cursor.getColumnIndexOrThrow(KeychainContract.Certs.KEY_ID_CERTIFIER);
// mIndexSignerUserId = cursor.getColumnIndexOrThrow(KeychainContract.Certs.SIGNER_UID);
// }
// }
//
// /**
// * Bind cursor data to the item list view
// * <p/>
// * NOTE: CursorAdapter already implements the ViewHolder pattern in its getView() method.
// * Thus no ViewHolder is required here.
// */
// @Override
// public void bindView(View view, Context context, Cursor cursor) {
//
// // set name and stuff, common to both key types
// TextView wSignerKeyId = (TextView) view.findViewById(R.id.signerKeyId);
// TextView wSignerName = (TextView) view.findViewById(R.id.signerName);
// TextView wSignStatus = (TextView) view.findViewById(R.id.signStatus);
//
// String signerKeyId = KeyFormattingUtils.beautifyKeyIdWithPrefix(
// cursor.getLong(mIndexSignerKeyId));
// OpenPgpUtils.UserId userId = KeyRing.splitUserId(cursor.getString(mIndexSignerUserId));
// if (userId.name != null) {
// wSignerName.setText(userId.name);
// } else {
// wSignerName.setText(R.string.user_id_no_name);
// }
// wSignerKeyId.setText(signerKeyId);
//
// switch (cursor.getInt(mIndexType)) {
// case WrappedSignature.DEFAULT_CERTIFICATION: // 0x10
// wSignStatus.setText(R.string.cert_default);
// break;
// case WrappedSignature.NO_CERTIFICATION: // 0x11
// wSignStatus.setText(R.string.cert_none);
// break;
// case WrappedSignature.CASUAL_CERTIFICATION: // 0x12
// wSignStatus.setText(R.string.cert_casual);
// break;
// case WrappedSignature.POSITIVE_CERTIFICATION: // 0x13
// wSignStatus.setText(R.string.cert_positive);
// break;
// case WrappedSignature.CERTIFICATION_REVOCATION: // 0x30
// wSignStatus.setText(R.string.cert_revoke);
// break;
// }
//
//
// view.setTag(R.id.tag_mki, cursor.getLong(mIndexMasterKeyId));
// view.setTag(R.id.tag_rank, cursor.getLong(mIndexRank));
// view.setTag(R.id.tag_certifierId, cursor.getLong(mIndexSignerKeyId));
// }
//
// @Override
// public View newView(Context context, Cursor cursor, ViewGroup parent) {
// return mInflater.inflate(R.layout.view_key_adv_certs_item, parent, false);
// }
//
// /**
// * Creates a new header view and binds the section headers to it. It uses the ViewHolder
// * pattern. Most functionality is similar to getView() from Android's CursorAdapter.
// * <p/>
// * NOTE: The variables mDataValid and mCursor are available due to the super class
// * CursorAdapter.
// */
// @Override
// public View getHeaderView(int position, View convertView, ViewGroup parent) {
// HeaderViewHolder holder;
// if (convertView == null) {
// holder = new HeaderViewHolder();
// convertView = mInflater.inflate(R.layout.view_key_adv_certs_header, parent, false);
// holder.text = (TextView) convertView.findViewById(R.id.stickylist_header_text);
// holder.count = (TextView) convertView.findViewById(R.id.certs_num);
// convertView.setTag(holder);
// } else {
// holder = (HeaderViewHolder) convertView.getTag();
// }
//
// if (!mDataValid) {
// // no data available at this point
// Log.d(Constants.TAG, "getHeaderView: No data available at this point!");
// return convertView;
// }
//
// if (!mCursor.moveToPosition(position)) {
// throw new IllegalStateException("couldn't move cursor to position " + position);
// }
//
// // set header text as first char in user id
// String userId = mCursor.getString(mIndexUserId);
// holder.text.setText(userId);
// holder.count.setVisibility(View.GONE);
// return convertView;
// }
//
// /**
// * Header IDs should be static, position=1 should always return the same Id that is.
// */
// @Override
// public long getHeaderId(int position) {
// if (!mDataValid) {
// // no data available at this point
// Log.d(Constants.TAG, "getHeaderView: No data available at this point!");
// return -1;
// }
//
// if (!mCursor.moveToPosition(position)) {
// throw new IllegalStateException("couldn't move cursor to position " + position);
// }
//
// // otherwise, return the first character of the name as ID
// return mCursor.getInt(mIndexRank);
//
// // sort by the first four characters (should be enough I guess?)
// // return ByteBuffer.wrap(userId.getBytes()).asLongBuffer().get(0);
// }
//
// class HeaderViewHolder {
// TextView text;
// TextView count;
// }
//
// }
//=======
//>>>>>>> cc6f9037948f8d3e5481c7479b1ce5e607e9a01f
}

View File

@@ -0,0 +1,230 @@
package org.sufficientlysecure.keychain.ui.adapter;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import org.openintents.openpgp.util.OpenPgpUtils;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.pgp.KeyRing;
import org.sufficientlysecure.keychain.pgp.WrappedSignature;
import org.sufficientlysecure.keychain.provider.KeychainContract;
import org.sufficientlysecure.keychain.provider.KeychainDatabase;
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
import org.sufficientlysecure.keychain.ui.util.adapter.CursorAdapter;
import org.sufficientlysecure.keychain.ui.util.adapter.SectionCursorAdapter;
import java.util.ArrayList;
import java.util.Arrays;
public class CertSectionedListAdapter extends SectionCursorAdapter<CertSectionedListAdapter.CertCursor, String,
CertSectionedListAdapter.CertItemViewHolder, CertSectionedListAdapter.CertSectionViewHolder> {
private CertListListener mListener;
public CertSectionedListAdapter(Context context, CertCursor cursor) {
super(context, CertCursor.wrap(cursor), 0);
}
public void setCertListListener(CertListListener listener) {
mListener = listener;
}
@Override
public long getIdFromCursor(CertCursor cursor) {
return cursor.getKeyId();
}
@Override
protected String getSectionFromCursor(CertCursor cursor) throws IllegalStateException {
return cursor.getRawSignerUserId();
}
@Override
protected CertSectionViewHolder onCreateSectionViewHolder(ViewGroup parent, int viewType) {
return new CertSectionViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.view_key_adv_certs_header, parent, false));
}
@Override
protected CertItemViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {
return new CertItemViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.view_key_adv_certs_item, parent, false));
}
@Override
protected void onBindSectionViewHolder(CertSectionViewHolder holder, String section) {
holder.bind(section);
}
@Override
protected void onBindItemViewHolder(CertItemViewHolder holder, CertCursor cursor) {
holder.bind(cursor);
}
class CertItemViewHolder extends SectionCursorAdapter.ViewHolder
implements View.OnClickListener {
private TextView mSignerKeyId;
private TextView mSignerName;
private TextView mSignStatus;
public CertItemViewHolder(View itemView) {
super(itemView);
itemView.setClickable(true);
itemView.setOnClickListener(this);
mSignerName = (TextView) itemView.findViewById(R.id.signerName);
mSignStatus = (TextView) itemView.findViewById(R.id.signStatus);
mSignerKeyId = (TextView) itemView.findViewById(R.id.signerKeyId);
}
public void bind(CertCursor cursor) {
String signerKeyId = KeyFormattingUtils.beautifyKeyIdWithPrefix(
cursor.getCertifierKeyId());
OpenPgpUtils.UserId userId = cursor.getSignerUserId();
if (userId.name != null) {
mSignerName.setText(userId.name);
} else {
mSignerName.setText(R.string.user_id_no_name);
}
mSignerKeyId.setText(signerKeyId);
switch (cursor.getType()) {
case WrappedSignature.DEFAULT_CERTIFICATION: // 0x10
mSignStatus.setText(R.string.cert_default);
break;
case WrappedSignature.NO_CERTIFICATION: // 0x11
mSignStatus.setText(R.string.cert_none);
break;
case WrappedSignature.CASUAL_CERTIFICATION: // 0x12
mSignStatus.setText(R.string.cert_casual);
break;
case WrappedSignature.POSITIVE_CERTIFICATION: // 0x13
mSignStatus.setText(R.string.cert_positive);
break;
case WrappedSignature.CERTIFICATION_REVOCATION: // 0x30
mSignStatus.setText(R.string.cert_revoke);
break;
}
}
@Override
public void onClick(View v) {
if(mListener != null) {
int index = getCursorPositionWithoutSections(getAdapterPosition());
if (moveCursor(index)) {
CertCursor cursor = getCursor();
mListener.onClick(
cursor.getKeyId(),
cursor.getCertifierKeyId(),
cursor.getRank()
);
}
}
}
}
static class CertSectionViewHolder extends SectionCursorAdapter.ViewHolder {
private TextView mHeaderText;
public CertSectionViewHolder(View itemView) {
super(itemView);
mHeaderText = (TextView) itemView.findViewById(R.id.stickylist_header_text);
}
public void bind(String text) {
mHeaderText.setText(text);
}
}
public static class CertCursor extends CursorAdapter.AbstractCursor {
public static final String[] CERTS_PROJECTION;
static {
ArrayList<String> projection = new ArrayList<>();
projection.addAll(Arrays.asList(AbstractCursor.PROJECTION));
projection.addAll(Arrays.asList(
KeychainContract.Certs.MASTER_KEY_ID,
KeychainContract.Certs.VERIFIED,
KeychainContract.Certs.TYPE,
KeychainContract.Certs.RANK,
KeychainContract.Certs.KEY_ID_CERTIFIER,
KeychainContract.Certs.USER_ID,
KeychainContract.Certs.SIGNER_UID
));
CERTS_PROJECTION = projection.toArray(new String[projection.size()]);
}
public static final String CERTS_SORT_ORDER =
KeychainDatabase.Tables.CERTS + "." + KeychainContract.Certs.RANK + " ASC, "
+ KeychainContract.Certs.VERIFIED + " DESC, "
+ KeychainDatabase.Tables.CERTS + "." + KeychainContract.Certs.TYPE + " DESC, "
+ KeychainContract.Certs.SIGNER_UID + " ASC";
public static CertCursor wrap(Cursor cursor) {
if(cursor != null) {
return new CertCursor(cursor);
} else {
return null;
}
}
private CertCursor(Cursor cursor) {
super(cursor);
}
public long getKeyId() {
int index = getColumnIndexOrThrow(KeychainContract.Certs.MASTER_KEY_ID);
return getLong(index);
}
public boolean isVerified() {
int index = getColumnIndexOrThrow(KeychainContract.Certs.VERIFIED);
return getInt(index) > 0;
}
public int getType() {
int index = getColumnIndexOrThrow(KeychainContract.Certs.TYPE);
return getInt(index);
}
public long getRank() {
int index = getColumnIndexOrThrow(KeychainContract.Certs.RANK);
return getLong(index);
}
public long getCertifierKeyId() {
int index = getColumnIndexOrThrow(KeychainContract.Certs.KEY_ID_CERTIFIER);
return getLong(index);
}
public String getRawUserId() {
int index = getColumnIndexOrThrow(KeychainContract.Certs.USER_ID);
return getString(index);
}
public String getRawSignerUserId() {
int index = getColumnIndexOrThrow(KeychainContract.Certs.SIGNER_UID);
return getString(index);
}
public OpenPgpUtils.UserId getUserId() {
return KeyRing.splitUserId(getRawUserId());
}
public OpenPgpUtils.UserId getSignerUserId() {
return KeyRing.splitUserId(getRawSignerUserId());
}
}
public interface CertListListener {
void onClick(long masterKeyId, long signerKeyId, long rank);
}
}

View File

@@ -0,0 +1,42 @@
package org.sufficientlysecure.keychain.ui.adapter;
import android.content.Context;
import android.database.Cursor;
import android.support.v7.widget.RecyclerView;
import org.sufficientlysecure.keychain.ui.util.adapter.CursorAdapter;
public abstract class KeyCursorAdapter<C extends CursorAdapter.KeyCursor, VH extends RecyclerView.ViewHolder>
extends CursorAdapter<C, VH> {
private String mQuery;
public KeyCursorAdapter(Context context, C cursor){
super(context, cursor, 0);
}
public void setQuery(String query) {
mQuery = query;
}
@Override
public int getItemViewType(int position) {
moveCursorOrThrow(position);
return getItemViewType(getCursor());
}
@Override
public long getIdFromCursor(C keyCursor) {
return keyCursor.getKeyId();
}
@Override
public void onBindViewHolder(VH holder, int position) {
moveCursorOrThrow(position);
onBindViewHolder(holder, getCursor(), mQuery);
}
public int getItemViewType(C keyCursor) { return 0; }
public abstract void onBindViewHolder(VH holder, C cursor, String query);
}

View File

@@ -0,0 +1,268 @@
package org.sufficientlysecure.keychain.ui.adapter;
import android.content.Context;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Pair;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.tonicartos.superslim.LayoutManager;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.operations.results.OperationResult;
import org.sufficientlysecure.keychain.ui.util.FormattingUtils;
import java.util.ArrayList;
import java.util.List;
public class NestedLogAdapter extends RecyclerView.Adapter<NestedLogAdapter.LogEntryViewHolder> {
private static final int ENTRY_TYPE_REGULAR = 0;
private static final int ENTRY_TYPE_SUBLOG = 1;
private static final int LOG_ENTRY_ITEM_INDENT = 2;
private final int mIndentFactor;
private LogActionListener mListener;
private List<Pair<OperationResult.LogEntryParcel, Integer>> mLogEntries;
public NestedLogAdapter(Context context) {
super();
mIndentFactor = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
(float) 8, context.getResources().getDisplayMetrics());
}
public NestedLogAdapter(Context context, OperationResult.OperationLog log) {
this(context);
setLog(log);
}
public void setListener(LogActionListener listener) {
mListener = listener;
}
public void setLog(OperationResult.OperationLog log) {
List<OperationResult.LogEntryParcel> list = log.toList();
if (mLogEntries != null) {
mLogEntries.clear();
} else {
mLogEntries = new ArrayList<>(list.size());
}
int lastSection = 0;
for (int i = 0; i < list.size(); i++) {
OperationResult.LogEntryParcel parcel = list.get(i);
if(parcel.mIndent < LOG_ENTRY_ITEM_INDENT) {
lastSection = i;
}
mLogEntries.add(new Pair<>(parcel, lastSection));
}
notifyDataSetChanged();
}
@Override
public int getItemCount() {
return mLogEntries != null ? mLogEntries.size() : 0;
}
@Override
public long getItemId(int position) {
OperationResult.LogEntryParcel parcel = getItem(position);
return parcel != null ? parcel.hashCode() : -1L;
}
public OperationResult.LogEntryParcel getItem(int position) {
return mLogEntries != null ?
mLogEntries.get(position).first : null;
}
public int getFirstSectionPosition(int position) {
return mLogEntries != null ?
mLogEntries.get(position).second : 0;
}
@Override
public int getItemViewType(int position) {
return (getItem(position) instanceof OperationResult.SubLogEntryParcel) ?
ENTRY_TYPE_SUBLOG : ENTRY_TYPE_REGULAR;
}
public boolean isSection(int position) {
return mLogEntries != null && mLogEntries.get(position).second == position;
}
@Override
public LogEntryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case ENTRY_TYPE_SUBLOG:
return new SublogEntryViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.log_display_sublog_item, parent, false));
case ENTRY_TYPE_REGULAR:
return new LogEntryViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.log_display_regular_item, parent, false));
default:
return null;
}
}
@Override
public void onBindViewHolder(LogEntryViewHolder holder, int position) {
LayoutManager.LayoutParams layoutParams = LayoutManager.LayoutParams
.from(holder.itemView.getLayoutParams());
layoutParams.isHeader = isSection(position);
layoutParams.setFirstPosition(getFirstSectionPosition(position));
holder.bind(getItem(position), mIndentFactor);
holder.itemView.setLayoutParams(layoutParams);
}
class LogEntryViewHolder extends RecyclerView.ViewHolder {
private TextView mLogText;
private ImageView mLogImg;
public LogEntryViewHolder(View itemView) {
super(itemView);
mLogText = (TextView) itemView.findViewById(R.id.log_text);
mLogImg = (ImageView) itemView.findViewById(R.id.log_img);
}
public void bind(OperationResult.LogEntryParcel entry, int indentFactor) {
String logText;
if (entry.mParameters != null
&& entry.mParameters.length > 0
&& entry.mParameters[0] instanceof Integer) {
logText = itemView.getResources().getQuantityString(entry.mType.getMsgId(),
(int) entry.mParameters[0], entry.mParameters);
} else {
logText = itemView.getResources().getString(entry.mType.getMsgId(),
entry.mParameters);
}
int textColor, indicatorColor;
textColor = indicatorColor = FormattingUtils.getColorFromAttr(
itemView.getContext(), R.attr.colorText);
switch (entry.mType.mLevel) {
case DEBUG:
textColor = Color.GRAY;
indicatorColor = Color.GRAY;
break;
case WARN:
indicatorColor = ContextCompat.getColor(itemView.getContext(),
R.color.android_orange_light);
break;
case ERROR:
indicatorColor = ContextCompat.getColor(itemView.getContext(),
R.color.android_red_light);
break;
case OK:
indicatorColor = ContextCompat.getColor(itemView.getContext(),
R.color.android_green_light);
break;
case CANCELLED:
indicatorColor = ContextCompat.getColor(itemView.getContext(),
R.color.android_red_light);
break;
}
mLogText.setText(logText);
mLogText.setTextColor(textColor);
mLogImg.setBackgroundColor(indicatorColor);
itemView.setPadding((entry.mIndent) * indentFactor, 0, 0, 0);
}
}
class SublogEntryViewHolder extends LogEntryViewHolder implements View.OnClickListener {
private TextView mSublogText;
private ImageView mSublogImg;
public SublogEntryViewHolder(View itemView) {
super(itemView);
itemView.setClickable(true);
itemView.setOnClickListener(this);
mSublogText = (TextView) itemView.findViewById(R.id.log_second_text);
mSublogImg = (ImageView) itemView.findViewById(R.id.log_second_img);
}
@Override
public void bind(OperationResult.LogEntryParcel entry, int indentFactor) {
super.bind(entry, indentFactor);
OperationResult.LogEntryParcel sublogEntry = ((OperationResult.SubLogEntryParcel) entry)
.getSubResult().getLog().getLast();
String logText;
if (sublogEntry.mParameters != null
&& sublogEntry.mParameters.length > 0
&& sublogEntry.mParameters[0] instanceof Integer) {
logText = itemView.getResources().getQuantityString(sublogEntry.mType.getMsgId(),
(int) sublogEntry.mParameters[0], sublogEntry.mParameters);
} else {
logText = itemView.getResources().getString(sublogEntry.mType.getMsgId(),
sublogEntry.mParameters);
}
int textColor, indicatorColor;
textColor = indicatorColor = FormattingUtils.getColorFromAttr(
itemView.getContext(), R.attr.colorText);
switch (sublogEntry.mType.mLevel) {
case DEBUG:
textColor = Color.GRAY;
indicatorColor = Color.GRAY;
break;
case WARN:
indicatorColor = ContextCompat.getColor(itemView.getContext(),
R.color.android_orange_light);
break;
case ERROR:
indicatorColor = ContextCompat.getColor(itemView.getContext(),
R.color.android_red_light);
break;
case OK:
indicatorColor = ContextCompat.getColor(itemView.getContext(),
R.color.android_green_light);
break;
case CANCELLED:
indicatorColor = ContextCompat.getColor(itemView.getContext(),
R.color.android_red_light);
break;
}
mSublogText.setText(logText);
mSublogText.setTextColor(textColor);
mSublogImg.setBackgroundColor(indicatorColor);
}
@Override
public void onClick(View v) {
if (mListener != null) {
OperationResult.LogEntryParcel parcel = getItem(getAdapterPosition());
if (parcel instanceof OperationResult.SubLogEntryParcel) {
mListener.onSubEntryClicked((OperationResult.SubLogEntryParcel) parcel);
}
}
}
}
public interface LogActionListener {
void onSubEntryClicked(OperationResult.SubLogEntryParcel subLogEntryParcel);
}
}

View File

@@ -166,7 +166,7 @@ abstract public class SelectKeyCursorAdapter extends CursorAdapter {
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = mInflater.inflate(R.layout.select_key_item, null);
View view = mInflater.inflate(R.layout.select_encrypt_key_item, null);
ViewHolderItem holder = new ViewHolderItem();
holder.view = view;
holder.mainUserId = (TextView) view.findViewById(R.id.select_key_item_name);

View File

@@ -1,6 +1,7 @@
package org.sufficientlysecure.keychain.ui.util.adapter;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.util.SparseArrayCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;
@@ -10,6 +11,10 @@ import com.tonicartos.superslim.LayoutManager;
import org.sufficientlysecure.keychain.util.Log;
import org.sufficientlysecure.keychain.util.Log;
import java.util.Objects;
/**
* @param <T> section type.
* @param <VH> the view holder extending {@code BaseViewHolder<Cursor>} that is bound to the cursor data.