Convert more of the Android SKD files from Java to Kotlin (#670)

This commit is contained in:
Artak Kalantarian
2023-01-10 05:41:33 -08:00
committed by GitHub
parent 9d99adc431
commit 4f15cf96e6
33 changed files with 2621 additions and 3094 deletions

View File

@@ -1,8 +1,6 @@
package com.mapbox.mapboxsdk.testapp.activity.offline
import android.annotation.TargetApi
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
@@ -77,11 +75,11 @@ class ChangeResourcesCachePathActivity :
Toast.makeText(this, "New path: $path", Toast.LENGTH_LONG).show()
offlineManager.listOfflineRegions(object : OfflineManager.ListOfflineRegionsCallback {
override fun onList(offlineRegions: Array<out OfflineRegion>?) {
override fun onList(offlineRegions: Array<OfflineRegion>?) {
Logger.i(TAG, "Number of saved offline regions in the new path: ${offlineRegions?.size}")
}
override fun onError(error: String?) {
override fun onError(error: String) {
Logger.e(TAG, error)
}
})
@@ -90,34 +88,11 @@ class ChangeResourcesCachePathActivity :
private fun Context.obtainExternalFilesPaths(): List<String> {
val paths = ArrayList<String>()
paths.add(this.filesDir.absolutePath)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
paths.addAll(obtainExternalFilesPathsKitKat())
} else {
paths.addAll(obtainExternalFilesPathsLegacy())
}
paths.addAll(obtainExternalFilesPathsKitKat())
paths.add("${File.separator}invalid${File.separator}cache${File.separator}path")
return paths
}
private fun Context.obtainExternalFilesPathsLegacy(): List<String> {
val postFix =
"${File.separator}Android${File.separator}data${File.separator}${this.packageName}${File.separator}files"
val paths = ArrayList<String>()
val externalStorage = System.getenv("EXTERNAL_STORAGE")
val secondaryStorage = System.getenv("SECONDARY_STORAGE")
if (externalStorage != null) {
paths.add(externalStorage + postFix)
}
if (secondaryStorage != null) {
val secPaths = secondaryStorage.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
for (path in secPaths) {
paths.add(path + postFix)
}
}
return paths
}
@TargetApi(Build.VERSION_CODES.KITKAT)
private fun Context.obtainExternalFilesPathsKitKat(): List<String> {
val paths = ArrayList<String>()
val extDirs = this.getExternalFilesDirs(null)

View File

@@ -101,8 +101,8 @@ class DeleteRegionActivity :
private fun loadOfflineRegions() {
OfflineManager.getInstance(this).listOfflineRegions(object : ListOfflineRegionsCallback {
override fun onList(offlineRegions: Array<OfflineRegion>) {
if (offlineRegions != null && offlineRegions.size > 0) {
override fun onList(offlineRegions: Array<OfflineRegion>?) {
if (offlineRegions != null && offlineRegions.isNotEmpty()) {
adapter!!.setOfflineRegions(Arrays.asList(*offlineRegions))
}
}
@@ -137,7 +137,7 @@ class DeleteRegionActivity :
return position.toLong()
}
override fun getView(position: Int, convertView: View, parent: ViewGroup): View {
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
var convertView = convertView
val holder: ViewHolder
if (convertView == null) {
@@ -150,7 +150,7 @@ class DeleteRegionActivity :
holder = convertView.tag as ViewHolder
}
holder.text!!.text = OfflineUtils.convertRegionName(getItem(position).metadata)
return convertView
return convertView!!
}
internal class ViewHolder {

View File

@@ -89,10 +89,10 @@ class DownloadRegionActivity : AppCompatActivity(), OfflineRegion.OfflineRegionO
definition,
byteArrayOf(),
object : OfflineManager.CreateOfflineRegionCallback {
override fun onCreate(region: OfflineRegion) {
logMessage("Region with id ${region.id} created")
offlineRegion = region
startDownload(region)
override fun onCreate(offlineRegion: OfflineRegion) {
logMessage("Region with id ${offlineRegion.id} created")
this@DownloadRegionActivity.offlineRegion = offlineRegion
startDownload(offlineRegion)
binding.fab.visibility = View.VISIBLE
}
@@ -181,12 +181,12 @@ class DownloadRegionActivity : AppCompatActivity(), OfflineRegion.OfflineRegionO
fun deleteOldOfflineRegions(onCompleted: () -> Unit) {
offlineManager.listOfflineRegions(object : OfflineManager.ListOfflineRegionsCallback {
override fun onList(offlineRegions: Array<out OfflineRegion>) {
val count = offlineRegions.size
override fun onList(offlineRegions: Array<OfflineRegion>?) {
val count = offlineRegions?.size ?: 0
var remainingCount = count
if (count > 0) {
logMessage("Deleting $count old region...")
offlineRegions.forEach {
offlineRegions?.forEach {
it.delete(object : OfflineRegion.OfflineRegionDeleteCallback {
override fun onDelete() {
Timber.d("Deleted region with id ${it.id}")

View File

@@ -42,13 +42,13 @@ class MergeOfflineRegionsActivity : AppCompatActivity() {
}
private val onRegionMergedListener = object : OfflineManager.MergeOfflineRegionsCallback {
override fun onMerge(offlineRegions: Array<OfflineRegion>) {
override fun onMerge(offlineRegions: Array<OfflineRegion>?) {
binding.mapView.getMapAsync {
it.setStyle(Style.Builder().fromUri(TEST_STYLE))
}
Toast.makeText(
this@MergeOfflineRegionsActivity,
String.format("Merged %d regions.", offlineRegions.size),
String.format("Merged %d regions.", offlineRegions?.size ?: 0),
Toast.LENGTH_LONG
).show()
}
@@ -69,11 +69,11 @@ class MergeOfflineRegionsActivity : AppCompatActivity() {
*/
private class MergeCallback(private var activityCallback: OfflineManager.MergeOfflineRegionsCallback?) : OfflineManager.MergeOfflineRegionsCallback {
override fun onMerge(offlineRegions: Array<out OfflineRegion>?) {
override fun onMerge(offlineRegions: Array<OfflineRegion>?) {
activityCallback?.onMerge(offlineRegions)
}
override fun onError(error: String?) {
override fun onError(error: String) {
activityCallback?.onError(error)
}

View File

@@ -145,9 +145,9 @@ class OfflineActivity : AppCompatActivity(), DownloadRegionDialogListener {
// Query the DB asynchronously
offlineManager!!.listOfflineRegions(object : ListOfflineRegionsCallback {
override fun onList(offlineRegions: Array<OfflineRegion>) {
override fun onList(offlineRegions: Array<OfflineRegion>?) {
// Check result
if (offlineRegions == null || offlineRegions.size == 0) {
if (offlineRegions == null || offlineRegions.isEmpty()) {
Toast.makeText(
this@OfflineActivity,
"You have no regions yet.",

View File

@@ -112,7 +112,7 @@ class UpdateMetadataActivity :
private fun loadOfflineRegions() {
OfflineManager.getInstance(this).listOfflineRegions(object : ListOfflineRegionsCallback {
override fun onList(offlineRegions: Array<OfflineRegion>) {
override fun onList(offlineRegions: Array<OfflineRegion>?) {
if (offlineRegions != null && offlineRegions.size > 0) {
adapter!!.setOfflineRegions(Arrays.asList(*offlineRegions))
}
@@ -137,7 +137,7 @@ class UpdateMetadataActivity :
}
}
private class OfflineRegionMetadataAdapter internal constructor(private val context: Context) :
private class OfflineRegionMetadataAdapter(private val context: Context) :
BaseAdapter() {
private var offlineRegions: List<OfflineRegion>
fun setOfflineRegions(offlineRegions: List<OfflineRegion>) {
@@ -157,8 +157,8 @@ class UpdateMetadataActivity :
return position.toLong()
}
override fun getView(position: Int, convertView: View, parent: ViewGroup): View {
var convertView = convertView
override fun getView(position: Int, convertViewParam: View?, parent: ViewGroup): View {
var convertView = convertViewParam
val holder: ViewHolder
if (convertView == null) {
holder = ViewHolder()
@@ -170,7 +170,7 @@ class UpdateMetadataActivity :
holder = convertView.tag as ViewHolder
}
holder.text!!.text = OfflineUtils.convertRegionName(getItem(position).metadata)
return convertView
return convertView!!
}
internal class ViewHolder {

View File

@@ -153,17 +153,22 @@ class RenderTestActivity : AppCompatActivity() {
Timber.d("Render test %s,%s", renderTestDefinition.name, renderTestDefinition.category)
mapSnapshotter = RenderTestSnapshotter(this, renderTestDefinition.toOptions())
mapSnapshotter.start(
MapSnapshotter.SnapshotReadyCallback { result: MapSnapshot ->
val snapshot = result.bitmap
imageView!!.setImageBitmap(snapshot)
renderResultMap[renderTestDefinition] = snapshot
if (renderResultMap.size != testSize) {
continueTesting(renderTestDefinition)
} else {
finishTesting()
object : MapSnapshotter.SnapshotReadyCallback {
override fun onSnapshotReady(snapshot: MapSnapshot) {
imageView!!.setImageBitmap(snapshot.bitmap)
renderResultMap[renderTestDefinition] = snapshot.bitmap
if (renderResultMap.size != testSize) {
continueTesting(renderTestDefinition)
} else {
finishTesting()
}
}
},
MapSnapshotter.ErrorHandler { error: String? -> Timber.e(error) }
object : MapSnapshotter.ErrorHandler {
override fun onError(error: String) {
Timber.e(error)
}
}
)
}

View File

@@ -198,6 +198,7 @@ class MapSnapshotterActivity : AppCompatActivity() {
}
options.withStyleBuilder(builder)
val snapshotter = MapSnapshotter(this@MapSnapshotterActivity, options)
snapshotter.setObserver(object : MapSnapshotter.Observer {
override fun onDidFinishLoadingStyle() {
Timber.i("onDidFinishLoadingStyle")
@@ -209,15 +210,21 @@ class MapSnapshotterActivity : AppCompatActivity() {
snapshotter.addImage(imageName, androidIcon!!, false)
}
})
snapshotter.start { snapshot: MapSnapshot ->
Timber.i("Got the snapshot")
val imageView = ImageView(this@MapSnapshotterActivity)
imageView.setImageBitmap(snapshot.bitmap)
grid.addView(
imageView,
GridLayout.LayoutParams(GridLayout.spec(row), GridLayout.spec(column))
)
}
snapshotter.start(
object : MapSnapshotter.SnapshotReadyCallback {
override fun onSnapshotReady(snapshot: MapSnapshot) {
Timber.i("Got the snapshot")
val imageView = ImageView(this@MapSnapshotterActivity)
imageView.setImageBitmap(snapshot.bitmap)
grid.addView(
imageView,
GridLayout.LayoutParams(GridLayout.spec(row), GridLayout.spec(column))
)
}
}
)
snapshotters.add(snapshotter)
}

View File

@@ -53,7 +53,11 @@ class MapSnapshotterLocalStyleActivity : AppCompatActivity(), MapSnapshotter.Sna
)
mapSnapshotter!!.start(
this@MapSnapshotterLocalStyleActivity,
MapSnapshotter.ErrorHandler { error: String? -> Timber.e(error) }
object : MapSnapshotter.ErrorHandler {
override fun onError(error: String) {
Timber.e(error)
}
}
)
}
})

View File

@@ -11,6 +11,7 @@ import com.mapbox.mapboxsdk.geometry.LatLng
import com.mapbox.mapboxsdk.maps.MapView
import com.mapbox.mapboxsdk.maps.MapboxMap
import com.mapbox.mapboxsdk.maps.Style
import com.mapbox.mapboxsdk.snapshotter.MapSnapshot
import com.mapbox.mapboxsdk.snapshotter.MapSnapshotter
import com.mapbox.mapboxsdk.style.expressions.Expression.within
import com.mapbox.mapboxsdk.style.layers.CircleLayer
@@ -37,16 +38,18 @@ class MapSnapshotterWithinExpression : AppCompatActivity() {
if (!snapshotInProgress) {
snapshotInProgress = true
snapshotter.setCameraPosition(mapboxMap.cameraPosition)
snapshotter.start {
binding.imageView.setImageBitmap(it.bitmap)
snapshotInProgress = false
}
snapshotter.start(object : MapSnapshotter.SnapshotReadyCallback {
override fun onSnapshotReady(snapshot: MapSnapshot) {
binding.imageView.setImageBitmap(snapshot.bitmap)
snapshotInProgress = false
}
})
}
}
}
private val snapshotterObserver = object : MapSnapshotter.Observer {
override fun onStyleImageMissing(imageName: String?) {
override fun onStyleImageMissing(imageName: String) {
}
override fun onDidFinishLoadingStyle() {
@@ -73,22 +76,14 @@ class MapSnapshotterWithinExpression : AppCompatActivity() {
mapboxMap = map
// Setup camera position above Georgetown
mapboxMap.cameraPosition = CameraPosition.Builder()
.target(LatLng(38.90628988399711, -77.06574689337494))
.zoom(15.5)
.build()
mapboxMap.cameraPosition = CameraPosition.Builder().target(LatLng(38.90628988399711, -77.06574689337494)).zoom(15.5).build()
// Wait for the map to become idle before manipulating the style and camera of the map
binding.mapView.addOnDidBecomeIdleListener(object : MapView.OnDidBecomeIdleListener {
override fun onDidBecomeIdle() {
mapboxMap.easeCamera(
CameraUpdateFactory.newCameraPosition(
CameraPosition.Builder()
.zoom(16.0)
.target(LatLng(38.905156245642814, -77.06535338052844))
.bearing(80.68015859462369)
.tilt(55.0)
.build()
CameraPosition.Builder().zoom(16.0).target(LatLng(38.905156245642814, -77.06535338052844)).bearing(80.68015859462369).tilt(55.0).build()
),
1000
)
@@ -103,74 +98,55 @@ class MapSnapshotterWithinExpression : AppCompatActivity() {
private fun setupStyle() {
// Assume the route is represented by an array of coordinates.
val coordinates = listOf<Point>(
Point.fromLngLat(
-77.06866264343262,
38.90506061276737
),
Point.fromLngLat(
-77.06283688545227,
38.905194197410545
),
Point.fromLngLat(
-77.06285834312439,
38.906429843444094
),
Point.fromLngLat(
-77.0630407333374,
38.90680554236621
)
Point.fromLngLat(-77.06866264343262, 38.90506061276737),
Point.fromLngLat(-77.06283688545227, 38.905194197410545),
Point.fromLngLat(-77.06285834312439, 38.906429843444094),
Point.fromLngLat(-77.0630407333374, 38.90680554236621)
)
// Setup style with additional layers,
// using streets as a base style
mapboxMap.setStyle(
Style.Builder()
.fromUri(Style.getPredefinedStyle("Streets"))
Style.Builder().fromUri(Style.getPredefinedStyle("Streets"))
) {
binding.mapView.addOnCameraDidChangeListener(cameraListener)
}
val options = MapSnapshotter.Options(
binding.imageView.measuredWidth / 2,
binding.imageView.measuredHeight / 2
)
val options = MapSnapshotter.Options(binding.imageView.measuredWidth / 2, binding.imageView.measuredHeight / 2)
.withCameraPosition(mapboxMap.cameraPosition)
.withPixelRatio(2.0f)
.withStyleBuilder(
Style.Builder()
.fromUri(Style.getPredefinedStyle("Streets"))
.withSources(
GeoJsonSource(
POINT_ID,
LineString.fromLngLats(coordinates)
.withPixelRatio(2.0f).withStyleBuilder(
Style.Builder().fromUri(Style.getPredefinedStyle("Streets")).withSources(
GeoJsonSource(
POINT_ID,
LineString.fromLngLats(coordinates)
),
GeoJsonSource(
FILL_ID,
FeatureCollection.fromFeature(
Feature.fromGeometry(bufferLineStringGeometry())
),
GeoJsonSource(
FILL_ID,
FeatureCollection.fromFeature(Feature.fromGeometry(bufferLineStringGeometry())),
GeoJsonOptions().withBuffer(0).withTolerance(0.0f)
)
)
.withLayerBelow(
LineLayer(LINE_ID, POINT_ID)
.withProperties(lineWidth(7.5f), lineColor(Color.LTGRAY)),
"poi-label"
)
.withLayerBelow(
CircleLayer(POINT_ID, POINT_ID)
.withProperties(
circleRadius(7.5f),
circleColor(Color.DKGRAY),
circleOpacity(0.75f)
),
"poi-label"
).withLayerBelow(
FillLayer(FILL_ID, FILL_ID)
.withProperties(
fillOpacity(0.12f),
fillColor(Color.YELLOW)
),
LINE_ID
GeoJsonOptions().withBuffer(0).withTolerance(0.0f)
)
).withLayerBelow(
LineLayer(LINE_ID, POINT_ID).withProperties(
lineWidth(7.5f),
lineColor(Color.LTGRAY)
),
"poi-label"
).withLayerBelow(
CircleLayer(POINT_ID, POINT_ID).withProperties(
circleRadius(7.5f),
circleColor(Color.DKGRAY),
circleOpacity(0.75f)
),
"poi-label"
).withLayerBelow(
FillLayer(FILL_ID, FILL_ID).withProperties(
fillOpacity(0.12f),
fillColor(Color.YELLOW)
),
LINE_ID
)
)
snapshotter = MapSnapshotter(this, options)
snapshotter.setObserver(snapshotterObserver)