MainFragment.kt 5.06 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
 * Copyright by the original author or authors.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package de.ccc.events.badge.card10.main

25
import android.bluetooth.BluetoothAdapter
26
27
28
29
30
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
Anon's avatar
Anon committed
31
import android.widget.TextView
32
import androidx.constraintlayout.widget.ConstraintLayout
Anon's avatar
Anon committed
33
import androidx.fragment.app.DialogFragment
34
import androidx.fragment.app.Fragment
35
import de.ccc.events.badge.card10.CARD10_BLUETOOTH_MAC_PREFIX
36
import de.ccc.events.badge.card10.R
37
import de.ccc.events.badge.card10.common.ConnectionService
Anon's avatar
Anon committed
38
import de.ccc.events.badge.card10.common.GattListener
Anon's avatar
Anon committed
39
import de.ccc.events.badge.card10.filetransfer.FileTransferFragment
40
import de.ccc.events.badge.card10.hatchery.AppListFragment
41
import de.ccc.events.badge.card10.mood.MoodFragment
42
import de.ccc.events.badge.card10.scanner.ScannerFragment
43
import de.ccc.events.badge.card10.sparkle.BeautifulFragment
Anon's avatar
Anon committed
44
import de.ccc.events.badge.card10.time.TimeUpdateDialog
45
import kotlinx.android.synthetic.main.main_fragment.*
Anon's avatar
Anon committed
46
47
import java.lang.IllegalStateException
import java.sql.Connection
48

Anon's avatar
Anon committed
49
class MainFragment : Fragment(), GattListener {
50
51
    private val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()

52
53
54
55
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?) =
        inflater.inflate(R.layout.main_fragment, container, false)

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Anon's avatar
Anon committed
56
57
58
59
60
61
        button_pair.setOnClickListener { startFragment(ScannerFragment()) }
        button_mood.setOnClickListener { startFragment(MoodFragment()) }
        button_beautiful.setOnClickListener { startFragment(BeautifulFragment()) }
        button_hatchery.setOnClickListener { startFragment(AppListFragment()) }
        button_send.setOnClickListener { startFragment(FileTransferFragment()) }

Anon's avatar
Anon committed
62
63
64
65
66
67
68
        button_set_time.setOnClickListener {
            val dialogFragment = TimeUpdateDialog()
            dialogFragment.show(fragmentManager, "time")
            dialogFragment.setTime()
            dialogFragment.dismiss()
        }

Anon's avatar
Anon committed
69
70
        ConnectionService.addGattListener("main", this)

71
72
        val bondedCard10s =
            bluetoothAdapter.bondedDevices.filter { it.address.startsWith(CARD10_BLUETOOTH_MAC_PREFIX, true) }
Anon's avatar
Anon committed
73

74
        if (bondedCard10s.isNotEmpty()) {
Anon's avatar
Anon committed
75
76
77
            val ctx = activity ?: throw IllegalStateException()
            ConnectionService.connect(ctx)
            showConnectingView()
78
        } else {
Anon's avatar
Anon committed
79
            label_status.text = getString(R.string.main_label_not_connected)
Anon's avatar
Anon committed
80
            showDisconnectedView()
81
        }
82
83
84
85
86
87
88
    }

    private fun startFragment(fragment: Fragment) {
        fragmentManager!!.beginTransaction()
            .replace(R.id.fragment_container, fragment)
            .addToBackStack(null)
            .commit()
89
90
    }

Anon's avatar
Anon committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
    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)
119
120
    }

Anon's avatar
Anon committed
121
122
    override fun onConnectionReady() {
        showConnectedView()
123
124
    }
}