Commit 4b971324 authored by Anon's avatar Anon
Browse files

MainFragment: Add connecting state.

parent 3624ec85
...@@ -28,12 +28,14 @@ import android.view.LayoutInflater ...@@ -28,12 +28,14 @@ import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.Button import android.widget.Button
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout import androidx.constraintlayout.widget.ConstraintLayout
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import de.ccc.events.badge.card10.CARD10_BLUETOOTH_MAC_PREFIX import de.ccc.events.badge.card10.CARD10_BLUETOOTH_MAC_PREFIX
import de.ccc.events.badge.card10.R import de.ccc.events.badge.card10.R
import de.ccc.events.badge.card10.common.ConnectionService 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.filetransfer.FileTransferFragment
import de.ccc.events.badge.card10.hatchery.AppListFragment import de.ccc.events.badge.card10.hatchery.AppListFragment
import de.ccc.events.badge.card10.mood.MoodFragment import de.ccc.events.badge.card10.mood.MoodFragment
...@@ -41,8 +43,10 @@ import de.ccc.events.badge.card10.scanner.ScannerFragment ...@@ -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.sparkle.BeautifulFragment
import de.ccc.events.badge.card10.time.TimeUpdateDialog import de.ccc.events.badge.card10.time.TimeUpdateDialog
import kotlinx.android.synthetic.main.main_fragment.* 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() private val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?) = override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?) =
...@@ -62,16 +66,18 @@ class MainFragment : Fragment() { ...@@ -62,16 +66,18 @@ class MainFragment : Fragment() {
dialogFragment.dismiss() dialogFragment.dismiss()
} }
ConnectionService.addGattListener("main", this)
val bondedCard10s = val bondedCard10s =
bluetoothAdapter.bondedDevices.filter { it.address.startsWith(CARD10_BLUETOOTH_MAC_PREFIX, true) } bluetoothAdapter.bondedDevices.filter { it.address.startsWith(CARD10_BLUETOOTH_MAC_PREFIX, true) }
if (bondedCard10s.isNotEmpty()) { if (bondedCard10s.isNotEmpty()) {
val device = bondedCard10s[0] val ctx = activity ?: throw IllegalStateException()
label_status.text = getString(R.string.main_label_paired, device.name, device.address) ConnectionService.connect(ctx)
showConnectedView(view) showConnectingView()
} else { } else {
label_status.text = getString(R.string.main_label_not_connected) label_status.text = getString(R.string.main_label_not_connected)
showDisconnectedView(view) showDisconnectedView()
} }
} }
...@@ -82,14 +88,37 @@ class MainFragment : Fragment() { ...@@ -82,14 +88,37 @@ class MainFragment : Fragment() {
.commit() .commit()
} }
private fun showConnectedView(view: View) { private fun showConnectedView() {
view.findViewById<ConstraintLayout>(R.id.container_connected).visibility = View.VISIBLE activity?.runOnUiThread {
view.findViewById<ConstraintLayout>(R.id.container_disconnected).visibility = View.GONE container_connected.visibility = View.VISIBLE
view.findViewById<Button>(R.id.button_pair).text = getString(R.string.main_button_manage_pairings) 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) { override fun onConnectionReady() {
view.findViewById<ConstraintLayout>(R.id.container_connected).visibility = View.GONE showConnectedView()
view.findViewById<ConstraintLayout>(R.id.container_disconnected).visibility = View.VISIBLE
} }
} }
...@@ -52,7 +52,8 @@ ...@@ -52,7 +52,8 @@
android:text="@string/main_button_browse_apps" android:text="@string/main_button_browse_apps"
app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="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" <Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
...@@ -62,7 +63,8 @@ ...@@ -62,7 +63,8 @@
android:text="@string/main_button_send_file" android:text="@string/main_button_send_file"
app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="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" <Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
...@@ -72,7 +74,8 @@ ...@@ -72,7 +74,8 @@
android:text="@string/main_button_mood" android:text="@string/main_button_mood"
app:layout_constraintTop_toBottomOf="@+id/button_send" app:layout_constraintTop_toBottomOf="@+id/button_send"
app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/> app:layout_constraintRight_toRightOf="parent"
android:enabled="false"/>
<Button android:layout_width="wrap_content" <Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
...@@ -82,7 +85,8 @@ ...@@ -82,7 +85,8 @@
android:text="@string/main_button_beautiful" android:text="@string/main_button_beautiful"
app:layout_constraintTop_toBottomOf="@+id/button_mood" app:layout_constraintTop_toBottomOf="@+id/button_mood"
app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/> app:layout_constraintRight_toRightOf="parent"
android:enabled="false"/>
<Button android:layout_width="wrap_content" <Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
...@@ -92,7 +96,8 @@ ...@@ -92,7 +96,8 @@
android:text="@string/main_button_set_time" android:text="@string/main_button_set_time"
app:layout_constraintTop_toBottomOf="@+id/button_beautiful" app:layout_constraintTop_toBottomOf="@+id/button_beautiful"
app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/> app:layout_constraintRight_toRightOf="parent"
android:enabled="false"/>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
......
...@@ -3,7 +3,8 @@ ...@@ -3,7 +3,8 @@
<string name="main_label_paired">You are paired to %1$s (%2$s)</string> <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_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_pair">Pair</string>
<string name="main_button_manage_pairings">Manage Paired Devices</string> <string name="main_button_manage_pairings">Manage Paired Devices</string>
<string name="main_button_browse_apps">Browse Apps</string> <string name="main_button_browse_apps">Browse Apps</string>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment