Commit c98b2d0c authored by Stefan Zabka's avatar Stefan Zabka
Browse files

Merge branch 'master' into navigation

parents 48154719 57f6bed7
Pipeline #2644 failed with stage
in 2 minutes and 50 seconds
......@@ -14,7 +14,7 @@ android {
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
versionName "0.1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
......
......@@ -63,10 +63,6 @@ object ConnectionService {
}
fun connect(context: Context) {
if (device == null) {
throw IllegalStateException()
}
// Use first BLE devices that is bonded
val bondedDevices = BluetoothAdapter.getDefaultAdapter().bondedDevices.filter {
it.address.startsWith(
......
/*
* 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.BluetoothGatt
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
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.main.MainFragment
import kotlinx.android.synthetic.main.batch_transfer_fragment.*
import java.lang.Exception
import java.lang.IllegalStateException
private const val TAG = "BatchTransferFragment"
class BatchTransferFragment : Fragment(), FileTransferListener, GattListener {
private lateinit var queue: TransferQueue
private var transfer: FileTransfer? = null
private var isCancelled = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val args = arguments ?: throw IllegalStateException()
val jobs = args.get("jobs") as? Array<TransferJob> ?: throw IllegalStateException()
queue = TransferQueue(jobs)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? =
inflater.inflate(R.layout.batch_transfer_fragment, container, false)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
label_status.text = getString(R.string.batch_transfer_label_initializing)
progress.max = 5
button_cancel.setOnClickListener {
// isCancelled = true
startTransfer()
}
button_done.setOnClickListener {
val fragment = MainFragment()
fragmentManager!!.beginTransaction()
.replace(R.id.fragment_container, fragment)
.addToBackStack(null)
.commit()
}
initConnection()
}
private fun initConnection() {
val ctx = context ?: throw IllegalStateException()
ConnectionService.connect(ctx)
}
private fun startTransfer() {
activity?.runOnUiThread {
label_status.text = getString(R.string.batch_transfer_label_transferring)
progress.max = queue.size
}
transferNext()
}
private fun transferNext() {
val item = queue.dequeue()
if (item == null || isCancelled) {
activity?.runOnUiThread {
progress.progress = 0
label_status.text = if (isCancelled) {
getString(R.string.batch_transfer_label_cancelled)
} else {
getString(R.string.batch_transfer_label_complete)
}
button_cancel.visibility = View.GONE
button_done.visibility = View.VISIBLE
}
} else {
transferItem(item)
}
}
private fun transferItem(transferJob: TransferJob) {
try {
val ctx = activity ?: throw IllegalStateException()
val reader = ChunkedReader(ctx, transferJob.sourceUri, ConnectionService.mtu)
val service = ConnectionService.leService ?: throw IllegalStateException()
transfer = FileTransfer(service, reader,this, transferJob.destPath)
} catch (e: Exception) {
Log.e(TAG, "Failed to initialize transfer")
return
}
}
override fun onConnectionStateChange(state: Int) {
if (state == BluetoothGatt.STATE_CONNECTED) {
startTransfer()
}
}
override fun onError() {
activity?.runOnUiThread {
label_status.text = getString(R.string.batch_transfer_label_error)
button_cancel.visibility = View.GONE
button_done.visibility = View.VISIBLE
}
}
override fun onFinish() {
activity?.runOnUiThread {
// TODO: Add workaround for broken progress bars
// https://stackoverflow.com/questions/4348032/android-progressbar-does-not-update-progress-view-drawable
progress.incrementProgressBy(1)
}
transferNext()
}
}
\ No newline at end of file
/*
* 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.net.Uri
import android.os.Parcel
import android.os.Parcelable
import kotlinx.android.parcel.Parcelize
// Ignore IDE warnings. @Parcelize will take care of everything
@Parcelize
data class TransferJob(
val sourceUri: Uri,
val destPath: String
) : Parcelable
\ No newline at end of file
/*
* 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.net.Uri
import java.util.*
import kotlin.NoSuchElementException
class TransferQueue() {
private val queue = LinkedList<TransferJob>()
val size: Int
get() = queue.size
constructor(jobs: Array<TransferJob>) : this() {
for (job in jobs) {
queue.add(job)
}
}
fun enqueue(sourceUri: Uri, destPath: String) {
queue.add(TransferJob(sourceUri, destPath))
}
fun dequeue(): TransferJob? {
return try {
queue.removeFirst()
} catch (e: NoSuchElementException) {
null
}
}
fun clear() = queue.clear()
}
\ No newline at end of file
......@@ -22,6 +22,7 @@
package de.ccc.events.badge.card10.hatchery
import android.net.Uri
import android.os.AsyncTask
import android.os.Bundle
import android.util.Log
......@@ -29,9 +30,13 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AlertDialog
import androidx.core.net.toUri
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import de.ccc.events.badge.card10.R
import de.ccc.events.badge.card10.common.LoadingDialog
import de.ccc.events.badge.card10.filetransfer.BatchTransferFragment
import de.ccc.events.badge.card10.filetransfer.TransferJob
import kotlinx.android.synthetic.main.app_detail_fragment.*
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream
......@@ -73,7 +78,8 @@ class AppDetailFragment : Fragment() {
) { dialog, _ -> dialog.dismiss() }
.create()
ReleaseDownload(app, ctx.cacheDir, loadingDialog, errorDialog).execute()
val fm = fragmentManager ?: throw java.lang.IllegalStateException()
ReleaseDownload(app, ctx.cacheDir, loadingDialog, errorDialog, fm).execute()
}
}
......@@ -81,10 +87,11 @@ class AppDetailFragment : Fragment() {
private val app: App,
private val cacheDir: File,
private val loadingDialog: LoadingDialog,
private val errorDialog: AlertDialog
) : AsyncTask<Void, Void, List<String>?>() {
private val errorDialog: AlertDialog,
private val fragmentManager: FragmentManager
) : AsyncTask<Void, Void, List<TransferJob>?>() {
override fun doInBackground(vararg p0: Void?): List<String>? {
override fun doInBackground(vararg p0: Void?): List<TransferJob>? {
return try {
cacheDir.deleteRecursively()
cacheDir.mkdir()
......@@ -96,7 +103,7 @@ class AppDetailFragment : Fragment() {
inputStream.copyTo(outputStream)
val appFiles = mutableListOf<String>()
val appFiles = mutableListOf<TransferJob>()
val tarStream = TarArchiveInputStream(GzipCompressorInputStream(file.inputStream()))
while (true) {
val entry = tarStream.nextTarEntry ?: break
......@@ -110,7 +117,7 @@ class AppDetailFragment : Fragment() {
targetFile.createNewFile()
Log.d(TAG, "Extracting ${entry.name} to ${targetFile.absolutePath}")
tarStream.copyTo(targetFile.outputStream())
appFiles.add("apps/${entry.name}")
appFiles.add(TransferJob(targetFile.toUri(), "apps/${entry.name}"))
}
val launcher = createLauncher(app.slug, cacheDir)
......@@ -122,17 +129,27 @@ class AppDetailFragment : Fragment() {
}
}
override fun onPostExecute(result: List<String>?) {
if (result == null) {
override fun onPostExecute(jobs: List<TransferJob>?) {
if (jobs == null) {
loadingDialog.dismiss()
errorDialog.show()
return
}
loadingDialog.dismiss()
val bundle = Bundle()
bundle.putParcelableArray("jobs", jobs.toTypedArray())
val fragment = BatchTransferFragment()
fragment.arguments = bundle
fragmentManager.beginTransaction()
.replace(R.id.fragment_container, fragment)
.addToBackStack(null)
.commit()
}
fun createLauncher(slug: String, cacheDir: File): String {
fun createLauncher(slug: String, cacheDir: File): TransferJob {
val fileName = "$slug.py"
val file = File(cacheDir, fileName)
file.createNewFile()
......@@ -145,7 +162,7 @@ class AppDetailFragment : Fragment() {
file.writeText(src)
return fileName
return TransferJob(file.toUri(), fileName)
}
}
}
......@@ -40,7 +40,7 @@ class HatcheryClient {
// TODO: Filter by category
val request = Request.Builder()
.url("$HATCHERY_BASE_URL/eggs/list/json")
.url("$HATCHERY_BASE_URL/basket/card10/list/json")
.build()
val response: Response
......
......@@ -35,6 +35,8 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import de.ccc.events.badge.card10.CARD10_BLUETOOTH_MAC_PREFIX
import de.ccc.events.badge.card10.R
......@@ -44,6 +46,7 @@ class ScannerFragment : Fragment() {
lateinit var bluetoothAdapter: BluetoothAdapter
val listAdapter = ScannerListAdapter({ device: Device -> deviceClicked(device) })
val viewModel: ScannerViewModel by lazy { ViewModelProviders.of(this).get(ScannerViewModel::class.java) }
val callback = object : ScanCallback() {
override fun onScanResult(callbackType: Int, result: ScanResult) {
......@@ -65,13 +68,20 @@ class ScannerFragment : Fragment() {
val receiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
System.out.println("===== onReceive " + intent)
val bondState = intent.extras!!.getInt(BluetoothDevice.EXTRA_BOND_STATE)
val device = intent.extras!!.getParcelable(BluetoothDevice.EXTRA_DEVICE) as BluetoothDevice?
viewModel.status.value =
if (bondState == BluetoothDevice.BOND_NONE) "NONE" else if (bondState == BluetoothDevice.BOND_BONDING) "BONDING" else if (bondState == BluetoothDevice.BOND_BONDED) "BONDED" else null
System.out.println("=== onReceive ${intent.action} ${viewModel.status.value} ${device}")
listAdapter.notifyDataSetChanged()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel.status.observe(
this,
Observer { text -> scanner_status.text = text })
activity?.registerReceiver(receiver, IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED))
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
for (device in bluetoothAdapter.bondedDevices.filter {
......
/*
* 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.scanner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
class ScannerViewModel : ViewModel() {
val status = MutableLiveData<String>()
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="@dimen/default_list_item_height">
android:layout_height="@dimen/default_list_item_height"
android:background="?selectableItemBackground">
<TextView
android:id="@+id/label_name"
......@@ -10,6 +12,6 @@
android:layout_height="wrap_content"
android:layout_gravity="start"
android:paddingLeft="@dimen/default_list_item_margin_side"
android:textStyle="bold"/>
android:textAppearance="@style/TextAppearance.AppCompat.Body2"/>
</LinearLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/activity_padding">
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:orientation="vertical"
android:gravity="center_horizontal">
<ProgressBar android:layout_width="@dimen/batch_transfer_progress"
android:layout_height="wrap_content"
android:id="@+id/progress"
android:indeterminate="false"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/label_status"/>
</LinearLayout>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:id="@+id/button_cancel"
android:text="@string/batch_transfer_button_cancel"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:id="@+id/button_done"
android:visibility="gone"
android:text="@string/batch_transfer_button_done"/>
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
......@@ -23,9 +23,26 @@
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/image_logo"/>
<Button android:id="@+id/button_pair"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/main_button_pair"
android:layout_marginTop="@dimen/main_label_margin"
style="@style/MainButton"
app:layout_constraintTop_toBottomOf="@id/label_status"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@+id/label_status"
app:layout_constraintTop_toBottomOf="@id/button_pair"
app:layout_constraintBottom_toBottomOf="parent"
android:id="@+id/container_disconnected">
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@id/button_pair"
android:id="@+id/container_connected">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
......@@ -46,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"
......
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/scanner_device_list"
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"/>
android:orientation="vertical"
android:layout_gravity="center_horizontal"
android:padding="16dp">
<androidx.recyclerview.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scanner_device_list"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0px"/>
<TextView android:id="@+id/scanner_status"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
......@@ -7,6 +7,8 @@
<dimen name="main_button_margin">16dp</dimen>
<dimen name="send_label_margin">24dp</dimen>
<dimen name="batch_transfer_progress">200dp</dimen>
<dimen name="app_list_item_padding">16dp</dimen>
<dimen name="app_detail_description_margin">16dp</dimen>
......@@ -16,5 +18,5 @@
<dimen name="loading_dialog_text_margin">16dp</dimen>