diff --git a/app/src/main/java/de/ccc/events/badge/card10/main/MainFragment.kt b/app/src/main/java/de/ccc/events/badge/card10/main/MainFragment.kt index b9af4e11c9721e5fc6f21613c38f66c0f28a370a..fda341621ea15cda5d0403cbf3614e9ae8952553 100644 --- a/app/src/main/java/de/ccc/events/badge/card10/main/MainFragment.kt +++ b/app/src/main/java/de/ccc/events/badge/card10/main/MainFragment.kt @@ -28,12 +28,14 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.Button +import android.widget.TextView import androidx.constraintlayout.widget.ConstraintLayout import androidx.fragment.app.DialogFragment import androidx.fragment.app.Fragment import de.ccc.events.badge.card10.CARD10_BLUETOOTH_MAC_PREFIX import de.ccc.events.badge.card10.R import de.ccc.events.badge.card10.common.ConnectionService +import de.ccc.events.badge.card10.common.GattListener import de.ccc.events.badge.card10.filetransfer.FileTransferFragment import de.ccc.events.badge.card10.hatchery.AppListFragment import de.ccc.events.badge.card10.mood.MoodFragment @@ -41,8 +43,10 @@ import de.ccc.events.badge.card10.scanner.ScannerFragment import de.ccc.events.badge.card10.sparkle.BeautifulFragment import de.ccc.events.badge.card10.time.TimeUpdateDialog import kotlinx.android.synthetic.main.main_fragment.* +import java.lang.IllegalStateException +import java.sql.Connection -class MainFragment : Fragment() { +class MainFragment : Fragment(), GattListener { private val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter() override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?) = @@ -62,16 +66,18 @@ class MainFragment : Fragment() { dialogFragment.dismiss() } + ConnectionService.addGattListener("main", this) + val bondedCard10s = bluetoothAdapter.bondedDevices.filter { it.address.startsWith(CARD10_BLUETOOTH_MAC_PREFIX, true) } if (bondedCard10s.isNotEmpty()) { - val device = bondedCard10s[0] - label_status.text = getString(R.string.main_label_paired, device.name, device.address) - showConnectedView(view) + val ctx = activity ?: throw IllegalStateException() + ConnectionService.connect(ctx) + showConnectingView() } else { label_status.text = getString(R.string.main_label_not_connected) - showDisconnectedView(view) + showDisconnectedView() } } @@ -82,14 +88,37 @@ class MainFragment : Fragment() { .commit() } - private fun showConnectedView(view: View) { - view.findViewById<ConstraintLayout>(R.id.container_connected).visibility = View.VISIBLE - view.findViewById<ConstraintLayout>(R.id.container_disconnected).visibility = View.GONE - view.findViewById<Button>(R.id.button_pair).text = getString(R.string.main_button_manage_pairings) + private fun showConnectedView() { + activity?.runOnUiThread { + container_connected.visibility = View.VISIBLE + container_disconnected.visibility = View.GONE + button_pair.text = getString(R.string.main_button_manage_pairings) + + button_hatchery.isEnabled = true + button_send.isEnabled = true + button_mood.isEnabled = true + button_beautiful.isEnabled = true + button_set_time.isEnabled = true + + val device = ConnectionService.device + label_status.text = getString(R.string.main_label_connected, device?.name, device?.address) + } + } + + private fun showConnectingView() { + val device = ConnectionService.device + label_status.text = getString(R.string.main_label_connecting, device?.name, device?.address) + button_pair.text = getString(R.string.main_button_manage_pairings) + } + + private fun showDisconnectedView() { + container_connected.visibility = View.GONE + container_disconnected.visibility = View.VISIBLE + + button_pair.text = getString(R.string.main_button_pair) } - private fun showDisconnectedView(view: View) { - view.findViewById<ConstraintLayout>(R.id.container_connected).visibility = View.GONE - view.findViewById<ConstraintLayout>(R.id.container_disconnected).visibility = View.VISIBLE + override fun onConnectionReady() { + showConnectedView() } } diff --git a/app/src/main/res/layout/main_fragment.xml b/app/src/main/res/layout/main_fragment.xml index 9332fc1a46da59cee645df620aa8d0cf2b8334b3..aa43459cd688e9c1a5b986a8e7c3c09da5051e70 100644 --- a/app/src/main/res/layout/main_fragment.xml +++ b/app/src/main/res/layout/main_fragment.xml @@ -52,7 +52,8 @@ android:text="@string/main_button_browse_apps" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" - app:layout_constraintTop_toTopOf="parent"/> + app:layout_constraintTop_toTopOf="parent" + android:enabled="false"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" @@ -62,7 +63,8 @@ android:text="@string/main_button_send_file" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" - app:layout_constraintTop_toBottomOf="@+id/button_hatchery"/> + app:layout_constraintTop_toBottomOf="@+id/button_hatchery" + android:enabled="false"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" @@ -72,7 +74,8 @@ android:text="@string/main_button_mood" app:layout_constraintTop_toBottomOf="@+id/button_send" app:layout_constraintLeft_toLeftOf="parent" - app:layout_constraintRight_toRightOf="parent"/> + app:layout_constraintRight_toRightOf="parent" + android:enabled="false"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" @@ -82,7 +85,8 @@ android:text="@string/main_button_beautiful" app:layout_constraintTop_toBottomOf="@+id/button_mood" app:layout_constraintLeft_toLeftOf="parent" - app:layout_constraintRight_toRightOf="parent"/> + app:layout_constraintRight_toRightOf="parent" + android:enabled="false"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" @@ -92,7 +96,8 @@ android:text="@string/main_button_set_time" app:layout_constraintTop_toBottomOf="@+id/button_beautiful" app:layout_constraintLeft_toLeftOf="parent" - app:layout_constraintRight_toRightOf="parent"/> + app:layout_constraintRight_toRightOf="parent" + android:enabled="false"/> </androidx.constraintlayout.widget.ConstraintLayout> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c9daf48715003a0614387fd8aee07324c0fc3c58..15ca45bfa675ecc8dd94eea2e0dacaf0694e6d29 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -3,7 +3,8 @@ <string name="main_label_paired">You are paired to %1$s (%2$s)</string> <string name="main_label_not_connected">You are currently not connected to your card10.</string> - <string name="main_label_status">You are connected to %1$s (%2$s)</string> + <string name="main_label_connected">You are connected to %1$s (%2$s)</string> + <string name="main_label_connecting">Connecting to %1$s (%2$s)</string> <string name="main_button_pair">Pair</string> <string name="main_button_manage_pairings">Manage Paired Devices</string> <string name="main_button_browse_apps">Browse Apps</string>