Program: 195338
Project: 195338
Buggy source:
public void refreshStats(int statsType,SparseArray<UserHandle> asUsers,long rawRealtimeUs,long rawUptimeUs){
  getStats();
  mMaxPower=0;
  mMaxRealPower=0;
  mComputedPower=0;
  mTotalPower=0;
  mUsageList.clear();
  mWifiSippers.clear();
  mBluetoothSippers.clear();
  mUserSippers.clear();
  mMobilemsppList.clear();
  if (mStats == null) {
    return;
  }
  if (mCpuPowerCalculator == null) {
    mCpuPowerCalculator=new CpuPowerCalculator(mPowerProfile);
  }
  mCpuPowerCalculator.reset();
  if (mWakelockPowerCalculator == null) {
    mWakelockPowerCalculator=new WakelockPowerCalculator(mPowerProfile);
  }
  mWakelockPowerCalculator.reset();
  if (mMobileRadioPowerCalculator == null) {
    mMobileRadioPowerCalculator=new MobileRadioPowerCalculator(mPowerProfile,mStats);
  }
  mMobileRadioPowerCalculator.reset(mStats);
  if (mWifiPowerCalculator == null) {
    if (checkHasWifiPowerReporting(mContext,mPowerProfile)) {
      mWifiPowerCalculator=new WifiPowerCalculator(mPowerProfile);
    }
 else {
      mWifiPowerCalculator=new WifiPowerEstimator(mPowerProfile);
    }
  }
  mWifiPowerCalculator.reset();
  if (mBluetoothPowerCalculator == null) {
    mBluetoothPowerCalculator=new BluetoothPowerCalculator();
  }
  mBluetoothPowerCalculator.reset();
  if (mSensorPowerCalculator == null) {
    mSensorPowerCalculator=new SensorPowerCalculator(mPowerProfile,(SensorManager)mContext.getSystemService(Context.SENSOR_SERVICE));
  }
  mSensorPowerCalculator.reset();
  mStatsType=statsType;
  mRawUptime=rawUptimeUs;
  mRawRealtime=rawRealtimeUs;
  mBatteryUptime=mStats.getBatteryUptime(rawUptimeUs);
  mBatteryRealtime=mStats.getBatteryRealtime(rawRealtimeUs);
  mTypeBatteryUptime=mStats.computeBatteryUptime(rawUptimeUs,mStatsType);
  mTypeBatteryRealtime=mStats.computeBatteryRealtime(rawRealtimeUs,mStatsType);
  mBatteryTimeRemaining=mStats.computeBatteryTimeRemaining(rawRealtimeUs);
  mChargeTimeRemaining=mStats.computeChargeTimeRemaining(rawRealtimeUs);
  if (DEBUG) {
    Log.d(TAG,"String_Node_Str" + (rawRealtimeUs / 1000) + "String_Node_Str"+ (rawUptimeUs / 1000));
    Log.d(TAG,"String_Node_Str" + (mBatteryRealtime / 1000) + "String_Node_Str"+ (mBatteryUptime / 1000));
    Log.d(TAG,"String_Node_Str" + (mTypeBatteryRealtime / 1000) + "String_Node_Str"+ (mTypeBatteryUptime / 1000));
  }
  mMinDrainedPower=(mStats.getLowDischargeAmountSinceCharge() * mPowerProfile.getBatteryCapacity()) / 100;
  mMaxDrainedPower=(mStats.getHighDischargeAmountSinceCharge() * mPowerProfile.getBatteryCapacity()) / 100;
  processAppUsage(asUsers);
  for (int i=0; i < mUsageList.size(); i++) {
    BatterySipper bs=mUsageList.get(i);
    bs.computeMobilemspp();
    if (bs.mobilemspp != 0) {
      mMobilemsppList.add(bs);
    }
  }
  for (int i=0; i < mUserSippers.size(); i++) {
    List<BatterySipper> user=mUserSippers.valueAt(i);
    for (int j=0; j < user.size(); j++) {
      BatterySipper bs=user.get(j);
      bs.computeMobilemspp();
      if (bs.mobilemspp != 0) {
        mMobilemsppList.add(bs);
      }
    }
  }
  Collections.sort(mMobilemsppList,new Comparator<BatterySipper>(){
    @Override public int compare(    BatterySipper lhs,    BatterySipper rhs){
      return Double.compare(rhs.mobilemspp,lhs.mobilemspp);
    }
  }
);
  processMiscUsage();
  Collections.sort(mUsageList);
  if (!mUsageList.isEmpty()) {
    mMaxRealPower=mMaxPower=mUsageList.get(0).totalPowerMah;
    final int usageListCount=mUsageList.size();
    for (int i=0; i < usageListCount; i++) {
      mComputedPower+=mUsageList.get(i).totalPowerMah;
    }
  }
  if (DEBUG) {
    Log.d(TAG,"String_Node_Str" + makemAh(mComputedPower) + "String_Node_Str"+ makemAh(mMinDrainedPower)+ "String_Node_Str"+ makemAh(mMaxDrainedPower));
  }
  mTotalPower=mComputedPower;
  if (mStats.getLowDischargeAmountSinceCharge() > 1) {
    if (mMinDrainedPower > mComputedPower) {
      double amount=mMinDrainedPower - mComputedPower;
      mTotalPower=mMinDrainedPower;
      BatterySipper bs=new BatterySipper(DrainType.UNACCOUNTED,null,amount);
      int index=Collections.binarySearch(mUsageList,bs);
      if (index < 0) {
        index=-(index + 1);
      }
      mUsageList.add(index,bs);
      mMaxPower=Math.max(mMaxPower,amount);
    }
 else     if (mMaxDrainedPower < mComputedPower) {
      double amount=mComputedPower - mMaxDrainedPower;
      BatterySipper bs=new BatterySipper(DrainType.OVERCOUNTED,null,amount);
      int index=Collections.binarySearch(mUsageList,bs);
      if (index < 0) {
        index=-(index + 1);
      }
      mUsageList.add(index,bs);
      mMaxPower=Math.max(mMaxPower,amount);
    }
  }
}

