implementing Speed Dialing

This commit is contained in:
tibbi
2020-05-10 21:44:32 +02:00
parent 7e7911dd00
commit 38c77c8154
12 changed files with 289 additions and 2 deletions

View File

@ -56,7 +56,12 @@
<activity
android:name="com.simplemobiletools.commons.activities.ManageBlockedNumbersActivity"
android:label="@string/blocked_numbers"
android:parentActivityName=".activities.SettingsActivity"/>
android:parentActivityName=".activities.SettingsActivity" />
<activity
android:name=".activities.ManageSpeedDialActivity"
android:label="@string/speed_dial"
android:parentActivityName=".activities.SettingsActivity" />
<activity
android:name=".activities.SettingsActivity"

View File

@ -0,0 +1,67 @@
package com.simplemobiletools.dialer.activities
import android.os.Bundle
import com.google.gson.Gson
import com.simplemobiletools.commons.extensions.updateTextColors
import com.simplemobiletools.commons.helpers.SimpleContactsHelper
import com.simplemobiletools.commons.models.SimpleContact
import com.simplemobiletools.dialer.R
import com.simplemobiletools.dialer.adapters.SpeedDialAdapter
import com.simplemobiletools.dialer.dialogs.SelectContactDialog
import com.simplemobiletools.dialer.extensions.config
import com.simplemobiletools.dialer.interfaces.RemoveSpeedDialListener
import com.simplemobiletools.dialer.models.SpeedDial
import kotlinx.android.synthetic.main.activity_manage_speed_dial.*
class ManageSpeedDialActivity : SimpleActivity(), RemoveSpeedDialListener {
private var allContacts = ArrayList<SimpleContact>()
private var speedDialValues = ArrayList<SpeedDial>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_manage_speed_dial)
speedDialValues = config.getSpeedDialValues()
updateAdapter()
SimpleContactsHelper(this).getAvailableContacts { contacts ->
allContacts = contacts
}
updateTextColors(manage_speed_dial_scrollview)
}
override fun onStop() {
super.onStop()
config.speedDial = Gson().toJson(speedDialValues)
}
private fun updateAdapter() {
SpeedDialAdapter(this, speedDialValues, this, speed_dial_list) {
val clickedContact = it as SpeedDial
if (allContacts.isEmpty()) {
return@SpeedDialAdapter
}
SelectContactDialog(this, allContacts) { selectedContact ->
speedDialValues.first { it.id == clickedContact.id }.apply {
displayName = selectedContact.name
number = selectedContact.phoneNumber
}
updateAdapter()
}
}.apply {
speed_dial_list.adapter = this
}
}
override fun removeSpeedDial(ids: ArrayList<Int>) {
ids.forEach {
val dialId = it
speedDialValues.first { it.id == dialId }.apply {
displayName = ""
number = ""
}
}
updateAdapter()
}
}

View File

