-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
GoogleMap.kt
816 lines (705 loc) · 29.4 KB
/
GoogleMap.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
/*
* SPDX-FileCopyrightText: 2023 microG Project Team
* SPDX-License-Identifier: Apache-2.0
*/
package org.microg.gms.maps.hms
import android.content.Context
import android.graphics.Bitmap
import android.location.Location
import android.os.*
import android.util.DisplayMetrics
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.LinearLayout
import androidx.collection.LongSparseArray
import com.google.android.gms.dynamic.IObjectWrapper
import com.google.android.gms.dynamic.unwrap
import com.google.android.gms.maps.GoogleMap.MAP_TYPE_HYBRID
import com.google.android.gms.maps.GoogleMap.MAP_TYPE_SATELLITE
import com.google.android.gms.maps.GoogleMap.MAP_TYPE_TERRAIN
import com.google.android.gms.maps.GoogleMapOptions
import com.google.android.gms.maps.internal.*
import com.google.android.gms.maps.model.*
import com.google.android.gms.maps.model.internal.*
import com.huawei.hms.maps.CameraUpdate
import com.huawei.hms.maps.HuaweiMap
import com.huawei.hms.maps.MapView
import com.huawei.hms.maps.MapsInitializer
import com.huawei.hms.maps.OnMapReadyCallback
import com.huawei.hms.maps.internal.IOnIndoorStateChangeListener
import com.huawei.hms.maps.internal.IOnInfoWindowCloseListener
import com.huawei.hms.maps.internal.IOnInfoWindowLongClickListener
import com.huawei.hms.maps.internal.IOnPoiClickListener
import com.huawei.hms.maps.model.Marker
import org.microg.gms.maps.hms.model.*
import org.microg.gms.maps.hms.utils.*
import java.util.concurrent.CopyOnWriteArrayList
import java.util.concurrent.atomic.AtomicBoolean
private fun <T : Any> LongSparseArray<T>.values() = (0 until size()).mapNotNull { valueAt(it) }
fun runOnMainLooper(forceQueue: Boolean = false, method: () -> Unit) {
if (!forceQueue && Looper.myLooper() == Looper.getMainLooper()) {
method()
} else {
Handler(Looper.getMainLooper()).post {
method()
}
}
}
class GoogleMapImpl(private val context: Context, var options: GoogleMapOptions) : IGoogleMapDelegate.Stub() {
val view: FrameLayout
var map: HuaweiMap? = null
private set
val dpiFactor: Float
get() = context.resources.displayMetrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT
private var mapView: MapView? = null
private var created = false
private var initialized = false
private var loaded = false
private val mapLock = Object()
private var latLngBounds: LatLngBounds? = null
private val internalOnInitializedCallbackList = CopyOnWriteArrayList<OnMapReadyCallback>()
private val initializedCallbackList = CopyOnWriteArrayList<IOnMapReadyCallback>()
private var loadedCallback: IOnMapLoadedCallback? = null
private var cameraChangeListener: IOnCameraChangeListener? = null
private var cameraMoveListener: IOnCameraMoveListener? = null
private var cameraMoveCanceledListener: IOnCameraMoveCanceledListener? = null
private var cameraMoveStartedListener: IOnCameraMoveStartedListener? = null
private var cameraIdleListener: IOnCameraIdleListener? = null
private var mapClickListener: IOnMapClickListener? = null
private var mapLongClickListener: IOnMapLongClickListener? = null
private val groundOverlays = mutableMapOf<String, GroundOverlayImpl>()
private val polylines = mutableMapOf<String, PolylineImpl>()
private val polygons = mutableMapOf<String, PolygonImpl>()
private val circles = mutableMapOf<String, CircleImpl>()
private val tileOverlays = mutableMapOf<String, TileOverlayImpl>()
private var storedMapType: Int = options.mapType
val waitingCameraUpdates = mutableListOf<CameraUpdate>()
var locationEnabled: Boolean = false
private var markerId = 0L
val markers = mutableMapOf<String, MarkerImpl>()
init {
val mapContext = MapContext(context)
BitmapDescriptorFactoryImpl.initialize(context.resources)
runOnMainLooper {
MapsInitializer.setApiKey(BuildConfig.HMSMAP_KEY)
}
this.view = object : FrameLayout(mapContext) {}
}
override fun getCameraPosition(): CameraPosition? = map?.cameraPosition?.toGms()
override fun getMaxZoomLevel(): Float = toHmsZoom(map?.maxZoomLevel ?: 18.toFloat())
override fun getMinZoomLevel(): Float = toHmsZoom(map?.minZoomLevel ?: 3.toFloat())
override fun moveCamera(cameraUpdate: IObjectWrapper?) {
val update = cameraUpdate.unwrap<CameraUpdate>() ?: return
synchronized(mapLock) {
if (initialized) {
this.map?.moveCamera(update)
} else {
waitingCameraUpdates.add(update)
}
}
}
override fun animateCamera(cameraUpdate: IObjectWrapper?) {
val update = cameraUpdate.unwrap<CameraUpdate>() ?: return
synchronized(mapLock) {
if (initialized) {
this.map?.animateCamera(update)
} else {
waitingCameraUpdates.add(update)
}
}
}
override fun animateCameraWithCallback(cameraUpdate: IObjectWrapper?, callback: ICancelableCallback?) {
val update = cameraUpdate.unwrap<CameraUpdate>() ?: return
synchronized(mapLock) {
if (initialized) {
this.map?.animateCamera(update, callback?.toHms())
} else {
waitingCameraUpdates.add(update)
afterInitialize { callback?.onFinish() }
}
}
}
override fun animateCameraWithDurationAndCallback(
cameraUpdate: IObjectWrapper?,
duration: Int,
callback: ICancelableCallback?
) {
val update = cameraUpdate.unwrap<CameraUpdate>() ?: return
synchronized(mapLock) {
if (initialized) {
this.map?.animateCamera(update, duration, callback?.toHms())
} else {
waitingCameraUpdates.add(update)
afterInitialize { callback?.onFinish() }
}
}
}
override fun stopAnimation() = map?.stopAnimation() ?: Unit
override fun setMapStyle(options: MapStyleOptions?): Boolean {
Log.d(TAG, "unimplemented Method: setMapStyle ${options?.getJson()}")
return true
}
override fun setMinZoomPreference(minZoom: Float) = afterInitialize {
it.setMinZoomPreference(toHmsZoom(minZoom))
}
override fun setMaxZoomPreference(maxZoom: Float) = afterInitialize {
it.setMaxZoomPreference(toHmsZoom(maxZoom))
}
override fun resetMinMaxZoomPreference() = afterInitialize {
it.setMinZoomPreference(3.toFloat())
it.setMaxZoomPreference(18.toFloat())
}
override fun setLatLngBoundsForCameraTarget(bounds: LatLngBounds?) = afterInitialize {
if (latLngBounds == null || bounds == null || latLngBounds!! != bounds) {
latLngBounds = bounds
it.setLatLngBoundsForCameraTarget(bounds?.toHms())
}
}
override fun addPolyline(options: PolylineOptions): IPolylineDelegate? {
val polyline = map?.addPolyline(options.toHms()) ?: return null
val polylineImpl = PolylineImpl(polyline, options)
polylines[polylineImpl.id] = polylineImpl
return polylineImpl
}
override fun addPolygon(options: PolygonOptions): IPolygonDelegate? {
val polygon = map?.addPolygon(options.toHms()) ?: return null
val polygonImpl = PolygonImpl(polygon)
polygons[polygonImpl.id] = polygonImpl
return polygonImpl
}
override fun addMarker(options: MarkerOptions): IMarkerDelegate {
val marker = MarkerImpl(this, "m${markerId++}", options)
if (map != null) {
marker.update()
} else {
markers[marker.id] = marker
}
return marker
}
override fun addGroundOverlay(options: GroundOverlayOptions): IGroundOverlayDelegate? {
Log.d(TAG, "Method: addGroundOverlay")
val groundOverlay = map?.addGroundOverlay(options.toHms()) ?: return null
val groundOverlayImpl = GroundOverlayImpl(groundOverlay)
groundOverlays[groundOverlayImpl.id] = groundOverlayImpl
return groundOverlayImpl
}
override fun addTileOverlay(options: TileOverlayOptions): ITileOverlayDelegate? {
Log.d(TAG, "Method: addTileOverlay")
val tileOverlay = map?.addTileOverlay(options.toHms()) ?: return null
val tileOverlayImpl = TileOverlayImpl(tileOverlay)
tileOverlays[tileOverlayImpl.id] = tileOverlayImpl
return tileOverlayImpl
}
override fun addCircle(options: CircleOptions): ICircleDelegate? {
val circle = map?.addCircle(options.toHms()) ?: return null
val circleImpl = CircleImpl(circle)
circles[circleImpl.id] = circleImpl
return circleImpl
}
override fun clear() {
map?.clear()
}
override fun getMapType(): Int {
return map?.mapType ?: storedMapType
}
override fun setMapType(type: Int) {
storedMapType = type
applyMapType()
}
fun applyMapType() {
// TODO: Serve map styles locally
when (storedMapType) {
MAP_TYPE_SATELLITE -> map?.mapType = HuaweiMap.MAP_TYPE_SATELLITE
MAP_TYPE_TERRAIN -> map?.mapType = HuaweiMap.MAP_TYPE_TERRAIN
MAP_TYPE_HYBRID -> map?.mapType = HuaweiMap.MAP_TYPE_HYBRID
//MAP_TYPE_NONE, MAP_TYPE_NORMAL,
else -> map?.mapType = HuaweiMap.MAP_TYPE_NORMAL
}
// map?.let { BitmapDescriptorFactoryImpl.registerMap(it) }
}
override fun isTrafficEnabled(): Boolean {
return map?.isTrafficEnabled ?: false
}
override fun setTrafficEnabled(traffic: Boolean) = afterInitialize {
Log.d(TAG, "setTrafficEnabled")
it.isTrafficEnabled = traffic
}
override fun isIndoorEnabled(): Boolean {
Log.d(TAG, "isIndoorEnabled")
return map?.isIndoorEnabled ?: false
}
override fun setIndoorEnabled(indoor: Boolean) = afterInitialize {
Log.d(TAG, "setIndoorEnabled")
it.isIndoorEnabled = indoor
}
override fun isMyLocationEnabled(): Boolean {
return map?.isMyLocationEnabled ?: false
}
override fun setMyLocationEnabled(myLocation: Boolean) = afterInitialize {
Log.d(TAG, "setMyLocationEnabled $myLocation")
it.isMyLocationEnabled = myLocation
}
override fun getMyLocation(): Location? {
Log.d(TAG, "deprecated Method: getMyLocation")
return null
}
override fun setLocationSource(locationSource: ILocationSourceDelegate?) = afterInitialize {
Log.d(TAG, "unimplemented Method: setLocationSource")
}
override fun setContentDescription(desc: String?) = afterInitialize {
Log.d(TAG, "setContentDescription desc -> $desc")
it.setContentDescription(desc)
}
override fun getUiSettings(): IUiSettingsDelegate =
map?.uiSettings?.let { UiSettingsImpl(it) } ?: UiSettingsCache().also {
internalOnInitializedCallbackList.add(it.getMapReadyCallback())
}
override fun getProjection(): IProjectionDelegate =
map?.projection?.let { ProjectionImpl(it) } ?: DummyProjection()
override fun setOnCameraChangeListener(listener: IOnCameraChangeListener?) = afterInitialize {
Log.d(TAG, "setOnCameraChangeListener");
cameraChangeListener = listener
it.setOnCameraIdleListener {
try {
cameraChangeListener?.onCameraChange(map?.cameraPosition?.toGms())
} catch (e: Exception) {
Log.w(TAG, e)
}
}
}
override fun setOnCircleClickListener(listener: IOnCircleClickListener?) = afterInitialize { hmap ->
Log.d(TAG, "setOnCircleClickListener")
hmap.setOnCircleClickListener { listener?.onCircleClick(circles[it.id]) }
}
override fun setOnGroundOverlayClickListener(listener: IOnGroundOverlayClickListener?) =
afterInitialize { hmap ->
Log.d(TAG, "Method: setOnGroundOverlayClickListener")
hmap.setOnGroundOverlayClickListener { listener?.onGroundOverlayClick(groundOverlays[it.id]) }
}
override fun setOnInfoWindowLongClickListener(listener: com.google.android.gms.maps.internal.IOnInfoWindowLongClickListener?) =
afterInitialize {
Log.d(TAG, "Not yet implemented setInfoWindowLongClickListener")
}
fun setOnIndoorStateChangeListener(listener: IOnIndoorStateChangeListener?) {
Log.d(TAG, "unimplemented Method: setOnIndoorStateChangeListener")
}
override fun setOnMapClickListener(listener: IOnMapClickListener?) = afterInitialize {
mapClickListener = listener
it.setOnMapClickListener { latlng ->
try {
mapClickListener?.onMapClick(latlng.toGms())
} catch (e: Exception) {
Log.w(TAG, e)
}
}
}
override fun setOnMapLongClickListener(listener: IOnMapLongClickListener?) = afterInitialize {
mapLongClickListener = listener
it.setOnMapLongClickListener { latlng ->
try {
mapLongClickListener?.onMapLongClick(latlng.toGms())
} catch (e: Exception) {
Log.w(TAG, e)
}
}
}
override fun setOnMarkerClickListener(listener: IOnMarkerClickListener?) = afterInitialize { hmap ->
hmap.setOnMarkerClickListener {
Log.d("GmsGoogleMap", "setOnMarkerClickListener marker id -> ${it.id}")
listener?.onMarkerClick(markers[it.id]) ?: false
}
}
override fun setOnMarkerDragListener(listener: IOnMarkerDragListener?) = afterInitialize {
it.setOnMarkerDragListener(object : HuaweiMap.OnMarkerDragListener {
override fun onMarkerDragStart(p0: Marker?) {
listener?.onMarkerDragStart(markers[p0?.id])
}
override fun onMarkerDrag(p0: Marker?) {
listener?.onMarkerDrag(markers[p0?.id])
}
override fun onMarkerDragEnd(p0: Marker?) {
listener?.onMarkerDragEnd(markers[p0?.id])
}
})
}
override fun setOnInfoWindowClickListener(listener: IOnInfoWindowClickListener?) = afterInitialize { hmap ->
Log.d(TAG, "setOnInfoWindowClickListener")
hmap.setOnInfoWindowClickListener { listener?.onInfoWindowClick(markers[it.id]) }
}
fun setOnInfoWindowCloseListener(listener: IOnInfoWindowCloseListener?) {
Log.d(TAG, "unimplemented Method: setOnInfoWindowCloseListener")
}
fun setOnInfoWindowLongClickListener(listener: IOnInfoWindowLongClickListener?) {
Log.d(TAG, "unimplemented Method: setOnInfoWindowLongClickListener")
}
override fun setInfoWindowAdapter(adapter: IInfoWindowAdapter?) = afterInitialize {
Log.d(TAG, "setInfoWindowAdapter")
it.setInfoWindowAdapter(object : HuaweiMap.InfoWindowAdapter {
override fun getInfoContents(p0: Marker?): View? {
return adapter?.getInfoContents(markers[p0?.id]).unwrap<View>()
}
override fun getInfoWindow(p0: Marker?): View? {
return adapter?.getInfoWindow(markers[p0?.id]).unwrap<View>()
}
})
}
override fun setOnMyLocationChangeListener(listener: IOnMyLocationChangeListener?) = afterInitialize {
Log.d(TAG, "deprecated Method: setOnMyLocationChangeListener")
}
override fun setOnMyLocationButtonClickListener(listener: IOnMyLocationButtonClickListener?) = afterInitialize {
Log.d(TAG, "setOnMyLocationButtonClickListener")
it.setOnMyLocationButtonClickListener { listener?.onMyLocationButtonClick() ?: false }
}
override fun setOnMyLocationClickListener(listener: IOnMyLocationClickListener?) = afterInitialize { hmap ->
Log.d(TAG, "setOnMyLocationClickListener")
hmap.setOnMyLocationClickListener { listener?.onMyLocationClick(it) }
}
fun setOnPoiClickListener(listener: IOnPoiClickListener?) {
Log.d(TAG, "unimplemented Method: setOnPoiClickListener")
}
override fun setOnPolygonClickListener(listener: IOnPolygonClickListener?) = afterInitialize { hmap ->
Log.d(TAG, "setOnPolygonClickListener")
hmap.setOnPolygonClickListener { listener?.onPolygonClick(polygons[it.id]) }
}
override fun setOnInfoWindowCloseListener(listener: com.google.android.gms.maps.internal.IOnInfoWindowCloseListener?) =
afterInitialize {
Log.d(TAG, "Not yet implemented setInfoWindowCloseListener")
}
override fun setOnPolylineClickListener(listener: IOnPolylineClickListener?) = afterInitialize { hmap ->
Log.d(TAG, "unimplemented Method: setOnPolylineClickListener")
hmap.setOnPolylineClickListener { listener?.onPolylineClick(polylines[it.id]) }
}
override fun snapshot(callback: ISnapshotReadyCallback, bitmap: IObjectWrapper?) = afterInitialize {
Log.d(TAG, "snapshot")
val hmsBitmap = bitmap.unwrap<Bitmap>() ?: return@afterInitialize
val hmsCallback = HuaweiMap.SnapshotReadyCallback { p0 -> callback.onBitmapReady(p0) }
it.snapshot(hmsCallback, hmsBitmap)
}
override fun snapshotForTest(callback: ISnapshotReadyCallback) = afterInitialize {
Log.d(TAG, "snapshotForTest")
val hmsCallback = HuaweiMap.SnapshotReadyCallback { p0 -> callback.onBitmapReady(p0) }
it.snapshot(hmsCallback)
}
override fun setPadding(left: Int, top: Int, right: Int, bottom: Int) = afterInitialize {
Log.d(TAG, "setPadding: $left $top $right $bottom")
it.setPadding(left, top, right, bottom)
}
override fun isBuildingsEnabled(): Boolean {
Log.d(TAG, "isBuildingsEnabled")
return map?.isBuildingsEnabled ?: true
}
override fun setBuildingsEnabled(buildings: Boolean) = afterInitialize {
Log.d(TAG, "setBuildingsEnabled: $buildings")
it.isBuildingsEnabled = buildings
}
override fun setOnMapLoadedCallback(callback: IOnMapLoadedCallback?) {
if (callback != null) {
synchronized(mapLock) {
if (loaded) {
Log.d(TAG, "Invoking callback instantly, as map is loaded")
try {
callback.onMapLoaded()
} catch (e: Exception) {
Log.w(TAG, e)
}
} else {
Log.d(TAG, "Delay callback invocation, as map is not yet loaded")
loadedCallback = callback
}
}
} else {
loadedCallback = null
}
}
override fun setCameraMoveStartedListener(listener: IOnCameraMoveStartedListener?) = afterInitialize { hmap ->
Log.d(TAG, "setCameraMoveStartedListener")
cameraMoveStartedListener = listener
hmap.setOnCameraMoveStartedListener {
try {
cameraMoveStartedListener?.onCameraMoveStarted(it)
} catch (e: Exception) {
Log.w(TAG, e)
}
}
}
override fun setCameraMoveListener(listener: IOnCameraMoveListener?) = afterInitialize {
Log.d(TAG, "setCameraMoveListener")
cameraMoveListener = listener
it.setOnCameraMoveListener {
try {
cameraMoveListener?.onCameraMove()
} catch (e: Exception) {
Log.w(TAG, e)
}
}
}
override fun setCameraMoveCanceledListener(listener: IOnCameraMoveCanceledListener?) = afterInitialize {
Log.d(TAG, "setCameraMoveCanceledListener")
cameraMoveCanceledListener = listener
it.setOnCameraMoveCanceledListener {
try {
cameraMoveCanceledListener?.onCameraMoveCanceled()
} catch (e: Exception) {
Log.w(TAG, e)
}
}
}
override fun setCameraIdleListener(listener: IOnCameraIdleListener?) = afterInitialize {
Log.d(TAG, "onCameraIdle: successful")
cameraIdleListener = listener
it.setOnCameraIdleListener {
try {
cameraIdleListener?.onCameraIdle()
} catch (e: Exception) {
Log.w(TAG, e)
}
}
}
override fun getTestingHelper(): IObjectWrapper? {
Log.d(TAG, "unimplemented Method: getTestingHelper")
return null
}
override fun setWatermarkEnabled(watermark: Boolean) {
Log.d(TAG, "unimplemented Method: setWatermarkEnabled")
}
override fun useViewLifecycleWhenInFragment(): Boolean {
Log.d(TAG, "unimplemented Method: useViewLifecycleWhenInFragment")
return false
}
override fun onCreate(savedInstanceState: Bundle?) {
if (!created) {
Log.d(TAG_LOGO, "create: ${context.packageName},\n$options")
val mapContext = MapContext(context)
MapsInitializer.initialize(mapContext)
val mapView = MapView(mapContext, options.toHms())
this.mapView = mapView
view.addView(mapView)
mapView.onCreate(savedInstanceState?.toHms())
mapView.getMapAsync(this::initMap)
created = true
runOnMainLooper(forceQueue = true) { tryRunUserInitializedCallbacks("onCreate") }
}
}
private fun fakeWatermark(method: () -> Unit) {
Log.d(TAG_LOGO, "start -> $view")
val view1 = view.getChildAt(0) as? ViewGroup
val view2 = view1?.getChildAt(0) as? ViewGroup
val view4 = view2?.getChildAt(1)
Log.d(TAG_LOGO, view4?.toString() ?: "view4 is null")
if (view4 is LinearLayout) {
view4.visibility = View.GONE
method()
} else {
Log.d(TAG_LOGO, "LinearLayout not found")
}
}
private fun getAllChildViews(view: View, index: Int): List<View>? {
Log.d(TAG_LOGO, "getAllChildViews: $index, $view")
if (view is LinearLayout) {
Log.d(TAG_LOGO, "legal: $index")
view.visibility = View.GONE
}
val allChildren: MutableList<View> = ArrayList()
if (view is ViewGroup) {
val vp = view
for (i in 0 until vp.childCount) {
val viewChild = vp.getChildAt(i)
Log.d(TAG_LOGO, "child:$index, $i, $viewChild")
allChildren.add(viewChild)
allChildren.addAll(getAllChildViews(viewChild, index + 1)!!)
}
}
return allChildren
}
private fun initMap(map: HuaweiMap) {
if (this.map != null) return
loaded = true
this.map = map
map.setOnCameraIdleListener {
try {
cameraChangeListener?.onCameraChange(map.cameraPosition.toGms())
} catch (e: Exception) {
Log.w(TAG, e)
}
}
map.setOnCameraIdleListener {
try {
cameraIdleListener?.onCameraIdle()
} catch (e: Exception) {
Log.w(TAG, e)
}
}
map.setOnCameraMoveListener {
try {
cameraMoveListener?.onCameraMove()
} catch (e: Exception) {
Log.w(TAG, e)
}
}
map.setOnCameraMoveStartedListener {
try {
cameraMoveStartedListener?.onCameraMoveStarted(it)
} catch (e: Exception) {
Log.w(TAG, e)
}
}
map.setOnCameraMoveCanceledListener {
try {
cameraMoveCanceledListener?.onCameraMoveCanceled()
} catch (e: Exception) {
Log.w(TAG, e)
}
}
map.setOnMapClickListener { latlng ->
try {
if (options.liteMode) {
val parentView = view.parent?.parent
// TODO hms not support disable click listener when liteMode, this just fix for teams
if (parentView != null && parentView::class.qualifiedName.equals("com.microsoft.teams.location.ui.map.MapViewLite")) {
val clickView = parentView as ViewGroup
clickView.performClick()
return@setOnMapClickListener
}
}
mapClickListener?.onMapClick(latlng.toGms())
} catch (e: Exception) {
Log.w(TAG, e)
}
}
map.setOnMapLongClickListener { latlng ->
try {
if (options.liteMode) {
val parentView = view.parent?.parent
// TODO hms not support disable click listener when liteMode, this just fix for teams
if (parentView != null && parentView::class.qualifiedName.equals("com.microsoft.teams.location.ui.map.MapViewLite")) {
val clickView = parentView as ViewGroup
clickView.performLongClick()
return@setOnMapLongClickListener
}
}
mapLongClickListener?.onMapLongClick(latlng.toGms())
} catch (e: Exception) {
Log.w(TAG, e)
}
}
synchronized(mapLock) {
initialized = true
markers.filter { it.key.startsWith("m") }.forEach { it.value.update() }
waitingCameraUpdates.forEach { map.moveCamera(it) }
val initializedCallbacks = ArrayList(internalOnInitializedCallbackList)
Log.d(TAG, "Invoking ${initializedCallbacks.size} internal callbacks now that the true map is initialized")
for (callback in initializedCallbacks) {
callback.onMapReady(map)
}
internalOnInitializedCallbackList.clear()
fakeWatermark { Log.d(TAG_LOGO, "fakeWatermark success") }
}
tryRunUserInitializedCallbacks(tag = "initMap")
}
override fun onResume() = mapView?.onResume() ?: Unit
override fun onPause() = mapView?.onPause() ?: Unit
override fun onDestroy() {
Log.d(TAG, "onDestroy")
initializedCallbackList.clear()
internalOnInitializedCallbackList.clear()
circles.map { it.value.remove() }
circles.clear()
polylines.map { it.value.remove() }
polylines.clear()
polygons.map { it.value.remove() }
polygons.clear()
markers.map { it.value.remove() }
markers.clear()
// BitmapDescriptorFactoryImpl.unregisterMap(map)
view.removeView(mapView)
// TODO can crash?
mapView?.onDestroy()
mapView = null
// Don't make it null; this object is not deleted immediately, and it may want to access map.* stuff
//map = null
created = false
initialized = false
loaded = false
}
override fun onStart() {
mapView?.onStart()
}
override fun onStop() {
mapView?.onStop()
}
override fun onEnterAmbient(bundle: Bundle?) {
Log.d(TAG, "unimplemented Method: onEnterAmbient")
}
override fun onExitAmbient() {
Log.d(TAG, "unimplemented Method: onExitAmbient")
}
override fun onLowMemory() = mapView?.onLowMemory() ?: Unit
override fun onSaveInstanceState(outState: Bundle) {
val newBundle = Bundle()
mapView?.onSaveInstanceState(newBundle)
outState.putAll(newBundle.toGms())
}
fun getMapAsync(callback: IOnMapReadyCallback) {
synchronized(mapLock) {
initializedCallbackList.add(callback)
}
tryRunUserInitializedCallbacks("getMapAsync")
}
private fun afterInitialize(runnable: (HuaweiMap) -> Unit) {
synchronized(mapLock) {
if (initialized) {
runnable(map!!)
} else {
internalOnInitializedCallbackList.add(OnMapReadyCallback {
runnable(it)
})
}
}
}
private var isInvokingInitializedCallbacks = AtomicBoolean(false)
private fun tryRunUserInitializedCallbacks(tag: String = "") {
synchronized(mapLock) {
if (initializedCallbackList.isEmpty()) return
}
val runCallbacks = {
synchronized(mapLock) {
initializedCallbackList.forEach {
try {
it.onMapReady(this)
} catch (e: Exception) {
Log.w(TAG, e)
}
}.also {
initializedCallbackList.clear()
}
}
}
if (initialized && map != null) {
Log.d("$TAG:$tag", "Invoking callback now, as map is initialized")
val wasCallbackActive = isInvokingInitializedCallbacks.getAndSet(true)
runOnMainLooper(forceQueue = wasCallbackActive) {
runCallbacks()
}
if (!wasCallbackActive) isInvokingInitializedCallbacks.set(false)
} else {
Log.d(
"$TAG:$tag",
"Initialized callbacks could not be run at this point, as the map view has not been created yet."
)
}
}
override fun onTransact(code: Int, data: Parcel, reply: Parcel?, flags: Int): Boolean =
if (super.onTransact(code, data, reply, flags)) {
Log.d(TAG, "onTransact: $code, $data, $flags")
true
} else {
Log.w(TAG, "onTransact [unknown]: $code, $data, $flags"); false
}
companion object {
private const val TAG = "GmsGoogleMap"
private const val TAG_LOGO = "fakeWatermark"
private const val MAX_TIMES = 300
}
}