Fixed version (ground truth):
public void refreshStats(int statsType,SparseArray<UserHandle> asUsers,long rawRealtimeUs,long rawUptimeUs){
  getStats();
  mMaxPower=0;
  mMaxRealPower=0;
  mComputedPower=0;
  mTotalPower=0;
  mUsageList.clear();
  mWifiSippers.clear();
  mBluetoothSippers.clear();
  mUserSippers.clear();
  mMobilemsppList.clear();
  if (mStats == null) {
    return;
  }
  if (mCpuPowerCalculator == null) {
    mCpuPowerCalculator=new CpuPowerCalculator(mPowerProfile);
  }
  mCpuPowerCalculator.reset();
  if (mWakelockPowerCalculator == null) {
    mWakelockPowerCalculator=new WakelockPowerCalculator(mPowerProfile);
  }
  mWakelockPowerCalculator.reset();
  if (mMobileRadioPowerCalculator == null) {
    mMobileRadioPowerCalculator=new MobileRadioPowerCalculator(mPowerProfile,mStats);
  }
  mMobileRadioPowerCalculator.reset(mStats);
  if (mWifiPowerCalculator == null) {
    if (checkHasWifiPowerReporting(mStats,mPowerProfile)) {
      mWifiPowerCalculator=new WifiPowerCalculator(mPowerProfile);
    }
 else {
      mWifiPowerCalculator=new WifiPowerEstimator(mPowerProfile);
    }
  }
  mWifiPowerCalculator.reset();
  if (mBluetoothPowerCalculator == null) {
    mBluetoothPowerCalculator=new BluetoothPowerCalculator();
  }
  mBluetoothPowerCalculator.reset();
  if (mSensorPowerCalculator == null) {
    mSensorPowerCalculator=new SensorPowerCalculator(mPowerProfile,(SensorManager)mContext.getSystemService(Context.SENSOR_SERVICE));
  }
  mSensorPowerCalculator.reset();
  mStatsType=statsType;
  mRawUptime=rawUptimeUs;
  mRawRealtime=rawRealtimeUs;
  mBatteryUptime=mStats.getBatteryUptime(rawUptimeUs);
  mBatteryRealtime=mStats.getBatteryRealtime(rawRealtimeUs);
  mTypeBatteryUptime=mStats.computeBatteryUptime(rawUptimeUs,mStatsType);
  mTypeBatteryRealtime=mStats.computeBatteryRealtime(rawRealtimeUs,mStatsType);
  mBatteryTimeRemaining=mStats.computeBatteryTimeRemaining(rawRealtimeUs);
  mChargeTimeRemaining=mStats.computeChargeTimeRemaining(rawRealtimeUs);
  if (DEBUG) {
    Log.d(TAG,"String_Node_Str" + (rawRealtimeUs / 1000) + "String_Node_Str"+ (rawUptimeUs / 1000));
    Log.d(TAG,"String_Node_Str" + (mBatteryRealtime / 1000) + "String_Node_Str"+ (mBatteryUptime / 1000));
    Log.d(TAG,"String_Node_Str" + (mTypeBatteryRealtime / 1000) + "String_Node_Str"+ (mTypeBatteryUptime / 1000));
  }
  mMinDrainedPower=(mStats.getLowDischargeAmountSinceCharge() * mPowerProfile.getBatteryCapacity()) / 100;
  mMaxDrainedPower=(mStats.getHighDischargeAmountSinceCharge() * mPowerProfile.getBatteryCapacity()) / 100;
  processAppUsage(asUsers);
  for (int i=0; i < mUsageList.size(); i++) {
    BatterySipper bs=mUsageList.get(i);
    bs.computeMobilemspp();
    if (bs.mobilemspp != 0) {
      mMobilemsppList.add(bs);
    }
  }
  for (int i=0; i < mUserSippers.size(); i++) {
    List<BatterySipper> user=mUserSippers.valueAt(i);
    for (int j=0; j < user.size(); j++) {
      BatterySipper bs=user.get(j);
      bs.computeMobilemspp();
      if (bs.mobilemspp != 0) {
        mMobilemsppList.add(bs);
      }
    }
  }
  Collections.sort(mMobilemsppList,new Comparator<BatterySipper>(){
    @Override public int compare(    BatterySipper lhs,    BatterySipper rhs){
      return Double.compare(rhs.mobilemspp,lhs.mobilemspp);
    }
  }
);
  processMiscUsage();
  Collections.sort(mUsageList);
  if (!mUsageList.isEmpty()) {
    mMaxRealPower=mMaxPower=mUsageList.get(0).totalPowerMah;
    final int usageListCount=mUsageList.size();
    for (int i=0; i < usageListCount; i++) {
      mComputedPower+=mUsageList.get(i).totalPowerMah;
    }
  }
  if (DEBUG) {
    Log.d(TAG,"String_Node_Str" + makemAh(mComputedPower) + "String_Node_Str"+ makemAh(mMinDrainedPower)+ "String_Node_Str"+ makemAh(mMaxDrainedPower));
  }
  mTotalPower=mComputedPower;
  if (mStats.getLowDischargeAmountSinceCharge() > 1) {
    if (mMinDrainedPower > mComputedPower) {
      double amount=mMinDrainedPower - mComputedPower;
      mTotalPower=mMinDrainedPower;
      BatterySipper bs=new BatterySipper(DrainType.UNACCOUNTED,null,amount);
      int index=Collections.binarySearch(mUsageList,bs);
      if (index < 0) {
        index=-(index + 1);
      }
      mUsageList.add(index,bs);
      mMaxPower=Math.max(mMaxPower,amount);
    }
 else     if (mMaxDrainedPower < mComputedPower) {
      double amount=mComputedPower - mMaxDrainedPower;
      BatterySipper bs=new BatterySipper(DrainType.OVERCOUNTED,null,amount);
      int index=Collections.binarySearch(mUsageList,bs);
      if (index < 0) {
        index=-(index + 1);
      }
      mUsageList.add(index,bs);
      mMaxPower=Math.max(mMaxPower,amount);
    }
  }
}