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