11

Google Play Services provides an ActivityRecognitionApi that lets you detect various user activities (via DetectedActivity) such as if the user is walking or running.

Is it possible to mock these activities for development and testing purposes?

AdamK
  • 21,199
  • 5
  • 42
  • 58

2 Answers2

17

Yes, it's possible, but only on the emulator (or a rooted device).

For example, to simulate the walking activity run:

adb root
adb shell am broadcast -a com.google.gservices.intent.action.GSERVICES_OVERRIDE -e 'location:mock_activity_type' 'WALKING'

And then restart Google Play Services (or restart the device):

adb shell ps -A | grep com.google.android.gms.persistent | awk '{print $2}' | xargs adb shell kill
AdamK
  • 21,199
  • 5
  • 42
  • 58
  • Any details?! Where I can find more documentation? I want to simulate a switch from WALKING to VEHICLE state... – Oleksandr Kucherenko Sep 18 '17 at 13:44
  • 1
    Sorry, unfortunately this is not documented anywhere officially. I received these instructions directly from the team so they should work. You can just switch out "WALKING" for "VEHICLE" to try and switch it. – AdamK Sep 18 '17 at 16:27
  • @AdamK The above doesn't work for me, physical activity transition do show up in my log but i am unable to simulate this. – Uzair Jul 10 '19 at 10:01
  • I tried this on both emulator and device (to test requestActivityTransitionUpdates) . Doesn't work for either :( – android developer Jan 19 '21 at 07:45
  • Unfortunately this was from 2017. I have no idea if this flag is still supported. If it's not working then likely not :( – AdamK Jan 19 '21 at 10:53
  • 1
    @AdamK Any other alternative? – android developer Jan 24 '21 at 11:15
6

It's possible to do it without the adb commands. Create and send an intent with the correct extra.

Add the transitions you need to a list and add that list to the constructor of an ActivityTransitionResult object. To create the extra, use SafeParcelableSerializer.serializeToIntentExtra with the key "com.google.android.location.internal.EXTRA_ACTIVITY_TRANSITION_RESULT"

I have used this code to simulate the transition from still to walking.

Intent intent = new Intent();
intent.setAction("MYLISTENINGACTION");
List<ActivityTransitionEvent> events = new ArrayList<>();
ActivityTransitionEvent transitionEvent;
transitionEvent = new ActivityTransitionEvent(DetectedActivity.STILL, 
   ActivityTransition.ACTIVITY_TRANSITION_EXIT, SystemClock.elapsedRealtimeNanos());
events.add(transitionEvent);
transitionEvent = new ActivityTransitionEvent(DetectedActivity.WALKING, 
   ActivityTransition.ACTIVITY_TRANSITION_ENTER, SystemClock.elapsedRealtimeNanos());
events.add(transitionEvent);
ActivityTransitionResult result = new ActivityTransitionResult(events);
SafeParcelableSerializer.serializeToIntentExtra(result, intent, 
   "com.google.android.location.internal.EXTRA_ACTIVITY_TRANSITION_RESULT");
sendBroadcast(intent);
PerO
  • 61
  • 1
  • 2
  • I am trying to import `com.google.android.gms.common.internal.safeparcel.SafeParcelableSerializer;` but getting error is there any work around for the same – Rajat Singh Aug 23 '22 at 13:52