forked from maplibre/maplibre-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port most Android TestApp utils to kotlin (maplibre#668)
- Loading branch information
Showing
18 changed files
with
409 additions
and
449 deletions.
There are no files selected for viewing
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
39 changes: 0 additions & 39 deletions
39
...pboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/utils/ApiKeyUtils.java
This file was deleted.
Oops, something went wrong.
34 changes: 34 additions & 0 deletions
34
...MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/utils/ApiKeyUtils.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,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 | ||
} | ||
} | ||
} |
26 changes: 0 additions & 26 deletions
26
...MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/utils/FontCache.java
This file was deleted.
Oops, something went wrong.
25 changes: 25 additions & 0 deletions
25
...d/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/utils/FontCache.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,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 | ||
} | ||
} |
51 changes: 0 additions & 51 deletions
51
...boxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/utils/GeoParseUtil.java
This file was deleted.
Oops, something went wrong.
52 changes: 52 additions & 0 deletions
52
...apboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/utils/GeoParseUtil.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,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() | ||
} | ||
} | ||
} |
32 changes: 0 additions & 32 deletions
32
...MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/utils/IconUtils.java
This file was deleted.
Oops, something went wrong.
31 changes: 31 additions & 0 deletions
31
...d/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/utils/IconUtils.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,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) | ||
} | ||
} |
Oops, something went wrong.