nus-yam/ex-repair huggingface dataset:
Program: 192951
Project: 192951
Buggy source:
/** 
 * If any of the apps don't have a last-used entry, add one now.
 * @param currentTimeMillis the current time
 * @param firstUpdate if it is the first update, touch all installed apps, otherwise onlytouch the system apps
 */
private void initializeDefaultsForApps(long currentTimeMillis,long deviceUsageTime,boolean firstUpdate){
  PackageManager pm=mContext.getPackageManager();
  List<PackageInfo> packages=pm.getInstalledPackagesAsUser(0,mUserId);
  final int packageCount=packages.size();
  for (int i=0; i < packageCount; i++) {
    final PackageInfo pi=packages.get(i);
    String packageName=pi.packageName;
    if (pi.applicationInfo != null && (firstUpdate || pi.applicationInfo.isSystemApp()) && getBeginIdleTime(packageName) == -1) {
      for (      IntervalStats stats : mCurrentStats) {
        stats.update(packageName,currentTimeMillis,Event.SYSTEM_INTERACTION);
        stats.updateBeginIdleTime(packageName,deviceUsageTime);
        mStatsChanged=true;
      }
    }
  }
  persistActiveStats();
}

Fixed version (ground truth):
/** 
 * If any of the apps don't have a last-used entry, add one now.
 * @param currentTimeMillis the current time
 * @param firstUpdate if it is the first update, touch all installed apps, otherwise onlytouch the system apps
 */
private void initializeDefaultsForApps(long currentTimeMillis,long deviceUsageTime,boolean firstUpdate){
  PackageManager pm=mContext.getPackageManager();
  List<PackageInfo> packages=pm.getInstalledPackagesAsUser(0,mUserId);
  final int packageCount=packages.size();
  for (int i=0; i < packageCount; i++) {
    final PackageInfo pi=packages.get(i);
    String packageName=pi.packageName;
    if (pi.applicationInfo != null && (firstUpdate || pi.applicationInfo.isSystemApp()) && getBeginIdleTime(packageName) == -1) {
      for (      IntervalStats stats : mCurrentStats) {
        stats.update(packageName,currentTimeMillis,Event.SYSTEM_INTERACTION);
        stats.updateBeginIdleTime(packageName,deviceUsageTime);
      }
      mAppIdleRollingWindow.update(packageName,currentTimeMillis,Event.SYSTEM_INTERACTION);
      mAppIdleRollingWindow.updateBeginIdleTime(packageName,deviceUsageTime);
      mStatsChanged=true;
    }
  }
  persistActiveStats();
}
Program: 192952
Project: 192952
Buggy source:
private static void mergePackageStats(IntervalStats dst,IntervalStats src){
  dst.endTime=Math.max(dst.endTime,src.endTime);
  final int srcPackageCount=src.packageStats.size();
  for (int i=0; i < srcPackageCount; i++) {
    final String packageName=src.packageStats.keyAt(i);
    final UsageStats srcStats=src.packageStats.valueAt(i);
    final UsageStats dstStats=dst.packageStats.get(packageName);
    if (dstStats == null) {
      dst.packageStats.put(packageName,new UsageStats(srcStats));
    }
 else {
      dstStats.add(src.packageStats.valueAt(i));
    }
  }
}

Fixed version (ground truth):
private static void mergePackageStats(IntervalStats dst,IntervalStats src,final long deviceUsageTime){
  dst.endTime=Math.max(dst.endTime,src.endTime);
  final int srcPackageCount=src.packageStats.size();
  for (int i=0; i < srcPackageCount; i++) {
    final String packageName=src.packageStats.keyAt(i);
    final UsageStats srcStats=src.packageStats.valueAt(i);
    UsageStats dstStats=dst.packageStats.get(packageName);
    if (dstStats == null) {
      dstStats=new UsageStats(srcStats);
      dst.packageStats.put(packageName,dstStats);
    }
 else {
      dstStats.add(src.packageStats.valueAt(i));
    }
    if (dstStats.mBeginIdleTime > deviceUsageTime) {
      dstStats.mBeginIdleTime=deviceUsageTime;
    }
  }
}
Program: 192953
Project: 192953
Buggy source:
@Override public void combine(IntervalStats stats,boolean mutable,List<IntervalStats> accumulatedResult){
  IntervalStats accum;
  if (accumulatedResult.isEmpty()) {
    accum=new IntervalStats();
    accum.beginTime=stats.beginTime;
    accumulatedResult.add(accum);
  }
 else {
    accum=accumulatedResult.get(0);
  }
  mergePackageStats(accum,stats);
}

Fixed version (ground truth):
@Override public void combine(IntervalStats stats,boolean mutable,List<IntervalStats> accumulatedResult){
  IntervalStats accum;
  if (accumulatedResult.isEmpty()) {
    accum=new IntervalStats();
    accum.beginTime=stats.beginTime;
    accumulatedResult.add(accum);
  }
 else {
    accum=accumulatedResult.get(0);
  }
  mergePackageStats(accum,stats,deviceUsageTime);
}
Program: 192954
Project: 192954
Buggy source:
void init(final long currentTimeMillis,final long deviceUsageTime){
  mDatabase.init(currentTimeMillis);
  int nullCount=0;
  for (int i=0; i < mCurrentStats.length; i++) {
    mCurrentStats[i]=mDatabase.getLatestUsageStats(i);
    if (mCurrentStats[i] == null) {
      nullCount++;
    }
  }
  if (nullCount > 0) {
    if (nullCount != mCurrentStats.length) {
      Slog.w(TAG,mLogPrefix + "String_Node_Str");
    }
 else {
    }
    loadActiveStats(currentTimeMillis,false);
  }
 else {
    updateRolloverDeadline();
  }
  for (  IntervalStats stat : mCurrentStats) {
    final int pkgCount=stat.packageStats.size();
    for (int i=0; i < pkgCount; i++) {
      UsageStats pkgStats=stat.packageStats.valueAt(i);
      if (pkgStats.mLastEvent == UsageEvents.Event.MOVE_TO_FOREGROUND || pkgStats.mLastEvent == UsageEvents.Event.CONTINUE_PREVIOUS_DAY) {
        stat.update(pkgStats.mPackageName,stat.lastTimeSaved,UsageEvents.Event.END_OF_DAY);
        notifyStatsChanged();
      }
    }
    stat.updateConfigurationStats(null,stat.lastTimeSaved);
  }
  if (mDatabase.isNewUpdate()) {
    initializeDefaultsForApps(currentTimeMillis,deviceUsageTime,mDatabase.isFirstUpdate());
  }
  refreshAppIdleRollingWindow(currentTimeMillis);
}

