Skip to content
This repository was archived by the owner on Nov 18, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.android.tools.build:gradle:3.0.0-beta7'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
}
}

allprojects {
repositories {
jcenter()
google()
}
}

Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Mon Feb 27 11:21:31 CET 2017
#Wed Sep 06 13:43:21 CDT 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
8 changes: 4 additions & 4 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ apply plugin: 'com.android.library'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"

buildToolsVersion '26.0.2'
defaultConfig {
minSdkVersion 16
targetSdkVersion 25
versionCode 4
versionName "1.2.0"
versionName '1.4.0'
setProperty("archivesBaseName", "android-material-chips-$versionName")
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
}
}

dependencies {
Expand Down
102 changes: 91 additions & 11 deletions library/src/main/java/com/doodle/android/chips/ChipsView.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.net.Uri;
import android.os.Build;
Expand Down Expand Up @@ -54,6 +55,7 @@

import com.doodle.android.chips.model.Contact;
import com.doodle.android.chips.util.Common;
import com.doodle.android.chips.util.LetterTileDrawable;
import com.doodle.android.chips.views.ChipsEditText;
import com.doodle.android.chips.views.ChipsVerticalLinearLayout;
import com.squareup.picasso.Callback;
Expand All @@ -63,6 +65,11 @@
import java.util.Collections;
import java.util.List;

/**
* Modified by Gagan Singh on 8/20/2017.
* Added Phone Number as alternate contact information, developer can decide between phone and email.
* Added Google's LetterTileDrawable to change the color of a contact chip based on an algorithm.
*/
public class ChipsView extends ScrollView implements ChipsEditText.InputConnectionWrapperInterface {

//<editor-fold desc="Static Fields">
Expand Down Expand Up @@ -94,6 +101,7 @@ public class ChipsView extends ScrollView implements ChipsEditText.InputConnecti
private int mChipsTextColorClicked;
private int mChipsTextColorErrorClicked;
private int mChipsPlaceholderResId;
private int mChipsWhitePlaceholderResId;
private
@ColorInt
int mChipsPlaceholderTint;
Expand All @@ -114,6 +122,7 @@ public class ChipsView extends ScrollView implements ChipsEditText.InputConnecti
private Object mCurrentEditTextSpan;
private ChipValidator mChipsValidator;
private Typeface mTypeface;
private LetterTileDrawable letterTitleDrawable;

// initials
private boolean mUseInitials = false;
Expand All @@ -126,32 +135,36 @@ public class ChipsView extends ScrollView implements ChipsEditText.InputConnecti
//<editor-fold desc="Constructors">
public ChipsView(Context context) {
super(context);
letterTitleDrawable = new LetterTileDrawable(getContext().getResources());
init();
}

public ChipsView(Context context, AttributeSet attrs) {
super(context, attrs);
letterTitleDrawable = new LetterTileDrawable(getContext().getResources());
initAttr(context, attrs);
init();
}

public ChipsView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
letterTitleDrawable = new LetterTileDrawable(getContext().getResources());
initAttr(context, attrs);
init();
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public ChipsView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
letterTitleDrawable = new LetterTileDrawable(getContext().getResources());
initAttr(context, attrs);
init();
}
//</editor-fold>

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if(mMaxHeight != DEFAULT_MAX_HEIGHT) {
if (mMaxHeight != DEFAULT_MAX_HEIGHT) {
heightMeasureSpec = MeasureSpec.makeMeasureSpec(mMaxHeight, MeasureSpec.AT_MOST);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Expand Down Expand Up @@ -183,6 +196,7 @@ private void initAttr(Context context, AttributeSet attrs) {
mChipsTextColorErrorClicked = a.getColor(R.styleable.ChipsView_cv_text_color_clicked, Color.WHITE);
mChipsTextColorIndelible = a.getColor(R.styleable.ChipsView_cv_text_color_indelible, mChipsTextColor);
mChipsPlaceholderResId = a.getResourceId(R.styleable.ChipsView_cv_icon_placeholder, R.drawable.ic_person_24dp);
mChipsWhitePlaceholderResId = a.getResourceId(R.styleable.ChipsView_cv_icon_placeholder, R.drawable.ic_person);
mChipsPlaceholderTint = a.getColor(R.styleable.ChipsView_cv_icon_placeholder_tint, 0);
mChipsDeleteResId = a.getResourceId(R.styleable.ChipsView_cv_icon_delete, R.drawable.ic_close_24dp);
mChipsHintText = a.getString(R.styleable.ChipsView_cv_text_hint);
Expand Down Expand Up @@ -236,11 +250,11 @@ private void init() {
editModeLinLayout.setOrientation(LinearLayout.HORIZONTAL);
mChipsContainer.addView(editModeLinLayout);

View view = new Chip("Test Chip", null, new Contact(null, null, "Test", "[email protected]", null)).getView();
View view = new Chip("Test Chip", null, new Contact(null, null, null, "Test", Contact.ContactType.EMAIL, "[email protected]", null)).getView();
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
editModeLinLayout.addView(view);

View view2 = new Chip("Indelible", null, new Contact(null, null, "Test", "[email protected]", null), true).getView();
View view2 = new Chip("Indelible", null, new Contact(null, null, null, "Test", Contact.ContactType.PHONE, "[email protected]", null), true).getView();
view2.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
editModeLinLayout.addView(view2);
}
Expand Down Expand Up @@ -327,11 +341,33 @@ public List<Chip> getChips() {
return Collections.unmodifiableList(mChipList);
}

public void clearAllChips() {
mChipList.clear();
if (mChipList.isEmpty()) {
mEditText.setHint(mChipsHintText);
}
onChipsChanged(true);
}

public boolean removeChipBy(Contact contact) {
for (int i = 0; i < mChipList.size(); i++) {
if (mChipList.get(i).mContact != null && mChipList.get(i).mContact.equals(contact)) {
mChipList.remove(i);
if(mChipList.isEmpty()) {
if (mChipList.isEmpty()) {
mEditText.setHint(mChipsHintText);
}
onChipsChanged(true);
return true;
}
}
return false;
}

public boolean removeChipBy(String id) {
for (int i = 0; i < mChipList.size(); i++) {
if (mChipList.get(i).mContact != null && mChipList.get(i).mContact.getId().equals(id)) {
mChipList.remove(i);
if (mChipList.isEmpty()) {
mEditText.setHint(mChipsHintText);
}
onChipsChanged(true);
Expand All @@ -345,7 +381,9 @@ public Contact tryToRecognizeAddress() {
String text = mEditText.getText().toString();
if (!TextUtils.isEmpty(text)) {
if (Common.isValidEmail(text)) {
return new Contact(text, "", null, text, null);
return new Contact(null, text, "", null, Contact.ContactType.EMAIL, text, null);
} else if (Common.isValidEmail(text)) {
return new Contact(null, text, "", null, Contact.ContactType.PHONE, text, null);
}
}
return null;
Expand All @@ -365,6 +403,7 @@ public EditText getEditText() {
//</editor-fold>

//<editor-fold desc="Private Methods">

/**
* rebuild all chips and place them right
*/
Expand Down Expand Up @@ -421,9 +460,12 @@ private boolean onEnterPressed(String text) {

if (Common.isValidEmail(text)) {
onEmailRecognized(text);
} else if (Common.isValidPhone(text)) {
onPhoneRecognized(text);
} else {
shouldDeleteText = onNonEmailRecognized(text);
}

if (shouldDeleteText) {
mEditText.setSelection(0);
}
Expand All @@ -432,7 +474,7 @@ private boolean onEnterPressed(String text) {
}

private void onEmailRecognized(String email) {
onEmailRecognized(new Contact(email, "", null, email, null));
onEmailRecognized(new Contact(null, email, "", null, Contact.ContactType.EMAIL, email, null));
}

private void onEmailRecognized(Contact contact) {
Expand All @@ -449,6 +491,24 @@ public void run() {
});
}

private void onPhoneRecognized(String phone) {
onPhoneRecognized(new Contact(null, phone, "", null, Contact.ContactType.PHONE, phone, null));
}

private void onPhoneRecognized(Contact contact) {
Chip chip = new Chip(contact.getDisplayName(), null, contact);
mChipList.add(chip);
if (mChipsListener != null) {
mChipsListener.onChipAdded(chip);
}
post(new Runnable() {
@Override
public void run() {
onChipsChanged(true);
}
});
}

private boolean onNonEmailRecognized(String text) {
if (mChipsListener != null) {
return mChipsListener.onInputNotRecognized(text);
Expand Down Expand Up @@ -482,7 +542,7 @@ private void onChipInteraction(Chip chip, boolean nameClicked) {
}
onChipsChanged(true);
if (nameClicked) {
mEditText.setText(chip.getContact().getEmailAddress());
mEditText.setText(chip.getContact().getContactInfo());
addLeadingMarginSpan();
mEditText.requestFocus();
mEditText.setSelection(mEditText.length());
Expand Down Expand Up @@ -636,7 +696,7 @@ public Chip(String label, Uri photoUri, Contact contact, boolean isIndelible) {
this.mIsIndelible = isIndelible;

if (mLabel == null) {
mLabel = contact.getEmailAddress();
mLabel = contact.getContactInfo();
}

if (mLabel.length() > MAX_LABEL_LENGTH) {
Expand Down Expand Up @@ -672,15 +732,28 @@ public View getView() {
} else {
((GradientDrawable) mView.getBackground()).setColor(mChipsBgColor);
}
mIconWrapper.setBackgroundResource(R.drawable.circle);
try {
mIconWrapper.setBackgroundResource(R.drawable.circle);
if (mContact.getId() != null) {
mIconWrapper.getBackground().setColorFilter(letterTitleDrawable.pickColor(mContact.getId()), PorterDuff.Mode.SRC_ATOP);
} else {
mIconWrapper.getBackground().setColorFilter(mChipsColor, PorterDuff.Mode.SRC_ATOP);
}
} catch (Exception e) {}

if (mIsIndelible) {
mTextView.setTextColor(mChipsTextColorIndelible);
} else {
mTextView.setTextColor(mChipsTextColor);
}

// set icon resources
mPersonIcon.setImageResource(mChipsPlaceholderResId);
if (mContact.getId() == null) {
mPersonIcon.setImageResource(mChipsPlaceholderResId);
} else {
mPersonIcon.setImageResource(mChipsWhitePlaceholderResId);
}

if (mChipsPlaceholderTint != 0) {
mPersonIcon.setColorFilter(mChipsPlaceholderTint, PorterDuff.Mode.SRC_ATOP);
}
Expand Down Expand Up @@ -770,7 +843,14 @@ public void onError() {
((GradientDrawable) mView.getBackground()).setColor(mChipsBgColor);
mTextView.setTextColor(mChipsTextColor);
}
mIconWrapper.getBackground().setColorFilter(mChipsColor, PorterDuff.Mode.SRC_ATOP);

try {
if (mContact.getId() != null) {
mIconWrapper.getBackground().setColorFilter(letterTitleDrawable.pickColor(mContact.getId()), PorterDuff.Mode.SRC_ATOP);
} else {
mIconWrapper.getBackground().setColorFilter(mChipsColor, PorterDuff.Mode.SRC_ATOP);
}
}catch(Exception e){}

if (mUseInitials) {
mInitials.animate().alpha(1f).setDuration(200).setStartDelay(100).start();
Expand Down
Loading