Skip to content

Commit

Permalink
Port most Android TestApp utils to kotlin (maplibre#668)
Browse files Browse the repository at this point in the history
  • Loading branch information
birkskyum authored Jan 9, 2023
1 parent 3b4bf34 commit 9d99adc
Show file tree
Hide file tree
Showing 18 changed files with 409 additions and 449 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ class MapLibreApplication : MultiDexApplication() {

private fun initializeMapbox() {
val apiKey = ApiKeyUtils.getApiKey(applicationContext)
validateApiKey(apiKey)
if (apiKey != null) {
validateApiKey(apiKey)
}
Mapbox.getInstance(applicationContext, apiKey, TILE_SERVER)
TileLoadingMeasurementUtils.setUpTileLoadingMeasurement()
MapStrictMode.setStrictModeEnabled(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import com.mapbox.mapboxsdk.style.layers.SymbolLayer
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource
import com.mapbox.mapboxsdk.testapp.R
import com.mapbox.mapboxsdk.testapp.databinding.ActivityLatlngboundsBinding
import com.mapbox.mapboxsdk.testapp.utils.GeoParseUtil.loadStringFromAssets
import com.mapbox.mapboxsdk.testapp.utils.GeoParseUtil
import com.mapbox.mapboxsdk.utils.BitmapUtils
import java.net.URISyntaxException

Expand Down Expand Up @@ -52,7 +52,7 @@ class LatLngBoundsActivity : AppCompatActivity() {
mapboxMap = map

val featureCollection: FeatureCollection =
fromJson(loadStringFromAssets(this, "points-sf.geojson"))
fromJson(GeoParseUtil.loadStringFromAssets(this, "points-sf.geojson"))
bounds = createBounds(featureCollection)

map.getCameraForLatLngBounds(bounds, createPadding(peekHeight))?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,21 +209,23 @@ class OfflineActivity : AppCompatActivity(), DownloadRegionDialogListener {
val metadata = OfflineUtils.convertRegionName(regionName)

// Create region
offlineManager!!.createOfflineRegion(
definition,
metadata,
object : CreateOfflineRegionCallback {
override fun onCreate(offlineRegion: OfflineRegion) {
Timber.d("Offline region created: %s", regionName)
this@OfflineActivity.offlineRegion = offlineRegion
launchDownload()
if (metadata != null) {
offlineManager!!.createOfflineRegion(
definition,
metadata,
object : CreateOfflineRegionCallback {
override fun onCreate(offlineRegion: OfflineRegion) {
Timber.d("Offline region created: %s", regionName)
this@OfflineActivity.offlineRegion = offlineRegion
launchDownload()
}

override fun onError(error: String) {
Timber.e("Error: %s", error)
}
}

override fun onError(error: String) {
Timber.e("Error: %s", error)
}
}
)
)
}
}

private fun launchDownload() {
Expand Down Expand Up @@ -308,7 +310,7 @@ class OfflineActivity : AppCompatActivity(), DownloadRegionDialogListener {

companion object {
// JSON encoding/decoding
const val JSON_CHARSET = "UTF-8"
val JSON_CHARSET = Charsets.UTF_8
const val JSON_FIELD_REGION_NAME = "FIELD_REGION_NAME"

// Style URL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ class UpdateMetadataActivity :
builder.setPositiveButton(
"OK"
) { dialog: DialogInterface?, which: Int ->
updateMetadata(
region,
OfflineUtils.convertRegionName(input.text.toString())
)
OfflineUtils.convertRegionName(input.text.toString())?.let {
updateMetadata(
region,
it
)
}
}
builder.setNegativeButton("Cancel") { dialog: DialogInterface, which: Int -> dialog.cancel() }
builder.show()
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.mapbox.mapboxsdk.testapp.utils

import android.content.Context
import com.mapbox.mapboxsdk.Mapbox
import java.lang.Exception

object ApiKeyUtils {
/**
*
*
* Returns the ApiKey set in the app resources.
*
* It will first search for a api key in the Mapbox object. If not found it
* will then attempt to load the api key from the
* `res/values/dev.xml` development file.
*
* @param context The [Context] of the [android.app.Activity] or [android.app.Fragment].
* @return The api key or null if not found.
*/
fun getApiKey(context: Context): String? {
return try {
// Read out AndroidManifest
val apiKey = Mapbox.getApiKey()
require(!(apiKey == null || apiKey.isEmpty()))
apiKey
} catch (exception: Exception) {
// Use fallback on string resource, used for development
// TODO:PP
val apiKeyResId = context.resources
.getIdentifier("api_key", "string", context.packageName)
if (apiKeyResId != 0) context.getString(apiKeyResId) else null
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.mapbox.mapboxsdk.testapp.utils

import android.content.Context
import android.graphics.Typeface
import timber.log.Timber
import java.lang.Exception
import java.util.*

object FontCache {
private val fontCache = Hashtable<String, Typeface?>()

@JvmStatic
operator fun get(name: String, context: Context): Typeface? {
var tf = fontCache[name]
if (tf == null) {
try {
tf = Typeface.createFromAsset(context.assets, name)
fontCache[name] = tf
} catch (exception: Exception) {
Timber.e("Font not found")
}
}
return tf
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.mapbox.mapboxsdk.testapp.utils

import android.content.Context
import android.text.TextUtils
import com.mapbox.geojson.FeatureCollection
import com.mapbox.geojson.Point
import com.mapbox.mapboxsdk.geometry.LatLng
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader
import java.io.Reader
import java.lang.NullPointerException
import java.lang.StringBuilder
import java.util.ArrayList
import kotlin.Throws

class GeoParseUtil {

companion object {
@Throws(IOException::class)
fun loadStringFromAssets(context: Context, fileName: String?): String {
if (TextUtils.isEmpty(fileName)) {
throw NullPointerException("No GeoJSON File Name passed in.")
}
val `is` = context.assets.open(fileName!!)
val rd = BufferedReader(InputStreamReader(`is`, Charsets.UTF_8))
return readAll(rd)
}

fun parseGeoJsonCoordinates(geojsonStr: String?): List<LatLng> {
val latLngs: MutableList<LatLng> = ArrayList()
val featureCollection = FeatureCollection.fromJson(geojsonStr!!)
for (feature in featureCollection.features()!!) {
if (feature.geometry() is Point) {
val point = feature.geometry() as Point?
latLngs.add(LatLng(point!!.latitude(), point.longitude()))
}
}
return latLngs
}

@Throws(IOException::class)
private fun readAll(rd: Reader): String {
val sb = StringBuilder()
var cp: Int
while (rd.read().also { cp = it } != -1) {
sb.append(cp.toChar())
}
return sb.toString()
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.mapbox.mapboxsdk.testapp.utils

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes
import androidx.core.content.res.ResourcesCompat
import androidx.core.graphics.drawable.DrawableCompat
import com.mapbox.mapboxsdk.annotations.Icon
import com.mapbox.mapboxsdk.annotations.IconFactory

object IconUtils {
/**
* Demonstrates converting any Drawable to an Icon, for use as a marker icon.
*/
@JvmStatic
fun drawableToIcon(context: Context, @DrawableRes id: Int, @ColorInt colorRes: Int): Icon {
val vectorDrawable = ResourcesCompat.getDrawable(context.resources, id, context.theme)
val bitmap = Bitmap.createBitmap(
vectorDrawable!!.intrinsicWidth,
vectorDrawable.intrinsicHeight,
Bitmap.Config.ARGB_8888
)
val canvas = Canvas(bitmap)
vectorDrawable.setBounds(0, 0, canvas.width, canvas.height)
DrawableCompat.setTint(vectorDrawable, colorRes)
vectorDrawable.draw(canvas)
return IconFactory.getInstance(context).fromBitmap(bitmap)
}
}
Loading

0 comments on commit 9d99adc

Please sign in to comment.