@@ -43,7 +43,6 @@ import org.radarbase.android.util.register
4343import org.radarbase.passive.google.places.GooglePlacesService.Companion.GOOGLE_PLACES_API_KEY_DEFAULT
4444import org.radarcns.kafka.ObservationKey
4545import org.radarcns.passive.google.GooglePlacesInfo
46- import org.radarcns.passive.google.PlacesType
4746import org.slf4j.LoggerFactory
4847import java.util.concurrent.TimeUnit
4948import java.util.concurrent.atomic.AtomicBoolean
@@ -60,7 +59,9 @@ class GooglePlacesManager(service: GooglePlacesService, @get: Synchronized priva
6059 private val isPermissionGranted: Boolean
6160 get() = checkLocationPermissions()
6261 private val placesClientCreated: Boolean
63- get() = state.placesClientCreated.get()
62+ get() = service.placesClientCreated.get()
63+ private val placesClient: PlacesClient ?
64+ get() = service.placesClient
6465 private val fromBroadcastRegistration: Boolean
6566 get()= state.fromBroadcast.get()
6667 var shouldFetchPlaceId: Boolean
@@ -83,8 +84,6 @@ class GooglePlacesManager(service: GooglePlacesService, @get: Synchronized priva
8384 private val detailsPlaceFields: List <Place .Field > = listOf (Place .Field .ADDRESS_COMPONENTS )
8485
8586 private var placesBroadcastReceiver: BroadcastRegistration ? = null
86- private var placesClient: PlacesClient ? = null
87- @Synchronized get() = if (placesClientCreated) field else null
8887 private var isRecentlySent: AtomicBoolean = AtomicBoolean (false )
8988
9089 private lateinit var currentPlaceRequest: FindCurrentPlaceRequest
@@ -108,8 +107,14 @@ class GooglePlacesManager(service: GooglePlacesService, @get: Synchronized priva
108107 register()
109108 status = SourceStatusListener .Status .READY
110109 placesProcessor.start()
111- placeHandler.execute { if (! placesClientCreated) createPlacesClient() }
112- service.broadcaster.register(DEVICE_LOCATION_CHANGED ) { _, _ ->
110+ placeHandler.execute { if (! placesClientCreated) {
111+ service.createPlacesClient()
112+ }
113+ if (! placesClientCreated && ! Places .isInitialized()) {
114+ updateApiKey(apiKey)
115+ } }
116+ updateConnected()
117+ placesBroadcastReceiver = service.broadcaster?.register(DEVICE_LOCATION_CHANGED ) { _, _ ->
113118 if (placesClientCreated) {
114119 placeHandler.execute {
115120 state.fromBroadcast.set(true )
@@ -120,17 +125,6 @@ class GooglePlacesManager(service: GooglePlacesService, @get: Synchronized priva
120125 }
121126 }
122127
123- @Synchronized
124- private fun createPlacesClient () {
125- try {
126- placesClient = Places .createClient(service)
127- state.placesClientCreated.set(true )
128- status = SourceStatusListener .Status .CONNECTED
129- } catch (ex: IllegalStateException ) {
130- logger.error(" Places client has not been initialized yet." )
131- }
132- }
133-
134128 fun updateApiKey (apiKey : String ) {
135129 when (apiKey) {
136130 GOOGLE_PLACES_API_KEY_DEFAULT -> {
@@ -144,15 +138,25 @@ class GooglePlacesManager(service: GooglePlacesService, @get: Synchronized priva
144138 }
145139 if (! Places .isInitialized()) {
146140 initializePlacesClient()
147- if (! placesClientCreated) createPlacesClient()
141+ if (! placesClientCreated) {
142+ service.createPlacesClient()
143+ updateConnected()
144+ }
148145 }
149146 }
150147 }
151148 }
152149
150+ private fun updateConnected () {
151+ if (Places .isInitialized()) {
152+ status = SourceStatusListener .Status .CONNECTED
153+ }
154+ }
155+
153156 private fun initializePlacesClient () {
154157 try {
155158 Places .initialize(service.applicationContext, apiKey)
159+ updateConnected()
156160 } catch (ex: Exception ) {
157161 logger.error(" Exception while initializing places API" , ex)
158162 service.internalError.set(true )
@@ -206,15 +210,17 @@ class GooglePlacesManager(service: GooglePlacesService, @get: Synchronized priva
206210
207211 private fun doSend (places : List <PlaceLikelihood >) {
208212 places.forEach { likelihood ->
209- val types = likelihood.place.types?.map { it.toPlacesType() }
213+ val types = likelihood.place.placeTypes
210214 val placeId: String? = if (shouldFetchPlaceId) likelihood.place.id else null
211215 val placesInfoBuilder = GooglePlacesInfo .newBuilder().apply {
212216 time = currentTime
213217 timeReceived = currentTime
214- type1 = types?.getOrNull(0 )
215- type2 = types?.getOrNull(1 )
216- type3 = types?.getOrNull(2 )
217- type4 = types?.getOrNull(3 )
218+ if (types != null ) {
219+ placeType1 = types.getOrNull(0 )
220+ placeType2 = types.getOrNull(1 )
221+ placeType3 = types.getOrNull(2 )
222+ placeType4 = types.getOrNull(3 )
223+ }
218224 this .likelihood = likelihood.likelihood
219225 fromBroadcast = fromBroadcastRegistration
220226 city = null
@@ -322,6 +328,8 @@ class GooglePlacesManager(service: GooglePlacesService, @get: Synchronized priva
322328 logger.warn(" Places receiver already unregistered in broadcast" )
323329 }
324330 placesBroadcastReceiver = null
331+ // Not using Places.deinitialize for now as it may prevent PlacesClient from being created during a plugin reload. Additionally, multiple calls to Places.initialize do not impact memory or CPU.
332+ // Places.deinitialize()
325333 placesProcessor.close()
326334 }
327335 }
@@ -338,148 +346,5 @@ class GooglePlacesManager(service: GooglePlacesService, @get: Synchronized priva
338346 private const val COUNTRY_KEY = " country"
339347 const val NUMBER_OF_ATTEMPTS_KEY = " number_of_attempts_key"
340348 const val DEVICE_LOCATION_CHANGED = " org.radarbase.passive.google.places.GooglePlacesManager.DEVICE_LOCATION_CHANGED"
341-
342- private fun Place.Type.toPlacesType (): PlacesType ? = when (this ) {
343- Place .Type .ACCOUNTING -> PlacesType .ACCOUNTING
344- Place .Type .ADMINISTRATIVE_AREA_LEVEL_1 -> PlacesType .ADMINISTRATIVE_AREA_LEVEL_1
345- Place .Type .ADMINISTRATIVE_AREA_LEVEL_2 -> PlacesType .ADMINISTRATIVE_AREA_LEVEL_2
346- Place .Type .ADMINISTRATIVE_AREA_LEVEL_3 -> PlacesType .ADMINISTRATIVE_AREA_LEVEL_3
347- Place .Type .ADMINISTRATIVE_AREA_LEVEL_4 -> PlacesType .ADMINISTRATIVE_AREA_LEVEL_4
348- Place .Type .ADMINISTRATIVE_AREA_LEVEL_5 -> PlacesType .ADMINISTRATIVE_AREA_LEVEL_5
349- Place .Type .AIRPORT -> PlacesType .AIRPORT
350- Place .Type .AMUSEMENT_PARK -> PlacesType .AMUSEMENT_PARK
351- Place .Type .AQUARIUM -> PlacesType .AQUARIUM
352- Place .Type .ARCHIPELAGO -> PlacesType .ARCHIPELAGO
353- Place .Type .ART_GALLERY -> PlacesType .ART_GALLERY
354- Place .Type .ATM -> PlacesType .ATM
355- Place .Type .BAKERY -> PlacesType .BAKERY
356- Place .Type .BANK -> PlacesType .BANK
357- Place .Type .BAR -> PlacesType .BAR
358- Place .Type .BEAUTY_SALON -> PlacesType .BEAUTY_SALON
359- Place .Type .BICYCLE_STORE -> PlacesType .BICYCLE_STORE
360- Place .Type .BOOK_STORE -> PlacesType .BOOK_STORE
361- Place .Type .BOWLING_ALLEY -> PlacesType .BOWLING_ALLEY
362- Place .Type .BUS_STATION -> PlacesType .BUS_STATION
363- Place .Type .CAFE -> PlacesType .CAFE
364- Place .Type .CAMPGROUND -> PlacesType .CAMPGROUND
365- Place .Type .CAR_DEALER -> PlacesType .CAR_DEALER
366- Place .Type .CAR_RENTAL -> PlacesType .CAR_RENTAL
367- Place .Type .CAR_REPAIR -> PlacesType .CAR_REPAIR
368- Place .Type .CAR_WASH -> PlacesType .CAR_WASH
369- Place .Type .CASINO -> PlacesType .CASINO
370- Place .Type .CEMETERY -> PlacesType .CEMETERY
371- Place .Type .CHURCH -> PlacesType .CHURCH
372- Place .Type .CITY_HALL -> PlacesType .CITY_HALL
373- Place .Type .CLOTHING_STORE -> PlacesType .CLOTHING_STORE
374- Place .Type .COLLOQUIAL_AREA -> PlacesType .COLLOQUIAL_AREA
375- Place .Type .CONTINENT -> PlacesType .CONTINENT
376- Place .Type .CONVENIENCE_STORE -> PlacesType .CONVENIENCE_STORE
377- Place .Type .COUNTRY -> PlacesType .COUNTRY
378- Place .Type .COURTHOUSE -> PlacesType .COURTHOUSE
379- Place .Type .DENTIST -> PlacesType .DENTIST
380- Place .Type .DEPARTMENT_STORE -> PlacesType .DEPARTMENT_STORE
381- Place .Type .DOCTOR -> PlacesType .DOCTOR
382- Place .Type .DRUGSTORE -> PlacesType .DRUGSTORE
383- Place .Type .ELECTRICIAN -> PlacesType .ELECTRICIAN
384- Place .Type .ELECTRONICS_STORE -> PlacesType .ELECTRONICS_STORE
385- Place .Type .EMBASSY -> PlacesType .EMBASSY
386- Place .Type .ESTABLISHMENT -> PlacesType .ESTABLISHMENT
387- Place .Type .FINANCE -> PlacesType .FINANCE
388- Place .Type .FIRE_STATION -> PlacesType .FIRE_STATION
389- Place .Type .FLOOR -> PlacesType .FLOOR
390- Place .Type .FLORIST -> PlacesType .FLORIST
391- Place .Type .FOOD -> PlacesType .FOOD
392- Place .Type .FUNERAL_HOME -> PlacesType .FUNERAL_HOME
393- Place .Type .FURNITURE_STORE -> PlacesType .FURNITURE_STORE
394- Place .Type .GAS_STATION -> PlacesType .GAS_STATION
395- Place .Type .GENERAL_CONTRACTOR -> PlacesType .GENERAL_CONTRACTOR
396- Place .Type .GEOCODE -> PlacesType .GEOCODE
397- Place .Type .GROCERY_OR_SUPERMARKET -> PlacesType .GROCERY_OR_SUPERMARKET
398- Place .Type .GYM -> PlacesType .GYM
399- Place .Type .HAIR_CARE -> PlacesType .HAIR_CARE
400- Place .Type .HARDWARE_STORE -> PlacesType .HARDWARE_STORE
401- Place .Type .HEALTH -> PlacesType .HEALTH
402- Place .Type .HINDU_TEMPLE -> PlacesType .HINDU_TEMPLE
403- Place .Type .HOME_GOODS_STORE -> PlacesType .HOME_GOODS_STORE
404- Place .Type .HOSPITAL -> PlacesType .HOSPITAL
405- Place .Type .INSURANCE_AGENCY -> PlacesType .INSURANCE_AGENCY
406- Place .Type .INTERSECTION -> PlacesType .INTERSECTION
407- Place .Type .JEWELRY_STORE -> PlacesType .JEWELRY_STORE
408- Place .Type .LAUNDRY -> PlacesType .LAUNDRY
409- Place .Type .LAWYER -> PlacesType .LAWYER
410- Place .Type .LIBRARY -> PlacesType .LIBRARY
411- Place .Type .LIGHT_RAIL_STATION -> PlacesType .LIGHT_RAIL_STATION
412- Place .Type .LIQUOR_STORE -> PlacesType .LIQUOR_STORE
413- Place .Type .LOCAL_GOVERNMENT_OFFICE -> PlacesType .LOCAL_GOVERNMENT_OFFICE
414- Place .Type .LOCALITY -> PlacesType .LOCALITY
415- Place .Type .LOCKSMITH -> PlacesType .LOCKSMITH
416- Place .Type .LODGING -> PlacesType .LODGING
417- Place .Type .MEAL_DELIVERY -> PlacesType .MEAL_DELIVERY
418- Place .Type .MEAL_TAKEAWAY -> PlacesType .MEAL_TAKEAWAY
419- Place .Type .MOSQUE -> PlacesType .MOSQUE
420- Place .Type .MOVIE_RENTAL -> PlacesType .MOVIE_RENTAL
421- Place .Type .MOVIE_THEATER -> PlacesType .MOVIE_THEATER
422- Place .Type .MOVING_COMPANY -> PlacesType .MOVING_COMPANY
423- Place .Type .MUSEUM -> PlacesType .MUSEUM
424- Place .Type .NATURAL_FEATURE -> PlacesType .NATURAL_FEATURE
425- Place .Type .NEIGHBORHOOD -> PlacesType .NEIGHBORHOOD
426- Place .Type .NIGHT_CLUB -> PlacesType .NIGHT_CLUB
427- Place .Type .PAINTER -> PlacesType .PAINTER
428- Place .Type .PARK -> PlacesType .PARK
429- Place .Type .PARKING -> PlacesType .PARKING
430- Place .Type .PET_STORE -> PlacesType .PET_STORE
431- Place .Type .PHARMACY -> PlacesType .PHARMACY
432- Place .Type .PHYSIOTHERAPIST -> PlacesType .PHYSIOTHERAPIST
433- Place .Type .PLACE_OF_WORSHIP -> PlacesType .PLACE_OF_WORSHIP
434- Place .Type .PLUMBER -> PlacesType .PLUMBER
435- Place .Type .PLUS_CODE -> PlacesType .PLUS_CODE
436- Place .Type .POINT_OF_INTEREST -> PlacesType .POINT_OF_INTEREST
437- Place .Type .POLICE -> PlacesType .POLICE
438- Place .Type .POLITICAL -> PlacesType .POLITICAL
439- Place .Type .POST_BOX -> PlacesType .POST_BOX
440- Place .Type .POST_OFFICE -> PlacesType .POST_OFFICE
441- Place .Type .POSTAL_CODE_PREFIX -> PlacesType .POSTAL_CODE_PREFIX
442- Place .Type .POSTAL_CODE_SUFFIX -> PlacesType .POSTAL_CODE_SUFFIX
443- Place .Type .POSTAL_CODE -> PlacesType .POSTAL_CODE
444- Place .Type .POSTAL_TOWN -> PlacesType .POSTAL_TOWN
445- Place .Type .PREMISE -> PlacesType .PREMISE
446- Place .Type .PRIMARY_SCHOOL -> PlacesType .PRIMARY_SCHOOL
447- Place .Type .REAL_ESTATE_AGENCY -> PlacesType .REAL_ESTATE_AGENCY
448- Place .Type .RESTAURANT -> PlacesType .RESTAURANT
449- Place .Type .ROOFING_CONTRACTOR -> PlacesType .ROOFING_CONTRACTOR
450- Place .Type .ROOM -> PlacesType .ROOM
451- Place .Type .ROUTE -> PlacesType .ROUTE
452- Place .Type .RV_PARK -> PlacesType .RV_PARK
453- Place .Type .SCHOOL -> PlacesType .SCHOOL
454- Place .Type .SECONDARY_SCHOOL -> PlacesType .SECONDARY_SCHOOL
455- Place .Type .SHOE_STORE -> PlacesType .SHOE_STORE
456- Place .Type .SHOPPING_MALL -> PlacesType .SHOPPING_MALL
457- Place .Type .SPA -> PlacesType .SPA
458- Place .Type .STADIUM -> PlacesType .STADIUM
459- Place .Type .STORAGE -> PlacesType .STORAGE
460- Place .Type .STORE -> PlacesType .STORE
461- Place .Type .STREET_ADDRESS -> PlacesType .STREET_ADDRESS
462- Place .Type .STREET_NUMBER -> PlacesType .STREET_NUMBER
463- Place .Type .SUBLOCALITY_LEVEL_1 -> PlacesType .SUBLOCALITY_LEVEL_1
464- Place .Type .SUBLOCALITY_LEVEL_2 -> PlacesType .SUBLOCALITY_LEVEL_2
465- Place .Type .SUBLOCALITY_LEVEL_3 -> PlacesType .SUBLOCALITY_LEVEL_3
466- Place .Type .SUBLOCALITY_LEVEL_4 -> PlacesType .SUBLOCALITY_LEVEL_4
467- Place .Type .SUBLOCALITY_LEVEL_5 -> PlacesType .SUBLOCALITY_LEVEL_5
468- Place .Type .SUBLOCALITY -> PlacesType .SUBLOCALITY
469- Place .Type .SUBPREMISE -> PlacesType .SUBPREMISE
470- Place .Type .SUBWAY_STATION -> PlacesType .SUBWAY_STATION
471- Place .Type .SUPERMARKET -> PlacesType .SUPERMARKET
472- Place .Type .SYNAGOGUE -> PlacesType .SYNAGOGUE
473- Place .Type .TAXI_STAND -> PlacesType .TAXI_STAND
474- Place .Type .TOURIST_ATTRACTION -> PlacesType .TOURIST_ATTRACTION
475- Place .Type .TOWN_SQUARE -> PlacesType .TOWN_SQUARE
476- Place .Type .TRAIN_STATION -> PlacesType .TRAIN_STATION
477- Place .Type .TRANSIT_STATION -> PlacesType .TRANSIT_STATION
478- Place .Type .TRAVEL_AGENCY -> PlacesType .TRAVEL_AGENCY
479- Place .Type .UNIVERSITY -> PlacesType .UNIVERSITY
480- Place .Type .VETERINARY_CARE -> PlacesType .VETERINARY_CARE
481- Place .Type .ZOO -> PlacesType .ZOO
482- else -> null
483- }
484349 }
485350}
0 commit comments