Skip to content

Commit

Permalink
implement basic bluetooth ui
Browse files Browse the repository at this point in the history
  • Loading branch information
newhinton committed Jan 8, 2024
1 parent 2fb3a48 commit 2f29a13
Show file tree
Hide file tree
Showing 10 changed files with 239 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {

Expand All @@ -368,6 +369,7 @@ class MainActivity : AppCompatActivity(), TimerInterface {
2 -> return CalendarFragment()
3 -> return KeywordFragment()
4 -> return WifiConnectedFragment()
5 -> return BluetoothFragment()
else -> return TimeFragment()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')"
}


Expand Down
Original file line number Diff line number Diff line change
@@ -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
* <p>
* Edited by: Felix Nüsse felix.nuesse(at)t-online.de
* <p>
* <p>
* This program is released under the GPLv3 license
* <p>
* <p>
* 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.
* <p>
* 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.
* <p>
* 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<BluetoothObject>) : RecyclerView.Adapter<BluetoothListAdapter.BluetoothViewHolder>() {

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)

}


5 changes: 5 additions & 0 deletions app/src/main/res/drawable/icon_bluetooth.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="@color/iconColor"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M17,7l-4.29,-4.29c-0.63,-0.63 -1.71,-0.19 -1.71,0.7v6.18L7.11,5.7c-0.39,-0.39 -1.02,-0.39 -1.41,0 -0.39,0.39 -0.39,1.02 0,1.41L10.59,12 5.7,16.89c-0.39,0.39 -0.39,1.02 0,1.41 0.39,0.39 1.02,0.39 1.41,0L11,14.41v6.18c0,0.89 1.08,1.34 1.71,0.71L17,17c0.39,-0.39 0.39,-1.02 0,-1.41L13.41,12 17,8.42c0.39,-0.39 0.39,-1.03 0,-1.42zM13,5.83l1.88,1.88L13,9.59L13,5.83zM14.88,16.29L13,18.17v-3.76l1.88,1.88z"/>
</vector>
6 changes: 6 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@
android:layout_height="wrap_content"
android:icon="@drawable/icon_wifi"
android:text="@string/main_tab_wifi_connected" />

<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="@drawable/icon_bluetooth"
android:text="@string/main_tab_bluetooth" />
</com.google.android.material.tabs.TabLayout>
</com.google.android.material.appbar.AppBarLayout>

Expand Down
54 changes: 54 additions & 0 deletions app/src/main/res/layout/adapter_bluetooth_list.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<androidx.cardview.widget.CardView
android:id="@+id/cardView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="@dimen/cardMargin"
android:padding="@dimen/cardPadding"
app:cardBackgroundColor="?attr/colorTertiaryContainer"
app:cardCornerRadius="@dimen/cornerRadius"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

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

<ImageView
android:id="@+id/volumeState"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@android:drawable/btn_dialog"
app:tint="@android:color/white" />

<TextView
android:id="@+id/rowTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="12dp"
android:text="title"
android:textColor="@android:color/white" />

<Spinner
android:id="@+id/volumeSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="32dp" />
</LinearLayout>

</androidx.cardview.widget.CardView>


</androidx.constraintlayout.widget.ConstraintLayout>
43 changes: 43 additions & 0 deletions app/src/main/res/layout/fragment_bluetooth.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.BluetoothFragment">


<ScrollView
android:id="@+id/BluetoothListContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">

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

<TextView
android:id="@+id/label_bluetooth_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="16dp"
android:gravity="center"
android:text="@string/fragment_title_bluetooth"
android:textStyle="bold" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/bluetooth_fragment_recylcer_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:layout_marginBottom="24dp"
android:layout_marginEnd="8dp" />

</LinearLayout>
</ScrollView>

</FrameLayout>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,6 @@

<string name="pause_check_schedule" translatable="false">Pause Timed Silence</string>
<string name="appwidget_text" translatable="false">donotdelete-placeholder</string>
<string name="fragment_title_bluetooth">Bluetooth</string>
<string name="main_tab_bluetooth">Bluetooth</string>
</resources>

0 comments on commit 2f29a13

Please sign in to comment.