Use Autofill in name text fields

This commit is contained in:
Naveen 2023-05-05 14:02:11 +05:30
parent f183cda73d
commit 4003246483
4 changed files with 197 additions and 8 deletions

View File

@ -17,10 +17,7 @@ import android.telephony.PhoneNumberUtils
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.view.WindowManager import android.view.WindowManager
import android.widget.EditText import android.widget.*
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import com.simplemobiletools.commons.dialogs.ConfirmationAdvancedDialog import com.simplemobiletools.commons.dialogs.ConfirmationAdvancedDialog
import com.simplemobiletools.commons.dialogs.RadioGroupDialog import com.simplemobiletools.commons.dialogs.RadioGroupDialog
@ -33,7 +30,9 @@ import com.simplemobiletools.commons.models.contacts.*
import com.simplemobiletools.commons.models.contacts.Email import com.simplemobiletools.commons.models.contacts.Email
import com.simplemobiletools.commons.models.contacts.Event import com.simplemobiletools.commons.models.contacts.Event
import com.simplemobiletools.commons.models.contacts.Organization import com.simplemobiletools.commons.models.contacts.Organization
import com.simplemobiletools.commons.views.MyAutoCompleteTextView
import com.simplemobiletools.contacts.pro.R import com.simplemobiletools.contacts.pro.R
import com.simplemobiletools.contacts.pro.adapters.AutoCompleteTextViewAdapter
import com.simplemobiletools.contacts.pro.dialogs.CustomLabelDialog import com.simplemobiletools.contacts.pro.dialogs.CustomLabelDialog
import com.simplemobiletools.contacts.pro.dialogs.ManageVisibleFieldsDialog import com.simplemobiletools.contacts.pro.dialogs.ManageVisibleFieldsDialog
import com.simplemobiletools.contacts.pro.dialogs.MyDatePickerDialog import com.simplemobiletools.contacts.pro.dialogs.MyDatePickerDialog
@ -254,6 +253,11 @@ class EditContactActivity : ContactActivity() {
setOnLongClickListener { toast(R.string.toggle_favorite); true; } setOnLongClickListener { toast(R.string.toggle_favorite); true; }
} }
val nameTextViews = arrayOf(contact_first_name, contact_middle_name, contact_surname).filter { it.isVisible() }
if (nameTextViews.isNotEmpty()) {
setupAutofill(nameTextViews)
}
updateTextColors(contact_scrollview) updateTextColors(contact_scrollview)
numberViewToColor?.setTextColor(properPrimaryColor) numberViewToColor?.setTextColor(properPrimaryColor)
emailViewToColor?.setTextColor(properPrimaryColor) emailViewToColor?.setTextColor(properPrimaryColor)
@ -1526,4 +1530,26 @@ class EditContactActivity : ContactActivity() {
getString(R.string.jabber) -> Im.PROTOCOL_JABBER getString(R.string.jabber) -> Im.PROTOCOL_JABBER
else -> Im.PROTOCOL_CUSTOM else -> Im.PROTOCOL_CUSTOM
} }
private fun setupAutofill(nameTextViews: List<MyAutoCompleteTextView>) {
ContactsHelper(this).getContacts { contacts ->
val adapter = AutoCompleteTextViewAdapter(this, contacts)
nameTextViews.forEach { view ->
view.setAdapter(adapter)
view.setOnItemClickListener { _, _, position, _ ->
val selectedContact = adapter.resultList[position]
if (contact_first_name.isVisible()) {
contact_first_name.setText(selectedContact.firstName)
}
if (contact_middle_name.isVisible()) {
contact_middle_name.setText(selectedContact.middleName)
}
if (contact_surname.isVisible()) {
contact_surname.setText(selectedContact.surname)
}
}
}
}
}
} }

View File

