Skip to content

Commit

Permalink
create bluetooth settings dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
newhinton committed Mar 3, 2024
1 parent c0a4928 commit 71020a8
Show file tree
Hide file tree
Showing 5 changed files with 416 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
package de.felixnuesse.timedsilence.dialogs

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.Window
import android.view.WindowManager
import android.widget.ArrayAdapter
import de.felixnuesse.timedsilence.R
import de.felixnuesse.timedsilence.databinding.DialogBluetoothBinding
import de.felixnuesse.timedsilence.extensions.TAG
import de.felixnuesse.timedsilence.fragments.BluetoothFragment
import de.felixnuesse.timedsilence.handler.calculator.HeadsetHandler
import de.felixnuesse.timedsilence.handler.volume.VolumeState.Companion.TIME_SETTING_LOUD
import de.felixnuesse.timedsilence.handler.volume.VolumeState.Companion.TIME_SETTING_SILENT
import de.felixnuesse.timedsilence.handler.volume.VolumeState.Companion.TIME_SETTING_VIBRATE
import de.felixnuesse.timedsilence.model.data.BluetoothObject
import de.felixnuesse.timedsilence.model.database.DatabaseHandler
import de.felixnuesse.timedsilence.util.WindowUtils


/**
* Copyright (C) 2024 Felix Nüsse
* Created on 03.03.2024
*
* Edited by: Felix Nüsse felix.nuesse(at)t-online.de
*
*
* This program is released under the GPLv3 license
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*
*
*/
class BluetoothDialog(context: Context) : Dialog(context, R.style.AlertDialogCustom) {

private var fragment: BluetoothFragment? = null

private lateinit var binding: DialogBluetoothBinding

private var selectedDeviceName = ""

constructor(context: Context, fragment: BluetoothFragment) : this(context) {
this.fragment = fragment
}

private var state: Int = 0

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
requestWindowFeature(Window.FEATURE_NO_TITLE)

window?.let { WindowUtils.applyDialogPaddingFixForDarkmode(context, it) }

binding = DialogBluetoothBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)

window!!.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT)
setCanceledOnTouchOutside(true)

val devices = arrayListOf<String>()

val existingDevices = HashMap<String, String>()
HeadsetHandler.getPairedDevicesWithChangesInVolume(context).forEach{
existingDevices[it.name] = it.address
}

HeadsetHandler.getPairedDevices(context).forEach{
if(!existingDevices.containsKey(it.name)){
devices.add(it.name)
}
}

ArrayAdapter(context, android.R.layout.simple_list_item_1, devices).also {
adapter -> binding.filledExposedDropdown.setAdapter(adapter)
}

binding.filledExposedDropdown.setOnItemClickListener{ adapter, _, position, _ ->
selectedDeviceName = adapter.adapter.getItem(position).toString()
}

hideAll()
binding.bluetoothBack.visibility = View.INVISIBLE
binding.bluetoothDialogTitle.text = context.getText(R.string.bluetooth_dialog_title_title)
binding.bluetoothLayout.visibility = View.VISIBLE

binding.bluetoothNext.setOnClickListener {
Log.e(TAG(), "BluetoothDialog: next!")

hideAll()
state++
decideState()
}

binding.bluetoothBack.setOnClickListener {
Log.e(TAG(), "BluetoothDialog: back!")

hideAll()
state--
decideState()
}

binding.bluetoothCancel.setOnClickListener {
Log.e(TAG(), "BluetoothDialog: cancel!")
this.cancel()
}

binding.bluetoothSave.setOnClickListener {
Log.e(TAG(), "BluetoothDialog: save!")

var device = BluetoothObject("", "")
HeadsetHandler.getPairedDevices(context).forEach{
if(it.name == selectedDeviceName) {
device = BluetoothObject(it.name, it.address)
}
}

if(device.address.isEmpty()) {
this.cancel()
}

device.volumeState = getValueForVolumeRadioGroup()
DatabaseHandler(context).addOrUpdateBluetooth(device)
fragment?.notifyChange()
this.cancel()
}
}

private fun hideAll() {
binding.bluetoothLayout.visibility = View.GONE
binding.bluetoothDialogRbVolume.visibility = View.GONE
}

