MainFragment.kt 4.28 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
31
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.constraintlayout.widget.ConstraintLayout
Anon's avatar
Anon committed
32
import androidx.fragment.app.DialogFragment
33
import androidx.fragment.app.Fragment
34
import de.ccc.events.badge.card10.CARD10_BLUETOOTH_MAC_PREFIX
35
import de.ccc.events.badge.card10.R
36
import de.ccc.events.badge.card10.common.ConnectionService
Anon's avatar
Anon committed
37
import de.ccc.events.badge.card10.filetransfer.FileTransferFragment
38
import de.ccc.events.badge.card10.hatchery.AppListFragment
39
import de.ccc.events.badge.card10.mood.MoodFragment
40
import de.ccc.events.badge.card10.scanner.ScannerFragment
41
import de.ccc.events.badge.card10.sparkle.BeautifulFragment
Anon's avatar
Anon committed
42
import de.ccc.events.badge.card10.time.TimeUpdateDialog
43
import kotlinx.android.synthetic.main.main_fragment.*
44
45

class MainFragment : Fragment() {
46
47
    private val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()

48
49
50
51
    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
52
53
54
55
56
57
        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
58
59
60
61
62
63
64
        button_set_time.setOnClickListener {
            val dialogFragment = TimeUpdateDialog()
            dialogFragment.show(fragmentManager, "time")
            dialogFragment.setTime()
            dialogFragment.dismiss()
        }

65
66
        val bondedCard10s =
            bluetoothAdapter.bondedDevices.filter { it.address.startsWith(CARD10_BLUETOOTH_MAC_PREFIX, true) }
Anon's avatar
Anon committed
67

68
        if (bondedCard10s.isNotEmpty()) {
Anon's avatar
Anon committed
69
            val device = bondedCard10s[0]
70
            label_status.text = getString(R.string.main_label_paired, device.name, device.address)
71
72
            showConnectedView(view)
        } else {
Anon's avatar
Anon committed
73
            label_status.text = getString(R.string.main_label_not_connected)
74
75
            showDisconnectedView(view)
        }
76
77
78
79
80
81
82
    }

    private fun startFragment(fragment: Fragment) {
        fragmentManager!!.beginTransaction()
            .replace(R.id.fragment_container, fragment)
            .addToBackStack(null)
            .commit()
83
84
85
86
87
    }

    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
88
        view.findViewById<Button>(R.id.button_pair).text = getString(R.string.main_button_manage_pairings)
89
90
91
92
93
94
95
    }

    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
    }
}