From 2f29a130d541155be1a4a3c46000302c5c5bcc11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20N=C3=BCsse?= Date: Mon, 8 Jan 2024 09:58:26 +0100 Subject: [PATCH] implement basic bluetooth ui --- .../felixnuesse/timedsilence/MainActivity.kt | 4 +- .../fragments/BluetoothFragment.kt | 47 ++++++++++++ .../handler/calculator/BluetoothHandler.kt | 8 +-- .../model/data/BluetoothObject.kt | 6 +- .../timedsilence/ui/BluetoothListAdapter.kt | 72 +++++++++++++++++++ app/src/main/res/drawable/icon_bluetooth.xml | 5 ++ app/src/main/res/layout/activity_main.xml | 6 ++ .../res/layout/adapter_bluetooth_list.xml | 54 ++++++++++++++ .../main/res/layout/fragment_bluetooth.xml | 43 +++++++++++ app/src/main/res/values/strings.xml | 2 + 10 files changed, 239 insertions(+), 8 deletions(-) create mode 100644 app/src/main/java/de/felixnuesse/timedsilence/fragments/BluetoothFragment.kt create mode 100644 app/src/main/java/de/felixnuesse/timedsilence/ui/BluetoothListAdapter.kt create mode 100644 app/src/main/res/drawable/icon_bluetooth.xml create mode 100644 app/src/main/res/layout/adapter_bluetooth_list.xml create mode 100644 app/src/main/res/layout/fragment_bluetooth.xml diff --git a/app/src/main/java/de/felixnuesse/timedsilence/MainActivity.kt b/app/src/main/java/de/felixnuesse/timedsilence/MainActivity.kt index c72a939..a0a7634 100644 --- a/app/src/main/java/de/felixnuesse/timedsilence/MainActivity.kt +++ b/app/src/main/java/de/felixnuesse/timedsilence/MainActivity.kt @@ -55,6 +55,7 @@ import com.google.android.material.tabs.TabLayout import de.felixnuesse.timedsilence.Constants.Companion.MAIN_ACTIVITY_LOAD_CALENDAR_FORCE import de.felixnuesse.timedsilence.IntroActivity.Companion.INTRO_PREFERENCES import de.felixnuesse.timedsilence.databinding.ActivityMainBinding +import de.felixnuesse.timedsilence.fragments.BluetoothFragment import de.felixnuesse.timedsilence.fragments.CalendarFragment import de.felixnuesse.timedsilence.fragments.KeywordFragment import de.felixnuesse.timedsilence.fragments.TimeFragment @@ -358,7 +359,7 @@ class MainActivity : AppCompatActivity(), TimerInterface { */ private inner class ScreenSlidePagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) { - override fun getCount(): Int = 5 + override fun getCount(): Int = 6 override fun getItem(position: Int): Fragment { @@ -368,6 +369,7 @@ class MainActivity : AppCompatActivity(), TimerInterface { 2 -> return CalendarFragment() 3 -> return KeywordFragment() 4 -> return WifiConnectedFragment() + 5 -> return BluetoothFragment() else -> return TimeFragment() } } diff --git a/app/src/main/java/de/felixnuesse/timedsilence/fragments/BluetoothFragment.kt b/app/src/main/java/de/felixnuesse/timedsilence/fragments/BluetoothFragment.kt new file mode 100644 index 0000000..bb84766 --- /dev/null +++ b/app/src/main/java/de/felixnuesse/timedsilence/fragments/BluetoothFragment.kt @@ -0,0 +1,47 @@ +package de.felixnuesse.timedsilence.fragments + +import android.os.Bundle +import androidx.fragment.app.Fragment +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.recyclerview.widget.RecyclerView +import de.felixnuesse.timedsilence.databinding.FragmentBluetoothBinding +import de.felixnuesse.timedsilence.handler.calculator.BluetoothHandler +import de.felixnuesse.timedsilence.ui.BluetoothListAdapter +import de.felixnuesse.timedsilence.ui.custom.NestedRecyclerManager + + +class BluetoothFragment : Fragment() { + + private lateinit var viewAdapter: RecyclerView.Adapter<*> + private lateinit var viewManager: RecyclerView.LayoutManager + + private var _binding: FragmentBluetoothBinding? = null + private val binding get() = _binding!! + + + override fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + _binding = FragmentBluetoothBinding.inflate(inflater, container, false) + return binding.root + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + viewManager = NestedRecyclerManager(view.context) + viewAdapter = BluetoothListAdapter(BluetoothHandler.getPairedDevices(view.context)) + + binding.bluetoothFragmentRecylcerListView.apply { + setHasFixedSize(true) + layoutManager = viewManager + adapter = viewAdapter + } + } + + override fun onDestroyView() { + super.onDestroyView() + _binding = null + } +} diff --git a/app/src/main/java/de/felixnuesse/timedsilence/handler/calculator/BluetoothHandler.kt b/app/src/main/java/de/felixnuesse/timedsilence/handler/calculator/BluetoothHandler.kt index c486398..93b5d91 100644 --- a/app/src/main/java/de/felixnuesse/timedsilence/handler/calculator/BluetoothHandler.kt +++ b/app/src/main/java/de/felixnuesse/timedsilence/handler/calculator/BluetoothHandler.kt @@ -57,6 +57,7 @@ class BluetoothHandler { } val btManager = context.getSystemService(BLUETOOTH_SERVICE) as BluetoothManager + //todo: Fix this. When the device does not have bluetooth, the app crashes val pairedDevices = btManager.adapter.bondedDevices @@ -71,11 +72,10 @@ class BluetoothHandler { } else { deviceName } - Log.e(TAG,"paired device: $deviceName at $macAddress + $aliasing ") - if( isConnected(device)){ - list.add(BluetoothObject(deviceName, macAddress, aliasing)) - } + val bluetoothObject = BluetoothObject(deviceName, macAddress, aliasing, isConnected(device)) + list.add(bluetoothObject) + Log.e(TAG,"paired device: $bluetoothObject") } } return list diff --git a/app/src/main/java/de/felixnuesse/timedsilence/model/data/BluetoothObject.kt b/app/src/main/java/de/felixnuesse/timedsilence/model/data/BluetoothObject.kt index fe90dd8..a7f602b 100644 --- a/app/src/main/java/de/felixnuesse/timedsilence/model/data/BluetoothObject.kt +++ b/app/src/main/java/de/felixnuesse/timedsilence/model/data/BluetoothObject.kt @@ -32,10 +32,10 @@ import kotlinx.serialization.Serializable */ @Serializable -data class BluetoothObject(var name: String, var address: String, var alias: String) { - constructor(name: String, address: String) : this(name, address, name) {} +data class BluetoothObject(var name: String, var address: String, var alias: String, var connected: Boolean) { + constructor(name: String, address: String) : this(name, address, name, false) {} override fun toString(): String { - return "BluetoothObject(name='$name', address='$address', alias='$alias')" + return "BluetoothObject(name='$name', address='$address', alias='$alias', connected='$connected')" } diff --git a/app/src/main/java/de/felixnuesse/timedsilence/ui/BluetoothListAdapter.kt b/app/src/main/java/de/felixnuesse/timedsilence/ui/BluetoothListAdapter.kt new file mode 100644 index 0000000..596132e --- /dev/null +++ b/app/src/main/java/de/felixnuesse/timedsilence/ui/BluetoothListAdapter.kt @@ -0,0 +1,72 @@ +package de.felixnuesse.timedsilence.ui; + +import android.view.LayoutInflater +import android.view.ViewGroup +import android.widget.ArrayAdapter +import androidx.recyclerview.widget.RecyclerView +import de.felixnuesse.timedsilence.R +import de.felixnuesse.timedsilence.databinding.AdapterBluetoothListBinding +import de.felixnuesse.timedsilence.model.data.BluetoothObject + + +/** + * Copyright (C) 2019 Felix Nüsse + * Created on 14.04.19 - 17:18 + *

