Skip to content

Commit

Permalink
Merge branch 'release/0.2.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrimault committed Feb 23, 2020
2 parents 3f7cb04 + 9544f09 commit 4ac1be4
Show file tree
Hide file tree
Showing 47 changed files with 803 additions and 441 deletions.
3 changes: 0 additions & 3 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions commons/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

version = "0.5.9"
version = "0.6.0"

android {
compileSdkVersion 28
Expand All @@ -17,7 +17,7 @@ android {
targetSdkVersion 28
versionCode updateVersionCode(module.name)
versionName version
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
Expand All @@ -41,19 +41,20 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.1"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.0"
api project(':mountpoint')

implementation 'com.google.android.material:material:1.0.0'
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.preference:preference:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.1"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.0"
api 'androidx.room:room-runtime:2.2.3'

testImplementation 'junit:junit:4.12'
testImplementation 'androidx.test:core:1.2.0'
testImplementation 'androidx.arch.core:core-testing:2.1.0'
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:3.0.0'
testImplementation 'org.robolectric:robolectric:4.3'
androidTestImplementation 'androidx.test:runner:1.2.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import androidx.lifecycle.MutableLiveData
import androidx.preference.PreferenceManager
import fr.geonature.commons.input.io.InputJsonReader
import fr.geonature.commons.input.io.InputJsonWriter
import fr.geonature.commons.util.FileUtils.getInputsFolder
import fr.geonature.commons.util.getInputsFolder
import fr.geonature.mountpoint.util.FileUtils
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.withContext
import java.io.File
import java.io.FileWriter
import java.io.IOException
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.withContext

/**
* Manage [AbstractInput]:
Expand Down Expand Up @@ -192,7 +193,7 @@ class InputManager<I : AbstractInput> private constructor(

@Throws(IOException::class)
private fun getInputExportFile(input: AbstractInput): File {
val inputDir = getInputsFolder(application)
val inputDir = FileUtils.getInputsFolder(application)
inputDir.mkdirs()

return File(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import android.app.Application
import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import fr.geonature.commons.model.MountPoint.StorageType.INTERNAL
import fr.geonature.commons.settings.io.AppSettingsJsonReader
import fr.geonature.commons.util.FileUtils.getFile
import fr.geonature.commons.util.FileUtils.getRootFolder
import java.io.File
import java.io.FileReader
import java.io.IOException
import fr.geonature.mountpoint.model.MountPoint.StorageType.INTERNAL
import fr.geonature.mountpoint.util.FileUtils.getFile
import fr.geonature.mountpoint.util.FileUtils.getRootFolder
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File
import java.io.FileReader
import java.io.IOException

/**
* Manage [IAppSettings].
Expand All @@ -40,7 +40,8 @@ class AppSettingsManager<AS : IAppSettings> private constructor(
getRootFolder(
application,
INTERNAL
).mkdirs()
)
.mkdirs()
}
}
}
Expand Down
184 changes: 43 additions & 141 deletions commons/src/main/java/fr/geonature/commons/util/FileUtils.kt
Original file line number Diff line number Diff line change
@@ -1,155 +1,57 @@
package fr.geonature.commons.util

import android.content.Context
import android.os.Environment
import android.util.Log
import fr.geonature.commons.model.MountPoint
import fr.geonature.commons.util.MountPointUtils.getExternalStorage
import fr.geonature.commons.util.MountPointUtils.getInternalStorage
import fr.geonature.mountpoint.model.MountPoint
import fr.geonature.mountpoint.util.FileUtils
import fr.geonature.mountpoint.util.MountPointUtils.getInternalStorage
import java.io.File

/**
* Helpers for [File] utilities.
*
* @author [S. Grimault](mailto:[email protected])
*/
object FileUtils {

private val TAG = FileUtils::class.java.name

/**
* Construct a file from the set of name elements.
*
* @param directory the parent directory
* @param names the name elements
*
* @return the corresponding file
*/
fun getFile(
directory: File,
vararg names: String
): File {

var file = directory

for (name in names) {
file = File(
file,
name
)
}

return file
}

/**
* Gets the relative path used by given package ID.
*
* @param packageId the package ID
*
* @return the relative path
*/
fun getRelativeSharedPath(packageId: String): String {
return arrayOf(
"Android",
"data",
packageId
).joinToString(
File.separator,
"",
File.separator
)
}
/**
* Gets the `inputs/` folder as `File` from given package ID.
* If package ID is `null` use given context.
* The relative path used is `inputs/<package_name>`
*
* @param context the current `Context`
* @param packageId the package ID (may be `null`)
*
* @return the `inputs/` folder as `File`
*/
fun FileUtils.getInputsFolder(
context: Context,
packageId: String? = null
): File {

return getFile(
getInternalStorage().mountPath,
getRelativeSharedPath(if (packageId.isNullOrBlank()) context.packageName else packageId),
"inputs"
)
}

/**
* Tries to find the mount point used by external storage as `File`.
* If not, returns the default `Environment.getExternalStorageDirectory()`.
*
* @param context the current `Context`
*
* @return the mount point as `File` used by external storage if available
*/
fun getExternalStorageDirectory(context: Context): File {
/**
* Gets the `databases/` folder as `File` used by this context.
*
* @param context the current `Context`
* @param storageType the [MountPoint.StorageType] to use
*
* @return the `databases/` folder
*/
fun FileUtils.getDatabaseFolder(
context: Context,
storageType: MountPoint.StorageType
): File {

val externalMountPoint = getExternalStorage(
return getFile(
getRootFolder(
context,
Environment.MEDIA_MOUNTED,
Environment.MEDIA_MOUNTED_READ_ONLY
)

if (externalMountPoint == null) {
Log.w(
TAG,
"getExternalStorageDirectory: external mount point is not available. Use default: " + getInternalStorage()
)

return getInternalStorage().mountPath
}

return externalMountPoint.mountPath
}

/**
* Gets the root folder as `File` used by this context.
*
* @param context the current `Context`
* @param storageType the [MountPoint.StorageType] to use
*
* @return the root folder as `File`
*/
fun getRootFolder(
context: Context,
storageType: MountPoint.StorageType
): File {

return getFile(
if (storageType === MountPoint.StorageType.EXTERNAL) getExternalStorageDirectory(context)
else getInternalStorage().mountPath,
getRelativeSharedPath(context.packageName)
)
}

/**
* Gets the `inputs/` folder as `File` from given package ID.
* If package ID is `null` use given context.
* The relative path used is `inputs/<package_name>`
*
* @param context the current `Context`
* @param packageId the package ID (may be `null`)
*
* @return the `inputs/` folder as `File`
*/
fun getInputsFolder(
context: Context,
packageId: String? = null
): File {

return getFile(
getInternalStorage().mountPath,
getRelativeSharedPath(if (packageId.isNullOrBlank()) context.packageName else packageId),
"inputs"
)
}

/**
* Gets the `databases/` folder as `File` used by this context.
*
* @param context the current `Context`
* @param storageType the [MountPoint.StorageType] to use
*
* @return the `databases/` folder
*/
fun getDatabaseFolder(
context: Context,
storageType: MountPoint.StorageType
): File {

return getFile(
getRootFolder(
context,
storageType
),
"databases"
)
}
}
storageType
),
"databases"
)
}
21 changes: 3 additions & 18 deletions commons/src/test/java/fr/geonature/commons/util/FileUtilsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package fr.geonature.commons.util

import android.app.Application
import androidx.test.core.app.ApplicationProvider
import fr.geonature.commons.util.FileUtils.getInputsFolder
import fr.geonature.commons.util.FileUtils.getRelativeSharedPath
import org.junit.Assert.assertEquals
import fr.geonature.mountpoint.util.FileUtils
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
Expand All @@ -26,24 +24,11 @@ class FileUtilsTest {
application = ApplicationProvider.getApplicationContext()
}

@Test
fun testGetRelativeSharedPath() {
assertEquals(
"Android/data/fr.geonature.sync/",
getRelativeSharedPath("fr.geonature.sync")
)

assertEquals(
"Android/data/fr.geonature.commons.test/",
getRelativeSharedPath(application.packageName)
)
}

@Test
fun testGetInputsFolder() {
assertTrue(getInputsFolder(application).absolutePath.contains("/Android/data/fr.geonature.commons.test/inputs"))
assertTrue(FileUtils.getInputsFolder(application).absolutePath.contains("/Android/data/fr.geonature.commons.test/inputs"))
assertTrue(
getInputsFolder(
FileUtils.getInputsFolder(
application,
"fr.geonature.sync"
).absolutePath.contains("/Android/data/fr.geonature.sync/inputs")
Expand Down
Loading

0 comments on commit 4ac1be4

Please sign in to comment.