@ -0,0 +1,102 @@
package com.simplemobiletools.contacts.pro.adapters
import android.graphics.drawable.BitmapDrawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.Filter
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions
import com.simplemobiletools.commons.extensions.getProperBackgroundColor
import com.simplemobiletools.commons.extensions.getProperTextColor
import com.simplemobiletools.commons.extensions.normalizeString
import com.simplemobiletools.commons.helpers.SimpleContactsHelper
import com.simplemobiletools.commons.models.contacts.Contact
import com.simplemobiletools.contacts.pro.R
import com.simplemobiletools.contacts.pro.activities.SimpleActivity
import kotlinx.android.synthetic.main.item_autocomplete_name_number.view.item_autocomplete_image
import kotlinx.android.synthetic.main.item_autocomplete_name_number.view.item_autocomplete_name
import kotlinx.android.synthetic.main.item_autocomplete_name_number.view.item_autocomplete_number
class AutoCompleteTextViewAdapter(val activity: SimpleActivity, val contacts: ArrayList<Contact>) : ArrayAdapter<Contact>(activity, 0, contacts) {
var resultList = ArrayList<Contact>()
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val contact = resultList[position]
var listItem = convertView
if (listItem == null || listItem.tag != contact.name.isNotEmpty()) {
listItem = LayoutInflater.from(activity).inflate(R.layout.item_autocomplete_name_number, parent, false)
}
val nameToUse = contact.name
val placeholder = BitmapDrawable(activity.resources, SimpleContactsHelper(context).getContactLetterIcon(nameToUse))
listItem!!.apply {
setBackgroundColor(context.getProperBackgroundColor())
item_autocomplete_name.setTextColor(context.getProperTextColor())
item_autocomplete_number.setTextColor(context.getProperTextColor())
tag = contact.name.isNotEmpty()
item_autocomplete_name.text = contact.name
item_autocomplete_number.text = contact.phoneNumbers.run {
firstOrNull { it.isPrimary }?.normalizedNumber ?: firstOrNull()?.normalizedNumber
}
val options = RequestOptions()
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
.error(placeholder)
.centerCrop()
Glide.with(context)
.load(contact.photoUri)
.transition(DrawableTransitionOptions.withCrossFade())
.placeholder(placeholder)
.apply(options)
.apply(RequestOptions.circleCropTransform())
.into(item_autocomplete_image)
}
return listItem
}
override fun getFilter() = object : Filter() {
override fun performFiltering(constraint: CharSequence?): FilterResults {
val filterResults = FilterResults()
if (constraint != null) {
resultList.clear()
val searchString = constraint.toString().normalizeString()
contacts.forEach {
if (it.name.contains(searchString, true)) {
resultList.add(it)
}
}
resultList.sortWith(compareBy<Contact>
{ it.name.startsWith(searchString, true) }.thenBy
{ it.name.contains(searchString, true) })
resultList.reverse()
filterResults.values = resultList
filterResults.count = resultList.size
}
return filterResults
}
override fun publishResults(constraint: CharSequence?, results: FilterResults?) {
if ((results?.count ?: -1) > 0) {
notifyDataSetChanged()
} else {
notifyDataSetInvalidated()
}
}
override fun convertResultToString(resultValue: Any?) = (resultValue as? Contact)?.name
}
override fun getItem(index: Int) = resultList[index]
override fun getCount() = resultList.size
}

View File

