Program: 181376
Project: 181376
Buggy source:
/** 
 * Ensure that the top activity in the stack is resumed.
 * @param prev The previously resumed activity, for when in the processof pausing; can be null to call from elsewhere.
 * @return Returns true if something is being resumed, or false ifnothing happened.
 */
final boolean resumeTopActivityLocked(ActivityRecord prev){
  ActivityRecord next=topRunningActivityLocked(null);
  final boolean userLeaving=mUserLeaving;
  mUserLeaving=false;
  if (next == null) {
    if (mMainStack) {
      return mService.startHomeActivityLocked();
    }
  }
  next.delayedResume=false;
  if (mResumedActivity == next && next.state == ActivityState.RESUMED) {
    mService.mWindowManager.executeAppTransition();
    mNoAnimActivities.clear();
    return false;
  }
  if ((mService.mSleeping || mService.mShuttingDown) && mLastPausedActivity == next && next.state == ActivityState.PAUSED) {
    mService.mWindowManager.executeAppTransition();
    mNoAnimActivities.clear();
    return false;
  }
  mStoppingActivities.remove(next);
  mWaitingVisibleActivities.remove(next);
  if (DEBUG_SWITCH)   Slog.v(TAG,"String_Node_Str" + next);
  if (mPausingActivity != null) {
    if (DEBUG_SWITCH)     Slog.v(TAG,"String_Node_Str" + mPausingActivity);
    return false;
  }
  if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
    long now=SystemClock.uptimeMillis();
    final boolean inTime=mLastStartedActivity.startTime != 0 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
    final int lastUid=mLastStartedActivity.info.applicationInfo.uid;
    final int nextUid=next.info.applicationInfo.uid;
    if (inTime && lastUid != nextUid && lastUid != next.launchedFromUid && mService.checkPermission(android.Manifest.permission.STOP_APP_SWITCHES,-1,next.launchedFromUid) != PackageManager.PERMISSION_GRANTED) {
      mService.showLaunchWarningLocked(mLastStartedActivity,next);
    }
 else {
      next.startTime=now;
      mLastStartedActivity=next;
    }
  }
 else {
    next.startTime=SystemClock.uptimeMillis();
    mLastStartedActivity=next;
  }
  if (mResumedActivity != null) {
    if (DEBUG_SWITCH)     Slog.v(TAG,"String_Node_Str");
    startPausingLocked(userLeaving,false);
    return true;
  }
  if (prev != null && prev != next) {
    if (!prev.waitingVisible && next != null && !next.nowVisible) {
      prev.waitingVisible=true;
      mWaitingVisibleActivities.add(prev);
      if (DEBUG_SWITCH)       Slog.v(TAG,"String_Node_Str" + prev);
    }
 else {
      if (prev.finishing) {
        mService.mWindowManager.setAppVisibility(prev,false);
        if (DEBUG_SWITCH)         Slog.v(TAG,"String_Node_Str" + prev + "String_Node_Str"+ (prev != null ? prev.waitingVisible : null)+ "String_Node_Str"+ next.nowVisible);
      }
 else {
        if (DEBUG_SWITCH)         Slog.v(TAG,"String_Node_Str" + prev + "String_Node_Str"+ (prev != null ? prev.waitingVisible : null)+ "String_Node_Str"+ next.nowVisible);
      }
    }
  }
  if (prev != null) {
    if (prev.finishing) {
      if (DEBUG_TRANSITION)       Slog.v(TAG,"String_Node_Str" + prev);
      if (mNoAnimActivities.contains(prev)) {
        mService.mWindowManager.prepareAppTransition(WindowManagerPolicy.TRANSIT_NONE);
      }
 else {
        mService.mWindowManager.prepareAppTransition(prev.task == next.task ? WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE : WindowManagerPolicy.TRANSIT_TASK_CLOSE);
      }
      mService.mWindowManager.setAppWillBeHidden(prev);
      mService.mWindowManager.setAppVisibility(prev,false);
    }
 else {
      if (DEBUG_TRANSITION)       Slog.v(TAG,"String_Node_Str" + prev);
      if (mNoAnimActivities.contains(next)) {
        mService.mWindowManager.prepareAppTransition(WindowManagerPolicy.TRANSIT_NONE);
      }
 else {
        mService.mWindowManager.prepareAppTransition(prev.task == next.task ? WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN : WindowManagerPolicy.TRANSIT_TASK_OPEN);
      }
    }
    if (false) {
      mService.mWindowManager.setAppWillBeHidden(prev);
      mService.mWindowManager.setAppVisibility(prev,false);
    }
  }
 else   if (mHistory.size() > 1) {
    if (DEBUG_TRANSITION)     Slog.v(TAG,"String_Node_Str");
    if (mNoAnimActivities.contains(next)) {
      mService.mWindowManager.prepareAppTransition(WindowManagerPolicy.TRANSIT_NONE);
    }
 else {
      mService.mWindowManager.prepareAppTransition(WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN);
    }
  }
  if (next.app != null && next.app.thread != null) {
    if (DEBUG_SWITCH)     Slog.v(TAG,"String_Node_Str" + next);
    mService.mWindowManager.setAppVisibility(next,true);
    ActivityRecord lastResumedActivity=mResumedActivity;
    ActivityState lastState=next.state;
    mService.updateCpuStats();
    next.state=ActivityState.RESUMED;
    mResumedActivity=next;
    next.task.touchActiveTime();
    mService.updateLruProcessLocked(next.app,true,true);
    updateLRUListLocked(next);
    boolean updated=false;
    if (mMainStack) {
synchronized (mService) {
        Configuration config=mService.mWindowManager.updateOrientationFromAppTokens(mService.mConfiguration,next.mayFreezeScreenLocked(next.app) ? next : null);
        if (config != null) {
          next.frozenBeforeDestroy=true;
        }
        updated=mService.updateConfigurationLocked(config,next);
      }
    }
    if (!updated) {
      ActivityRecord nextNext=topRunningActivityLocked(null);
      if (DEBUG_SWITCH)       Slog.i(TAG,"String_Node_Str" + next + "String_Node_Str"+ nextNext);
      if (nextNext != next) {
        mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
      }
      if (mMainStack) {
        mService.setFocusedActivityLocked(next);
      }
      ensureActivitiesVisibleLocked(null,0);
      mService.mWindowManager.executeAppTransition();
      mNoAnimActivities.clear();
      return true;
    }
    try {
      ArrayList a=next.results;
      if (a != null) {
        final int N=a.size();
        if (!next.finishing && N > 0) {
          if (DEBUG_RESULTS)           Slog.v(TAG,"String_Node_Str" + next + "String_Node_Str"+ a);
          next.app.thread.scheduleSendResult(next,a);
        }
      }
      if (next.newIntents != null) {
        next.app.thread.scheduleNewIntent(next.newIntents,next);
      }
      EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,System.identityHashCode(next),next.task.taskId,next.shortComponentName);
      next.app.thread.scheduleResumeActivity(next,mService.isNextTransitionForward());
      pauseIfSleepingLocked();
    }
 catch (    Exception e) {
      next.state=lastState;
      mResumedActivity=lastResumedActivity;
      Slog.i(TAG,"String_Node_Str" + next);
      if (!next.hasBeenLaunched) {
        next.hasBeenLaunched=true;
      }
 else {
        if (SHOW_APP_STARTING_PREVIEW && mMainStack) {
          mService.mWindowManager.setAppStartingWindow(next,next.packageName,next.theme,next.nonLocalizedLabel,next.labelRes,next.icon,null,true);
        }
      }
      startSpecificActivityLocked(next,true,false);
      return true;
    }
    try {
      next.visible=true;
      completeResumeLocked(next);
    }
 catch (    Exception e) {
      Slog.w(TAG,"String_Node_Str" + next,e);
      requestFinishActivityLocked(next,Activity.RESULT_CANCELED,null,"String_Node_Str");
      return true;
    }
    next.icicle=null;
    next.haveState=false;
    next.stopped=false;
  }
 else {
    if (!next.hasBeenLaunched) {
      next.hasBeenLaunched=true;
    }
 else {
      if (SHOW_APP_STARTING_PREVIEW) {
        mService.mWindowManager.setAppStartingWindow(next,next.packageName,next.theme,next.nonLocalizedLabel,next.labelRes,next.icon,null,true);
      }
      if (DEBUG_SWITCH)       Slog.v(TAG,"String_Node_Str" + next);
    }
    startSpecificActivityLocked(next,true,true);
  }
  return true;
}

