From 71020a8e6834e7f2e6062a6b4ccf940a02d82c6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20N=C3=BCsse?= Date: Sun, 3 Mar 2024 12:17:30 +0100 Subject: [PATCH] create bluetooth settings dialog --- .../timedsilence/dialogs/BluetoothDialog.kt | 190 ++++++++++++++++++ .../fragments/BluetoothFragment.kt | 21 +- app/src/main/res/layout/dialog_bluetooth.xml | 139 +++++++++++++ .../main/res/layout/fragment_bluetooth.xml | 90 ++++++--- app/src/main/res/values/strings.xml | 3 + 5 files changed, 416 insertions(+), 27 deletions(-) create mode 100644 app/src/main/java/de/felixnuesse/timedsilence/dialogs/BluetoothDialog.kt create mode 100644 app/src/main/res/layout/dialog_bluetooth.xml diff --git a/app/src/main/java/de/felixnuesse/timedsilence/dialogs/BluetoothDialog.kt b/app/src/main/java/de/felixnuesse/timedsilence/dialogs/BluetoothDialog.kt new file mode 100644 index 0000000..ea5ae7f --- /dev/null +++ b/app/src/main/java/de/felixnuesse/timedsilence/dialogs/BluetoothDialog.kt @@ -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() + + val existingDevices = HashMap() + 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 + + } + + } + + } +} \ No newline at end of file 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 b30d565..9ff88aa 100644 --- a/app/src/main/java/de/felixnuesse/timedsilence/fragments/BluetoothFragment.kt +++ b/app/src/main/java/de/felixnuesse/timedsilence/fragments/BluetoothFragment.kt @@ -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 @@ -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() { @@ -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()) + } } diff --git a/app/src/main/res/layout/dialog_bluetooth.xml b/app/src/main/res/layout/dialog_bluetooth.xml new file mode 100644 index 0000000..f365338 --- /dev/null +++ b/app/src/main/res/layout/dialog_bluetooth.xml @@ -0,0 +1,139 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +