FileTransferFragment.kt 6.42 KB
Newer Older
Anon's avatar
Anon committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
 * 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.filetransfer

import android.bluetooth.BluetoothAdapter
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
Anon's avatar
Anon committed
33
import android.widget.EditText
Anon's avatar
Anon committed
34
35
36
import android.widget.ProgressBar
import android.widget.TextView
import androidx.annotation.UiThread
37
import androidx.appcompat.app.AlertDialog
Anon's avatar
Anon committed
38
import androidx.fragment.app.Fragment
39
import de.ccc.events.badge.card10.CARD10_BLUETOOTH_MAC_PREFIX
Anon's avatar
Anon committed
40
import de.ccc.events.badge.card10.R
41
import de.ccc.events.badge.card10.common.ConnectionException
Anon's avatar
Anon committed
42
43
import de.ccc.events.badge.card10.common.ConnectionService
import de.ccc.events.badge.card10.common.GattListener
44
import de.ccc.events.badge.card10.main.MainFragment
Anon's avatar
Anon committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.lang.Exception
import java.lang.IllegalStateException

private const val TAG = "FileTransferFragment"
private const val INTENT_RESULT_CODE_FILE = 1

class FileTransferFragment : Fragment(), GattListener, FileTransferListener{
    private var isSending = false
    private var transfer: FileTransfer? = null

    private lateinit var buttonPickFile: Button
    private lateinit var buttonStartStop: Button
    private lateinit var tvSelected: TextView
    private lateinit var tvStatus: TextView
    private lateinit var progressBar: ProgressBar
Anon's avatar
Anon committed
60
    private lateinit var inputDestination: EditText
Anon's avatar
Anon committed
61
62
63
64
65
66
67
68
69

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.file_transfer_fragment, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        tvSelected = view.findViewById(R.id.label_selected)
        tvStatus = view.findViewById(R.id.label_status)
        progressBar = view.findViewById(R.id.progress)
Anon's avatar
Anon committed
70
        inputDestination = view.findViewById(R.id.input_destination)
Anon's avatar
Anon committed
71
72
73
74
75
76
77
78
79
80
81

        buttonPickFile = view.findViewById(R.id.button_pick_file)
        buttonPickFile.setOnClickListener {
            val intent = Intent(Intent.ACTION_GET_CONTENT)
            intent.addCategory(Intent.CATEGORY_OPENABLE)
            intent.type = "*/*"
            startActivityForResult(intent, INTENT_RESULT_CODE_FILE)
        }

        buttonStartStop = view.findViewById(R.id.button_start_stop_transfer)

82
83
84
85
86
87
88
        try {
            toggleControls()
        } catch (e: ConnectionException) {
            showError(e.message)
        } catch (e: Exception) {
            showError(getString(R.string.connection_error_generic))
        }
Anon's avatar
Anon committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (requestCode != INTENT_RESULT_CODE_FILE) {
            return
        }

        val ctx = context ?: throw IllegalStateException()

        val uri = data?.data ?: return

        try {
            val reader = ChunkedReader(ctx, uri, ConnectionService.mtu)
            val service = ConnectionService.leService ?: throw IllegalStateException()

Anon's avatar
Anon committed
104
            transfer = FileTransfer(service, reader, this, inputDestination.text.toString())
Anon's avatar
Anon committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
        } catch (e: Exception) {
            Log.e(TAG, "Failed to initialize transfer")
            return
        }

        buttonStartStop.isEnabled = true
        tvSelected.text = uri.path
    }

    private fun toggleControls() {
        if (isSending) {
            activity?.runOnUiThread {
                progressBar.visibility = View.VISIBLE
                buttonPickFile.isEnabled = false
                buttonStartStop.text = getString(R.string.file_transfer_button_stop_transfer)
            }

            buttonStartStop.setOnClickListener {
                transfer?.abort()
                transfer = null
                isSending = false

                toggleControls()
            }
        } else {
            activity?.runOnUiThread {
                progressBar.visibility = View.INVISIBLE
                buttonPickFile.isEnabled = true
                buttonStartStop.text = getString(R.string.file_transfer_button_start_transfer)
            }

            buttonStartStop.setOnClickListener {
                transfer?.start()
                isSending = true

                tvStatus.text = "STARTED"
                toggleControls()
            }
        }

        activity?.runOnUiThread {
            buttonStartStop.isEnabled = transfer != null
        }
    }

    override fun onError() {
        activity?.runOnUiThread {
            tvStatus.text = "ERROR"
        }

    }

    override fun onFinish() {
        isSending = false
        transfer = null

        activity?.runOnUiThread {
            tvStatus.text = "FINISHED"
        }
        toggleControls()
    }
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182

    private fun showError(message: String?) {
        val ctx = context ?: throw IllegalStateException()
        val fm = fragmentManager ?: throw IllegalStateException()

        val errorDialog =
            AlertDialog.Builder(ctx)
                .setMessage(message ?: getString(R.string.connection_error_generic))
                .setPositiveButton(R.string.dialog_action_ok) {
                    dialog, _ -> dialog.dismiss()
                    fm.beginTransaction()
                        .replace(R.id.fragment_container, MainFragment())
                        .addToBackStack(null)
                        .commit()
                }
                .show()
    }
Anon's avatar
Anon committed
183
}