show dummy item if there are no secret keys

This commit is contained in:
Vincent Breitmoser 2015-08-14 14:47:20 +02:00
parent 94683607d6
commit b2d447ab32
3 changed files with 121 additions and 4 deletions

View file

@ -29,6 +29,8 @@ import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.database.MergeCursor;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
@ -265,7 +267,6 @@ public class KeyListFragment extends LoaderFragment
static final String ORDER =
KeyRings.HAS_ANY_SECRET + " DESC, UPPER(" + KeyRings.USER_ID + ") ASC";
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// This is called when a new Loader needs to be created. This
@ -298,6 +299,22 @@ public class KeyListFragment extends LoaderFragment
// Swap the new cursor in. (The framework will take care of closing the
// old cursor once we return.)
mAdapter.setSearchQuery(mQuery);
if (data != null && data.moveToFirst()) {
boolean isSecret = data.getInt(KeyListAdapter.INDEX_HAS_ANY_SECRET) != 0;
if (!isSecret) {
MatrixCursor headerCursor = new MatrixCursor(KeyListAdapter.PROJECTION);
Long[] row = new Long[KeyListAdapter.PROJECTION.length];
row[KeyListAdapter.INDEX_HAS_ANY_SECRET] = 1L;
row[KeyListAdapter.INDEX_MASTER_KEY_ID] = 0L;
headerCursor.addRow(row);
Cursor dataCursor = data;
data = new MergeCursor(new Cursor[] {
headerCursor, dataCursor
});
}
}
mAdapter.swapCursor(data);
mStickyList.setAdapter(mAdapter);
@ -722,6 +739,29 @@ public class KeyListFragment extends LoaderFragment
return v;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
boolean isSecret = cursor.getInt(INDEX_HAS_ANY_SECRET) != 0;
long masterKeyId = cursor.getLong(INDEX_MASTER_KEY_ID);
if (isSecret && masterKeyId == 0L) {
// sort of a hack: if this item isn't enabled, we make it clickable
// to intercept its click events
view.setClickable(true);
KeyItemViewHolder h = (KeyItemViewHolder) view.getTag();
h.setDummy(new OnClickListener() {
@Override
public void onClick(View v) {
createKey();
}
});
return;
}
super.bindView(view, context, cursor);
}
private class HeaderViewHolder {
TextView mText;
TextView mCount;
@ -760,6 +800,10 @@ public class KeyListFragment extends LoaderFragment
if (mCursor.getInt(INDEX_HAS_ANY_SECRET) != 0) {
{ // set contact count
int num = mCursor.getCount();
// If this is a dummy secret key, subtract one
if (mCursor.getLong(INDEX_MASTER_KEY_ID) == 0L) {
num -= 1;
}
String contactsTotal = mContext.getResources().getQuantityString(R.plurals.n_keys, num, num);
holder.mCount.setText(contactsTotal);
holder.mCount.setVisibility(View.VISIBLE);
@ -818,8 +862,9 @@ public class KeyListFragment extends LoaderFragment
public boolean isAnySecretSelected() {
for (int pos : mSelection.keySet()) {
if (isSecretAvailable(pos))
if (isSecretAvailable(pos)) {
return true;
}
}
return false;
}

View file

@ -31,6 +31,7 @@ import android.support.v4.widget.CursorAdapter;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
@ -89,6 +90,8 @@ public class KeyAdapter extends CursorAdapter {
public static class KeyItemViewHolder {
public View mView;
public View mLayoutDummy;
public View mLayoutData;
public Long mMasterKeyId;
public TextView mMainUserId;
public TextView mMainUserIdRest;
@ -101,6 +104,8 @@ public class KeyAdapter extends CursorAdapter {
public KeyItemViewHolder(View view) {
mView = view;
mLayoutData = view.findViewById(R.id.key_list_item_data);
mLayoutDummy = view.findViewById(R.id.key_list_item_dummy);
mMainUserId = (TextView) view.findViewById(R.id.key_list_item_name);
mMainUserIdRest = (TextView) view.findViewById(R.id.key_list_item_email);
mStatus = (ImageView) view.findViewById(R.id.key_list_item_status_icon);
@ -111,6 +116,9 @@ public class KeyAdapter extends CursorAdapter {
public void setData(Context context, KeyItem item, Highlighter highlighter, boolean enabled) {
mLayoutData.setVisibility(View.VISIBLE);
mLayoutDummy.setVisibility(View.GONE);
mDisplayedItem = item;
{ // set name and stuff, common to both key types
@ -129,7 +137,7 @@ public class KeyAdapter extends CursorAdapter {
}
// sort of a hack: if this item isn't enabled, we make it clickable
// to intercept its click events
// to intercept its click events. either way, no listener!
mView.setClickable(!enabled);
{ // set edit button and status, specific by key type
@ -200,6 +208,20 @@ public class KeyAdapter extends CursorAdapter {
}
/** Shows the "you have no keys yet" dummy view, and sets an OnClickListener. */
public void setDummy(OnClickListener listener) {
// just reset everything to display the dummy layout
mLayoutDummy.setVisibility(View.VISIBLE);
mLayoutData.setVisibility(View.GONE);
mSlinger.setVisibility(View.GONE);
mStatus.setVisibility(View.GONE);
mView.setClickable(false);
mLayoutDummy.setOnClickListener(listener);
}
}
public boolean isEnabled(Cursor cursor) {

View file

@ -11,6 +11,55 @@
android:focusable="false">
<LinearLayout
android:id="@+id/key_list_item_dummy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:focusable="true"
android:visibility="gone"
android:background="?android:selectableItemBackground"
tools:visibility="visible">
<LinearLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:orientation="vertical"
android:paddingLeft="8dp"
android:paddingRight="4dp"
android:paddingTop="4dp"
android:paddingBottom="4dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="You don't have any keys yet!"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="Click here to create or import one."
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="16dp"
android:src="@drawable/ic_key_plus_grey600_24dp"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/key_list_item_data"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
@ -20,7 +69,8 @@
android:paddingLeft="8dp"
android:paddingRight="4dp"
android:paddingTop="4dp"
android:paddingBottom="4dp">
android:paddingBottom="4dp"
tools:visibility="gone">
<TextView
android:id="@+id/key_list_item_name"