+ * 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 BluetoothListAdapter(private var myDataset: ArrayList) : RecyclerView.Adapter() { + + 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 calObject= myDataset[position] + val context = holder.view.root.context + + holder.view.rowTitle.text = calObject.name + + 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) + ) + + val spinner = holder.view.volumeSpinner + val adapter = ArrayAdapter( + context, + android.R.layout.simple_spinner_item, + spinnerArray + ) + + spinner.adapter = adapter + } + + override fun getItemCount() = myDataset.size + + class BluetoothViewHolder(val view: AdapterBluetoothListBinding) : RecyclerView.ViewHolder(view.root) + +} + + diff --git a/app/src/main/res/drawable/icon_bluetooth.xml b/app/src/main/res/drawable/icon_bluetooth.xml new file mode 100644 index 0000000..93f7aa0 --- /dev/null +++ b/app/src/main/res/drawable/icon_bluetooth.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index bf96c15..14cb24e 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -56,6 +56,12 @@ android:layout_height="wrap_content" android:icon="@drawable/icon_wifi" android:text="@string/main_tab_wifi_connected" /> + + diff --git a/app/src/main/res/layout/adapter_bluetooth_list.xml b/app/src/main/res/layout/adapter_bluetooth_list.xml new file mode 100644 index 0000000..de56489 --- /dev/null +++ b/app/src/main/res/layout/adapter_bluetooth_list.xml @@ -0,0 +1,54 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_bluetooth.xml b/app/src/main/res/layout/fragment_bluetooth.xml new file mode 100644 index 0000000..cf4e406 --- /dev/null +++ b/app/src/main/res/layout/fragment_bluetooth.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 89639ff..89aefac 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -146,4 +146,6 @@ Pause Timed Silence donotdelete-placeholder + Bluetooth + Bluetooth