code reformatting

This commit is contained in:
Vincent Breitmoser 2017-02-23 13:13:40 +01:00
parent 923d91b012
commit e18466c290

View file

@ -1,306 +1,269 @@
package com.fsck.k9.ui; package com.fsck.k9.ui;
import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.ImageView;
import android.content.AsyncQueryHandler; import android.content.AsyncQueryHandler;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.database.Cursor; import android.database.Cursor;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Email; import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.Contacts; import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Intents; import android.provider.ContactsContract.Intents;
import android.provider.ContactsContract.PhoneLookup; import android.provider.ContactsContract.PhoneLookup;
import android.provider.ContactsContract.QuickContact; import android.provider.ContactsContract.QuickContact;
import android.provider.ContactsContract.RawContacts; import android.provider.ContactsContract.RawContacts;
import android.util.AttributeSet;
import android.view.View; import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.ImageView;
/** /**
* ContactBadge replaces the android ContactBadge for custom drawing. * ContactBadge replaces the android ContactBadge for custom drawing.
* * <p>
* Based on QuickContactBadge: * Based on QuickContactBadge:
* https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/widget/QuickContactBadge.java * https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/widget/QuickContactBadge.java
*/ */
public class ContactBadge extends ImageView implements OnClickListener { public class ContactBadge extends ImageView implements OnClickListener {
private Uri mContactUri; private Uri mContactUri;
private String mContactEmail; private String mContactEmail;
private String mContactPhone; private String mContactPhone;
private QueryHandler mQueryHandler; private QueryHandler mQueryHandler;
private Bundle mExtras = null; private Bundle mExtras = null;
protected String[] mExcludeMimes = null; protected String[] mExcludeMimes = null;
static final private int TOKEN_EMAIL_LOOKUP = 0; static final private int TOKEN_EMAIL_LOOKUP = 0;
static final private int TOKEN_PHONE_LOOKUP = 1; static final private int TOKEN_PHONE_LOOKUP = 1;
static final private int TOKEN_EMAIL_LOOKUP_AND_TRIGGER = 2; static final private int TOKEN_EMAIL_LOOKUP_AND_TRIGGER = 2;
static final private int TOKEN_PHONE_LOOKUP_AND_TRIGGER = 3; static final private int TOKEN_PHONE_LOOKUP_AND_TRIGGER = 3;
static final private String EXTRA_URI_CONTENT = "uri_content"; static final private String EXTRA_URI_CONTENT = "uri_content";
static final String[] EMAIL_LOOKUP_PROJECTION = new String[] { static final String[] EMAIL_LOOKUP_PROJECTION = new String[] {
RawContacts.CONTACT_ID, RawContacts.CONTACT_ID,
Contacts.LOOKUP_KEY, Contacts.LOOKUP_KEY,
}; };
static final int EMAIL_ID_COLUMN_INDEX = 0; static final int EMAIL_ID_COLUMN_INDEX = 0;
static final int EMAIL_LOOKUP_STRING_COLUMN_INDEX = 1; static final int EMAIL_LOOKUP_STRING_COLUMN_INDEX = 1;
static final String[] PHONE_LOOKUP_PROJECTION = new String[] { static final String[] PHONE_LOOKUP_PROJECTION = new String[] {
PhoneLookup._ID, PhoneLookup._ID,
PhoneLookup.LOOKUP_KEY, PhoneLookup.LOOKUP_KEY,
}; };
static final int PHONE_ID_COLUMN_INDEX = 0; static final int PHONE_ID_COLUMN_INDEX = 0;
static final int PHONE_LOOKUP_STRING_COLUMN_INDEX = 1; static final int PHONE_LOOKUP_STRING_COLUMN_INDEX = 1;
public ContactBadge(Context context) { public ContactBadge(Context context) {
this(context, null); this(context, null);
} }
public ContactBadge(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ContactBadge(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mQueryHandler = new QueryHandler(context.getContentResolver());
setOnClickListener(this);
}
@Override public ContactBadge(Context context, AttributeSet attrs) {
protected void drawableStateChanged() { this(context, attrs, 0);
super.drawableStateChanged(); }
}
/** This call has no effect anymore, as there is only one QuickContact mode */ public ContactBadge(Context context, AttributeSet attrs, int defStyle) {
@SuppressWarnings("unused") super(context, attrs, defStyle);
public void setMode(int size) { mQueryHandler = new QueryHandler(context.getContentResolver());
} setOnClickListener(this);
}
@Override @Override
protected void onDraw(Canvas canvas) { protected void drawableStateChanged() {
super.onDraw(canvas); super.drawableStateChanged();
} }
/** True if a contact, an email address or a phone number has been assigned */ /**
private boolean isAssigned() { * This call has no effect anymore, as there is only one QuickContact mode
return mContactUri != null || mContactEmail != null || mContactPhone != null; */
} @SuppressWarnings("unused")
public void setMode(int size) {
}
/** @Override
* Assign the contact uri that this ContactBadge should be associated protected void onDraw(Canvas canvas) {
* with. Note that this is only used for displaying the QuickContact window and super.onDraw(canvas);
* won't bind the contact's photo for you. Call {@link #setImageDrawable(Drawable)} to set the }
* photo.
*
* @param contactUri Either a {@link Contacts#CONTENT_URI} or
* {@link Contacts#CONTENT_LOOKUP_URI} style URI.
*/
public void assignContactUri(Uri contactUri) {
mContactUri = contactUri;
mContactEmail = null;
mContactPhone = null;
onContactUriChanged();
}
/** /**
* Assign a contact based on an email address. This should only be used when * True if a contact, an email address or a phone number has been assigned
* the contact's URI is not available, as an extra query will have to be */
* performed to lookup the URI based on the email. private boolean isAssigned() {
* return mContactUri != null || mContactEmail != null || mContactPhone != null;
* @param emailAddress The email address of the contact. }
* @param lazyLookup If this is true, the lookup query will not be performed
* until this view is clicked.
*/
public void assignContactFromEmail(String emailAddress, boolean lazyLookup) {
assignContactFromEmail(emailAddress, lazyLookup, null);
}
/** /**
* Assign a contact based on an email address. This should only be used when * Assign the contact uri that this ContactBadge should be associated
* the contact's URI is not available, as an extra query will have to be * with. Note that this is only used for displaying the QuickContact window and
* performed to lookup the URI based on the email. * won't bind the contact's photo for you. Call {@link #setImageDrawable(Drawable)} to set the
* photo.
*
* @param contactUri
* Either a {@link Contacts#CONTENT_URI} or
* {@link Contacts#CONTENT_LOOKUP_URI} style URI.
*/
public void assignContactUri(Uri contactUri) {
mContactUri = contactUri;
mContactEmail = null;
mContactPhone = null;
onContactUriChanged();
}
@param emailAddress The email address of the contact. /**
@param lazyLookup If this is true, the lookup query will not be performed * Assign a contact based on an email address. This should only be used when
until this view is clicked. * the contact's URI is not available, as an extra query will have to be
@param extras A bundle of extras to populate the contact edit page with if the contact * performed to lookup the URI based on the email.
is not found and the user chooses to add the email address to an existing contact or *
create a new contact. Uses the same string constants as those found in * @param emailAddress
{@link android.provider.ContactsContract.Intents.Insert} * The email address of the contact.
*/ * @param lazyLookup
* If this is true, the lookup query will not be performed
* until this view is clicked.
*/
public void assignContactFromEmail(String emailAddress, boolean lazyLookup) {
assignContactFromEmail(emailAddress, lazyLookup, null);
}
public void assignContactFromEmail(String emailAddress, boolean lazyLookup, Bundle extras) { /**
mContactEmail = emailAddress; * Assign a contact based on an email address. This should only be used when
mExtras = extras; * the contact's URI is not available, as an extra query will have to be
if (!lazyLookup) { * performed to lookup the URI based on the email.
mQueryHandler.startQuery(TOKEN_EMAIL_LOOKUP, null, *
Uri.withAppendedPath(Email.CONTENT_LOOKUP_URI, Uri.encode(mContactEmail)), * @param emailAddress
EMAIL_LOOKUP_PROJECTION, null, null, null); * The email address of the contact.
} else { * @param lazyLookup
mContactUri = null; * If this is true, the lookup query will not be performed
onContactUriChanged(); * until this view is clicked.
} * @param extras
} * A bundle of extras to populate the contact edit page with if the contact
* is not found and the user chooses to add the email address to an existing contact or
* create a new contact. Uses the same string constants as those found in
* {@link android.provider.ContactsContract.Intents.Insert}
*/
public void assignContactFromEmail(String emailAddress, boolean lazyLookup, Bundle extras) {
mContactEmail = emailAddress;
mExtras = extras;
if (!lazyLookup) {
mQueryHandler.startQuery(TOKEN_EMAIL_LOOKUP, null,
Uri.withAppendedPath(Email.CONTENT_LOOKUP_URI, Uri.encode(mContactEmail)),
EMAIL_LOOKUP_PROJECTION, null, null, null);
} else {
mContactUri = null;
onContactUriChanged();
}
}
/** private void onContactUriChanged() {
* Assign a contact based on a phone number. This should only be used when setEnabled(isAssigned());
* the contact's URI is not available, as an extra query will have to be }
* performed to lookup the URI based on the phone number.
*
* @param phoneNumber The phone number of the contact.
* @param lazyLookup If this is true, the lookup query will not be performed
* until this view is clicked.
*/
public void assignContactFromPhone(String phoneNumber, boolean lazyLookup) { @Override
assignContactFromPhone(phoneNumber, lazyLookup, new Bundle()); public void onClick(View v) {
} // If contact has been assigned, mExtras should no longer be null, but do a null check
// anyway just in case assignContactFromPhone or Email was called with a null bundle or
// wasn't assigned previously.
final Bundle extras = (mExtras == null) ? new Bundle() : mExtras;
if (mContactUri != null) {
QuickContact.showQuickContact(getContext(), ContactBadge.this, mContactUri,
QuickContact.MODE_LARGE, mExcludeMimes);
} else if (mContactEmail != null) {
extras.putString(EXTRA_URI_CONTENT, mContactEmail);
mQueryHandler.startQuery(TOKEN_EMAIL_LOOKUP_AND_TRIGGER, extras,
Uri.withAppendedPath(Email.CONTENT_LOOKUP_URI, Uri.encode(mContactEmail)),
EMAIL_LOOKUP_PROJECTION, null, null, null);
} else if (mContactPhone != null) {
extras.putString(EXTRA_URI_CONTENT, mContactPhone);
mQueryHandler.startQuery(TOKEN_PHONE_LOOKUP_AND_TRIGGER, extras,
Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, mContactPhone),
PHONE_LOOKUP_PROJECTION, null, null, null);
}
}
/** @Override
* Assign a contact based on a phone number. This should only be used when public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
* the contact's URI is not available, as an extra query will have to be super.onInitializeAccessibilityEvent(event);
* performed to lookup the URI based on the phone number. event.setClassName(ContactBadge.class.getName());
* }
* @param phoneNumber The phone number of the contact.
* @param lazyLookup If this is true, the lookup query will not be performed
* until this view is clicked.
* @param extras A bundle of extras to populate the contact edit page with if the contact
* is not found and the user chooses to add the phone number to an existing contact or
* create a new contact. Uses the same string constants as those found in
* {@link android.provider.ContactsContract.Intents.Insert}
*/
public void assignContactFromPhone(String phoneNumber, boolean lazyLookup, Bundle extras) {
mContactPhone = phoneNumber;
mExtras = extras;
if (!lazyLookup) {
mQueryHandler.startQuery(TOKEN_PHONE_LOOKUP, null,
Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, mContactPhone),
PHONE_LOOKUP_PROJECTION, null, null, null);
} else {
mContactUri = null;
onContactUriChanged();
}
}
private void onContactUriChanged() { @Override
setEnabled(isAssigned()); public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
} super.onInitializeAccessibilityNodeInfo(info);
info.setClassName(ContactBadge.class.getName());
}
@Override private class QueryHandler extends AsyncQueryHandler {
public void onClick(View v) {
// If contact has been assigned, mExtras should no longer be null, but do a null check
// anyway just in case assignContactFromPhone or Email was called with a null bundle or
// wasn't assigned previously.
final Bundle extras = (mExtras == null) ? new Bundle() : mExtras;
if (mContactUri != null) {
QuickContact.showQuickContact(getContext(), ContactBadge.this, mContactUri,
QuickContact.MODE_LARGE, mExcludeMimes);
} else if (mContactEmail != null) {
extras.putString(EXTRA_URI_CONTENT, mContactEmail);
mQueryHandler.startQuery(TOKEN_EMAIL_LOOKUP_AND_TRIGGER, extras,
Uri.withAppendedPath(Email.CONTENT_LOOKUP_URI, Uri.encode(mContactEmail)),
EMAIL_LOOKUP_PROJECTION, null, null, null);
} else if (mContactPhone != null) {
extras.putString(EXTRA_URI_CONTENT, mContactPhone);
mQueryHandler.startQuery(TOKEN_PHONE_LOOKUP_AND_TRIGGER, extras,
Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, mContactPhone),
PHONE_LOOKUP_PROJECTION, null, null, null);
}
}
@Override QueryHandler(ContentResolver cr) {
public void onInitializeAccessibilityEvent(AccessibilityEvent event) { super(cr);
super.onInitializeAccessibilityEvent(event); }
event.setClassName(ContactBadge.class.getName());
}
@Override @Override
public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
super.onInitializeAccessibilityNodeInfo(info); Uri lookupUri = null;
info.setClassName(ContactBadge.class.getName()); Uri createUri = null;
} boolean trigger = false;
final Bundle extras = (cookie != null) ? (Bundle) cookie : new Bundle();
try {
switch (token) {
case TOKEN_PHONE_LOOKUP_AND_TRIGGER:
trigger = true;
createUri = Uri.fromParts("tel", extras.getString(EXTRA_URI_CONTENT), null);
/** //$FALL-THROUGH$
* Set a list of specific MIME-types to exclude and not display. For case TOKEN_PHONE_LOOKUP: {
* example, this can be used to hide the {@link Contacts#CONTENT_ITEM_TYPE} if (cursor != null && cursor.moveToFirst()) {
* profile icon. long contactId = cursor.getLong(PHONE_ID_COLUMN_INDEX);
*/ String lookupKey = cursor.getString(PHONE_LOOKUP_STRING_COLUMN_INDEX);
public void setExcludeMimes(String[] excludeMimes) { lookupUri = Contacts.getLookupUri(contactId, lookupKey);
mExcludeMimes = excludeMimes; }
}
private class QueryHandler extends AsyncQueryHandler { break;
}
case TOKEN_EMAIL_LOOKUP_AND_TRIGGER:
trigger = true;
createUri = Uri.fromParts("mailto",
extras.getString(EXTRA_URI_CONTENT), null);
public QueryHandler(ContentResolver cr) { //$FALL-THROUGH$
super(cr); case TOKEN_EMAIL_LOOKUP: {
} if (cursor != null && cursor.moveToFirst()) {
long contactId = cursor.getLong(EMAIL_ID_COLUMN_INDEX);
String lookupKey = cursor.getString(EMAIL_LOOKUP_STRING_COLUMN_INDEX);
lookupUri = Contacts.getLookupUri(contactId, lookupKey);
}
break;
}
}
} finally {
if (cursor != null) {
cursor.close();
}
}
@Override mContactUri = lookupUri;
protected void onQueryComplete(int token, Object cookie, Cursor cursor) { onContactUriChanged();
Uri lookupUri = null;
Uri createUri = null;
boolean trigger = false;
final Bundle extras = (cookie != null) ? (Bundle) cookie : new Bundle();
try {
switch(token) {
case TOKEN_PHONE_LOOKUP_AND_TRIGGER:
trigger = true;
createUri = Uri.fromParts("tel", extras.getString(EXTRA_URI_CONTENT), null);
//$FALL-THROUGH$ if (trigger && lookupUri != null) {
case TOKEN_PHONE_LOOKUP: { // Found contact, so trigger QuickContact
if (cursor != null && cursor.moveToFirst()) { QuickContact.showQuickContact(getContext(), ContactBadge.this, lookupUri,
long contactId = cursor.getLong(PHONE_ID_COLUMN_INDEX); QuickContact.MODE_LARGE, ContactBadge.this.mExcludeMimes);
String lookupKey = cursor.getString(PHONE_LOOKUP_STRING_COLUMN_INDEX); } else if (createUri != null) {
lookupUri = Contacts.getLookupUri(contactId, lookupKey); // Prompt user to add this person to contacts
} final Intent intent = new Intent(Intents.SHOW_OR_CREATE_CONTACT, createUri);
extras.remove(EXTRA_URI_CONTENT);
break; intent.putExtras(extras);
} getContext().startActivity(intent);
case TOKEN_EMAIL_LOOKUP_AND_TRIGGER: }
trigger = true; }
createUri = Uri.fromParts("mailto", }
extras.getString(EXTRA_URI_CONTENT), null);
//$FALL-THROUGH$
case TOKEN_EMAIL_LOOKUP: {
if (cursor != null && cursor.moveToFirst()) {
long contactId = cursor.getLong(EMAIL_ID_COLUMN_INDEX);
String lookupKey = cursor.getString(EMAIL_LOOKUP_STRING_COLUMN_INDEX);
lookupUri = Contacts.getLookupUri(contactId, lookupKey);
}
break;
}
}
} finally {
if (cursor != null) {
cursor.close();
}
}
mContactUri = lookupUri;
onContactUriChanged();
if (trigger && lookupUri != null) {
// Found contact, so trigger QuickContact
QuickContact.showQuickContact(getContext(), ContactBadge.this, lookupUri,
QuickContact.MODE_LARGE, ContactBadge.this.mExcludeMimes);
} else if (createUri != null) {
// Prompt user to add this person to contacts
final Intent intent = new Intent(Intents.SHOW_OR_CREATE_CONTACT, createUri);
extras.remove(EXTRA_URI_CONTENT);
intent.putExtras(extras);
getContext().startActivity(intent);
}
}
}
} }