Skip to content

Commit 135edcc

Browse files
Merge pull request #1109 from JSunde/startup
Convert MarkerIconFactory and test to Kotlin
2 parents 983c1ab + 1e0a9a2 commit 135edcc

File tree

4 files changed

+148
-155
lines changed

4 files changed

+148
-155
lines changed

gnd/src/main/java/com/google/android/gnd/ui/MarkerIconFactory.java

Lines changed: 0 additions & 74 deletions
This file was deleted.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.android.gnd.ui
17+
18+
import android.content.Context
19+
import javax.inject.Singleton
20+
import javax.inject.Inject
21+
import dagger.hilt.android.qualifiers.ApplicationContext
22+
import android.graphics.Bitmap
23+
import android.graphics.Canvas
24+
import androidx.appcompat.content.res.AppCompatResources
25+
import com.google.android.gnd.R
26+
import com.google.android.gnd.ui.home.mapcontainer.MapContainerViewModel
27+
import android.graphics.PorterDuff
28+
import androidx.annotation.ColorInt
29+
import androidx.core.content.res.ResourcesCompat
30+
import com.google.android.gms.maps.model.BitmapDescriptor
31+
import com.google.android.gms.maps.model.BitmapDescriptorFactory
32+
33+
@Singleton
34+
class MarkerIconFactory @Inject constructor(@ApplicationContext private val context: Context) {
35+
fun getMarkerBitmap(color: Int, currentZoomLevel: Float): Bitmap {
36+
val outline = AppCompatResources.getDrawable(context, R.drawable.ic_marker_outline)
37+
val fill = AppCompatResources.getDrawable(context, R.drawable.ic_marker_fill)
38+
val overlay = AppCompatResources.getDrawable(context, R.drawable.ic_marker_overlay)
39+
// TODO: Adjust size based on selection state.
40+
var scale = ResourcesCompat.getFloat(context.resources, R.dimen.marker_bitmap_default_scale)
41+
if (currentZoomLevel >= MapContainerViewModel.ZOOM_LEVEL_THRESHOLD) {
42+
scale = ResourcesCompat.getFloat(context.resources, R.dimen.marker_bitmap_zoomed_scale)
43+
}
44+
val width = (outline!!.intrinsicWidth * scale).toInt()
45+
val height = (outline.intrinsicHeight * scale).toInt()
46+
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
47+
val canvas = Canvas(bitmap)
48+
outline.setBounds(0, 0, width, height)
49+
outline.draw(canvas)
50+
fill!!.setColorFilter(color, PorterDuff.Mode.SRC_ATOP)
51+
fill.setBounds(0, 0, width, height)
52+
fill.draw(canvas)
53+
overlay!!.setBounds(0, 0, width, height)
54+
overlay.draw(canvas)
55+
return bitmap
56+
}
57+
58+
fun getMarkerIcon(@ColorInt color: Int, currentZoomLevel: Float): BitmapDescriptor {
59+
val bitmap = getMarkerBitmap(color, currentZoomLevel)
60+
// TODO: Cache rendered bitmaps.
61+
return BitmapDescriptorFactory.fromBitmap(bitmap)
62+
}
63+
}

gnd/src/test/java/com/google/android/gnd/ui/MarkerIconFactoryTest.java

Lines changed: 0 additions & 81 deletions
This file was deleted.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.android.gnd.ui
17+
18+
import android.content.Context
19+
import dagger.hilt.android.testing.HiltAndroidTest
20+
import org.junit.runner.RunWith
21+
import org.robolectric.RobolectricTestRunner
22+
import com.google.android.gnd.BaseHiltTest
23+
import javax.inject.Inject
24+
import dagger.hilt.android.qualifiers.ApplicationContext
25+
import org.junit.Before
26+
import androidx.appcompat.content.res.AppCompatResources
27+
import com.google.android.gnd.R
28+
import android.graphics.Bitmap
29+
import android.graphics.Color
30+
import androidx.core.content.res.ResourcesCompat
31+
import com.google.android.gnd.ui.home.mapcontainer.MapContainerViewModel
32+
import com.google.common.truth.Truth.assertThat
33+
import org.junit.Test
34+
35+
@HiltAndroidTest
36+
@RunWith(RobolectricTestRunner::class)
37+
class MarkerIconFactoryTest : BaseHiltTest() {
38+
@Inject
39+
@ApplicationContext
40+
lateinit var context: Context
41+
42+
@Inject
43+
lateinit var markerIconFactory: MarkerIconFactory
44+
private var markerUnscaledWidth = 0
45+
private var markerUnscaledHeight = 0
46+
47+
@Before
48+
override fun setUp() {
49+
super.setUp()
50+
val outline = AppCompatResources.getDrawable(context, R.drawable.ic_marker_outline)
51+
markerUnscaledWidth = outline!!.intrinsicWidth
52+
markerUnscaledHeight = outline.intrinsicHeight
53+
}
54+
55+
@Test
56+
fun markerBitmap_zoomedOut_scaleIsSetCorrectly() {
57+
val bitmap = markerIconFactory.getMarkerBitmap(
58+
Color.BLUE,
59+
MapContainerViewModel.ZOOM_LEVEL_THRESHOLD - 0.1f
60+
)
61+
62+
val scale =
63+
ResourcesCompat.getFloat(context.resources, R.dimen.marker_bitmap_default_scale)
64+
verifyBitmapScale(bitmap, scale)
65+
}
66+
67+
@Test
68+
fun markerBitmap_zoomedIn_scaleIsSetCorrectly() {
69+
val bitmap = markerIconFactory.getMarkerBitmap(
70+
Color.BLUE,
71+
MapContainerViewModel.ZOOM_LEVEL_THRESHOLD
72+
)
73+
74+
val scale =
75+
ResourcesCompat.getFloat(context.resources, R.dimen.marker_bitmap_zoomed_scale)
76+
verifyBitmapScale(bitmap, scale)
77+
}
78+
79+
private fun verifyBitmapScale(bitmap: Bitmap, scale: Float) {
80+
val expectedWidth = (markerUnscaledWidth * scale).toInt()
81+
val expectedHeight = (markerUnscaledHeight * scale).toInt()
82+
assertThat(bitmap.width).isEqualTo(expectedWidth)
83+
assertThat(bitmap.height).isEqualTo(expectedHeight)
84+
}
85+
}

0 commit comments

Comments
 (0)