-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6169 from grzesiek2010/COLLECT-5846
Allow MBTile files to be imported
- Loading branch information
Showing
29 changed files
with
1,187 additions
and
273 deletions.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
androidshared/src/main/java/org/odk/collect/androidshared/system/UriExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package org.odk.collect.androidshared.system | ||
|
||
import android.content.ContentResolver | ||
import android.content.Context | ||
import android.net.Uri | ||
import android.provider.OpenableColumns | ||
import android.webkit.MimeTypeMap | ||
import androidx.core.net.toFile | ||
import java.io.File | ||
import java.io.FileOutputStream | ||
|
||
fun Uri.copyToFile(context: Context, dest: File) { | ||
try { | ||
context.contentResolver.openInputStream(this)?.use { inputStream -> | ||
FileOutputStream(dest).use { outputStream -> | ||
inputStream.copyTo(outputStream) | ||
} | ||
} | ||
} catch (e: Exception) { | ||
// ignore | ||
} | ||
} | ||
|
||
fun Uri.getFileExtension(context: Context): String? { | ||
var extension = getFileName(context)?.substringAfterLast(".", "") | ||
|
||
if (extension.isNullOrEmpty()) { | ||
val mimeType = context.contentResolver.getType(this) | ||
|
||
extension = if (scheme == ContentResolver.SCHEME_CONTENT) { | ||
MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType) | ||
} else { | ||
MimeTypeMap.getFileExtensionFromUrl(toString()) | ||
} | ||
|
||
if (extension.isNullOrEmpty()) { | ||
extension = mimeType?.substringAfterLast("/", "") | ||
} | ||
} | ||
|
||
return if (extension.isNullOrEmpty()) { | ||
null | ||
} else { | ||
".$extension" | ||
} | ||
} | ||
|
||
fun Uri.getFileName(context: Context): String? { | ||
var fileName: String? = null | ||
|
||
try { | ||
when (scheme) { | ||
ContentResolver.SCHEME_FILE -> fileName = toFile().name | ||
ContentResolver.SCHEME_CONTENT -> { | ||
val cursor = context.contentResolver.query(this, null, null, null, null) | ||
cursor?.use { | ||
if (it.moveToFirst()) { | ||
val fileNameColumnIndex = it.getColumnIndex(OpenableColumns.DISPLAY_NAME) | ||
if (fileNameColumnIndex != -1) { | ||
fileName = it.getString(fileNameColumnIndex) | ||
} | ||
} | ||
} | ||
} | ||
ContentResolver.SCHEME_ANDROID_RESOURCE -> { | ||
// for uris like [android.resource://com.example.app/1234567890] | ||
val resourceId = lastPathSegment?.toIntOrNull() | ||
if (resourceId != null) { | ||
fileName = context.resources.getResourceName(resourceId) | ||
} else { | ||
// for uris like [android.resource://com.example.app/raw/sample] | ||
val packageName = authority | ||
if (pathSegments.size >= 2) { | ||
val resourceType = pathSegments[0] | ||
val resourceEntryName = pathSegments[1] | ||
val resId = context.resources.getIdentifier(resourceEntryName, resourceType, packageName) | ||
if (resId != 0) { | ||
fileName = context.resources.getResourceName(resId) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (fileName == null) { | ||
fileName = path?.substringAfterLast("/") | ||
} | ||
} catch (e: Exception) { | ||
// ignore | ||
} | ||
|
||
return fileName | ||
} |
44 changes: 44 additions & 0 deletions
44
androidshared/src/test/java/org/odk/collect/androidshared/system/UriExtTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.odk.collect.androidshared.system | ||
|
||
import android.app.Application | ||
import androidx.core.net.toUri | ||
import androidx.test.core.app.ApplicationProvider | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import org.hamcrest.CoreMatchers.equalTo | ||
import org.hamcrest.MatcherAssert.assertThat | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.odk.collect.shared.TempFiles | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class UriExtTest { | ||
private val context = ApplicationProvider.getApplicationContext<Application>() | ||
|
||
@Test | ||
fun `copyToFile copies the source file to the target file`() { | ||
val sourceFile = TempFiles.createTempFile().also { | ||
it.writeText("blah") | ||
} | ||
val sourceFileUri = sourceFile.toUri() | ||
val targetFile = TempFiles.createTempFile() | ||
|
||
sourceFileUri.copyToFile(context, targetFile) | ||
assertThat(targetFile.readText(), equalTo(sourceFile.readText())) | ||
} | ||
|
||
@Test | ||
fun `getFileExtension returns file extension`() { | ||
val file = TempFiles.createTempFile(".jpg") | ||
val fileUri = file.toUri() | ||
|
||
assertThat(fileUri.getFileExtension(context), equalTo(".jpg")) | ||
} | ||
|
||
@Test | ||
fun `getFileName returns file name`() { | ||
val file = TempFiles.createTempFile() | ||
val fileUri = file.toUri() | ||
|
||
assertThat(fileUri.getFileName(context), equalTo(file.name)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 0 additions & 18 deletions
18
collect_app/src/main/res/layout/reference_layer_help_footer.xml
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<vector | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:viewportWidth="24" | ||
android:viewportHeight="24" | ||
android:autoMirrored="true" | ||
android:tint="?colorOnSurface"> | ||
|
||
<path | ||
android:fillColor="?colorOnSurface" | ||
android:pathData="M11.99,18.54l-7.37,-5.73L3,14.07l9,7 9,-7 -1.63,-1.27 -7.38,5.74zM12,16l7.36,-5.73L21,9l-9,-7 -9,7 1.63,1.27L12,16z"/> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.