The first transition is always the same - Activity Recognition API - Activity Transition
Asked Answered
M

1

11

I use activity recognition api and activity transitions. When I first run the app, the first transition is always the. In this app it's WALKING-ENTER. When I tried with only IN_VEHICLE-ENTER and IN_VEHICLE-EXIT in transitions, it was IN_VEHICLE-ENTER. I thought about ignoring the first transition but a device I've tested on didn't have such a problem. The devices that had these problem were Android 8.1, and the device didn't have the problem was 6.0.

MainActivity extends AppCompatActivity

private static Intent serviceIntent;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    int PERMISSION_ALL = 1;
    String[] PERMISSIONS = {Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION};

    if(!hasPermissions(this, PERMISSIONS)){
        ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
    }


    Button button = findViewById(R.id.button);
    button.setOnClickListener(
            new View.OnClickListener() {
                public void onClick(View view) {
                    serviceIntent = new Intent(MainActivity.this, ServiceS.class);
                    MainActivity.this.startService(serviceIntent);
                }
            });

}

public static boolean hasPermissions(Context context, String... permissions) {
    if (permissions != null) {
        for (String permission : permissions) {
            if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
    }
    return true;
}

ServiceS extends Service

public ServiceS() {
    super();
}


public static void locationArrived(Context context, Location location) {
    Log.d("hmm: ", location.toString());
}

@SuppressLint("MissingPermission")
@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onCreate() {
    initActivityTransition();
    super.onCreate();
}

@Override
public void onDestroy() {
    Log.d("hmm: ", "Updates stopped!");
    Task task = ActivityRecognition.getClient(this)
            .removeActivityTransitionUpdates(activityPendingIntent);
    super.onDestroy();
}

// ACTIVITY TRANSITION BLOCK START

private static List<ActivityTransition> transitions = new ArrayList<>();
private static PendingIntent activityPendingIntent;
private static ActivityTransitionRequest transitionRequest;

void initActivityTransition(){
    transitions.add(
            new ActivityTransition.Builder()
                    .setActivityType(DetectedActivity.IN_VEHICLE)
                    .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
                    .build());
    transitions.add(
            new ActivityTransition.Builder()
                    .setActivityType(DetectedActivity.IN_VEHICLE)
                    .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
                    .build());
    transitions.add(
            new ActivityTransition.Builder()
                    .setActivityType(DetectedActivity.WALKING)
                    .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
                    .build());
    transitions.add(
            new ActivityTransition.Builder()
                    .setActivityType(DetectedActivity.WALKING)
                    .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
                    .build());
    transitions.add(
            new ActivityTransition.Builder()
                    .setActivityType(DetectedActivity.STILL)
                    .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
                    .build());
    transitions.add(
            new ActivityTransition.Builder()
                    .setActivityType(DetectedActivity.STILL)
                    .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
                    .build());


    Intent activityIntentService = new Intent(this, TransitionReceiver.class);
    activityPendingIntent = PendingIntent.getBroadcast(this, 1, activityIntentService, PendingIntent.FLAG_UPDATE_CURRENT);

    Log.d("hmm: ","DriveBuddyService - initActivityTransition");
}

static void transitionArrived(final ActivityTransitionEvent event, final Context context){
    Log.d("hmm: ", event.toString());

    Toast.makeText(context, event.getActivityType() + "-" + event.getTransitionType(), Toast.LENGTH_LONG).show();

}

@Override
public void onStart(Intent intent, int startId) {
    if(transitionRequest==null) {
        transitionRequest = new ActivityTransitionRequest(transitions);
    }
    Task task = ActivityRecognition.getClient(this)
            .requestActivityTransitionUpdates(transitionRequest, activityPendingIntent);
    super.onStart(intent, startId);
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

TransitionReceiver extends BroadcastReceiver.

public void onReceive(final Context context, Intent intent) {
    Log.d("hmm: ","DriveBuddyTransitionReceiver - Enter");
    if (ActivityTransitionResult.hasResult(intent)) {
        ActivityTransitionResult result = ActivityTransitionResult.extractResult(intent);
        for (ActivityTransitionEvent event : result.getTransitionEvents()) {
            ServiceS.transitionArrived(event, context);
        }
    }
}
Malapropos answered 9/5, 2018 at 16:21 Comment(0)
L
13

I observed the same thing too. It seems that the last activity is kept in cache or something like that. I saw this happening on Android 7.1 and Android 8.1.

To counter this I check the elapsed time of when the transition happened. I ignore the callback if the transition happened over 30 seconds ago.

This is what my code looks like:

@Override
public void onReceive(Context context, Intent i) {

    if (ActivityTransitionResult.hasResult(i)) {
        ActivityTransitionResult result = ActivityTransitionResult.extractResult(i);

        for (ActivityTransitionEvent event : result.getTransitionEvents()) {
            //continue only if the activity happened in the last 30 seconds
            //for some reason callbacks are received for old activities when the receiver is registered
            if(((SystemClock.elapsedRealtime()-(event.getElapsedRealTimeNanos()/1000000))/1000) <= 30) {
                //activity transition is legit. Do stuff here..
            }
        }
    }
}

I've typically seen callbacks arrive 0-10 seconds of them occurring.

Leaves answered 8/6, 2018 at 6:21 Comment(3)
I have OnePlus 6 with android 9 and according to my tests, I sometimes receive events that happened longer than 5 seconds ago (9-10 seconds ago approx), which should be treated as reliable events.Lamoree
@Lamoree true, I have seen that too. I have changed this to 30 seconds in my code. Will update my code here as well.Leaves
It happened the same thing to me. Now I know why. ThanksFabriane

© 2022 - 2024 — McMap. All rights reserved.