ImportKeys: Use data binding for extra informations about the key and refactoring

This commit is contained in:
Andrea Torlaschi 2016-08-04 15:29:33 +02:00
parent 6ecc53d0a6
commit efa651284e
7 changed files with 159 additions and 123 deletions

View file

@ -166,9 +166,6 @@ public class ImportKeysAdapter extends RecyclerView.Adapter<ImportKeysAdapter.Vi
b.expand.setRotation(rotation);
}
if (showed) {
b.extraUserIds.setEntry(entry);
}
b.extraContainer.setVisibility(showed ? View.VISIBLE : View.GONE);
}

View file

@ -1,4 +1,4 @@
package org.sufficientlysecure.keychain.ui.adapter;
package org.sufficientlysecure.keychain.ui.bindings;
import android.content.Context;
import android.content.res.Resources;
@ -8,12 +8,10 @@ import android.widget.ImageView;
import android.widget.TextView;
import org.sufficientlysecure.keychain.R;
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.util.LruCache;
public class ImportKeysAdapterBinding {
public class ImportKeysBindings {
@BindingAdapter({"app:keyUserId", "app:keySecret", "app:keyRevokedOrExpired", "app:query"})
public static void setUserId(TextView textView, CharSequence userId, boolean secret,
@ -28,11 +26,11 @@ public class ImportKeysAdapterBinding {
if (secret) {
userId = resources.getString(R.string.secret_key) + " " + userId;
} else {
Highlighter highlighter = getHighlighter(context, query);
Highlighter highlighter = ImportKeysBindingsUtils.getHighlighter(context, query);
userId = highlighter.highlight(userId);
}
textView.setText(userId);
textView.setTextColor(getColor(context, revokedOrExpired));
textView.setTextColor(ImportKeysBindingsUtils.getColor(context, revokedOrExpired));
if (secret) {
textView.setTextColor(Color.RED);
@ -48,28 +46,9 @@ public class ImportKeysAdapterBinding {
if (userEmail == null)
userEmail = "";
Highlighter highlighter = getHighlighter(context, query);
Highlighter highlighter = ImportKeysBindingsUtils.getHighlighter(context, query);
textView.setText(highlighter.highlight(userEmail));
textView.setTextColor(getColor(context, revokedOrExpired));
}
@BindingAdapter({"app:keyId", "app:keyRevokedOrExpired"})
public static void setKeyId(TextView textView, String keyId, boolean revokedOrExpired) {
Context context = textView.getContext();
if (keyId == null)
keyId = "";
textView.setText(KeyFormattingUtils.beautifyKeyIdWithPrefix(keyId));
textView.setTextColor(getColor(context, revokedOrExpired));
}
private static int getColor(Context context, boolean revokedOrExpired) {
if (revokedOrExpired) {
return context.getResources().getColor(R.color.key_flag_gray);
} else {
return FormattingUtils.getColorFromAttr(context, R.attr.colorText);
}
textView.setTextColor(ImportKeysBindingsUtils.getColor(context, revokedOrExpired));
}
@BindingAdapter({"app:keyRevoked", "app:keyExpired"})
@ -85,16 +64,4 @@ public class ImportKeysAdapterBinding {
}
}
private static LruCache<String, Highlighter> highlighterCache = new LruCache<>(1);
private static Highlighter getHighlighter(Context context, String query) {
Highlighter highlighter = highlighterCache.get(query);
if (highlighter == null) {
highlighter = new Highlighter(context, query);
highlighterCache.put(query, highlighter);
}
return highlighter;
}
}

View file

@ -0,0 +1,32 @@
package org.sufficientlysecure.keychain.ui.bindings;
import android.content.Context;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.ui.util.FormattingUtils;
import org.sufficientlysecure.keychain.ui.util.Highlighter;
import org.sufficientlysecure.keychain.util.LruCache;
public class ImportKeysBindingsUtils {
private static LruCache<String, Highlighter> highlighterCache = new LruCache<>(1);
public static Highlighter getHighlighter(Context context, String query) {
Highlighter highlighter = highlighterCache.get(query);
if (highlighter == null) {
highlighter = new Highlighter(context, query);
highlighterCache.put(query, highlighter);
}
return highlighter;
}
public static int getColor(Context context, boolean revokedOrExpired) {
if (revokedOrExpired) {
return context.getResources().getColor(R.color.key_flag_gray);
} else {
return FormattingUtils.getColorFromAttr(context, R.attr.colorText);
}
}
}

View file

@ -0,0 +1,79 @@
package org.sufficientlysecure.keychain.ui.bindings;
import android.content.Context;
import android.databinding.BindingAdapter;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.ui.util.FormattingUtils;
import org.sufficientlysecure.keychain.ui.util.Highlighter;
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Map;
public class ImportKeysExtraBindings {
@BindingAdapter({"app:keyId", "app:keyRevokedOrExpired"})
public static void setKeyId(TextView textView, String keyId, boolean revokedOrExpired) {
Context context = textView.getContext();
if (keyId == null)
keyId = "";
textView.setText(KeyFormattingUtils.beautifyKeyIdWithPrefix(keyId));
textView.setTextColor(ImportKeysBindingsUtils.getColor(context, revokedOrExpired));
}
@BindingAdapter({"app:keyUserIds", "app:keyRevokedOrExpired", "app:query"})
public static void setUserIds(LinearLayout linearLayout, ArrayList userIds,
boolean revokedOrExpired, String query) {
linearLayout.removeAllViews();
if (userIds != null) {
Context context = linearLayout.getContext();
Highlighter highlighter = ImportKeysBindingsUtils.getHighlighter(context, query);
ArrayList<Map.Entry<String, HashSet<String>>> uIds = userIds;
for (Map.Entry<String, HashSet<String>> pair : uIds) {
String name = pair.getKey();
HashSet<String> emails = pair.getValue();
LayoutInflater inflater = LayoutInflater.from(context);
TextView uidView = (TextView) inflater.inflate(
R.layout.import_keys_list_entry_user_id, null);
uidView.setText(highlighter.highlight(name));
uidView.setPadding(0, 0, FormattingUtils.dpToPx(context, 8), 0);
if (revokedOrExpired) {
uidView.setTextColor(context.getResources().getColor(R.color.key_flag_gray));
} else {
uidView.setTextColor(FormattingUtils.getColorFromAttr(context, R.attr.colorText));
}
linearLayout.addView(uidView);
for (String email : emails) {
TextView emailView = (TextView) inflater.inflate(
R.layout.import_keys_list_entry_user_id, null);
emailView.setPadding(
FormattingUtils.dpToPx(context, 16), 0,
FormattingUtils.dpToPx(context, 8), 0);
emailView.setText(highlighter.highlight(email));
if (revokedOrExpired) {
emailView.setTextColor(context.getResources().getColor(R.color.key_flag_gray));
} else {
emailView.setTextColor(FormattingUtils.getColorFromAttr(context, R.attr.colorText));
}
linearLayout.addView(emailView);
}
}
}
}
}

View file

@ -1,69 +0,0 @@
package org.sufficientlysecure.keychain.ui.widget;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.keyimport.ImportKeysListEntry;
import org.sufficientlysecure.keychain.ui.util.FormattingUtils;
import org.sufficientlysecure.keychain.ui.util.Highlighter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Map;
public class ImportUserIdsView extends LinearLayout {
public ImportUserIdsView(Context context, AttributeSet attrs) {
super(context, attrs);
setOrientation(VERTICAL);
}
public void setEntry(ImportKeysListEntry entry) {
removeAllViews();
Context context = getContext();
Highlighter highlighter = new Highlighter(context, entry.getQuery());
// we want conventional gpg UserIDs first, then Keybase proofs
ArrayList<Map.Entry<String, HashSet<String>>> sortedIds = entry.getSortedUserIds();
for (Map.Entry<String, HashSet<String>> pair : sortedIds) {
String cUserId = pair.getKey();
HashSet<String> cEmails = pair.getValue();
LayoutInflater inflater = LayoutInflater.from(context);
TextView uidView = (TextView) inflater.inflate(
R.layout.import_keys_list_entry_user_id, null);
uidView.setText(highlighter.highlight(cUserId));
uidView.setPadding(0, 0, FormattingUtils.dpToPx(context, 8), 0);
if (entry.isRevokedOrExpired()) {
uidView.setTextColor(context.getResources().getColor(R.color.key_flag_gray));
} else {
uidView.setTextColor(FormattingUtils.getColorFromAttr(context, R.attr.colorText));
}
addView(uidView);
for (String email : cEmails) {
TextView emailView = (TextView) inflater.inflate(
R.layout.import_keys_list_entry_user_id, null);
emailView.setPadding(
FormattingUtils.dpToPx(context, 16), 0,
FormattingUtils.dpToPx(context, 8), 0);
emailView.setText(highlighter.highlight(email));
if (entry.isRevokedOrExpired()) {
emailView.setTextColor(context.getResources().getColor(R.color.key_flag_gray));
} else {
emailView.setTextColor(FormattingUtils.getColorFromAttr(context, R.attr.colorText));
}
addView(emailView);
}
}
}
}

View file

@ -6,7 +6,6 @@
<import type="org.sufficientlysecure.keychain.keyimport.ImportKeysListEntry" />
<variable name="nonInteractive" type="boolean" />
<variable name="entry" type="ImportKeysListEntry" />
</data>
@ -67,13 +66,6 @@
app:keyUserEmail="@{entry.primaryUserId.email}"
app:query="@{entry.query}" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
app:keyId="@{entry.keyIdHex}"
app:keyRevokedOrExpired="@{entry.revokedOrExpired}" />
</LinearLayout>
<ImageView
@ -133,10 +125,12 @@
android:paddingTop="16dp"
android:visibility="gone">
<org.sufficientlysecure.keychain.ui.widget.ImportUserIdsView
android:id="@+id/extra_user_ids"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<include
layout="@layout/import_keys_list_item_extra"
app:keyIdHex="@{entry.keyIdHex}"
app:keyUserIds="@{entry.sortedUserIds}"
app:query="@{entry.query}"
app:revokedOrExpired="@{entry.revokedOrExpired}" />
</LinearLayout>

View file

@ -0,0 +1,36 @@
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import type="java.util.ArrayList" />
<variable name="keyIdHex" type="String" />
<variable name="keyUserIds" type="ArrayList" />
<variable name="query" type="String" />
<variable name="revokedOrExpired" type="boolean" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
app:keyId="@{keyIdHex}"
app:keyRevokedOrExpired="@{revokedOrExpired}" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:orientation="vertical"
app:keyRevokedOrExpired="@{revokedOrExpired}"
app:keyUserIds="@{keyUserIds}"
app:query="@{query}" />
</LinearLayout>
</layout>