Skip to content

Commit

Permalink
switch bluetooth list item to "default" layout like the others
Browse files Browse the repository at this point in the history
  • Loading branch information
newhinton committed Mar 3, 2024
1 parent c8fc078 commit c0a4928
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
}

Expand All @@ -47,9 +47,7 @@ class HeadsetHandler {
fun getPairedDevices(context: Context): ArrayList<BluetoothObject> {
val list = arrayListOf<BluetoothObject>()

Log.e(TAG, "paired dev")
if(!PermissionManager(context).grantedBluetoothAccess()) {
Log.e(TAG, "no perm")
PermissionManager(context).requestBluetooth()
return list
}
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -97,6 +92,19 @@ class HeadsetHandler {
return result
}

fun getPairedDevicesWithChangesInVolume(context: Context): ArrayList<BluetoothObject> {
var result = arrayListOf<BluetoothObject>()
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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -41,19 +41,16 @@ import de.felixnuesse.timedsilence.model.database.DatabaseHandler
*/
class BluetoothListAdapter(private var myDataset: ArrayList<BluetoothObject>, private var mContext: Context) : RecyclerView.Adapter<BluetoothListAdapter.BluetoothViewHolder>() {


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
Expand All @@ -63,42 +60,52 @@ class BluetoothListAdapter(private var myDataset: ArrayList<BluetoothObject>, 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
Expand Down
96 changes: 67 additions & 29 deletions app/src/main/res/layout/adapter_bluetooth_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,93 @@

<androidx.cardview.widget.CardView
android:id="@+id/cardView"
style="@style/primaryCard"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="@dimen/cardMargin"
android:padding="@dimen/cardPadding"
style="@style/primaryCard"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintEnd_toStartOf="@+id/edit_element"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<LinearLayout
android:id="@+id/layoutWeekdays"
android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:gravity="center"
android:weightSum="10">
android:orientation="horizontal">

<ImageView
android:id="@+id/volumeState"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
android:layout_weight="1"
style="@style/primaryCard"
app:srcCompat="@drawable/icon_bluetooth" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@android:drawable/btn_dialog" />

<TextView
android:id="@+id/rowTitle"
android:layout_width="0dp"
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="title"
style="@style/primaryCard" />

<Spinner
android:id="@+id/volumeSpinner"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:layout_weight="4" />
android:layout_marginStart="12dp"
android:orientation="vertical">

<TextView
android:id="@+id/rowTitle"
style="@style/primaryCard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="@string/adapter_bluetooth_device_name"
android:text="title" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<ImageView
android:id="@+id/deviceType"
style="@style/primaryCard"
android:layout_width="16dp"
android:layout_height="match_parent"
app:srcCompat="@drawable/icon_bluetooth" />

<TextView
android:id="@+id/deviceTypeDescription"
style="@style/primaryCard"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:contentDescription="@string/adapter_bluetooth_description_of_bluetooth_device_type"
android:text="TextView" />
</LinearLayout>

</LinearLayout>

</LinearLayout>

</androidx.cardview.widget.CardView>


<ImageButton
android:id="@+id/delete_element"
android:layout_width="48dp"
android:layout_height="48dp"
android:background="@null"
android:src="@drawable/icon_delete"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ImageButton
android:id="@+id/edit_element"
android:layout_width="48dp"
android:layout_height="48dp"
android:background="@null"
android:src="@drawable/icon_edit"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/delete_element"
app:layout_constraintTop_toTopOf="parent" />



</androidx.constraintlayout.widget.ConstraintLayout>
2 changes: 1 addition & 1 deletion app/src/main/res/layout/adapter_calendar_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
</LinearLayout>
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,16 @@
<string name="fragment_title_bluetooth">Bluetooth</string>
<string name="main_tab_bluetooth">Bluetooth</string>
<string name="checkup_collapse_priority_contacts">collapse list of priority contacts</string>
<string name="adapter_bluetooth_description_of_bluetooth_device_type">Description of bluetooth device type</string>
<string name="adapter_bluetooth_device_name">bluetooth device name</string>
<string name="adapter_bluetooth_device_description_computer">Computer</string>
<string name="adapter_bluetooth_device_description_phone">Phone</string>
<string name="adapter_bluetooth_device_description_networking">Networking</string>
<string name="adapter_bluetooth_device_description_audio_video">Audio/Video</string>
<string name="adapter_bluetooth_device_description_peripheral">Peripheral</string>
<string name="adapter_bluetooth_device_description_imaging">Imaging</string>
<string name="adapter_bluetooth_device_description_wearable">Wearable</string>
<string name="adapter_bluetooth_device_description_toy">Toy</string>
<string name="adapter_bluetooth_device_description_health">Health</string>
<string name="adapter_bluetooth_device_description_misc">Misc</string>
</resources>

0 comments on commit c0a4928

Please sign in to comment.