Fixed version (ground truth):
void init(final long currentTimeMillis,final long deviceUsageTime){
  mDatabase.init(currentTimeMillis);
  int nullCount=0;
  for (int i=0; i < mCurrentStats.length; i++) {
    mCurrentStats[i]=mDatabase.getLatestUsageStats(i);
    if (mCurrentStats[i] == null) {
      nullCount++;
    }
  }
  if (nullCount > 0) {
    if (nullCount != mCurrentStats.length) {
      Slog.w(TAG,mLogPrefix + "String_Node_Str");
    }
 else {
    }
    loadActiveStats(currentTimeMillis,false);
  }
 else {
    updateRolloverDeadline();
  }
  for (  IntervalStats stat : mCurrentStats) {
    final int pkgCount=stat.packageStats.size();
    for (int i=0; i < pkgCount; i++) {
      UsageStats pkgStats=stat.packageStats.valueAt(i);
      if (pkgStats.mLastEvent == UsageEvents.Event.MOVE_TO_FOREGROUND || pkgStats.mLastEvent == UsageEvents.Event.CONTINUE_PREVIOUS_DAY) {
        stat.update(pkgStats.mPackageName,stat.lastTimeSaved,UsageEvents.Event.END_OF_DAY);
        notifyStatsChanged();
      }
    }
    stat.updateConfigurationStats(null,stat.lastTimeSaved);
  }
  refreshAppIdleRollingWindow(currentTimeMillis,deviceUsageTime);
  if (mDatabase.isNewUpdate()) {
    initializeDefaultsForApps(currentTimeMillis,deviceUsageTime,mDatabase.isFirstUpdate());
  }
}
Program: 192955
Project: 192955
Buggy source:
void reportEvent(UsageEvents.Event event,long deviceUsageTime){
  if (DEBUG) {
    Slog.d(TAG,mLogPrefix + "String_Node_Str" + event.mPackage+ "String_Node_Str"+ event.mTimeStamp+ "String_Node_Str"+ eventToString(event.mEventType));
  }
  if (event.mTimeStamp >= mDailyExpiryDate.getTimeInMillis()) {
    rolloverStats(event.mTimeStamp);
  }
  final IntervalStats currentDailyStats=mCurrentStats[UsageStatsManager.INTERVAL_DAILY];
  final Configuration newFullConfig=event.mConfiguration;
  if (event.mEventType == UsageEvents.Event.CONFIGURATION_CHANGE && currentDailyStats.activeConfiguration != null) {
    event.mConfiguration=Configuration.generateDelta(currentDailyStats.activeConfiguration,newFullConfig);
  }
  if (currentDailyStats.events == null) {
    currentDailyStats.events=new TimeSparseArray<>();
  }
  if (event.mEventType != UsageEvents.Event.SYSTEM_INTERACTION) {
    currentDailyStats.events.put(event.mTimeStamp,event);
  }
  for (  IntervalStats stats : mCurrentStats) {
    if (event.mEventType == UsageEvents.Event.CONFIGURATION_CHANGE) {
      stats.updateConfigurationStats(newFullConfig,event.mTimeStamp);
    }
 else {
      stats.update(event.mPackage,event.mTimeStamp,event.mEventType);
      stats.updateBeginIdleTime(event.mPackage,deviceUsageTime);
    }
  }
  if (event.mEventType != Event.CONFIGURATION_CHANGE) {
    mAppIdleRollingWindow.update(event.mPackage,event.mTimeStamp,event.mEventType);
    mAppIdleRollingWindow.updateBeginIdleTime(event.mPackage,deviceUsageTime);
  }
  notifyStatsChanged();
}

Fixed version (ground truth):
void reportEvent(UsageEvents.Event event,long deviceUsageTime){
  if (DEBUG) {
    Slog.d(TAG,mLogPrefix + "String_Node_Str" + event.mPackage+ "String_Node_Str"+ event.mTimeStamp+ "String_Node_Str"+ eventToString(event.mEventType));
  }
  if (event.mTimeStamp >= mDailyExpiryDate.getTimeInMillis()) {
    rolloverStats(event.mTimeStamp,deviceUsageTime);
  }
  final IntervalStats currentDailyStats=mCurrentStats[UsageStatsManager.INTERVAL_DAILY];
  final Configuration newFullConfig=event.mConfiguration;
  if (event.mEventType == UsageEvents.Event.CONFIGURATION_CHANGE && currentDailyStats.activeConfiguration != null) {
    event.mConfiguration=Configuration.generateDelta(currentDailyStats.activeConfiguration,newFullConfig);
  }
  if (currentDailyStats.events == null) {
    currentDailyStats.events=new TimeSparseArray<>();
  }
  if (event.mEventType != UsageEvents.Event.SYSTEM_INTERACTION) {
    currentDailyStats.events.put(event.mTimeStamp,event);
  }
  for (  IntervalStats stats : mCurrentStats) {
    if (event.mEventType == UsageEvents.Event.CONFIGURATION_CHANGE) {
      stats.updateConfigurationStats(newFullConfig,event.mTimeStamp);
    }
 else {
      stats.update(event.mPackage,event.mTimeStamp,event.mEventType);
      stats.updateBeginIdleTime(event.mPackage,deviceUsageTime);
    }
  }
  if (event.mEventType != Event.CONFIGURATION_CHANGE) {
    mAppIdleRollingWindow.update(event.mPackage,event.mTimeStamp,event.mEventType);
    mAppIdleRollingWindow.updateBeginIdleTime(event.mPackage,deviceUsageTime);
  }
  notifyStatsChanged();
}
Program: 192956
Project: 192956
Buggy source:
/** 
 * App idle operates on a rolling window of time. When we roll over time, we end up with a period of time where in-memory stats are empty and we don't hit the disk for older stats for performance reasons. Suddenly all apps will become idle. Instead, at times we do a deep query to find all the apps that have run in the past few days and keep the cached data up to date.
 * @param currentTimeMillis
 */
void refreshAppIdleRollingWindow(long currentTimeMillis){
  List<IntervalStats> stats=mDatabase.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,currentTimeMillis - (1000 * 60 * 60* 24* 2),currentTimeMillis,sPackageStatsMerger);
  if (stats == null || stats.isEmpty()) {
    mAppIdleRollingWindow=new IntervalStats();
    mergePackageStats(mAppIdleRollingWindow,mCurrentStats[UsageStatsManager.INTERVAL_YEARLY]);
  }
 else {
    mAppIdleRollingWindow=stats.get(0);
  }
}

Fixed version (ground truth):
/** 
 * App idle operates on a rolling window of time. When we roll over time, we end up with a period of time where in-memory stats are empty and we don't hit the disk for older stats for performance reasons. Suddenly all apps will become idle. Instead, at times we do a deep query to find all the apps that have run in the past few days and keep the cached data up to date.
 * @param currentTimeMillis
 */
