Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 0 additions & 74 deletions gnd/src/main/java/com/google/android/gnd/ui/MarkerIconFactory.java

This file was deleted.

63 changes: 63 additions & 0 deletions gnd/src/main/java/com/google/android/gnd/ui/MarkerIconFactory.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.gnd.ui

import android.content.Context
import javax.inject.Singleton
import javax.inject.Inject
import dagger.hilt.android.qualifiers.ApplicationContext
import android.graphics.Bitmap
import android.graphics.Canvas
import androidx.appcompat.content.res.AppCompatResources
import com.google.android.gnd.R
import com.google.android.gnd.ui.home.mapcontainer.MapContainerViewModel
import android.graphics.PorterDuff
import androidx.annotation.ColorInt
import androidx.core.content.res.ResourcesCompat
import com.google.android.gms.maps.model.BitmapDescriptor
import com.google.android.gms.maps.model.BitmapDescriptorFactory

@Singleton
class MarkerIconFactory @Inject constructor(@ApplicationContext private val context: Context) {
fun getMarkerBitmap(color: Int, currentZoomLevel: Float): Bitmap {
val outline = AppCompatResources.getDrawable(context, R.drawable.ic_marker_outline)
val fill = AppCompatResources.getDrawable(context, R.drawable.ic_marker_fill)
val overlay = AppCompatResources.getDrawable(context, R.drawable.ic_marker_overlay)
// TODO: Adjust size based on selection state.
var scale = ResourcesCompat.getFloat(context.resources, R.dimen.marker_bitmap_default_scale)
if (currentZoomLevel >= MapContainerViewModel.ZOOM_LEVEL_THRESHOLD) {
scale = ResourcesCompat.getFloat(context.resources, R.dimen.marker_bitmap_zoomed_scale)
}
val width = (outline!!.intrinsicWidth * scale).toInt()
val height = (outline.intrinsicHeight * scale).toInt()
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
outline.setBounds(0, 0, width, height)
outline.draw(canvas)
fill!!.setColorFilter(color, PorterDuff.Mode.SRC_ATOP)
fill.setBounds(0, 0, width, height)
fill.draw(canvas)
overlay!!.setBounds(0, 0, width, height)
overlay.draw(canvas)
return bitmap
}

fun getMarkerIcon(@ColorInt color: Int, currentZoomLevel: Float): BitmapDescriptor {
val bitmap = getMarkerBitmap(color, currentZoomLevel)
// TODO: Cache rendered bitmaps.
return BitmapDescriptorFactory.fromBitmap(bitmap)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.gnd.ui

import android.content.Context
import dagger.hilt.android.testing.HiltAndroidTest
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import com.google.android.gnd.BaseHiltTest
import javax.inject.Inject
import dagger.hilt.android.qualifiers.ApplicationContext
import org.junit.Before
import androidx.appcompat.content.res.AppCompatResources
import com.google.android.gnd.R
import android.graphics.Bitmap
import android.graphics.Color
import androidx.core.content.res.ResourcesCompat
import com.google.android.gnd.ui.home.mapcontainer.MapContainerViewModel
import com.google.common.truth.Truth.assertThat
import org.junit.Test

@HiltAndroidTest
@RunWith(RobolectricTestRunner::class)
class MarkerIconFactoryTest : BaseHiltTest() {
@Inject
@ApplicationContext
lateinit var context: Context

@Inject
lateinit var markerIconFactory: MarkerIconFactory
private var markerUnscaledWidth = 0
private var markerUnscaledHeight = 0

@Before
override fun setUp() {
super.setUp()
val outline = AppCompatResources.getDrawable(context, R.drawable.ic_marker_outline)
markerUnscaledWidth = outline!!.intrinsicWidth
markerUnscaledHeight = outline.intrinsicHeight
}

@Test
fun markerBitmap_zoomedOut_scaleIsSetCorrectly() {
val bitmap = markerIconFactory.getMarkerBitmap(
Color.BLUE,
MapContainerViewModel.ZOOM_LEVEL_THRESHOLD - 0.1f
)

val scale =
ResourcesCompat.getFloat(context.resources, R.dimen.marker_bitmap_default_scale)
verifyBitmapScale(bitmap, scale)
}

@Test
fun markerBitmap_zoomedIn_scaleIsSetCorrectly() {
val bitmap = markerIconFactory.getMarkerBitmap(
Color.BLUE,
MapContainerViewModel.ZOOM_LEVEL_THRESHOLD
)

val scale =
ResourcesCompat.getFloat(context.resources, R.dimen.marker_bitmap_zoomed_scale)
verifyBitmapScale(bitmap, scale)
}

private fun verifyBitmapScale(bitmap: Bitmap, scale: Float) {
val expectedWidth = (markerUnscaledWidth * scale).toInt()
val expectedHeight = (markerUnscaledHeight * scale).toInt()
assertThat(bitmap.width).isEqualTo(expectedWidth)
assertThat(bitmap.height).isEqualTo(expectedHeight)
}
}