Skip to content

Commit

Permalink
Un-break ./gradlew build
Browse files Browse the repository at this point in the history
  • Loading branch information
cortinico committed Oct 5, 2024
1 parent efddbee commit afcd1f6
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 37 deletions.
11 changes: 6 additions & 5 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
plugins {
id("com.android.application")
kotlin("android")
kotlin("kapt")
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.kotlin.kapt)
}

android {
Expand All @@ -11,9 +11,9 @@ android {
minSdk = libs.versions.min.sdk.version.get().toInt()
targetSdk = libs.versions.target.sdk.version.get().toInt()
namespace = "it.ncorti.emgvisualizer"

applicationId = "it.ncorti.emgvisualizer"

versionCode = 5
versionName = "3.0.0-alpha1"
}
Expand All @@ -23,6 +23,7 @@ android {
}
kotlinOptions {
jvmTarget = JavaVersion.VERSION_17.toString()
freeCompilerArgs = listOf("-Xstring-concat=inline")
}
buildTypes {
getByName("release") {
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="28" />

<uses-feature android:name="android.hardware.bluetooth_le"
android:required="true" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package it.ncorti.emgvisualizer.ui.export

import android.Manifest
import android.Manifest.permission.WRITE_EXTERNAL_STORAGE
import android.content.ContentValues
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.content.pm.PackageManager.PERMISSION_GRANTED
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.view.LayoutInflater
Expand All @@ -19,6 +21,7 @@ import it.ncorti.emgvisualizer.R
import it.ncorti.emgvisualizer.databinding.LayoutExportBinding
import java.io.File
import java.io.FileOutputStream
import java.util.Locale
import javax.inject.Inject

private const val REQUEST_WRITE_EXTERNAL_CODE = 2
Expand All @@ -42,7 +45,11 @@ class ExportFragment : BaseFragment<ExportContract.Presenter>(), ExportContract.
super.onAttach(context)
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = LayoutExportBinding.inflate(inflater, container, false)
setHasOptionsMenu(true)
return binding.root
Expand All @@ -66,7 +73,11 @@ class ExportFragment : BaseFragment<ExportContract.Presenter>(), ExportContract.
}

override fun showNotStreamingErrorMessage() {
Toast.makeText(activity, "You can't collect points if Myo is not streaming!", Toast.LENGTH_SHORT).show()
Toast.makeText(
activity,
"You can't collect points if Myo is not streaming!",
Toast.LENGTH_SHORT
).show()
}

override fun showCollectionStarted() {
Expand All @@ -78,7 +89,7 @@ class ExportFragment : BaseFragment<ExportContract.Presenter>(), ExportContract.
}

override fun showCollectedPoints(totalPoints: Int) {
binding.pointsCount.text = totalPoints.toString()
binding.pointsCount.text = String.format(Locale.getDefault(), totalPoints.toString())
}

override fun enableResetButton() {
Expand Down Expand Up @@ -112,41 +123,72 @@ class ExportFragment : BaseFragment<ExportContract.Presenter>(), ExportContract.
}

override fun saveCsvFile(content: String) {
context?.apply {
val hasPermission = (
ContextCompat.checkSelfPermission(
this,
WRITE_EXTERNAL_STORAGE
) == PERMISSION_GRANTED
val fileName = getCsvFilename()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
// Use scoped storage for API level 29 and above
val contentResolver = context?.contentResolver
val contentValues = ContentValues().apply {
put(android.provider.MediaStore.MediaColumns.DISPLAY_NAME, fileName)
put(android.provider.MediaStore.MediaColumns.MIME_TYPE, "text/plain")
put(
android.provider.MediaStore.MediaColumns.RELATIVE_PATH,
Environment.DIRECTORY_DOCUMENTS
)
if (hasPermission) {
writeToFile(content)
}
val uri: Uri? = contentResolver?.insert(
android.provider.MediaStore.Files.getContentUri("external"),
contentValues
)
if (uri != null) {
contentResolver.openOutputStream(uri)?.use { outputStream ->
outputStream.write(content.toByteArray())
}
} else {
fileContentToSave = content
requestPermissions(
arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
REQUEST_WRITE_EXTERNAL_CODE
)
// Handle failure to create the file
error("Failed to create file for scoped storage")
}
} else {
context?.apply {
val hasPermission = (
ContextCompat.checkSelfPermission(
this,
WRITE_EXTERNAL_STORAGE
) == PERMISSION_GRANTED
)
if (hasPermission) {
writeToFile(fileName, content)
} else {
fileContentToSave = content
requestPermissions(
arrayOf(WRITE_EXTERNAL_STORAGE),
REQUEST_WRITE_EXTERNAL_CODE
)
}
}
}
}

private fun writeToFile(content: String) {
private fun writeToFile(fileName: String, content: String) {
val storageDir =
File("${Environment.getExternalStorageDirectory().absolutePath}/myo_emg")
storageDir.mkdir()
val outfile = File(storageDir, "myo_emg_export_${System.currentTimeMillis()}.csv")
val outfile = File(storageDir, fileName)
val fileOutputStream = FileOutputStream(outfile)
fileOutputStream.write(content.toByteArray())
fileOutputStream.close()
Toast.makeText(activity, "Saved to: ${outfile.path}", Toast.LENGTH_LONG).show()
}

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
@Deprecated("Deprecated in Java")
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
when (requestCode) {
REQUEST_WRITE_EXTERNAL_CODE -> {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
fileContentToSave?.apply { writeToFile(this) }
fileContentToSave?.apply { writeToFile(getCsvFilename(), this) }
} else {
Toast.makeText(
activity, getString(R.string.write_permission_denied_message),
Expand All @@ -156,4 +198,6 @@ class ExportFragment : BaseFragment<ExportContract.Presenter>(), ExportContract.
}
}
}

private fun getCsvFilename() = "myo_emg_export_${System.currentTimeMillis()}.csv"
}
7 changes: 4 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
plugins {
id("com.android.application") apply false version "8.5.1"
id("com.android.library") apply false version "8.5.1"
kotlin("android") apply false version "1.9.20"
alias(libs.plugins.android.application) apply false
alias(libs.plugins.android.library) apply false
alias(libs.plugins.kotlin.android) apply false
alias(libs.plugins.kotlin.kapt) apply false
alias(libs.plugins.detekt)
}

Expand Down
8 changes: 5 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ min_sdk_version = "21"
target_sdk_version = "34"
compile_sdk_version = "34"

agp = "8.5.2"
agp = "8.7.0"
androidx_recyclerview = "1.3.2"
appcompat = "1.6.1"
appintro = "5.1.0"
Expand Down Expand Up @@ -34,8 +34,10 @@ mockito_kotlin = { module = "com.nhaarman.mockitokotlin2:mockito-kotlin", versio
rxandroid2 = { module = "io.reactivex.rxjava2:rxandroid", version.ref = "rxandroid2" }
rxjava2 = { module = "io.reactivex.rxjava2:rxjava", version.ref = "rxjava2" }
detekt_formatting = { module = "io.gitlab.arturbosch.detekt:detekt-formatting", version.ref = "detekt" }
kgp = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
agp = { module = "com.android.tools.build:gradle", version.ref = "agp" }

[plugins]
detekt = { id = "io.gitlab.arturbosch.detekt", version.ref = "detekt" }
android-application = { id = "com.android.application", version.ref = "agp" }
android-library = { id = "com.android.library", version.ref = "agp" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
kotlin-kapt = { id = "org.jetbrains.kotlin.kapt", version.ref = "kotlin" }
4 changes: 2 additions & 2 deletions myonnaise/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
id("com.android.library")
kotlin("android")
alias(libs.plugins.android.library)
alias(libs.plugins.kotlin.android)
}

android {
Expand Down
4 changes: 2 additions & 2 deletions sensorgraphview/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
id("com.android.library")
kotlin("android")
alias(libs.plugins.android.library)
alias(libs.plugins.kotlin.android)
}

android {
Expand Down

0 comments on commit afcd1f6

Please sign in to comment.