void refreshAppIdleRollingWindow(final long currentTimeMillis,final long deviceUsageTime){
  List<IntervalStats> stats=mDatabase.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,currentTimeMillis - (1000 * 60 * 60* 24* 2),currentTimeMillis,new StatCombiner<IntervalStats>(){
    @Override public void combine(    IntervalStats stats,    boolean mutable,    List<IntervalStats> accumulatedResult){
      IntervalStats accum;
      if (accumulatedResult.isEmpty()) {
        accum=new IntervalStats();
        accum.beginTime=stats.beginTime;
        accumulatedResult.add(accum);
      }
 else {
        accum=accumulatedResult.get(0);
      }
      mergePackageStats(accum,stats,deviceUsageTime);
    }
  }
);
  if (stats == null || stats.isEmpty()) {
    mAppIdleRollingWindow=new IntervalStats();
    mergePackageStats(mAppIdleRollingWindow,mCurrentStats[UsageStatsManager.INTERVAL_YEARLY],deviceUsageTime);
  }
 else {
    mAppIdleRollingWindow=stats.get(0);
  }
}
Program: 192958
Project: 192958
Buggy source:
/** 
 * This should be the only way to get the time from the system.
 */
private long checkAndGetTimeLocked(){
  final long actualSystemTime=System.currentTimeMillis();
  final long actualRealtime=SystemClock.elapsedRealtime();
  final long expectedSystemTime=(actualRealtime - mRealTimeSnapshot) + mSystemTimeSnapshot;
  boolean resetBeginIdleTime=false;
  if (Math.abs(actualSystemTime - expectedSystemTime) > TIME_CHANGE_THRESHOLD_MILLIS) {
    if (Math.abs(actualSystemTime - expectedSystemTime) > mAppIdleDurationMillis) {
      mScreenOnSystemTimeSnapshot=actualSystemTime;
      mScreenOnTime=0;
      resetBeginIdleTime=true;
    }
    final int userCount=mUserState.size();
    for (int i=0; i < userCount; i++) {
      final UserUsageStatsService service=mUserState.valueAt(i);
      service.onTimeChanged(expectedSystemTime,actualSystemTime,resetBeginIdleTime);
    }
    mRealTimeSnapshot=actualRealtime;
    mSystemTimeSnapshot=actualSystemTime;
  }
  return actualSystemTime;
}

Fixed version (ground truth):
/** 
 * This should be the only way to get the time from the system.
 */
private long checkAndGetTimeLocked(){
  final long actualSystemTime=System.currentTimeMillis();
  final long actualRealtime=SystemClock.elapsedRealtime();
  final long expectedSystemTime=(actualRealtime - mRealTimeSnapshot) + mSystemTimeSnapshot;
  boolean resetBeginIdleTime=false;
  if (Math.abs(actualSystemTime - expectedSystemTime) > TIME_CHANGE_THRESHOLD_MILLIS) {
    if (Math.abs(actualSystemTime - expectedSystemTime) > mAppIdleDurationMillis) {
      mScreenOnSystemTimeSnapshot=actualSystemTime;
      mScreenOnTime=0;
      resetBeginIdleTime=true;
    }
    final int userCount=mUserState.size();
    for (int i=0; i < userCount; i++) {
      final UserUsageStatsService service=mUserState.valueAt(i);
      service.onTimeChanged(expectedSystemTime,actualSystemTime,mScreenOnTime,resetBeginIdleTime);
    }
    mRealTimeSnapshot=actualRealtime;
    mSystemTimeSnapshot=actualSystemTime;
  }
  return actualSystemTime;
}
Program: 192959
Project: 192959
Buggy source:
void onTimeChanged(long oldTime,long newTime,boolean resetBeginIdleTime){
  persistActiveStats();
  mDatabase.onTimeChanged(newTime - oldTime);
  loadActiveStats(newTime,resetBeginIdleTime);
  refreshAppIdleRollingWindow(newTime);
}

Fixed version (ground truth):
void onTimeChanged(long oldTime,long newTime,long deviceUsageTime,boolean resetBeginIdleTime){
  persistActiveStats();
  mDatabase.onTimeChanged(newTime - oldTime);
  loadActiveStats(newTime,resetBeginIdleTime);
  refreshAppIdleRollingWindow(newTime,deviceUsageTime);
}
Program: 192960
Project: 192960
Buggy source:
/** 
 * If any of the apps don't have a last-used entry, add one now.
 * @param currentTimeMillis the current time
 * @param firstUpdate if it is the first update, touch all installed apps, otherwise onlytouch the system apps
 */
private void initializeDefaultsForApps(long currentTimeMillis,long deviceUsageTime,boolean firstUpdate){
  PackageManager pm=mContext.getPackageManager();
  List<PackageInfo> packages=pm.getInstalledPackagesAsUser(0,mUserId);
  final int packageCount=packages.size();
  for (int i=0; i < packageCount; i++) {
    final PackageInfo pi=packages.get(i);
    String packageName=pi.packageName;
    if (pi.applicationInfo != null && (firstUpdate || pi.applicationInfo.isSystemApp()) && getBeginIdleTime(packageName) == -1) {
      for (      IntervalStats stats : mCurrentStats) {
        stats.update(packageName,currentTimeMillis,Event.SYSTEM_INTERACTION);
        stats.updateBeginIdleTime(packageName,deviceUsageTime);
        mStatsChanged=true;
      }
    }
  }
  persistActiveStats();
}

Fixed version (ground truth):
/** 
 * If any of the apps don't have a last-used entry, add one now.
 * @param currentTimeMillis the current time
 * @param firstUpdate if it is the first update, touch all installed apps, otherwise onlytouch the system apps
 */
private void initializeDefaultsForApps(long currentTimeMillis,long deviceUsageTime,boolean firstUpdate){
  PackageManager pm=mContext.getPackageManager();
  List<PackageInfo> packages=pm.getInstalledPackagesAsUser(0,mUserId);
  final int packageCount=packages.size();
  for (int i=0; i < packageCount; i++) {
    final PackageInfo pi=packages.get(i);
    String packageName=pi.packageName;
    if (pi.applicationInfo != null && (firstUpdate || pi.applicationInfo.isSystemApp()) && getBeginIdleTime(packageName) == -1) {
      for (      IntervalStats stats : mCurrentStats) {
        stats.update(packageName,currentTimeMillis,Event.SYSTEM_INTERACTION);
        stats.updateBeginIdleTime(packageName,deviceUsageTime);
      }
      mAppIdleRollingWindow.update(packageName,currentTimeMillis,Event.SYSTEM_INTERACTION);
      mAppIdleRollingWindow.updateBeginIdleTime(packageName,deviceUsageTime);
      mStatsChanged=true;
    }
  }
  persistActiveStats();
}
Program: 192961
Project: 192961
Buggy source:
private static void mergePackageStats(IntervalStats dst,IntervalStats src){
  dst.endTime=Math.max(dst.endTime,src.endTime);
  final int srcPackageCount=src.packageStats.size();
  for (int i=0; i < srcPackageCount; i++) {
    final String packageName=src.packageStats.keyAt(i);
    final UsageStats srcStats=src.packageStats.valueAt(i);
    final UsageStats dstStats=dst.packageStats.get(packageName);
    if (dstStats == null) {
      dst.packageStats.put(packageName,new UsageStats(srcStats));
    }
 else {
      dstStats.add(src.packageStats.valueAt(i));
    }
  }
}