@ -27,6 +27,7 @@ class SettingsActivity : SimpleActivity() {
setupCustomizeColors()
setupUseEnglish()
setupManageBlockedNumbers()
setupManageSpeedDial()
setupChangeDateTimeFormat()
updateTextColors(settings_holder)
invalidateOptionsMenu()
@ -64,6 +65,14 @@ class SettingsActivity : SimpleActivity() {
}
}
private fun setupManageSpeedDial() {
settings_manage_speed_dial_holder.setOnClickListener {
Intent(this, ManageSpeedDialActivity::class.java).apply {
startActivity(this)
}
}
}
private fun setupChangeDateTimeFormat() {
settings_change_date_time_format_holder.setOnClickListener {
ChangeDateTimeFormatDialog(this) {}

View File

@ -0,0 +1,80 @@
package com.simplemobiletools.dialer.adapters
import android.view.Menu
import android.view.View
import android.view.ViewGroup
import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
import com.simplemobiletools.commons.views.MyRecyclerView
import com.simplemobiletools.dialer.R
import com.simplemobiletools.dialer.activities.SimpleActivity
import com.simplemobiletools.dialer.interfaces.RemoveSpeedDialListener
import com.simplemobiletools.dialer.models.SpeedDial
import kotlinx.android.synthetic.main.item_speed_dial.view.*
import java.util.*
class SpeedDialAdapter(activity: SimpleActivity, var speedDialValues: ArrayList<SpeedDial>, private val removeListener: RemoveSpeedDialListener,
recyclerView: MyRecyclerView, itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, null, itemClick) {
init {
setupDragListener(true)
}
override fun getActionMenuId() = R.menu.cab_delete_only
override fun prepareActionMode(menu: Menu) {}
override fun actionItemPressed(id: Int) {
if (selectedKeys.isEmpty()) {
return
}
when (id) {
R.id.cab_delete -> deleteSpeedDial()
}
}
override fun getSelectableItemCount() = speedDialValues.size
override fun getIsItemSelectable(position: Int) = speedDialValues[position].isValid()
override fun getItemSelectionKey(position: Int) = speedDialValues.getOrNull(position)?.hashCode()
override fun getItemKeyPosition(key: Int) = speedDialValues.indexOfFirst { it.hashCode() == key }
override fun onActionModeCreated() {}
override fun onActionModeDestroyed() {}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = createViewHolder(R.layout.item_speed_dial, parent)
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val speedDial = speedDialValues[position]
holder.bindView(speedDial, true, true) { itemView, layoutPosition ->
setupView(itemView, speedDial)
}
bindViewHolder(holder)
}
override fun getItemCount() = speedDialValues.size
private fun getSelectedItems() = speedDialValues.filter { selectedKeys.contains(it.hashCode()) } as ArrayList<SpeedDial>
private fun deleteSpeedDial() {
val ids = getSelectedItems().map { it.id }.toMutableList() as ArrayList<Int>
removeListener.removeSpeedDial(ids)
finishActMode()
}
private fun setupView(view: View, speedDial: SpeedDial) {
view.apply {
var displayName = "${speedDial.id}. "
displayName += if (speedDial.isValid()) speedDial.displayName else ""
speed_dial_label.apply {
text = displayName
isSelected = selectedKeys.contains(speedDial.hashCode())
setTextColor(textColor)
}
}
}
}

View File

@ -0,0 +1,27 @@
package com.simplemobiletools.dialer.dialogs
import androidx.appcompat.app.AlertDialog
import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.commons.models.SimpleContact
import com.simplemobiletools.dialer.R
import com.simplemobiletools.dialer.activities.SimpleActivity
import com.simplemobiletools.dialer.adapters.ContactsAdapter
import kotlinx.android.synthetic.main.layout_select_contact.view.*
class SelectContactDialog(val activity: SimpleActivity, allContacts: ArrayList<SimpleContact>, val callback: (selectedContact: SimpleContact) -> Unit) {
private var dialog: AlertDialog? = null
private var view = activity.layoutInflater.inflate(R.layout.layout_select_contact, null)
init {
view.select_contact_list.adapter = ContactsAdapter(activity, allContacts, view.select_contact_list) {
callback(it as SimpleContact)
dialog?.dismiss()
}
dialog = AlertDialog.Builder(activity)
.setNegativeButton(R.string.cancel, null)
.create().apply {
activity.setupDialogStuff(view, this)
}
}
}

View File

@ -0,0 +1,5 @@
package com.simplemobiletools.dialer.interfaces
interface RemoveSpeedDialListener {
fun removeSpeedDial(ids: ArrayList<Int>)
}

View File

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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/manage_speed_dial_scrollview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/manage_speed_dial_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:ignore="HardcodedText">
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/manage_speed_dial_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/normal_margin"
android:text="@string/speed_dial_label"
android:textSize="@dimen/bigger_text_size" />
<com.simplemobiletools.commons.views.MyRecyclerView
android:id="@+id/speed_dial_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:scrollbars="none"
app:layoutManager="com.simplemobiletools.commons.views.MyLinearLayoutManager" />
</LinearLayout>
</ScrollView>

View File

@ -72,7 +72,7 @@
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:paddingStart="@dimen/medium_margin"
android:text="@string/manage_blocked_numbers"/>
android:text="@string/manage_blocked_numbers" />
</RelativeLayout>
@ -96,5 +96,26 @@
android:text="@string/change_date_and_time_format" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/settings_manage_speed_dial_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/medium_margin"
android:background="?attr/selectableItemBackground"
android:paddingStart="@dimen/normal_margin"
android:paddingTop="@dimen/activity_margin"
android:paddingEnd="@dimen/normal_margin"
android:paddingBottom="@dimen/activity_margin">
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/settings_manage_speed_dial"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:paddingStart="@dimen/medium_margin"
android:text="@string/manage_speed_dial" />
</RelativeLayout>
</LinearLayout>
</ScrollView>

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<com.simplemobiletools.commons.views.MyTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/speed_dial_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:background="?attr/selectableItemBackground"
android:ellipsize="end"
android:foreground="@drawable/selector"
android:lines="1"
android:maxLines="1"
android:padding="@dimen/activity_margin"
android:singleLine="true"
android:textSize="@dimen/bigger_text_size" />

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/select_contact_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.simplemobiletools.commons.views.MyRecyclerView
android:id="@+id/select_contact_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:scrollbars="none"
app:layoutManager="com.simplemobiletools.commons.views.MyLinearLayoutManager" />
</RelativeLayout>

View File

@ -27,6 +27,11 @@
<string name="select_sim">Selecione um SIM para esta chamada</string>
<string name="always_use_this_sim">Utilizar sempre este SIM para ligar a este número</string>
<!-- Speed dial -->
<string name="speed_dial">Ligação rápida</string>
<string name="manage_speed_dial">Gerir ligações rápidas</string>
<string name="speed_dial_label">Clique no número para atribuir um contacto a uma ligação rápida. Posteriormente, poderá ligar diretamente ao contacto através da tecla de ligação rápida.</string>
<!-- Strings displayed only on Google Playstore. Optional, but good to have -->
<!-- App title has to have less than 50 characters. If you cannot squeeze it, just remove a part of it -->
<string name="app_title">Simple Dialer - Manage your phone calls easily</string>

View File

@ -27,6 +27,11 @@
<string name="select_sim">Select a SIM for this call</string>
<string name="always_use_this_sim">Always use this SIM for this number</string>
<!-- Speed dial -->
<string name="speed_dial">Speed dial</string>
<string name="manage_speed_dial">Manage speed dial</string>
<string name="speed_dial_label">Click on a number to assign a contact to it. You can then quickly call the given contact by long pressing the given number at the dialer.</string>
<!-- Strings displayed only on Google Playstore. Optional, but good to have -->
<!-- App title has to have less than 50 characters. If you cannot squeeze it, just remove a part of it -->
<string name="app_title">Simple Dialer - Manage your phone calls easily</string>