Program: 21807
Project: 21807
Buggy source:
void setOnBattery(boolean onBattery,int oldStatus,int level){
synchronized (this) {
    boolean doWrite=false;
    Message m=mHandler.obtainMessage(MSG_REPORT_POWER_CHANGE);
    m.arg1=onBattery ? 1 : 0;
    mHandler.sendMessage(m);
    mOnBattery=mOnBatteryInternal=onBattery;
    long uptime=SystemClock.uptimeMillis() * 1000;
    long mSecRealtime=SystemClock.elapsedRealtime();
    long realtime=mSecRealtime * 1000;
    if (onBattery) {
      if (oldStatus == BatteryManager.BATTERY_STATUS_FULL || level >= 100 || (mDischargeCurrentLevel < 20 && level > 90)) {
        doWrite=true;
        resetAllStatsLocked();
        mDischargeStartLevel=level;
        mLowDischargeAmountSinceCharge=0;
        mHighDischargeAmountSinceCharge=0;
      }
      updateKernelWakelocksLocked();
      mHistoryCur.batteryLevel=(byte)level;
      mHistoryCur.states&=~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
      if (DEBUG_HISTORY)       Slog.v(TAG,"String_Node_Str" + Integer.toHexString(mHistoryCur.states));
      addHistoryRecordLocked(mSecRealtime);
      mTrackBatteryUptimeStart=uptime;
      mTrackBatteryRealtimeStart=realtime;
      mUnpluggedBatteryUptime=getBatteryUptimeLocked(uptime);
      mUnpluggedBatteryRealtime=getBatteryRealtimeLocked(realtime);
      mDischargeCurrentLevel=mDischargeUnplugLevel=level;
      doUnplugLocked(mUnpluggedBatteryUptime,mUnpluggedBatteryRealtime);
    }
 else {
      updateKernelWakelocksLocked();
      mHistoryCur.batteryLevel=(byte)level;
      mHistoryCur.states|=HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
      if (DEBUG_HISTORY)       Slog.v(TAG,"String_Node_Str" + Integer.toHexString(mHistoryCur.states));
      addHistoryRecordLocked(mSecRealtime);
      mTrackBatteryPastUptime+=uptime - mTrackBatteryUptimeStart;
      mTrackBatteryPastRealtime+=realtime - mTrackBatteryRealtimeStart;
      mDischargeCurrentLevel=level;
      if (level < mDischargeUnplugLevel) {
        mLowDischargeAmountSinceCharge+=mDischargeUnplugLevel - level - 1;
        mHighDischargeAmountSinceCharge+=mDischargeUnplugLevel - level;
      }
      doPlugLocked(getBatteryUptimeLocked(uptime),getBatteryRealtimeLocked(realtime));
    }
    if (doWrite || (mLastWriteTime + (60 * 1000)) < mSecRealtime) {
      if (mFile != null) {
        writeAsyncLocked();
      }
    }
  }
}

Fixed version (ground truth):
void setOnBattery(boolean onBattery,int oldStatus,int level){
synchronized (this) {
    boolean doWrite=false;
    Message m=mHandler.obtainMessage(MSG_REPORT_POWER_CHANGE);
    m.arg1=onBattery ? 1 : 0;
    mHandler.sendMessage(m);
    mOnBattery=mOnBatteryInternal=onBattery;
    long uptime=SystemClock.uptimeMillis() * 1000;
    long mSecRealtime=SystemClock.elapsedRealtime();
    long realtime=mSecRealtime * 1000;
    if (onBattery) {
      if (oldStatus == BatteryManager.BATTERY_STATUS_FULL || level >= 95 || (mDischargeCurrentLevel < 30 && level >= 90)) {
        doWrite=true;
        resetAllStatsLocked();
        mDischargeStartLevel=level;
        mLowDischargeAmountSinceCharge=0;
        mHighDischargeAmountSinceCharge=0;
      }
      updateKernelWakelocksLocked();
      mHistoryCur.batteryLevel=(byte)level;
      mHistoryCur.states&=~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
      if (DEBUG_HISTORY)       Slog.v(TAG,"String_Node_Str" + Integer.toHexString(mHistoryCur.states));
      addHistoryRecordLocked(mSecRealtime);
      mTrackBatteryUptimeStart=uptime;
      mTrackBatteryRealtimeStart=realtime;
      mUnpluggedBatteryUptime=getBatteryUptimeLocked(uptime);
      mUnpluggedBatteryRealtime=getBatteryRealtimeLocked(realtime);
      mDischargeCurrentLevel=mDischargeUnplugLevel=level;
      doUnplugLocked(mUnpluggedBatteryUptime,mUnpluggedBatteryRealtime);
    }
 else {
      updateKernelWakelocksLocked();
      mHistoryCur.batteryLevel=(byte)level;
      mHistoryCur.states|=HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
      if (DEBUG_HISTORY)       Slog.v(TAG,"String_Node_Str" + Integer.toHexString(mHistoryCur.states));
      addHistoryRecordLocked(mSecRealtime);
      mTrackBatteryPastUptime+=uptime - mTrackBatteryUptimeStart;
      mTrackBatteryPastRealtime+=realtime - mTrackBatteryRealtimeStart;
      mDischargeCurrentLevel=level;
      if (level < mDischargeUnplugLevel) {
        mLowDischargeAmountSinceCharge+=mDischargeUnplugLevel - level - 1;
        mHighDischargeAmountSinceCharge+=mDischargeUnplugLevel - level;
      }
      doPlugLocked(getBatteryUptimeLocked(uptime),getBatteryRealtimeLocked(realtime));
    }
    if (doWrite || (mLastWriteTime + (60 * 1000)) < mSecRealtime) {
      if (mFile != null) {
        writeAsyncLocked();
      }
    }
  }
}