Fixed version (ground truth):
private static void mergePackageStats(IntervalStats dst,IntervalStats src,final long deviceUsageTime){
  dst.endTime=Math.max(dst.endTime,src.endTime);
  final int srcPackageCount=src.packageStats.size();
  for (int i=0; i < srcPackageCount; i++) {
    final String packageName=src.packageStats.keyAt(i);
    final UsageStats srcStats=src.packageStats.valueAt(i);
    UsageStats dstStats=dst.packageStats.get(packageName);
    if (dstStats == null) {
      dstStats=new UsageStats(srcStats);
      dst.packageStats.put(packageName,dstStats);
    }
 else {
      dstStats.add(src.packageStats.valueAt(i));
    }
    if (dstStats.mBeginIdleTime > deviceUsageTime) {
      dstStats.mBeginIdleTime=deviceUsageTime;
    }
  }
}
Program: 192962
Project: 192962
Buggy source:
@Override public void combine(IntervalStats stats,boolean mutable,List<IntervalStats> accumulatedResult){
  IntervalStats accum;
  if (accumulatedResult.isEmpty()) {
    accum=new IntervalStats();
    accum.beginTime=stats.beginTime;
    accumulatedResult.add(accum);
  }
 else {
    accum=accumulatedResult.get(0);
  }
  mergePackageStats(accum,stats);
}

Fixed version (ground truth):
@Override public void combine(IntervalStats stats,boolean mutable,List<IntervalStats> accumulatedResult){
  IntervalStats accum;
  if (accumulatedResult.isEmpty()) {
    accum=new IntervalStats();
    accum.beginTime=stats.beginTime;
    accumulatedResult.add(accum);
  }
 else {
    accum=accumulatedResult.get(0);
  }
  mergePackageStats(accum,stats,deviceUsageTime);
}
Program: 192963
Project: 192963
Buggy source:
void init(final long currentTimeMillis,final long deviceUsageTime){
  mDatabase.init(currentTimeMillis);
  int nullCount=0;
  for (int i=0; i < mCurrentStats.length; i++) {
    mCurrentStats[i]=mDatabase.getLatestUsageStats(i);
    if (mCurrentStats[i] == null) {
      nullCount++;
    }
  }
  if (nullCount > 0) {
    if (nullCount != mCurrentStats.length) {
      Slog.w(TAG,mLogPrefix + "String_Node_Str");
    }
 else {
    }
    loadActiveStats(currentTimeMillis,false);
  }
 else {
    updateRolloverDeadline();
  }
  for (  IntervalStats stat : mCurrentStats) {
    final int pkgCount=stat.packageStats.size();
    for (int i=0; i < pkgCount; i++) {
      UsageStats pkgStats=stat.packageStats.valueAt(i);
      if (pkgStats.mLastEvent == UsageEvents.Event.MOVE_TO_FOREGROUND || pkgStats.mLastEvent == UsageEvents.Event.CONTINUE_PREVIOUS_DAY) {
        stat.update(pkgStats.mPackageName,stat.lastTimeSaved,UsageEvents.Event.END_OF_DAY);
        notifyStatsChanged();
      }
    }
    stat.updateConfigurationStats(null,stat.lastTimeSaved);
  }
  if (mDatabase.isNewUpdate()) {
    initializeDefaultsForApps(currentTimeMillis,deviceUsageTime,mDatabase.isFirstUpdate());
  }
  refreshAppIdleRollingWindow(currentTimeMillis);
}

Fixed version (ground truth):
void init(final long currentTimeMillis,final long deviceUsageTime){
  mDatabase.init(currentTimeMillis);
  int nullCount=0;
  for (int i=0; i < mCurrentStats.length; i++) {
    mCurrentStats[i]=mDatabase.getLatestUsageStats(i);
    if (mCurrentStats[i] == null) {
      nullCount++;
    }
  }
  if (nullCount > 0) {
    if (nullCount != mCurrentStats.length) {
      Slog.w(TAG,mLogPrefix + "String_Node_Str");
    }
 else {
    }
    loadActiveStats(currentTimeMillis,false);
  }
 else {
    updateRolloverDeadline();
  }
  for (  IntervalStats stat : mCurrentStats) {
    final int pkgCount=stat.packageStats.size();
    for (int i=0; i < pkgCount; i++) {
      UsageStats pkgStats=stat.packageStats.valueAt(i);
      if (pkgStats.mLastEvent == UsageEvents.Event.MOVE_TO_FOREGROUND || pkgStats.mLastEvent == UsageEvents.Event.CONTINUE_PREVIOUS_DAY) {
        stat.update(pkgStats.mPackageName,stat.lastTimeSaved,UsageEvents.Event.END_OF_DAY);
        notifyStatsChanged();
      }
    }
    stat.updateConfigurationStats(null,stat.lastTimeSaved);
  }
  refreshAppIdleRollingWindow(currentTimeMillis,deviceUsageTime);
  if (mDatabase.isNewUpdate()) {
    initializeDefaultsForApps(currentTimeMillis,deviceUsageTime,mDatabase.isFirstUpdate());
  }
}
Program: 192964
Project: 192964
Buggy source:
void reportEvent(UsageEvents.Event event,long deviceUsageTime){
  if (DEBUG) {
    Slog.d(TAG,mLogPrefix + "String_Node_Str" + event.mPackage+ "String_Node_Str"+ event.mTimeStamp+ "String_Node_Str"+ eventToString(event.mEventType));
  }
  if (event.mTimeStamp >= mDailyExpiryDate.getTimeInMillis()) {
    rolloverStats(event.mTimeStamp);
  }
  final IntervalStats currentDailyStats=mCurrentStats[UsageStatsManager.INTERVAL_DAILY];
  final Configuration newFullConfig=event.mConfiguration;
  if (event.mEventType == UsageEvents.Event.CONFIGURATION_CHANGE && currentDailyStats.activeConfiguration != null) {
    event.mConfiguration=Configuration.generateDelta(currentDailyStats.activeConfiguration,newFullConfig);
  }
  if (currentDailyStats.events == null) {
    currentDailyStats.events=new TimeSparseArray<>();
  }
  if (event.mEventType != UsageEvents.Event.SYSTEM_INTERACTION) {
    currentDailyStats.events.put(event.mTimeStamp,event);
  }
  for (  IntervalStats stats : mCurrentStats) {
    if (event.mEventType == UsageEvents.Event.CONFIGURATION_CHANGE) {
      stats.updateConfigurationStats(newFullConfig,event.mTimeStamp);
    }
 else {
      stats.update(event.mPackage,event.mTimeStamp,event.mEventType);
      stats.updateBeginIdleTime(event.mPackage,deviceUsageTime);
    }
  }
  if (event.mEventType != Event.CONFIGURATION_CHANGE) {
    mAppIdleRollingWindow.update(event.mPackage,event.mTimeStamp,event.mEventType);
    mAppIdleRollingWindow.updateBeginIdleTime(event.mPackage,deviceUsageTime);
  }
  notifyStatsChanged();
}

