Skip to content

Invalid task, not in foreground #14

@wilmxre

Description

@wilmxre

Sometimes, when you call RNLockTask.startLockTask(), you can get this error: Invalid task, not in foreground. I don't know a clear reproducible way to trigger this error yet, but i got a lot of error reportings on Sentry.

From further investigation i found out that this error is thrown in this part of the ActivityManagerService implementation
(line 8853: https://android.googlesource.com/platform/frameworks/base/+/android-5.0.2_r1/services/core/java/com/android/server/am/ActivityManagerService.java):

 void startLockTaskMode(TaskRecord task) {
  final String pkg;
  synchronized(this) {
    pkg = task.intent.getComponent().getPackageName();
  }
  boolean isSystemInitiated = Binder.getCallingUid() == Process.SYSTEM_UID;
  if (!isSystemInitiated && !isLockTaskAuthorized(pkg)) {
    final TaskRecord taskRecord = task;
    mHandler.post(new Runnable() {
      @Override
      public void run() {
        mLockToAppRequest.showLockTaskPrompt(taskRecord);
      }
    });
    return;
  }
  long ident = Binder.clearCallingIdentity();
  try {
    synchronized(this) {
      // Since we lost lock on task, make sure it is still there.
      task = mStackSupervisor.anyTaskForIdLocked(task.taskId);
      if (task != null) {
        if (!isSystemInitiated &&
          ((mFocusedActivity == null) || (task != mFocusedActivity.task))) {
          throw new IllegalArgumentException("Invalid task, not in foreground");
        }
        mStackSupervisor.setLockTaskModeLocked(task, !isSystemInitiated);
      }
    }
  } finally {
    Binder.restoreCallingIdentity(ident);
  }
}

it seems that there is no focused activity, but i am almost 100% sure the startLockTask() is called when the app is in foreground.

i submitted this ticket to raise awareness and maybe if someone has faced this issue, they might have some good insight to handle it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions