e-mission / e-mission-data-collection

Repository for our own data collection
BSD 3-Clause "New" or "Revised" License
4 stars 19 forks source link

initializing the google API client in onStartCommand() throws IllegalStateException #132

Closed shankari closed 3 years ago

shankari commented 8 years ago
09-29 13:08:59.273  7699  7699 E AndroidRuntime: java.lang.IllegalStateException: Cannot 
invoke getConnectionResult unless GoogleApiClient is connected
09-29 13:08:59.273  7699  7699 E AndroidRuntime:        at com.google.android.gms.interna
l.zzmg.getConnectionResult(Unknown Source)
09-29 13:08:59.273  7699  7699 E AndroidRuntime:        at edu.berkeley.eecs.emission.cordova.tracker.location.TripDiaryStateMachineService.onConnected(TripDiaryStateMachineService.java:130)
09-29 13:08:59.273  7699  7699 E AndroidRuntime:        at com.google.android.gms.common.internal.zzk.zzk(Unknown Source)
09-29 13:08:59.273  7699  7699 E AndroidRuntime:        at com.google.android.gms.internal.zzmg.zzi(Unknown Source)
09-29 13:08:59.273  7699  7699 E AndroidRuntime:        at com.google.android.gms.internal.zzme.zzpi(Unknown Source)
09-29 13:08:59.273  7699  7699 E AndroidRuntime:        at com.google.android.gms.internal.zzme.onConnected(Unknown Source)
09-29 13:08:59.273  7699  7699 E AndroidRuntime:        at com.google.android.gms.internal.zzmi.onConnected(Unknown Source)
09-29 13:08:59.273  7699  7699 E AndroidRuntime:        at com.google.android.gms.internal.zzlz.onConnected(Unknown Source)
09-29 13:08:59.273  7699  7699 E AndroidRuntime:        at com.google.android.gms.common.internal.zzj$zzg.zzqv(Unknown Source)
09-29 13:08:59.273  7699  7699 E AndroidRuntime:        at com.google.android.gms.common.internal.zzj$zza.zzc(Unknown Source)
09-29 13:08:59.273  7699  7699 E AndroidRuntime:        at com.google.android.gms.common.internal.zzj$zza.zzv(Unknown Source)
09-29 13:08:59.273  7699  7699 E AndroidRuntime:        at com.google.android.gms.common.internal.zzj$zzc.zzqx(Unknown Source)
09-29 13:08:59.273  7699  7699 E AndroidRuntime:        at com.google.android.gms.common.internal.zzj$zzb.handleMessage(Unknown Source)
09-29 13:08:59.273  7699  7699 E AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:102)
09-29 13:08:59.273  7699  7699 E AndroidRuntime:        at android.os.Looper.loop(Looper.java:148)
09-29 13:08:59.273  7699  7699 E AndroidRuntime:        at android.app.ActivityThread.main(ActivityThread.java:5417)
09-29 13:08:59.273  7699  7699 E AndroidRuntime:        at java.lang.reflect.Method.invoke(Native Method)
09-29 13:08:59.273  7699  7699 E AndroidRuntime:        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
09-29 13:08:59.273  7699  7699 E AndroidRuntime:        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
shankari commented 8 years ago

We need to initialize the google API client in here. Otherwise, the service could be started twice, so we would create two clients, and by the time the first one calls onConnected(), our instance variable may have switched to the un-connected one. So we will try to use our instance variable, which will crash.

http://stackoverflow.com/questions/29343922/googleapiclient-is-throwing-googleapiclient-is-not-connected-yet-after-onconne

shankari commented 8 years ago

What should we do about the other instance variables? Let's think through it.

The other variables cannot really be moved to onCreate mTransition: this depends on the intent passed in for the start mCurrState: this needs a context to be read from

I do see a similar issue with using instance variables to pass things around, though. if we are started twice, with two different transitions, they will overwrite each other and some transitions may be missed.

How do we pass information to the onConnected callback?

shankari commented 8 years ago

A better option might be to use blockingConnect. https://developers.google.com/android/reference/com/google/android/gms/common/api/GoogleApiClient#blockingConnect()

The challenge is that this cannot be called in the UI thread, so we need to call it from a separate thread. These are pretty simple fixes, but it is pretty scary to be making these changes at the last minute without enough time to test them.

I am going to leave this unchanged for now - it's been working fine on android until now - make the changes on Friday and test them out over the weekend.

I will only fix the move to onCreate, which is really simple

shankari commented 8 years ago

because an alternative is to store all the pending actions in a queue and handle all of them on connect.

shankari commented 3 years ago

we have moved to the new GoogleApiClient which manages the connection logic internally https://github.com/e-mission/e-mission-data-collection/commit/b7ceceefa2899d40c6d2a58ce472a2b2c1168df4 and https://github.com/e-mission/e-mission-data-collection/commit/8363241c8dac06fe8f980c9332fe287c91f8ebce