Fixed version (ground truth):
void reportEvent(UsageEvents.Event event,long deviceUsageTime){
  if (DEBUG) {
    Slog.d(TAG,mLogPrefix + "String_Node_Str" + event.mPackage+ "String_Node_Str"+ event.mTimeStamp+ "String_Node_Str"+ eventToString(event.mEventType));
  }
  if (event.mTimeStamp >= mDailyExpiryDate.getTimeInMillis()) {
    rolloverStats(event.mTimeStamp,deviceUsageTime);
  }
  final IntervalStats currentDailyStats=mCurrentStats[UsageStatsManager.INTERVAL_DAILY];
  final Configuration newFullConfig=event.mConfiguration;
  if (event.mEventType == UsageEvents.Event.CONFIGURATION_CHANGE && currentDailyStats.activeConfiguration != null) {
    event.mConfiguration=Configuration.generateDelta(currentDailyStats.activeConfiguration,newFullConfig);
  }
  if (currentDailyStats.events == null) {
    currentDailyStats.events=new TimeSparseArray<>();
  }
  if (event.mEventType != UsageEvents.Event.SYSTEM_INTERACTION) {
    currentDailyStats.events.put(event.mTimeStamp,event);
  }
  for (  IntervalStats stats : mCurrentStats) {
    if (event.mEventType == UsageEvents.Event.CONFIGURATION_CHANGE) {
      stats.updateConfigurationStats(newFullConfig,event.mTimeStamp);
    }
 else {
      stats.update(event.mPackage,event.mTimeStamp,event.mEventType);
      stats.updateBeginIdleTime(event.mPackage,deviceUsageTime);
    }
  }
  if (event.mEventType != Event.CONFIGURATION_CHANGE) {
    mAppIdleRollingWindow.update(event.mPackage,event.mTimeStamp,event.mEventType);
    mAppIdleRollingWindow.updateBeginIdleTime(event.mPackage,deviceUsageTime);
  }
  notifyStatsChanged();
}
Program: 192965
Project: 192965
Buggy source:
/** 
 * App idle operates on a rolling window of time. When we roll over time, we end up with a period of time where in-memory stats are empty and we don't hit the disk for older stats for performance reasons. Suddenly all apps will become idle. Instead, at times we do a deep query to find all the apps that have run in the past few days and keep the cached data up to date.
 * @param currentTimeMillis
 */
void refreshAppIdleRollingWindow(long currentTimeMillis){
  List<IntervalStats> stats=mDatabase.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,currentTimeMillis - (1000 * 60 * 60* 24* 2),currentTimeMillis,sPackageStatsMerger);
  if (stats == null || stats.isEmpty()) {
    mAppIdleRollingWindow=new IntervalStats();
    mergePackageStats(mAppIdleRollingWindow,mCurrentStats[UsageStatsManager.INTERVAL_YEARLY]);
  }
 else {
    mAppIdleRollingWindow=stats.get(0);
  }
}

Fixed version (ground truth):
/** 
 * App idle operates on a rolling window of time. When we roll over time, we end up with a period of time where in-memory stats are empty and we don't hit the disk for older stats for performance reasons. Suddenly all apps will become idle. Instead, at times we do a deep query to find all the apps that have run in the past few days and keep the cached data up to date.
 * @param currentTimeMillis
 */
void refreshAppIdleRollingWindow(final long currentTimeMillis,final long deviceUsageTime){
  List<IntervalStats> stats=mDatabase.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,currentTimeMillis - (1000 * 60 * 60* 24* 2),currentTimeMillis,new StatCombiner<IntervalStats>(){
    @Override public void combine(    IntervalStats stats,    boolean mutable,    List<IntervalStats> accumulatedResult){
      IntervalStats accum;
      if (accumulatedResult.isEmpty()) {
        accum=new IntervalStats();
        accum.beginTime=stats.beginTime;
        accumulatedResult.add(accum);
      }
 else {
        accum=accumulatedResult.get(0);
      }
      mergePackageStats(accum,stats,deviceUsageTime);
    }
  }
);
  if (stats == null || stats.isEmpty()) {
    mAppIdleRollingWindow=new IntervalStats();
    mergePackageStats(mAppIdleRollingWindow,mCurrentStats[UsageStatsManager.INTERVAL_YEARLY],deviceUsageTime);
  }
 else {
    mAppIdleRollingWindow=stats.get(0);
  }
}
Program: 192967
Project: 192967
Buggy source:
/** 
 * This should be the only way to get the time from the system.
 */
private long checkAndGetTimeLocked(){
  final long actualSystemTime=System.currentTimeMillis();
  final long actualRealtime=SystemClock.elapsedRealtime();
  final long expectedSystemTime=(actualRealtime - mRealTimeSnapshot) + mSystemTimeSnapshot;
  boolean resetBeginIdleTime=false;
  if (Math.abs(actualSystemTime - expectedSystemTime) > TIME_CHANGE_THRESHOLD_MILLIS) {
    if (Math.abs(actualSystemTime - expectedSystemTime) > mAppIdleDurationMillis) {
      mScreenOnSystemTimeSnapshot=actualSystemTime;
      mScreenOnTime=0;
      resetBeginIdleTime=true;
    }
    final int userCount=mUserState.size();
    for (int i=0; i < userCount; i++) {
      final UserUsageStatsService service=mUserState.valueAt(i);
      service.onTimeChanged(expectedSystemTime,actualSystemTime,resetBeginIdleTime);
    }
    mRealTimeSnapshot=actualRealtime;
    mSystemTimeSnapshot=actualSystemTime;
  }
  return actualSystemTime;
}

Fixed version (ground truth):
/** 
 * This should be the only way to get the time from the system.
 */
private long checkAndGetTimeLocked(){
  final long actualSystemTime=System.currentTimeMillis();
  final long actualRealtime=SystemClock.elapsedRealtime();
  final long expectedSystemTime=(actualRealtime - mRealTimeSnapshot) + mSystemTimeSnapshot;
  boolean resetBeginIdleTime=false;
  if (Math.abs(actualSystemTime - expectedSystemTime) > TIME_CHANGE_THRESHOLD_MILLIS) {
    if (Math.abs(actualSystemTime - expectedSystemTime) > mAppIdleDurationMillis) {
      mScreenOnSystemTimeSnapshot=actualSystemTime;
      mScreenOnTime=0;
      resetBeginIdleTime=true;
    }
    final int userCount=mUserState.size();
    for (int i=0; i < userCount; i++) {
      final UserUsageStatsService service=mUserState.valueAt(i);
      service.onTimeChanged(expectedSystemTime,actualSystemTime,mScreenOnTime,resetBeginIdleTime);
    }
    mRealTimeSnapshot=actualRealtime;
    mSystemTimeSnapshot=actualSystemTime;
  }
  return actualSystemTime;
}
Program: 192968
Project: 192968
Buggy source:
void onTimeChanged(long oldTime,long newTime,boolean resetBeginIdleTime){
  persistActiveStats();
  mDatabase.onTimeChanged(newTime - oldTime);
  loadActiveStats(newTime,resetBeginIdleTime);
  refreshAppIdleRollingWindow(newTime);
}

