Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing Activity Recognition Transition API

I am currently developing an app which uses Activity Recognition Transition API (the new one, not the old, check the link below). My question is, how can I test my app? More exactly, how can I "manually" trigger transition events? Do I really have to put my phone and laptop into my backpack and go for a ride on a bicycle to trigger the ON_BYCICLE/ACTIVITY_TRANSITION_ENTER event? There must be an easier way :) Maybe using adb? https://developer.android.com/studio/test/command-line

API documentation: https://developer.android.com/guide/topics/location/transitions

like image 793
Andras Sanislo Avatar asked Jun 16 '26 09:06

Andras Sanislo


2 Answers

You can use following code to emulate the event

        var intent = Intent()

        // Your broadcast receiver action

        intent.action = BuildConfig.APPLICATION_ID + "TRANSITIONS_RECEIVER_ACTION"
        var events: ArrayList<ActivityTransitionEvent> = arrayListOf()

        // You can set desired events with their corresponding state

        var transitionEvent = ActivityTransitionEvent(DetectedActivity.IN_VEHICLE, ActivityTransition.ACTIVITY_TRANSITION_ENTER, SystemClock.elapsedRealtimeNanos())
        events.add(transitionEvent)
        var result = ActivityTransitionResult(events)
        SafeParcelableSerializer.serializeToIntentExtra(result, intent, "com.google.android.location.internal.EXTRA_ACTIVITY_TRANSITION_RESULT")
        activity?.sendBroadcast(intent)

I have created simple activity with two buttons in my application where I broadcast these events(Start and Stop) respectively. This helps me debug the application gracefully.

like image 72
Uzair Avatar answered Jun 18 '26 00:06

Uzair


I don't know of any way to simulate a transition event from outside your app. But within your app, you can construct a suitable Intent and send it to your receiver.

Add this method to any Context (e.g. an Activity or Service): (Kotlin)

class MyService: Service() {
  fun sendFakeActivityTransitionEvent() {
    // name your intended recipient class
    val intent = Intent(this, MyReceiver::class.java)
    
    val events: ArrayList<ActivityTransitionEvent> = arrayListOf()
    
    // create fake events
    events.add(
      ActivityTransitionEvent(
        DetectedActivity.ON_BICYCLE,
        ActivityTransition.ACTIVITY_TRANSITION_ENTER,
        SystemClock.elapsedRealtimeNanos()
      )
    )
    
    // finally, serialize and send
    val result = ActivityTransitionResult(events)
    SafeParcelableSerializer.serializeToIntentExtra(
      result,
      intent,
      "com.google.android.location.internal.EXTRA_ACTIVITY_TRANSITION_RESULT"
    )
    this.sendBroadcast(intent)
  }
}

Substitute your desired recipient for MyReceiver -- any subclass of BroadcastReceiver should work.

Then call sendFakeActivityTransitionEvent() when desired.

like image 40
Evan Heidtmann Avatar answered Jun 17 '26 23:06

Evan Heidtmann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!