private fun getValueForVolumeRadioGroup(): Int{
when (binding.bluetoothDialogRbVolume.checkedRadioButtonId) {
R.id.keyword_dialog_rb_loud -> return TIME_SETTING_LOUD
R.id.keyword_dialog_rb_silent -> return TIME_SETTING_SILENT
R.id.keyword_dialog_rb_vibrate -> return TIME_SETTING_VIBRATE
}
return TIME_SETTING_VIBRATE
}

private fun decideState() {

if(state==0){
binding.bluetoothBack.visibility = View.INVISIBLE
binding.bluetoothSave.visibility = View.GONE
binding.bluetoothNext.visibility = View.VISIBLE
}else if (state == 1){
binding.bluetoothSave.visibility = View.VISIBLE
binding.bluetoothBack.visibility = View.VISIBLE
binding.bluetoothNext.visibility = View.GONE
}else {
binding.bluetoothBack.visibility = View.VISIBLE
binding.bluetoothNext.visibility = View.VISIBLE
binding.bluetoothSave.visibility = View.GONE
}

when (state) {
0 -> {
binding.bluetoothDialogTitle.text = context.getText(R.string.keyword_dialog_title_title)
binding.bluetoothLayout.visibility = View.VISIBLE
}
1 -> {
binding.bluetoothDialogTitle.text = context.getText(R.string.schedule_dialog_title_volume)
binding.bluetoothDialogRbVolume.visibility = View.VISIBLE

}

}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import de.felixnuesse.timedsilence.databinding.FragmentBluetoothBinding
import de.felixnuesse.timedsilence.dialogs.BluetoothDialog
import de.felixnuesse.timedsilence.handler.calculator.HeadsetHandler
import de.felixnuesse.timedsilence.ui.BluetoothListAdapter

Expand All @@ -32,6 +33,10 @@ class BluetoothFragment : Fragment() {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
setList(view.context)

binding.buttonAddDevice.setOnClickListener {
BluetoothDialog(view.context, this).show()
}
}

override fun onResume() {
Expand All @@ -50,15 +55,23 @@ class BluetoothFragment : Fragment() {
layoutManager = viewManager
adapter = viewAdapter
}

binding.noPairedMessage.visibility = if(pairedDevices.size == 0) {
View.VISIBLE
if(HeadsetHandler.getPairedDevices(context).size == 0) {
binding.noPairedMessage.visibility = View.VISIBLE
binding.bluetoothFragmentRecylcerListView.visibility = View.GONE
binding.buttonAddDevice.visibility = View.GONE
} else {
View.GONE
binding.noPairedMessage.visibility = View.GONE
binding.bluetoothFragmentRecylcerListView.visibility = View.VISIBLE
binding.buttonAddDevice.visibility = View.VISIBLE
}

}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}

fun notifyChange() {
setList(requireContext())
}
}
139 changes: 139 additions & 0 deletions app/src/main/res/layout/dialog_bluetooth.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/colorPrimaryContainer"
android:orientation="vertical">

<TextView
android:id="@+id/bluetooth_dialog_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<LinearLayout
android:id="@+id/bluetooth_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">

<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/dialog_bluetooth_select_device">

<com.google.android.material.textfield.MaterialAutoCompleteTextView
android:id="@+id/filled_exposed_dropdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>


<RadioGroup
android:id="@+id/bluetooth_dialog_rb_volume"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">

<RadioButton
android:id="@+id/bluetooth_dialog_rb_loud"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/volume_setting_loud" />

<RadioButton
android:id="@+id/bluetooth_dialog_rb_silent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/volume_setting_silent" />

<RadioButton
android:id="@+id/bluetooth_dialog_rb_vibrate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="true"
android:text="@string/volume_setting_vibrate" />
</RadioGroup>

</LinearLayout>

</ScrollView>

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

<Button
android:id="@+id/bluetoothCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
android:minWidth="0dp"
android:text="@string/cancel" />

<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />

<Button
android:id="@+id/bluetoothBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
android:minWidth="0dp"
android:text="@string/back" />

<Button
android:id="@+id/bluetoothNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
android:minWidth="0dp"
android:text="@string/next" />

<Button
android:id="@+id/bluetoothSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
android:minWidth="0dp"
android:text="@string/calendar_dialog_save"
android:visibility="gone" />
</LinearLayout>


</LinearLayout>
Loading

0 comments on commit 71020a8

Please sign in to comment.