Fixed version (ground truth):
void onTimeChanged(long oldTime,long newTime,long deviceUsageTime,boolean resetBeginIdleTime){
  persistActiveStats();
  mDatabase.onTimeChanged(newTime - oldTime);
  loadActiveStats(newTime,resetBeginIdleTime);
  refreshAppIdleRollingWindow(newTime,deviceUsageTime);
}
Program: 192969
Project: 192969
Buggy source:
/** 
 * If any of the apps don't have a last-used entry, add one now.
 * @param currentTimeMillis the current time
 * @param firstUpdate if it is the first update, touch all installed apps, otherwise onlytouch the system apps
 */
private void initializeDefaultsForApps(long currentTimeMillis,long deviceUsageTime,boolean firstUpdate){
  PackageManager pm=mContext.getPackageManager();
  List<PackageInfo> packages=pm.getInstalledPackagesAsUser(0,mUserId);
  final int packageCount=packages.size();
  for (int i=0; i < packageCount; i++) {
    final PackageInfo pi=packages.get(i);
    String packageName=pi.packageName;
    if (pi.applicationInfo != null && (firstUpdate || pi.applicationInfo.isSystemApp()) && getBeginIdleTime(packageName) == -1) {
      for (      IntervalStats stats : mCurrentStats) {
        stats.update(packageName,currentTimeMillis,Event.SYSTEM_INTERACTION);
        stats.updateBeginIdleTime(packageName,deviceUsageTime);
        mStatsChanged=true;
      }
    }
  }
  persistActiveStats();
}

Fixed version (ground truth):
/** 
 * If any of the apps don't have a last-used entry, add one now.
 * @param currentTimeMillis the current time
 * @param firstUpdate if it is the first update, touch all installed apps, otherwise onlytouch the system apps
 */
private void initializeDefaultsForApps(long currentTimeMillis,long deviceUsageTime,boolean firstUpdate){
  PackageManager pm=mContext.getPackageManager();
  List<PackageInfo> packages=pm.getInstalledPackagesAsUser(0,mUserId);
  final int packageCount=packages.size();
  for (int i=0; i < packageCount; i++) {
    final PackageInfo pi=packages.get(i);
    String packageName=pi.packageName;
    if (pi.applicationInfo != null && (firstUpdate || pi.applicationInfo.isSystemApp()) && getBeginIdleTime(packageName) == -1) {
      for (      IntervalStats stats : mCurrentStats) {
        stats.update(packageName,currentTimeMillis,Event.SYSTEM_INTERACTION);
        stats.updateBeginIdleTime(packageName,deviceUsageTime);
      }
      mAppIdleRollingWindow.update(packageName,currentTimeMillis,Event.SYSTEM_INTERACTION);
      mAppIdleRollingWindow.updateBeginIdleTime(packageName,deviceUsageTime);
      mStatsChanged=true;
    }
  }
  persistActiveStats();
}
Program: 192970
Project: 192970
Buggy source:
private static void mergePackageStats(IntervalStats dst,IntervalStats src){
  dst.endTime=Math.max(dst.endTime,src.endTime);
  final int srcPackageCount=src.packageStats.size();
  for (int i=0; i < srcPackageCount; i++) {
    final String packageName=src.packageStats.keyAt(i);
    final UsageStats srcStats=src.packageStats.valueAt(i);
    final UsageStats dstStats=dst.packageStats.get(packageName);
    if (dstStats == null) {
      dst.packageStats.put(packageName,new UsageStats(srcStats));
    }
 else {
      dstStats.add(src.packageStats.valueAt(i));
    }
  }
}

Fixed version (ground truth):
private static void mergePackageStats(IntervalStats dst,IntervalStats src,final long deviceUsageTime){
  dst.endTime=Math.max(dst.endTime,src.endTime);
  final int srcPackageCount=src.packageStats.size();
  for (int i=0; i < srcPackageCount; i++) {
    final String packageName=src.packageStats.keyAt(i);
    final UsageStats srcStats=src.packageStats.valueAt(i);
    UsageStats dstStats=dst.packageStats.get(packageName);
    if (dstStats == null) {
      dstStats=new UsageStats(srcStats);
      dst.packageStats.put(packageName,dstStats);
    }
 else {
      dstStats.add(src.packageStats.valueAt(i));
    }
    if (dstStats.mBeginIdleTime > deviceUsageTime) {
      dstStats.mBeginIdleTime=deviceUsageTime;
    }
  }
}
Program: 192971
Project: 192971
Buggy source:
@Override public void combine(IntervalStats stats,boolean mutable,List<IntervalStats> accumulatedResult){
  IntervalStats accum;
  if (accumulatedResult.isEmpty()) {
    accum=new IntervalStats();
    accum.beginTime=stats.beginTime;
    accumulatedResult.add(accum);
  }
 else {
    accum=accumulatedResult.get(0);
  }
  mergePackageStats(accum,stats);
}

Fixed version (ground truth):
@Override public void combine(IntervalStats stats,boolean mutable,List<IntervalStats> accumulatedResult){
  IntervalStats accum;
  if (accumulatedResult.isEmpty()) {
    accum=new IntervalStats();
    accum.beginTime=stats.beginTime;
    accumulatedResult.add(accum);
  }
 else {
    accum=accumulatedResult.get(0);
  }
  mergePackageStats(accum,stats,deviceUsageTime);
}
Program: 192972
Project: 192972
Buggy source:
void init(final long currentTimeMillis,final long deviceUsageTime){
  mDatabase.init(currentTimeMillis);
  int nullCount=0;
  for (int i=0; i < mCurrentStats.length; i++) {
    mCurrentStats[i]=mDatabase.getLatestUsageStats(i);
    if (mCurrentStats[i] == null) {
      nullCount++;
    }
  }
  if (nullCount > 0) {
    if (nullCount != mCurrentStats.length) {
      Slog.w(TAG,mLogPrefix + "String_Node_Str");
    }
 else {
    }
    loadActiveStats(currentTimeMillis,false);
  }
 else {
    updateRolloverDeadline();
  }
  for (  IntervalStats stat : mCurrentStats) {
    final int pkgCount=stat.packageStats.size();
    for (int i=0; i < pkgCount; i++) {
      UsageStats pkgStats=stat.packageStats.valueAt(i);
      if (pkgStats.mLastEvent == UsageEvents.Event.MOVE_TO_FOREGROUND || pkgStats.mLastEvent == UsageEvents.Event.CONTINUE_PREVIOUS_DAY) {
        stat.update(pkgStats.mPackageName,stat.lastTimeSaved,UsageEvents.Event.END_OF_DAY);
        notifyStatsChanged();
      }
    }
    stat.updateConfigurationStats(null,stat.lastTimeSaved);
  }
  if (mDatabase.isNewUpdate()) {
    initializeDefaultsForApps(currentTimeMillis,deviceUsageTime,mDatabase.isFirstUpdate());
  }
  refreshAppIdleRollingWindow(currentTimeMillis);
}

