Skip to content
Snippets Groups Projects
Commit 4b971324 authored by Anon's avatar Anon
Browse files

MainFragment: Add connecting state.

parent 3624ec85
No related branches found
No related tags found
No related merge requests found
......@@ -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()
}
}
......@@ -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>
......
......@@ -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>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment