From c0a4928587798563e5513dd14f97882e507b4d67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20N=C3=BCsse?= Date: Sun, 3 Mar 2024 11:24:54 +0100 Subject: [PATCH] switch bluetooth list item to "default" layout like the others --- .../fragments/BluetoothFragment.kt | 2 +- .../handler/calculator/HeadsetHandler.kt | 28 ++++-- .../model/database/DatabaseHandler.kt | 9 ++ .../timedsilence/ui/BluetoothListAdapter.kt | 73 +++++++------- .../res/layout/adapter_bluetooth_list.xml | 96 +++++++++++++------ .../main/res/layout/adapter_calendar_list.xml | 2 +- app/src/main/res/values/strings.xml | 12 +++ 7 files changed, 148 insertions(+), 74 deletions(-) diff --git a/app/src/main/java/de/felixnuesse/timedsilence/fragments/BluetoothFragment.kt b/app/src/main/java/de/felixnuesse/timedsilence/fragments/BluetoothFragment.kt index 47c4e6a5..b30d565d 100644 --- a/app/src/main/java/de/felixnuesse/timedsilence/fragments/BluetoothFragment.kt +++ b/app/src/main/java/de/felixnuesse/timedsilence/fragments/BluetoothFragment.kt @@ -42,7 +42,7 @@ class BluetoothFragment : Fragment() { private fun setList(context: Context) { viewManager = LinearLayoutManager(context) - val pairedDevices = HeadsetHandler.getPairedDevicesWithDatabaseState(context) + val pairedDevices = HeadsetHandler.getPairedDevicesWithChangesInVolume(context) viewAdapter = BluetoothListAdapter(pairedDevices, context) binding.bluetoothFragmentRecylcerListView.apply { diff --git a/app/src/main/java/de/felixnuesse/timedsilence/handler/calculator/HeadsetHandler.kt b/app/src/main/java/de/felixnuesse/timedsilence/handler/calculator/HeadsetHandler.kt index 8b877364..818dc024 100644 --- a/app/src/main/java/de/felixnuesse/timedsilence/handler/calculator/HeadsetHandler.kt +++ b/app/src/main/java/de/felixnuesse/timedsilence/handler/calculator/HeadsetHandler.kt @@ -9,6 +9,8 @@ import android.media.AudioDeviceInfo import android.media.AudioManager import android.os.Build import android.util.Log +import de.felixnuesse.timedsilence.extensions.TAG +import de.felixnuesse.timedsilence.handler.volume.VolumeState import de.felixnuesse.timedsilence.model.data.BluetoothObject import de.felixnuesse.timedsilence.model.database.DatabaseHandler import de.felixnuesse.timedsilence.util.PermissionManager @@ -18,18 +20,16 @@ import java.lang.reflect.Method class HeadsetHandler { companion object { - private const val TAG = "BluetoothHandler" - //https://stackoverflow.com/questions/16395054/check-whether-headphones-are-plugged-in fun headphonesConnected(context: Context): Boolean { val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager? var isConnected = false - Log.d(TAG, "Checking devices") + Log.d(TAG(), "Checking devices") for (deviceInfo in audioManager!!.getDevices(AudioManager.GET_DEVICES_OUTPUTS)) { - Log.d(TAG, "Devicetype: "+deviceInfo.type) + Log.d(TAG(), "Devicetype: "+deviceInfo.type) when (deviceInfo.type) { AudioDeviceInfo.TYPE_WIRED_HEADPHONES -> isConnected = true @@ -38,7 +38,7 @@ class HeadsetHandler { AudioDeviceInfo.TYPE_BLUETOOTH_A2DP -> isConnected = true } } - Log.d(TAG, "Found Headset: $isConnected") + Log.d(TAG(), "Found Headset: $isConnected") return isConnected } @@ -47,9 +47,7 @@ class HeadsetHandler { fun getPairedDevices(context: Context): ArrayList { val list = arrayListOf() - Log.e(TAG, "paired dev") if(!PermissionManager(context).grantedBluetoothAccess()) { - Log.e(TAG, "no perm") PermissionManager(context).requestBluetooth() return list } @@ -61,8 +59,6 @@ class HeadsetHandler { } val pairedDevices = btManager.adapter.bondedDevices - - Log.e(TAG, "paired dev ${pairedDevices.size}") if (pairedDevices.size > 0) { for (device in pairedDevices) { @@ -77,7 +73,6 @@ class HeadsetHandler { val bluetoothObject = BluetoothObject(deviceName, macAddress, aliasing, isConnected(device)) bluetoothObject.type = device.bluetoothClass.majorDeviceClass list.add(bluetoothObject) - Log.e(TAG,"paired device: $bluetoothObject") } } return list @@ -97,6 +92,19 @@ class HeadsetHandler { return result } + fun getPairedDevicesWithChangesInVolume(context: Context): ArrayList { + var result = arrayListOf() + var devices = getPairedDevicesWithDatabaseState(context) + + devices.forEach { bl -> + if(bl.volumeState != 0) { + result.add(bl) + } + } + + return result + } + private fun isConnected(device: BluetoothDevice): Boolean { return try { val m: Method = device.javaClass.getMethod("isConnected") diff --git a/app/src/main/java/de/felixnuesse/timedsilence/model/database/DatabaseHandler.kt b/app/src/main/java/de/felixnuesse/timedsilence/model/database/DatabaseHandler.kt index 8337af92..ea1b2cc2 100644 --- a/app/src/main/java/de/felixnuesse/timedsilence/model/database/DatabaseHandler.kt +++ b/app/src/main/java/de/felixnuesse/timedsilence/model/database/DatabaseHandler.kt @@ -762,4 +762,13 @@ class DatabaseHandler (context: Context) : SQLiteOpenHelper(context, DATABASE_NA db.close() } + fun deleteBluetoothDevice(macadress: String): Int { + val db = writableDatabase + val selection = BLUETOOTH_MAC + " LIKE ?" + val selectionArgs = arrayOf(macadress) + val retcode: Int = db.delete(BLUETOOTH_TABLE, selection, selectionArgs) + db.close() + return retcode + } + } \ No newline at end of file diff --git a/app/src/main/java/de/felixnuesse/timedsilence/ui/BluetoothListAdapter.kt b/app/src/main/java/de/felixnuesse/timedsilence/ui/BluetoothListAdapter.kt index a9fbcddd..d60db2bf 100644 --- a/app/src/main/java/de/felixnuesse/timedsilence/ui/BluetoothListAdapter.kt +++ b/app/src/main/java/de/felixnuesse/timedsilence/ui/BluetoothListAdapter.kt @@ -5,12 +5,12 @@ import android.content.Context import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import android.widget.AdapterView -import android.widget.ArrayAdapter import android.widget.Toast +import androidx.appcompat.content.res.AppCompatResources import androidx.recyclerview.widget.RecyclerView import de.felixnuesse.timedsilence.R import de.felixnuesse.timedsilence.databinding.AdapterBluetoothListBinding +import de.felixnuesse.timedsilence.handler.volume.VolumeState import de.felixnuesse.timedsilence.model.data.BluetoothObject import de.felixnuesse.timedsilence.model.database.DatabaseHandler @@ -41,19 +41,16 @@ import de.felixnuesse.timedsilence.model.database.DatabaseHandler */ class BluetoothListAdapter(private var myDataset: ArrayList, private var mContext: Context) : RecyclerView.Adapter() { - - private var db = DatabaseHandler(mContext) override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BluetoothViewHolder { val binding = AdapterBluetoothListBinding.inflate(LayoutInflater.from(parent.context), parent, false) return BluetoothViewHolder(binding) } override fun onBindViewHolder(holder: BluetoothViewHolder, position: Int) { - val context = holder.view.root.context val bluetoothDevice = myDataset[position] holder.view.rowTitle.text = bluetoothDevice.name - val resourceId = when(bluetoothDevice.type) { + val resourceIdDeviceType = when(bluetoothDevice.type) { BluetoothClass.Device.Major.COMPUTER -> R.drawable.icon_computer BluetoothClass.Device.Major.PHONE -> R.drawable.icon_phone BluetoothClass.Device.Major.NETWORKING -> R.drawable.icon_lan @@ -63,42 +60,52 @@ class BluetoothListAdapter(private var myDataset: ArrayList, pr BluetoothClass.Device.Major.WEARABLE -> R.drawable.icon_watch BluetoothClass.Device.Major.TOY -> R.drawable.icon_toy BluetoothClass.Device.Major.HEALTH -> R.drawable.icon_health - - else -> { - R.drawable.icon_bluetooth - } + else -> R.drawable.icon_bluetooth } - holder.view.volumeState.setImageDrawable(mContext.getDrawable(resourceId)) + holder.view.deviceType.setImageDrawable(AppCompatResources.getDrawable(mContext, resourceIdDeviceType)) + + val stringIdDeviceDescription = when(bluetoothDevice.type) { + BluetoothClass.Device.Major.COMPUTER -> R.string.adapter_bluetooth_device_description_computer + BluetoothClass.Device.Major.PHONE -> R.string.adapter_bluetooth_device_description_phone + BluetoothClass.Device.Major.NETWORKING -> R.string.adapter_bluetooth_device_description_networking + BluetoothClass.Device.Major.AUDIO_VIDEO -> R.string.adapter_bluetooth_device_description_audio_video + BluetoothClass.Device.Major.PERIPHERAL -> R.string.adapter_bluetooth_device_description_peripheral + BluetoothClass.Device.Major.IMAGING -> R.string.adapter_bluetooth_device_description_imaging + BluetoothClass.Device.Major.WEARABLE -> R.string.adapter_bluetooth_device_description_wearable + BluetoothClass.Device.Major.TOY -> R.string.adapter_bluetooth_device_description_toy + BluetoothClass.Device.Major.HEALTH -> R.string.adapter_bluetooth_device_description_health + else -> R.string.adapter_bluetooth_device_description_misc + } - val spinnerArray = listOf( - context.getString(R.string.volume_setting_unset), - context.getString(R.string.volume_setting_silent), - context.getString(R.string.volume_setting_vibrate), - context.getString(R.string.volume_setting_loud) - ) + holder.view.deviceTypeDescription.text = mContext.getString(stringIdDeviceDescription) - val spinner = holder.view.volumeSpinner - val adapter = ArrayAdapter( - context, - android.R.layout.simple_spinner_item, - spinnerArray - ) + val resourceIdVolumeState = when (bluetoothDevice.volumeState) { + VolumeState.TIME_SETTING_LOUD -> R.drawable.icon_volume_up + VolumeState.TIME_SETTING_VIBRATE -> R.drawable.icon_vibration + VolumeState.TIME_SETTING_SILENT -> R.drawable.icon_volume_off + else -> R.drawable.icon_volume_up + } - spinner.adapter = adapter + holder.view.volumeState.setImageDrawable(AppCompatResources.getDrawable(mContext, resourceIdVolumeState)) - spinner.setSelection(bluetoothDevice.volumeState) + holder.view.editElement.setOnClickListener { + //todo: Implement adding bluetooth device + Toast.makeText(mContext, "Not Implemented! Todo!", Toast.LENGTH_SHORT).show() + //db.addOrUpdateBluetooth(bluetoothDevice) + } - spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { - override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) { - bluetoothDevice.volumeState = position - db.addOrUpdateBluetooth(bluetoothDevice) - } + holder.view.deleteElement.setOnClickListener { + DatabaseHandler(mContext).deleteBluetoothDevice(bluetoothDevice.address) + removeAt(position) + } + } - override fun onNothingSelected(parent: AdapterView<*>?) { - } - } + private fun removeAt(position: Int) { + myDataset.removeAt(position) + notifyItemRemoved(position) + notifyItemRangeChanged(position, myDataset.size) } override fun getItemCount() = myDataset.size diff --git a/app/src/main/res/layout/adapter_bluetooth_list.xml b/app/src/main/res/layout/adapter_bluetooth_list.xml index 87140946..6985126e 100644 --- a/app/src/main/res/layout/adapter_bluetooth_list.xml +++ b/app/src/main/res/layout/adapter_bluetooth_list.xml @@ -8,55 +8,93 @@ + android:orientation="horizontal"> + android:layout_width="wrap_content" + android:layout_height="wrap_content" + app:srcCompat="@android:drawable/btn_dialog" /> - - - + android:layout_marginStart="12dp" + android:orientation="vertical"> + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/adapter_calendar_list.xml b/app/src/main/res/layout/adapter_calendar_list.xml index 4cea8da7..9d4cf2a9 100644 --- a/app/src/main/res/layout/adapter_calendar_list.xml +++ b/app/src/main/res/layout/adapter_calendar_list.xml @@ -37,7 +37,7 @@ android:id="@+id/textView_calendar_row_title" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:paddingStart="12dp" + android:layout_marginStart="12dp" android:text="title" android:textColor="@android:color/white" /> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c35843dc..b13ba78b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -168,4 +168,16 @@ Bluetooth Bluetooth collapse list of priority contacts + Description of bluetooth device type + bluetooth device name + Computer + Phone + Networking + Audio/Video + Peripheral + Imaging + Wearable + Toy + Health + Misc