move formatting of key items to new class
This commit is contained in:
committed by
Vincent Breitmoser
parent
76e624f12f
commit
f8eb6275f5
@@ -1,13 +1,9 @@
|
||||
package org.sufficientlysecure.keychain.ui.adapter;
|
||||
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
import android.text.format.DateUtils;
|
||||
import android.view.View;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
@@ -16,14 +12,12 @@ import eu.davidea.flexibleadapter.FlexibleAdapter;
|
||||
import eu.davidea.flexibleadapter.items.IFilterable;
|
||||
import eu.davidea.flexibleadapter.items.IFlexible;
|
||||
import eu.davidea.viewholders.FlexibleViewHolder;
|
||||
|
||||
import org.sufficientlysecure.keychain.R;
|
||||
import org.sufficientlysecure.keychain.model.SubKey.UnifiedKeyInfo;
|
||||
import org.sufficientlysecure.keychain.ui.adapter.FlexibleKeyDetailsItem.FlexibleKeyItemViewHolder;
|
||||
import org.sufficientlysecure.keychain.ui.adapter.FlexibleKeyItem.FlexibleSectionableKeyItem;
|
||||
import org.sufficientlysecure.keychain.ui.util.FormattingUtils;
|
||||
import org.sufficientlysecure.keychain.ui.util.Highlighter;
|
||||
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
|
||||
import org.sufficientlysecure.keychain.ui.util.PackageIconGetter;
|
||||
import org.sufficientlysecure.keychain.ui.util.KeyInfoFormatter;
|
||||
|
||||
|
||||
public class FlexibleKeyDetailsItem extends FlexibleSectionableKeyItem<FlexibleKeyItemViewHolder>
|
||||
@@ -77,8 +71,6 @@ public class FlexibleKeyDetailsItem extends FlexibleSectionableKeyItem<FlexibleK
|
||||
}
|
||||
|
||||
class FlexibleKeyItemViewHolder extends FlexibleViewHolder {
|
||||
private static final long JUST_NOW_THRESHOLD = DateUtils.MINUTE_IN_MILLIS * 5;
|
||||
|
||||
private final TextView vMainUserId;
|
||||
private final TextView vMainUserIdRest;
|
||||
private final TextView vCreationDate;
|
||||
@@ -98,140 +90,14 @@ public class FlexibleKeyDetailsItem extends FlexibleSectionableKeyItem<FlexibleK
|
||||
public void bind(UnifiedKeyInfo keyInfo, String highlightString) {
|
||||
setEnabled(true);
|
||||
|
||||
Context context = itemView.getContext();
|
||||
Highlighter highlighter = new Highlighter(context, highlightString);
|
||||
|
||||
{ // set name and stuff, common to both key types
|
||||
if (keyInfo.name() == null) {
|
||||
if (keyInfo.email() != null) {
|
||||
vMainUserId.setText(highlighter.highlight(keyInfo.email()));
|
||||
vMainUserIdRest.setVisibility(View.GONE);
|
||||
} else {
|
||||
vMainUserId.setText(R.string.user_id_no_name);
|
||||
}
|
||||
} else {
|
||||
vMainUserId.setText(highlighter.highlight(keyInfo.name()));
|
||||
// for some reason, this hangs for me
|
||||
// FlexibleUtils.highlightText(vMainUserId, keyInfo.name(), highlightString);
|
||||
if (keyInfo.email() != null) {
|
||||
vMainUserIdRest.setText(highlighter.highlight(keyInfo.email()));
|
||||
vMainUserIdRest.setVisibility(View.VISIBLE);
|
||||
} else {
|
||||
vMainUserIdRest.setVisibility(View.GONE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{ // set edit button and status, specific by key type. Note: order is important!
|
||||
int textColor;
|
||||
if (keyInfo.is_revoked()) {
|
||||
KeyFormattingUtils.setStatusImage(
|
||||
context,
|
||||
vStatusIcon,
|
||||
null,
|
||||
KeyFormattingUtils.State.REVOKED,
|
||||
R.color.key_flag_gray
|
||||
);
|
||||
|
||||
vStatusIcon.setVisibility(View.VISIBLE);
|
||||
textColor = ContextCompat.getColor(context, R.color.key_flag_gray);
|
||||
} else if (keyInfo.is_expired()) {
|
||||
KeyFormattingUtils.setStatusImage(
|
||||
context,
|
||||
vStatusIcon,
|
||||
null,
|
||||
KeyFormattingUtils.State.EXPIRED,
|
||||
R.color.key_flag_gray
|
||||
);
|
||||
|
||||
vStatusIcon.setVisibility(View.VISIBLE);
|
||||
textColor = ContextCompat.getColor(context, R.color.key_flag_gray);
|
||||
} else if (!keyInfo.is_secure()) {
|
||||
KeyFormattingUtils.setStatusImage(
|
||||
context,
|
||||
vStatusIcon,
|
||||
null,
|
||||
KeyFormattingUtils.State.INSECURE,
|
||||
R.color.key_flag_gray
|
||||
);
|
||||
|
||||
vStatusIcon.setVisibility(View.VISIBLE);
|
||||
textColor = ContextCompat.getColor(context, R.color.key_flag_gray);
|
||||
} else if (keyInfo.has_any_secret()) {
|
||||
vStatusIcon.setVisibility(View.GONE);
|
||||
textColor = FormattingUtils.getColorFromAttr(context, R.attr.colorText);
|
||||
} else {
|
||||
// this is a public key - show if it's verified
|
||||
if (keyInfo.is_verified()) {
|
||||
KeyFormattingUtils.setStatusImage(
|
||||
context,
|
||||
vStatusIcon,
|
||||
KeyFormattingUtils.State.VERIFIED
|
||||
);
|
||||
|
||||
vStatusIcon.setVisibility(View.VISIBLE);
|
||||
} else {
|
||||
KeyFormattingUtils.setStatusImage(
|
||||
context,
|
||||
vStatusIcon,
|
||||
KeyFormattingUtils.State.UNVERIFIED
|
||||
);
|
||||
|
||||
vStatusIcon.setVisibility(View.VISIBLE);
|
||||
}
|
||||
textColor = FormattingUtils.getColorFromAttr(context, R.attr.colorText);
|
||||
}
|
||||
|
||||
vMainUserId.setTextColor(textColor);
|
||||
vMainUserIdRest.setTextColor(textColor);
|
||||
|
||||
if (keyInfo.has_duplicate() || keyInfo.has_any_secret()) {
|
||||
vCreationDate.setText(getSecretKeyReadableTime(context, keyInfo));
|
||||
vCreationDate.setTextColor(textColor);
|
||||
vCreationDate.setVisibility(View.VISIBLE);
|
||||
} else {
|
||||
vCreationDate.setVisibility(View.GONE);
|
||||
}
|
||||
}
|
||||
|
||||
{ // set icons
|
||||
|
||||
if (!keyInfo.has_any_secret() && !keyInfo.autocrypt_package_names().isEmpty()) {
|
||||
String packageName = keyInfo.autocrypt_package_names().get(0);
|
||||
Drawable drawable = PackageIconGetter.getInstance(context).getDrawableForPackageName(packageName);
|
||||
if (drawable != null) {
|
||||
vTrustIdIcon.setImageDrawable(drawable);
|
||||
vTrustIdIcon.setVisibility(View.VISIBLE);
|
||||
} else {
|
||||
vTrustIdIcon.setVisibility(View.GONE);
|
||||
}
|
||||
} else {
|
||||
vTrustIdIcon.setVisibility(View.GONE);
|
||||
}
|
||||
}
|
||||
KeyInfoFormatter keyInfoFormatter = new KeyInfoFormatter(itemView.getContext(), keyInfo, highlightString);
|
||||
keyInfoFormatter.formatUserId(vMainUserId, vMainUserIdRest);
|
||||
keyInfoFormatter.formatCreationDate(vCreationDate);
|
||||
keyInfoFormatter.greyInvalidKeys(Arrays.asList(vMainUserId, vMainUserIdRest, vCreationDate));
|
||||
keyInfoFormatter.formatStatusIcon(vStatusIcon);
|
||||
keyInfoFormatter.formatTrustIcon(vTrustIdIcon);
|
||||
}
|
||||
|
||||
|
||||
@NonNull
|
||||
private String getSecretKeyReadableTime(Context context, UnifiedKeyInfo keyInfo) {
|
||||
long creationMillis = keyInfo.creation() * 1000;
|
||||
|
||||
boolean allowRelativeTimestamp = keyInfo.has_duplicate();
|
||||
if (allowRelativeTimestamp) {
|
||||
long creationAgeMillis = System.currentTimeMillis() - creationMillis;
|
||||
if (creationAgeMillis < JUST_NOW_THRESHOLD) {
|
||||
return context.getString(R.string.label_key_created_just_now);
|
||||
}
|
||||
}
|
||||
|
||||
String dateTime = DateUtils.formatDateTime(context,
|
||||
creationMillis,
|
||||
DateUtils.FORMAT_SHOW_DATE
|
||||
| DateUtils.FORMAT_SHOW_TIME
|
||||
| DateUtils.FORMAT_SHOW_YEAR
|
||||
| DateUtils.FORMAT_ABBREV_MONTH);
|
||||
return context.getString(R.string.label_key_created, dateTime);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,167 @@
|
||||
package org.sufficientlysecure.keychain.ui.util;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
import android.text.format.DateUtils;
|
||||
import android.view.View;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.sufficientlysecure.keychain.R;
|
||||
import org.sufficientlysecure.keychain.model.SubKey;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class KeyInfoFormatter {
|
||||
|
||||
private static final long JUST_NOW_THRESHOLD = DateUtils.MINUTE_IN_MILLIS * 5;
|
||||
|
||||
private Context context;
|
||||
private SubKey.UnifiedKeyInfo keyInfo;
|
||||
private Highlighter highlighter;
|
||||
|
||||
public KeyInfoFormatter(Context context, SubKey.UnifiedKeyInfo keyInfo, String highlightString) {
|
||||
this.context = context;
|
||||
this.keyInfo = keyInfo;
|
||||
highlighter = new Highlighter(context, highlightString);
|
||||
}
|
||||
|
||||
public void formatUserId(TextView name, TextView email) {
|
||||
if (keyInfo.name() == null) {
|
||||
if (keyInfo.email() != null) {
|
||||
name.setText(highlighter.highlight(keyInfo.email()));
|
||||
email.setVisibility(View.GONE);
|
||||
} else {
|
||||
name.setText(R.string.user_id_no_name);
|
||||
}
|
||||
} else {
|
||||
name.setText(highlighter.highlight(keyInfo.name()));
|
||||
if (keyInfo.email() != null) {
|
||||
email.setText(highlighter.highlight(keyInfo.email()));
|
||||
email.setVisibility(View.VISIBLE);
|
||||
} else {
|
||||
email.setVisibility(View.GONE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void formatCreationDate(TextView creationDate) {
|
||||
if (keyInfo.has_duplicate() || keyInfo.has_any_secret()) {
|
||||
creationDate.setText(getSecretKeyReadableTime(context, keyInfo));
|
||||
creationDate.setVisibility(View.VISIBLE);
|
||||
} else {
|
||||
creationDate.setVisibility(View.GONE);
|
||||
}
|
||||
}
|
||||
|
||||
public void greyInvalidKeys(List<TextView> textviews) {
|
||||
int textColor;
|
||||
if (keyInfo.is_revoked()) {
|
||||
textColor = ContextCompat.getColor(context, R.color.key_flag_gray);
|
||||
} else if (keyInfo.is_expired()) {
|
||||
textColor = ContextCompat.getColor(context, R.color.key_flag_gray);
|
||||
} else if (!keyInfo.is_secure()) {
|
||||
textColor = ContextCompat.getColor(context, R.color.key_flag_gray);
|
||||
} else if (keyInfo.has_any_secret()) {
|
||||
textColor = FormattingUtils.getColorFromAttr(context, R.attr.colorText);
|
||||
} else {
|
||||
textColor = FormattingUtils.getColorFromAttr(context, R.attr.colorText);
|
||||
}
|
||||
for (TextView textView : textviews) {
|
||||
textView.setTextColor(textColor);
|
||||
}
|
||||
}
|
||||
|
||||
public void formatStatusIcon(ImageView statusIcon) {
|
||||
if (keyInfo.is_revoked()) {
|
||||
KeyFormattingUtils.setStatusImage(
|
||||
context,
|
||||
statusIcon,
|
||||
null,
|
||||
KeyFormattingUtils.State.REVOKED,
|
||||
R.color.key_flag_gray
|
||||
);
|
||||
|
||||
statusIcon.setVisibility(View.VISIBLE);
|
||||
} else if (keyInfo.is_expired()) {
|
||||
KeyFormattingUtils.setStatusImage(
|
||||
context,
|
||||
statusIcon,
|
||||
null,
|
||||
KeyFormattingUtils.State.EXPIRED,
|
||||
R.color.key_flag_gray
|
||||
);
|
||||
|
||||
statusIcon.setVisibility(View.VISIBLE);
|
||||
} else if (!keyInfo.is_secure()) {
|
||||
KeyFormattingUtils.setStatusImage(
|
||||
context,
|
||||
statusIcon,
|
||||
null,
|
||||
KeyFormattingUtils.State.INSECURE,
|
||||
R.color.key_flag_gray
|
||||
);
|
||||
|
||||
statusIcon.setVisibility(View.VISIBLE);
|
||||
} else if (keyInfo.has_any_secret()) {
|
||||
statusIcon.setVisibility(View.GONE);
|
||||
} else {
|
||||
// this is a public key - show if it's verified
|
||||
if (keyInfo.is_verified()) {
|
||||
KeyFormattingUtils.setStatusImage(
|
||||
context,
|
||||
statusIcon,
|
||||
KeyFormattingUtils.State.VERIFIED
|
||||
);
|
||||
|
||||
statusIcon.setVisibility(View.VISIBLE);
|
||||
} else {
|
||||
KeyFormattingUtils.setStatusImage(
|
||||
context,
|
||||
statusIcon,
|
||||
KeyFormattingUtils.State.UNVERIFIED
|
||||
);
|
||||
|
||||
statusIcon.setVisibility(View.VISIBLE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void formatTrustIcon(ImageView trustIdIcon) {
|
||||
if (!keyInfo.has_any_secret() && !keyInfo.autocrypt_package_names().isEmpty()) {
|
||||
String packageName = keyInfo.autocrypt_package_names().get(0);
|
||||
Drawable drawable = PackageIconGetter.getInstance(context).getDrawableForPackageName(packageName);
|
||||
if (drawable != null) {
|
||||
trustIdIcon.setImageDrawable(drawable);
|
||||
trustIdIcon.setVisibility(View.VISIBLE);
|
||||
} else {
|
||||
trustIdIcon.setVisibility(View.GONE);
|
||||
}
|
||||
} else {
|
||||
trustIdIcon.setVisibility(View.GONE);
|
||||
}
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private String getSecretKeyReadableTime(Context context, SubKey.UnifiedKeyInfo keyInfo) {
|
||||
long creationMillis = keyInfo.creation() * 1000;
|
||||
|
||||
boolean allowRelativeTimestamp = keyInfo.has_duplicate();
|
||||
if (allowRelativeTimestamp) {
|
||||
long creationAgeMillis = System.currentTimeMillis() - creationMillis;
|
||||
if (creationAgeMillis < JUST_NOW_THRESHOLD) {
|
||||
return context.getString(R.string.label_key_created_just_now);
|
||||
}
|
||||
}
|
||||
|
||||
String dateTime = DateUtils.formatDateTime(context,
|
||||
creationMillis,
|
||||
DateUtils.FORMAT_SHOW_DATE
|
||||
| DateUtils.FORMAT_SHOW_TIME
|
||||
| DateUtils.FORMAT_SHOW_YEAR
|
||||
| DateUtils.FORMAT_ABBREV_MONTH);
|
||||
return context.getString(R.string.label_key_created, dateTime);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user