code cleanup in Contact class

This commit is contained in:
Daniel Gultsch 2018-03-23 16:59:42 +01:00
parent ce00767777
commit acdf822415
1 changed files with 30 additions and 26 deletions

View File

@ -5,6 +5,7 @@ import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.provider.ContactsContract; import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.text.TextUtils; import android.text.TextUtils;
import org.json.JSONArray; import org.json.JSONArray;
@ -37,18 +38,18 @@ public class Contact implements ListItem, Blockable {
public static final String LAST_PRESENCE = "last_presence"; public static final String LAST_PRESENCE = "last_presence";
public static final String LAST_TIME = "last_time"; public static final String LAST_TIME = "last_time";
public static final String GROUPS = "groups"; public static final String GROUPS = "groups";
protected String accountUuid; private String accountUuid;
protected String systemName; private String systemName;
protected String serverName; private String serverName;
protected String presenceName; private String presenceName;
protected String commonName; private String commonName;
protected Jid jid; protected Jid jid;
protected int subscription = 0; private int subscription = 0;
protected String systemAccount; private String systemAccount;
protected String photoUri; private String photoUri;
protected JSONObject keys = new JSONObject(); private final JSONObject keys;
protected JSONArray groups = new JSONArray(); private JSONArray groups = new JSONArray();
protected final Presences presences = new Presences(); private final Presences presences = new Presences();
protected Account account; protected Account account;
protected Avatar avatar; protected Avatar avatar;
@ -57,9 +58,9 @@ public class Contact implements ListItem, Blockable {
private String mLastPresence = null; private String mLastPresence = null;
public Contact(final String account, final String systemName, final String serverName, public Contact(final String account, final String systemName, final String serverName,
final Jid jid, final int subscription, final String photoUri, final Jid jid, final int subscription, final String photoUri,
final String systemAccount, final String keys, final String avatar, final long lastseen, final String systemAccount, final String keys, final String avatar, final long lastseen,
final String presence, final String groups) { final String presence, final String groups) {
this.accountUuid = account; this.accountUuid = account;
this.systemName = systemName; this.systemName = systemName;
this.serverName = serverName; this.serverName = serverName;
@ -67,11 +68,13 @@ public class Contact implements ListItem, Blockable {
this.subscription = subscription; this.subscription = subscription;
this.photoUri = photoUri; this.photoUri = photoUri;
this.systemAccount = systemAccount; this.systemAccount = systemAccount;
JSONObject tmpJsonObject;
try { try {
this.keys = (keys == null ? new JSONObject("") : new JSONObject(keys)); tmpJsonObject = (keys == null ? new JSONObject("") : new JSONObject(keys));
} catch (JSONException e) { } catch (JSONException e) {
this.keys = new JSONObject(); tmpJsonObject = new JSONObject();
} }
this.keys = tmpJsonObject;
if (avatar != null) { if (avatar != null) {
this.avatar = new Avatar(); this.avatar = new Avatar();
this.avatar.sha1sum = avatar; this.avatar.sha1sum = avatar;
@ -88,6 +91,7 @@ public class Contact implements ListItem, Blockable {
public Contact(final Jid jid) { public Contact(final Jid jid) {
this.jid = jid; this.jid = jid;
this.keys = new JSONObject();
} }
public static Contact fromCursor(final Cursor cursor) { public static Contact fromCursor(final Cursor cursor) {
@ -119,7 +123,7 @@ public class Contact implements ListItem, Blockable {
return this.systemName; return this.systemName;
} else if (!TextUtils.isEmpty(this.serverName)) { } else if (!TextUtils.isEmpty(this.serverName)) {
return this.serverName; return this.serverName;
} else if (!TextUtils.isEmpty(this.presenceName) && mutualPresenceSubscription() ) { } else if (!TextUtils.isEmpty(this.presenceName) && mutualPresenceSubscription()) {
return this.presenceName; return this.presenceName;
} else if (jid.getLocal() != null) { } else if (jid.getLocal() != null) {
return JidHelper.localPartOrFallback(jid); return JidHelper.localPartOrFallback(jid);
@ -159,16 +163,16 @@ public class Contact implements ListItem, Blockable {
needle = needle.toLowerCase(Locale.US).trim(); needle = needle.toLowerCase(Locale.US).trim();
String[] parts = needle.split("\\s+"); String[] parts = needle.split("\\s+");
if (parts.length > 1) { if (parts.length > 1) {
for(int i = 0; i < parts.length; ++i) { for (String part : parts) {
if (!match(context, parts[i])) { if (!match(context, part)) {
return false; return false;
} }
} }
return true; return true;
} else { } else {
return jid.toString().contains(needle) || return jid.toString().contains(needle) ||
getDisplayName().toLowerCase(Locale.US).contains(needle) || getDisplayName().toLowerCase(Locale.US).contains(needle) ||
matchInTag(context, needle); matchInTag(context, needle);
} }
} }
@ -277,8 +281,8 @@ public class Contact implements ListItem, Blockable {
this.systemAccount = account; this.systemAccount = account;
} }
public List<String> getGroups() { private List<String> getGroups() {
ArrayList<String> groups = new ArrayList<String>(); ArrayList<String> groups = new ArrayList<>();
for (int i = 0; i < this.groups.length(); ++i) { for (int i = 0; i < this.groups.length(); ++i) {
try { try {
groups.add(this.groups.getString(i)); groups.add(this.groups.getString(i));
@ -325,8 +329,8 @@ public class Contact implements ListItem, Blockable {
public boolean showInRoster() { public boolean showInRoster() {
return (this.getOption(Contact.Options.IN_ROSTER) && (!this return (this.getOption(Contact.Options.IN_ROSTER) && (!this
.getOption(Contact.Options.DIRTY_DELETE))) .getOption(Contact.Options.DIRTY_DELETE)))
|| (this.getOption(Contact.Options.DIRTY_PUSH)); || (this.getOption(Contact.Options.DIRTY_PUSH));
} }
public void parseSubscriptionFromElement(Element item) { public void parseSubscriptionFromElement(Element item) {
@ -390,7 +394,7 @@ public class Contact implements ListItem, Blockable {
} }
@Override @Override
public int compareTo(final ListItem another) { public int compareTo(@NonNull final ListItem another) {
return this.getDisplayName().compareToIgnoreCase( return this.getDisplayName().compareToIgnoreCase(
another.getDisplayName()); another.getDisplayName());
} }