Fixed version (ground truth):
/** 
 * Ensure that the top activity in the stack is resumed.
 * @param prev The previously resumed activity, for when in the processof pausing; can be null to call from elsewhere.
 * @return Returns true if something is being resumed, or false ifnothing happened.
 */
final boolean resumeTopActivityLocked(ActivityRecord prev){
  ActivityRecord next=topRunningActivityLocked(null);
  final boolean userLeaving=mUserLeaving;
  mUserLeaving=false;
  if (next == null) {
    if (mMainStack) {
      return mService.startHomeActivityLocked();
    }
  }
  next.delayedResume=false;
  if (mResumedActivity == next && next.state == ActivityState.RESUMED) {
    mService.mWindowManager.executeAppTransition();
    mNoAnimActivities.clear();
    return false;
  }
  if ((mService.mSleeping || mService.mShuttingDown) && mLastPausedActivity == next && next.state == ActivityState.PAUSED) {
    mService.mWindowManager.executeAppTransition();
    mNoAnimActivities.clear();
    return false;
  }
  mStoppingActivities.remove(next);
  mWaitingVisibleActivities.remove(next);
  if (DEBUG_SWITCH)   Slog.v(TAG,"String_Node_Str" + next);
  if (mPausingActivity != null) {
    if (DEBUG_SWITCH)     Slog.v(TAG,"String_Node_Str" + mPausingActivity);
    return false;
  }
  if (false) {
    if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
      long now=SystemClock.uptimeMillis();
      final boolean inTime=mLastStartedActivity.startTime != 0 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
      final int lastUid=mLastStartedActivity.info.applicationInfo.uid;
      final int nextUid=next.info.applicationInfo.uid;
      if (inTime && lastUid != nextUid && lastUid != next.launchedFromUid && mService.checkPermission(android.Manifest.permission.STOP_APP_SWITCHES,-1,next.launchedFromUid) != PackageManager.PERMISSION_GRANTED) {
        mService.showLaunchWarningLocked(mLastStartedActivity,next);
      }
 else {
        next.startTime=now;
        mLastStartedActivity=next;
      }
    }
 else {
      next.startTime=SystemClock.uptimeMillis();
      mLastStartedActivity=next;
    }
  }
  if (mResumedActivity != null) {
    if (DEBUG_SWITCH)     Slog.v(TAG,"String_Node_Str");
    startPausingLocked(userLeaving,false);
    return true;
  }
  if (prev != null && prev != next) {
    if (!prev.waitingVisible && next != null && !next.nowVisible) {
      prev.waitingVisible=true;
      mWaitingVisibleActivities.add(prev);
      if (DEBUG_SWITCH)       Slog.v(TAG,"String_Node_Str" + prev);
    }
 else {
      if (prev.finishing) {
        mService.mWindowManager.setAppVisibility(prev,false);
        if (DEBUG_SWITCH)         Slog.v(TAG,"String_Node_Str" + prev + "String_Node_Str"+ (prev != null ? prev.waitingVisible : null)+ "String_Node_Str"+ next.nowVisible);
      }
 else {
        if (DEBUG_SWITCH)         Slog.v(TAG,"String_Node_Str" + prev + "String_Node_Str"+ (prev != null ? prev.waitingVisible : null)+ "String_Node_Str"+ next.nowVisible);
      }
    }
  }
  if (prev != null) {
    if (prev.finishing) {
      if (DEBUG_TRANSITION)       Slog.v(TAG,"String_Node_Str" + prev);
      if (mNoAnimActivities.contains(prev)) {
        mService.mWindowManager.prepareAppTransition(WindowManagerPolicy.TRANSIT_NONE);
      }
 else {
        mService.mWindowManager.prepareAppTransition(prev.task == next.task ? WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE : WindowManagerPolicy.TRANSIT_TASK_CLOSE);
      }
      mService.mWindowManager.setAppWillBeHidden(prev);
      mService.mWindowManager.setAppVisibility(prev,false);
    }
 else {
      if (DEBUG_TRANSITION)       Slog.v(TAG,"String_Node_Str" + prev);
      if (mNoAnimActivities.contains(next)) {
        mService.mWindowManager.prepareAppTransition(WindowManagerPolicy.TRANSIT_NONE);
      }
 else {
        mService.mWindowManager.prepareAppTransition(prev.task == next.task ? WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN : WindowManagerPolicy.TRANSIT_TASK_OPEN);
      }
    }
    if (false) {
      mService.mWindowManager.setAppWillBeHidden(prev);
      mService.mWindowManager.setAppVisibility(prev,false);
    }
  }
 else   if (mHistory.size() > 1) {
    if (DEBUG_TRANSITION)     Slog.v(TAG,"String_Node_Str");
    if (mNoAnimActivities.contains(next)) {
      mService.mWindowManager.prepareAppTransition(WindowManagerPolicy.TRANSIT_NONE);
    }
 else {
      mService.mWindowManager.prepareAppTransition(WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN);
    }
  }
  if (next.app != null && next.app.thread != null) {
    if (DEBUG_SWITCH)     Slog.v(TAG,"String_Node_Str" + next);
    mService.mWindowManager.setAppVisibility(next,true);
    ActivityRecord lastResumedActivity=mResumedActivity;
    ActivityState lastState=next.state;
    mService.updateCpuStats();
    next.state=ActivityState.RESUMED;
    mResumedActivity=next;
    next.task.touchActiveTime();
    mService.updateLruProcessLocked(next.app,true,true);
    updateLRUListLocked(next);
    boolean updated=false;
    if (mMainStack) {
synchronized (mService) {
        Configuration config=mService.mWindowManager.updateOrientationFromAppTokens(mService.mConfiguration,next.mayFreezeScreenLocked(next.app) ? next : null);
        if (config != null) {
          next.frozenBeforeDestroy=true;
        }
        updated=mService.updateConfigurationLocked(config,next);
      }
    }
    if (!updated) {
      ActivityRecord nextNext=topRunningActivityLocked(null);
      if (DEBUG_SWITCH)       Slog.i(TAG,"String_Node_Str" + next + "String_Node_Str"+ nextNext);
      if (nextNext != next) {
        mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
      }
      if (mMainStack) {
        mService.setFocusedActivityLocked(next);
      }
      ensureActivitiesVisibleLocked(null,0);
      mService.mWindowManager.executeAppTransition();
      mNoAnimActivities.clear();
      return true;
    }
    try {
      ArrayList a=next.results;
      if (a != null) {
        final int N=a.size();
        if (!next.finishing && N > 0) {
          if (DEBUG_RESULTS)           Slog.v(TAG,"String_Node_Str" + next + "String_Node_Str"+ a);
          next.app.thread.scheduleSendResult(next,a);
        }
      }
      if (next.newIntents != null) {
        next.app.thread.scheduleNewIntent(next.newIntents,next);
      }
      EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,System.identityHashCode(next),next.task.taskId,next.shortComponentName);
      next.app.thread.scheduleResumeActivity(next,mService.isNextTransitionForward());
      pauseIfSleepingLocked();
    }
 catch (    Exception e) {
      next.state=lastState;
      mResumedActivity=lastResumedActivity;
      Slog.i(TAG,"String_Node_Str" + next);
      if (!next.hasBeenLaunched) {
        next.hasBeenLaunched=true;
      }
 else {
        if (SHOW_APP_STARTING_PREVIEW && mMainStack) {
          mService.mWindowManager.setAppStartingWindow(next,next.packageName,next.theme,next.nonLocalizedLabel,next.labelRes,next.icon,null,true);
        }
      }
      startSpecificActivityLocked(next,true,false);
      return true;
    }
    try {
      next.visible=true;
      completeResumeLocked(next);
    }
 catch (    Exception e) {
      Slog.w(TAG,"String_Node_Str" + next,e);
      requestFinishActivityLocked(next,Activity.RESULT_CANCELED,null,"String_Node_Str");
      return true;
    }
    next.icicle=null;
    next.haveState=false;
    next.stopped=false;
  }
 else {
    if (!next.hasBeenLaunched) {
      next.hasBeenLaunched=true;
    }
 else {
      if (SHOW_APP_STARTING_PREVIEW) {
        mService.mWindowManager.setAppStartingWindow(next,next.packageName,next.theme,next.nonLocalizedLabel,next.labelRes,next.icon,null,true);
      }
      if (DEBUG_SWITCH)       Slog.v(TAG,"String_Node_Str" + next);
    }
    startSpecificActivityLocked(next,true,true);
  }
  return true;
}