Fixed version (ground truth):
void init(final long currentTimeMillis,final long deviceUsageTime){
  mDatabase.init(currentTimeMillis);
  int nullCount=0;
  for (int i=0; i < mCurrentStats.length; i++) {
    mCurrentStats[i]=mDatabase.getLatestUsageStats(i);
    if (mCurrentStats[i] == null) {
      nullCount++;
    }
  }
  if (nullCount > 0) {
    if (nullCount != mCurrentStats.length) {
      Slog.w(TAG,mLogPrefix + "String_Node_Str");
    }
 else {
    }
    loadActiveStats(currentTimeMillis,false);
  }
 else {
    updateRolloverDeadline();
  }
  for (  IntervalStats stat : mCurrentStats) {
    final int pkgCount=stat.packageStats.size();
    for (int i=0; i < pkgCount; i++) {
      UsageStats pkgStats=stat.packageStats.valueAt(i);
      if (pkgStats.mLastEvent == UsageEvents.Event.MOVE_TO_FOREGROUND || pkgStats.mLastEvent == UsageEvents.Event.CONTINUE_PREVIOUS_DAY) {
        stat.update(pkgStats.mPackageName,stat.lastTimeSaved,UsageEvents.Event.END_OF_DAY);
        notifyStatsChanged();
      }
    }
    stat.updateConfigurationStats(null,stat.lastTimeSaved);
  }
  refreshAppIdleRollingWindow(currentTimeMillis,deviceUsageTime);
  if (mDatabase.isNewUpdate()) {
    initializeDefaultsForApps(currentTimeMillis,deviceUsageTime,mDatabase.isFirstUpdate());
  }
}
Program: 192973
Project: 192973
Buggy source:
void reportEvent(UsageEvents.Event event,long deviceUsageTime){
  if (DEBUG) {
    Slog.d(TAG,mLogPrefix + "String_Node_Str" + event.mPackage+ "String_Node_Str"+ event.mTimeStamp+ "String_Node_Str"+ eventToString(event.mEventType));
  }
  if (event.mTimeStamp >= mDailyExpiryDate.getTimeInMillis()) {
    rolloverStats(event.mTimeStamp);
  }
  final IntervalStats currentDailyStats=mCurrentStats[UsageStatsManager.INTERVAL_DAILY];
  final Configuration newFullConfig=event.mConfiguration;
  if (event.mEventType == UsageEvents.Event.CONFIGURATION_CHANGE && currentDailyStats.activeConfiguration != null) {
    event.mConfiguration=Configuration.generateDelta(currentDailyStats.activeConfiguration,newFullConfig);
  }
  if (currentDailyStats.events == null) {
    currentDailyStats.events=new TimeSparseArray<>();
  }
  if (event.mEventType != UsageEvents.Event.SYSTEM_INTERACTION) {
    currentDailyStats.events.put(event.mTimeStamp,event);
  }
  for (  IntervalStats stats : mCurrentStats) {
    if (event.mEventType == UsageEvents.Event.CONFIGURATION_CHANGE) {
      stats.updateConfigurationStats(newFullConfig,event.mTimeStamp);
    }
 else {
      stats.update(event.mPackage,event.mTimeStamp,event.mEventType);
      stats.updateBeginIdleTime(event.mPackage,deviceUsageTime);
    }
  }
  if (event.mEventType != Event.CONFIGURATION_CHANGE) {
    mAppIdleRollingWindow.update(event.mPackage,event.mTimeStamp,event.mEventType);
    mAppIdleRollingWindow.updateBeginIdleTime(event.mPackage,deviceUsageTime);
  }
  notifyStatsChanged();
}

Fixed version (ground truth):
void reportEvent(UsageEvents.Event event,long deviceUsageTime){
  if (DEBUG) {
    Slog.d(TAG,mLogPrefix + "String_Node_Str" + event.mPackage+ "String_Node_Str"+ event.mTimeStamp+ "String_Node_Str"+ eventToString(event.mEventType));
  }
  if (event.mTimeStamp >= mDailyExpiryDate.getTimeInMillis()) {
    rolloverStats(event.mTimeStamp,deviceUsageTime);
  }
  final IntervalStats currentDailyStats=mCurrentStats[UsageStatsManager.INTERVAL_DAILY];
  final Configuration newFullConfig=event.mConfiguration;
  if (event.mEventType == UsageEvents.Event.CONFIGURATION_CHANGE && currentDailyStats.activeConfiguration != null) {
    event.mConfiguration=Configuration.generateDelta(currentDailyStats.activeConfiguration,newFullConfig);
  }
  if (currentDailyStats.events == null) {
    currentDailyStats.events=new TimeSparseArray<>();
  }
  if (event.mEventType != UsageEvents.Event.SYSTEM_INTERACTION) {
    currentDailyStats.events.put(event.mTimeStamp,event);
  }
  for (  IntervalStats stats : mCurrentStats) {
    if (event.mEventType == UsageEvents.Event.CONFIGURATION_CHANGE) {
      stats.updateConfigurationStats(newFullConfig,event.mTimeStamp);
    }
 else {
      stats.update(event.mPackage,event.mTimeStamp,event.mEventType);
      stats.updateBeginIdleTime(event.mPackage,deviceUsageTime);
    }
  }
  if (event.mEventType != Event.CONFIGURATION_CHANGE) {
    mAppIdleRollingWindow.update(event.mPackage,event.mTimeStamp,event.mEventType);
    mAppIdleRollingWindow.updateBeginIdleTime(event.mPackage,deviceUsageTime);
  }
  notifyStatsChanged();
}
Program: 192974
Project: 192974
Buggy source:
/** 
 * App idle operates on a rolling window of time. When we roll over time, we end up with a period of time where in-memory stats are empty and we don't hit the disk for older stats for performance reasons. Suddenly all apps will become idle. Instead, at times we do a deep query to find all the apps that have run in the past few days and keep the cached data up to date.
 * @param currentTimeMillis
 */
void refreshAppIdleRollingWindow(long currentTimeMillis){
  List<IntervalStats> stats=mDatabase.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,currentTimeMillis - (1000 * 60 * 60* 24* 2),currentTimeMillis,sPackageStatsMerger);
  if (stats == null || stats.isEmpty()) {
    mAppIdleRollingWindow=new IntervalStats();
    mergePackageStats(mAppIdleRollingWindow,mCurrentStats[UsageStatsManager.INTERVAL_YEARLY]);
  }
 else {
    mAppIdleRollingWindow=stats.get(0);
  }
}

Fixed version (ground truth):
/** 
 * App idle operates on a rolling window of time. When we roll over time, we end up with a period of time where in-memory stats are empty and we don't hit the disk for older stats for performance reasons. Suddenly all apps will become idle. Instead, at times we do a deep query to find all the apps that have run in the past few days and keep the cached data up to date.
 * @param currentTimeMillis
 */