@ -11,7 +11,8 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:scrollbars="none" android:scrollbars="none"
android:visibility="gone"> android:visibility="gone"
tools:visibility="visible">
<RelativeLayout <RelativeLayout
android:id="@+id/contact_holder" android:id="@+id/contact_holder"
@ -90,7 +91,7 @@
android:textCursorDrawable="@null" android:textCursorDrawable="@null"
android:textSize="@dimen/bigger_text_size" /> android:textSize="@dimen/bigger_text_size" />
<com.simplemobiletools.commons.views.MyEditText <com.simplemobiletools.commons.views.MyAutoCompleteTextView
android:id="@+id/contact_first_name" android:id="@+id/contact_first_name"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -99,6 +100,7 @@
android:layout_marginEnd="@dimen/activity_margin" android:layout_marginEnd="@dimen/activity_margin"
android:layout_marginBottom="@dimen/normal_margin" android:layout_marginBottom="@dimen/normal_margin"
android:layout_toEndOf="@+id/contact_name_image" android:layout_toEndOf="@+id/contact_name_image"
android:completionThreshold="2"
android:hint="@string/first_name" android:hint="@string/first_name"
android:inputType="textCapWords" android:inputType="textCapWords"
android:lines="1" android:lines="1"
@ -107,7 +109,7 @@
android:textCursorDrawable="@null" android:textCursorDrawable="@null"
android:textSize="@dimen/bigger_text_size" /> android:textSize="@dimen/bigger_text_size" />
<com.simplemobiletools.commons.views.MyEditText <com.simplemobiletools.commons.views.MyAutoCompleteTextView
android:id="@+id/contact_middle_name" android:id="@+id/contact_middle_name"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -116,6 +118,7 @@
android:layout_marginEnd="@dimen/activity_margin" android:layout_marginEnd="@dimen/activity_margin"
android:layout_marginBottom="@dimen/normal_margin" android:layout_marginBottom="@dimen/normal_margin"
android:layout_toEndOf="@+id/contact_name_image" android:layout_toEndOf="@+id/contact_name_image"
android:completionThreshold="2"
android:hint="@string/middle_name" android:hint="@string/middle_name"
android:inputType="textCapWords" android:inputType="textCapWords"
android:lines="1" android:lines="1"
@ -124,7 +127,7 @@
android:textCursorDrawable="@null" android:textCursorDrawable="@null"
android:textSize="@dimen/bigger_text_size" /> android:textSize="@dimen/bigger_text_size" />
<com.simplemobiletools.commons.views.MyEditText <com.simplemobiletools.commons.views.MyAutoCompleteTextView
android:id="@+id/contact_surname" android:id="@+id/contact_surname"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -133,6 +136,7 @@
android:layout_marginEnd="@dimen/activity_margin" android:layout_marginEnd="@dimen/activity_margin"
android:layout_marginBottom="@dimen/normal_margin" android:layout_marginBottom="@dimen/normal_margin"
android:layout_toEndOf="@+id/contact_name_image" android:layout_toEndOf="@+id/contact_name_image"
android:completionThreshold="2"
android:hint="@string/surname" android:hint="@string/surname"
android:inputType="textCapWords" android:inputType="textCapWords"
android:lines="1" android:lines="1"

View File

@ -0,0 +1,57 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/item_autocomplete_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="@dimen/small_margin"
android:paddingTop="@dimen/medium_margin"
android:paddingEnd="@dimen/medium_margin"
android:paddingBottom="@dimen/medium_margin">
<ImageView
android:id="@+id/item_autocomplete_image"
android:layout_width="@dimen/list_avatar_size"
android:layout_height="@dimen/list_avatar_size"
android:layout_margin="@dimen/tiny_margin"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/item_autocomplete_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:lines="1"
android:maxLines="1"
android:paddingStart="@dimen/medium_margin"
android:paddingEnd="@dimen/medium_margin"
android:singleLine="true"
android:textSize="@dimen/bigger_text_size"
app:layout_constraintBottom_toTopOf="@+id/item_autocomplete_number"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/item_autocomplete_image"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed"
tools:text="Simple Mobile" />
<TextView
android:id="@+id/item_autocomplete_number"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@+id/item_autocomplete_name"
android:layout_marginEnd="8dp"
android:alpha="0.8"
android:lines="1"
android:maxLines="1"
android:paddingStart="@dimen/medium_margin"
android:paddingEnd="@dimen/medium_margin"
android:singleLine="true"
android:textSize="@dimen/normal_text_size"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/item_autocomplete_image"
app:layout_constraintTop_toBottomOf="@+id/item_autocomplete_name"
tools:text="hello@simplemobiletools.com" />
</androidx.constraintlayout.widget.ConstraintLayout>