mirror of
https://github.com/home-assistant/android
synced 2024-09-06 17:23:32 +00:00
Suggest or highlight current SSID in home SSID dialog (#2457)
- Show a chip suggesting the user to add the SSID of the currently connected network, if not already added to the list of home SSIDs, in the home SSID dialog - Change icon for listed SSIDs in home SSID dialog to DKGRAY by default, and only show currently connected SSID icon in accent color - Add missing contentDescription for ImageButtons - Use correct tint namespace - Update deprecated code
This commit is contained in:
parent
90b8d03002
commit
f4c4d72518
|
@ -2,15 +2,21 @@ package io.homeassistant.companion.android.settings.ssid
|
|||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.net.wifi.WifiManager
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.inputmethod.EditorInfo
|
||||
import androidx.preference.PreferenceDialogFragmentCompat
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import io.homeassistant.companion.android.R
|
||||
import io.homeassistant.companion.android.common.data.wifi.WifiHelper
|
||||
import io.homeassistant.companion.android.databinding.DialogSsidBinding
|
||||
import javax.inject.Inject
|
||||
import io.homeassistant.companion.android.common.R as commonR
|
||||
|
||||
@AndroidEntryPoint
|
||||
class SsidDialogFragment : PreferenceDialogFragmentCompat() {
|
||||
|
||||
companion object {
|
||||
|
@ -21,6 +27,9 @@ class SsidDialogFragment : PreferenceDialogFragmentCompat() {
|
|||
}
|
||||
}
|
||||
|
||||
@Inject
|
||||
lateinit var wifiHelper: WifiHelper
|
||||
|
||||
private lateinit var binding: DialogSsidBinding
|
||||
private val ssidAdapter = SsidRecyclerViewAdapter()
|
||||
|
||||
|
@ -44,12 +53,25 @@ class SsidDialogFragment : PreferenceDialogFragmentCompat() {
|
|||
binding.rvSsids.adapter = ssidAdapter
|
||||
|
||||
val ssids = getSsidPreference().getSsids()
|
||||
ssidAdapter.submitSet(ssids)
|
||||
ssidAdapter.submitSet(ssids.map { SsidRecyclerViewAdapter.SsidEntry(it, isConnectedToSsid(it)) }.toSet())
|
||||
|
||||
val currentSsid = wifiHelper.getWifiSsid().removeSurrounding("\"")
|
||||
if (currentSsid.isBlank() || currentSsid in ssids ||
|
||||
(Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && currentSsid == WifiManager.UNKNOWN_SSID)
|
||||
) {
|
||||
binding.suggestCurrentSsid.visibility = View.GONE
|
||||
} else {
|
||||
binding.suggestCurrentSsid.visibility = View.VISIBLE
|
||||
binding.suggestCurrentSsid.text = getString(commonR.string.add_ssid_name_suggestion, currentSsid)
|
||||
binding.suggestCurrentSsid.setOnClickListener {
|
||||
addSsidFromSuggestion()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun addSsidFromInput(): Boolean {
|
||||
val input = binding.inputSsid.text?.toString() ?: return false
|
||||
val result = if (!input.isBlank()) submitSsid(input) else return false
|
||||
val result = if (input.isNotBlank()) submitSsid(input) else return false
|
||||
if (result) {
|
||||
binding.inputContainer.error = null
|
||||
binding.inputSsid.text = null
|
||||
|
@ -59,12 +81,21 @@ class SsidDialogFragment : PreferenceDialogFragmentCompat() {
|
|||
return result
|
||||
}
|
||||
|
||||
private fun addSsidFromSuggestion(): Boolean {
|
||||
val input = wifiHelper.getWifiSsid().removeSurrounding("\"")
|
||||
val result = if (input.isNotBlank()) submitSsid(input) else return false
|
||||
if (result) {
|
||||
binding.suggestCurrentSsid.visibility = View.GONE
|
||||
} // else shouldn't happen, so don't do anything
|
||||
return result
|
||||
}
|
||||
|
||||
private fun submitSsid(ssid: String): Boolean {
|
||||
val ssids = ssidAdapter.currentList
|
||||
if (ssids.contains(ssid)) {
|
||||
if (ssids.any { it.name == ssid }) {
|
||||
return false
|
||||
}
|
||||
ssidAdapter.submitList(ssids + ssid)
|
||||
ssidAdapter.submitList(ssids + SsidRecyclerViewAdapter.SsidEntry(ssid, isConnectedToSsid(ssid)))
|
||||
return true
|
||||
}
|
||||
|
||||
|
@ -72,16 +103,20 @@ class SsidDialogFragment : PreferenceDialogFragmentCompat() {
|
|||
return preference as SsidPreference
|
||||
}
|
||||
|
||||
private fun isConnectedToSsid(ssid: String): Boolean {
|
||||
return wifiHelper.getWifiSsid().removeSurrounding("\"") == ssid
|
||||
}
|
||||
|
||||
override fun onDialogClosed(positiveResult: Boolean) {
|
||||
if (positiveResult) {
|
||||
val ssids = ssidAdapter.currentList.toSortedSet()
|
||||
val ssids = ssidAdapter.currentList.toMutableList()
|
||||
val input = binding.inputSsid.text?.toString()
|
||||
if (!input.isNullOrBlank()) {
|
||||
ssids.add(input)
|
||||
ssids.add(SsidRecyclerViewAdapter.SsidEntry(input, isConnectedToSsid(input)))
|
||||
}
|
||||
val ssidPreference = getSsidPreference()
|
||||
if (preference.callChangeListener(ssids)) {
|
||||
ssidPreference.setSsids(ssids)
|
||||
ssidPreference.setSsids(ssids.map { it.name }.sorted().toSet())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,16 +1,24 @@
|
|||
package io.homeassistant.companion.android.settings.ssid
|
||||
|
||||
import android.graphics.Color
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.ViewGroup
|
||||
import androidx.appcompat.content.res.AppCompatResources
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.recyclerview.widget.AsyncDifferConfig
|
||||
import androidx.recyclerview.widget.ListAdapter
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import io.homeassistant.companion.android.R
|
||||
import io.homeassistant.companion.android.databinding.ItemSsidBinding
|
||||
import io.homeassistant.companion.android.util.recyclerview.GenericMapperDiffCallback
|
||||
import io.homeassistant.companion.android.common.R as commonR
|
||||
|
||||
class SsidRecyclerViewAdapter : ListAdapter<String, SsidRecyclerViewAdapter.SsidBindingViewHolder>(
|
||||
AsyncDifferConfig.Builder(GenericMapperDiffCallback<String> { element -> element }).build()
|
||||
) {
|
||||
class SsidRecyclerViewAdapter :
|
||||
ListAdapter<SsidRecyclerViewAdapter.SsidEntry, SsidRecyclerViewAdapter.SsidBindingViewHolder>(
|
||||
AsyncDifferConfig.Builder(GenericMapperDiffCallback<SsidEntry> { element -> element })
|
||||
.build()
|
||||
) {
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SsidBindingViewHolder {
|
||||
val binding = ItemSsidBinding.inflate(LayoutInflater.from(parent.context), parent, false)
|
||||
|
@ -23,10 +31,21 @@ class SsidRecyclerViewAdapter : ListAdapter<String, SsidRecyclerViewAdapter.Ssid
|
|||
override fun onBindViewHolder(holder: SsidBindingViewHolder, position: Int) {
|
||||
val ssidEntry = getItem(position)
|
||||
val binding = ItemSsidBinding.bind(holder.itemView)
|
||||
binding.ssidText.text = ssidEntry
|
||||
try {
|
||||
val unwrappedDrawable = AppCompatResources.getDrawable(binding.ssidIcon.context, R.drawable.ic_wifi)
|
||||
if (ssidEntry.connected) {
|
||||
unwrappedDrawable?.setTint(ContextCompat.getColor(binding.ssidIcon.context, commonR.color.colorAccent))
|
||||
} else {
|
||||
unwrappedDrawable?.setTint(Color.DKGRAY)
|
||||
}
|
||||
binding.ssidIcon.setImageDrawable(unwrappedDrawable)
|
||||
} catch (e: Exception) {
|
||||
Log.e("SsidRecyclerViewAdapter", "Unable to set the icon tint", e)
|
||||
}
|
||||
binding.ssidText.text = ssidEntry.name
|
||||
}
|
||||
|
||||
fun submitSet(set: Set<String>) = submitList(set.sorted())
|
||||
fun submitSet(set: Set<SsidEntry>) = submitList(set.sortedBy { it.name })
|
||||
|
||||
class SsidBindingViewHolder(
|
||||
binding: ItemSsidBinding,
|
||||
|
@ -34,7 +53,9 @@ class SsidRecyclerViewAdapter : ListAdapter<String, SsidRecyclerViewAdapter.Ssid
|
|||
) : RecyclerView.ViewHolder(binding.root) {
|
||||
|
||||
init {
|
||||
binding.actionRemove.setOnClickListener { clickListener(adapterPosition) }
|
||||
binding.actionRemove.setOnClickListener { clickListener(absoluteAdapterPosition) }
|
||||
}
|
||||
}
|
||||
|
||||
data class SsidEntry(val name: String, val connected: Boolean)
|
||||
}
|
||||
|
|
|
@ -37,20 +37,31 @@
|
|||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="16dp"
|
||||
android:contentDescription="@null"
|
||||
android:contentDescription="@string/add_ssid"
|
||||
android:layout_gravity="center_vertical"
|
||||
android:src="@drawable/ic_plus"
|
||||
android:tint="@android:color/white"/>
|
||||
app:tint="@android:color/white"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<com.google.android.material.chip.Chip
|
||||
android:id="@+id/suggestCurrentSsid"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="16dp"
|
||||
android:text="@string/add_ssid_name_suggestion"
|
||||
app:chipIcon="@drawable/ic_wifi"
|
||||
app:chipIconSize="20dp"
|
||||
app:chipIconTint="?attr/colorControlNormal"
|
||||
android:visibility="gone"
|
||||
tools:visibility="visible" />
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/rvSsids"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="1"
|
||||
android:layout_marginTop="16dp"
|
||||
android:layout_marginBottom="16dp"
|
||||
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
|
||||
tools:itemCount="4"
|
||||
tools:listitem="@layout/item_ssid" />
|
||||
|
|
|
@ -33,9 +33,9 @@
|
|||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="?attr/selectableItemBackground"
|
||||
android:contentDescription="@null"
|
||||
android:contentDescription="@string/remove_ssid"
|
||||
android:src="@drawable/ic_clear_black"
|
||||
android:tint="@color/colorWarning"
|
||||
app:tint="@color/colorWarning"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintRight_toRightOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
|
|
@ -5,6 +5,8 @@
|
|||
<string name="activity_intent_error">Unable to send activity intent, please check command format</string>
|
||||
<string name="add_service_data_field">Add Field</string>
|
||||
<string name="add_shortcut">Add Shortcut</string>
|
||||
<string name="add_ssid">Add SSID</string>
|
||||
<string name="add_ssid_name_suggestion">Add %1$s</string>
|
||||
<string name="add_widget">Add widget</string>
|
||||
<string name="all_entities">All entities</string>
|
||||
<string name="app_name">Home Assistant</string>
|
||||
|
@ -390,6 +392,7 @@
|
|||
<string name="registerDevice">Register watch</string>
|
||||
<string name="remaining">Remaining</string>
|
||||
<string name="remember">Remember</string>
|
||||
<string name="remove_ssid">Remove SSID</string>
|
||||
<string name="required_fields">requires a username and password.</string>
|
||||
<string name="resets_at">Resets at</string>
|
||||
<string name="retry">Retry</string>
|
||||
|
|
Loading…
Reference in a new issue