void refreshAppIdleRollingWindow(final long currentTimeMillis,final long deviceUsageTime){
  List<IntervalStats> stats=mDatabase.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,currentTimeMillis - (1000 * 60 * 60* 24* 2),currentTimeMillis,new StatCombiner<IntervalStats>(){
    @Override public void combine(    IntervalStats stats,    boolean mutable,    List<IntervalStats> accumulatedResult){
      IntervalStats accum;
      if (accumulatedResult.isEmpty()) {
        accum=new IntervalStats();
        accum.beginTime=stats.beginTime;
        accumulatedResult.add(accum);
      }
 else {
        accum=accumulatedResult.get(0);
      }
      mergePackageStats(accum,stats,deviceUsageTime);
    }
  }
);
  if (stats == null || stats.isEmpty()) {
    mAppIdleRollingWindow=new IntervalStats();
    mergePackageStats(mAppIdleRollingWindow,mCurrentStats[UsageStatsManager.INTERVAL_YEARLY],deviceUsageTime);
  }
 else {
    mAppIdleRollingWindow=stats.get(0);
  }
}
Program: 192978
Project: 192978
Buggy source:
private void insertTaskAtTop(TaskRecord task,ActivityRecord newActivity){
  if (task.isOverHomeStack()) {
    final TaskRecord nextTask=getNextTask(task);
    if (nextTask != null) {
      nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
    }
  }
  if (isOnHomeDisplay()) {
    ActivityStack lastStack=mStackSupervisor.getLastStack();
    final boolean fromHome=lastStack.isHomeStack();
    if (!isHomeStack() && (fromHome || topTask() != task)) {
      task.setTaskToReturnTo(fromHome ? lastStack.topTask() == null ? HOME_ACTIVITY_TYPE : lastStack.topTask().taskType : APPLICATION_ACTIVITY_TYPE);
    }
  }
 else {
    task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
  }
  mTaskHistory.remove(task);
  int taskNdx=mTaskHistory.size();
  final boolean notShownWhenLocked=(newActivity != null && (newActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) || (newActivity == null && task.topRunningActivityLocked() == null);
  if (!mStackSupervisor.isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
    while (--taskNdx >= 0) {
      final TaskRecord tmpTask=mTaskHistory.get(taskNdx);
      if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId) || tmpTask.topRunningActivityLocked() == null) {
        break;
      }
    }
    ++taskNdx;
  }
  mTaskHistory.add(taskNdx,task);
  updateTaskMovement(task,true);
}

Fixed version (ground truth):
private void insertTaskAtTop(TaskRecord task,ActivityRecord newActivity){
  if (task.isOverHomeStack()) {
    final TaskRecord nextTask=getNextTask(task);
    if (nextTask != null) {
      nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
    }
  }
  if (isOnHomeDisplay()) {
    ActivityStack lastStack=mStackSupervisor.getLastStack();
    final boolean fromHome=lastStack.isHomeStack();
    if (!isHomeStack() && (fromHome || topTask() != task)) {
      int returnToType=APPLICATION_ACTIVITY_TYPE;
      if (fromHome && StackId.allowTopTaskToReturnHome(mStackId)) {
        returnToType=lastStack.topTask() == null ? HOME_ACTIVITY_TYPE : lastStack.topTask().taskType;
      }
      task.setTaskToReturnTo(returnToType);
    }
  }
 else {
    task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
  }
  mTaskHistory.remove(task);
  int taskNdx=mTaskHistory.size();
  final boolean notShownWhenLocked=(newActivity != null && (newActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) || (newActivity == null && task.topRunningActivityLocked() == null);
  if (!mStackSupervisor.isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
    while (--taskNdx >= 0) {
      final TaskRecord tmpTask=mTaskHistory.get(taskNdx);
      if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId) || tmpTask.topRunningActivityLocked() == null) {
        break;
      }
    }
    ++taskNdx;
  }
  mTaskHistory.add(taskNdx,task);
  updateTaskMovement(task,true);
}
Program: 192981
Project: 192981
Buggy source:
final void activityStoppedLocked(ActivityRecord r,Bundle icicle,PersistableBundle persistentState,CharSequence description){
  if (r.state != ActivityState.STOPPING) {
    Slog.i(TAG,"String_Node_Str" + r);
    mHandler.removeMessages(STOP_TIMEOUT_MSG,r);
    return;
  }
  if (persistentState != null) {
    r.persistentState=persistentState;
    mService.notifyTaskPersisterLocked(r.task,false);
  }
  if (DEBUG_SAVED_STATE)   Slog.i(TAG_SAVED_STATE,"String_Node_Str" + r + "String_Node_Str"+ icicle);
  if (icicle != null) {
    r.icicle=icicle;
    r.haveState=true;
    r.launchCount=0;
    r.updateThumbnailLocked(null,description);
  }
  if (!r.stopped) {
    if (DEBUG_STATES)     Slog.v(TAG_STATES,"String_Node_Str" + r + "String_Node_Str");
    mHandler.removeMessages(STOP_TIMEOUT_MSG,r);
    r.stopped=true;
    r.state=ActivityState.STOPPED;
    if (getVisibleBehindActivity() == r) {
      mStackSupervisor.requestVisibleBehindLocked(r,false);
    }
    if (r.finishing) {
      r.clearOptionsLocked();
    }
 else {
      if (r.configDestroy) {
        destroyActivityLocked(r,true,"String_Node_Str");
        mStackSupervisor.resumeFocusedStackTopActivityLocked();
      }
 else {
        mStackSupervisor.updatePreviousProcessLocked(r);
      }
    }
  }
}

Fixed version (ground truth):
final void activityStoppedLocked(ActivityRecord r,Bundle icicle,PersistableBundle persistentState,CharSequence description){
  if (r.state != ActivityState.STOPPING) {
    Slog.i(TAG,"String_Node_Str" + r);
    mHandler.removeMessages(STOP_TIMEOUT_MSG,r);
    return;
  }
  if (persistentState != null) {
    r.persistentState=persistentState;
    mService.notifyTaskPersisterLocked(r.task,false);
  }
  if (DEBUG_SAVED_STATE)   Slog.i(TAG_SAVED_STATE,"String_Node_Str" + r + "String_Node_Str"+ icicle);
  if (icicle != null) {
    r.icicle=icicle;
    r.haveState=true;
    r.launchCount=0;
    r.updateThumbnailLocked(null,description);
  }
  if (!r.stopped) {
    if (DEBUG_STATES)     Slog.v(TAG_STATES,"String_Node_Str" + r + "String_Node_Str");
    mHandler.removeMessages(STOP_TIMEOUT_MSG,r);
    r.stopped=true;
    r.state=ActivityState.STOPPED;
    if (getVisibleBehindActivity() == r) {
      mStackSupervisor.requestVisibleBehindLocked(r,false);
    }
    if (r.finishing) {
      r.clearOptionsLocked();
    }
 else {
      if (r.deferRelaunchUntilPaused) {
        destroyActivityLocked(r,true,"String_Node_Str");
        mStackSupervisor.resumeFocusedStackTopActivityLocked();
      }
 else {
        mStackSupervisor.updatePreviousProcessLocked(r);
      }
    }
  }
}