Program: 199669
Project: 199669
Buggy source:
private void updatePowerState(){
  final boolean mustNotify;
  boolean mustInitialize=false;
  boolean updateAutoBrightness=mTwilightChanged;
  boolean wasDim=false;
  mTwilightChanged=false;
synchronized (mLock) {
    mPendingUpdatePowerStateLocked=false;
    if (mPendingRequestLocked == null) {
      return;
    }
    if (mPowerRequest == null) {
      mPowerRequest=new DisplayPowerRequest(mPendingRequestLocked);
      mWaitingForNegativeProximity=mPendingWaitForNegativeProximityLocked;
      mPendingWaitForNegativeProximityLocked=false;
      mPendingRequestChangedLocked=false;
      mustInitialize=true;
    }
 else     if (mPendingRequestChangedLocked) {
      if (mPowerRequest.screenAutoBrightnessAdjustment != mPendingRequestLocked.screenAutoBrightnessAdjustment) {
        updateAutoBrightness=true;
      }
      wasDim=(mPowerRequest.screenState == DisplayPowerRequest.SCREEN_STATE_DIM);
      mPowerRequest.copyFrom(mPendingRequestLocked);
      mWaitingForNegativeProximity|=mPendingWaitForNegativeProximityLocked;
      mPendingWaitForNegativeProximityLocked=false;
      mPendingRequestChangedLocked=false;
      mDisplayReadyLocked=false;
    }
    mustNotify=!mDisplayReadyLocked;
  }
  if (mustInitialize) {
    initialize();
  }
  if (mProximitySensor != null) {
    if (mPowerRequest.useProximitySensor && mPowerRequest.screenState != DisplayPowerRequest.SCREEN_STATE_OFF) {
      setProximitySensorEnabled(true);
      if (!mScreenOffBecauseOfProximity && mProximity == PROXIMITY_POSITIVE) {
        mScreenOffBecauseOfProximity=true;
        sendOnProximityPositiveWithWakelock();
        setScreenOn(false);
      }
    }
 else     if (mWaitingForNegativeProximity && mScreenOffBecauseOfProximity && mProximity == PROXIMITY_POSITIVE && mPowerRequest.screenState != DisplayPowerRequest.SCREEN_STATE_OFF) {
      setProximitySensorEnabled(true);
    }
 else {
      setProximitySensorEnabled(false);
      mWaitingForNegativeProximity=false;
    }
    if (mScreenOffBecauseOfProximity && mProximity != PROXIMITY_POSITIVE) {
      mScreenOffBecauseOfProximity=false;
      sendOnProximityNegativeWithWakelock();
    }
  }
 else {
    mWaitingForNegativeProximity=false;
  }
  if (mLightSensor != null) {
    setLightSensorEnabled(mPowerRequest.useAutoBrightness && wantScreenOn(mPowerRequest.screenState),updateAutoBrightness);
  }
  if (wantScreenOn(mPowerRequest.screenState)) {
    int target;
    boolean slow;
    if (mScreenAutoBrightness >= 0 && mLightSensorEnabled) {
      target=mScreenAutoBrightness;
      slow=mUsingScreenAutoBrightness;
      mUsingScreenAutoBrightness=true;
    }
 else {
      target=mPowerRequest.screenBrightness;
      slow=false;
      mUsingScreenAutoBrightness=false;
    }
    if (mPowerRequest.screenState == DisplayPowerRequest.SCREEN_STATE_DIM) {
      target=Math.min(target - SCREEN_DIM_MINIMUM_REDUCTION,mScreenBrightnessDimConfig);
      slow=false;
    }
 else     if (wasDim) {
      slow=false;
    }
    animateScreenBrightness(clampScreenBrightness(target),slow ? BRIGHTNESS_RAMP_RATE_SLOW : BRIGHTNESS_RAMP_RATE_FAST);
  }
 else {
    mUsingScreenAutoBrightness=false;
  }
  if (!mScreenOffBecauseOfProximity) {
    if (wantScreenOn(mPowerRequest.screenState)) {
      if (!mElectronBeamOffAnimator.isStarted()) {
        setScreenOn(true);
        if (mPowerRequest.blockScreenOn && mPowerState.getElectronBeamLevel() == 0.0f) {
          blockScreenOn();
        }
 else {
          unblockScreenOn();
          if (USE_ELECTRON_BEAM_ON_ANIMATION) {
            if (!mElectronBeamOnAnimator.isStarted()) {
              if (mPowerState.getElectronBeamLevel() == 1.0f) {
                mPowerState.dismissElectronBeam();
              }
 else               if (mPowerState.prepareElectronBeam(mElectronBeamFadesConfig ? ElectronBeam.MODE_FADE : ElectronBeam.MODE_WARM_UP)) {
                mElectronBeamOnAnimator.start();
              }
 else {
                mElectronBeamOnAnimator.end();
              }
            }
          }
 else {
            mPowerState.setElectronBeamLevel(1.0f);
            mPowerState.dismissElectronBeam();
          }
        }
      }
    }
 else {
      if (!mElectronBeamOnAnimator.isStarted()) {
        if (!mElectronBeamOffAnimator.isStarted()) {
          if (mPowerState.getElectronBeamLevel() == 0.0f) {
            setScreenOn(false);
          }
 else           if (mPowerState.prepareElectronBeam(mElectronBeamFadesConfig ? ElectronBeam.MODE_FADE : ElectronBeam.MODE_COOL_DOWN) && mPowerState.isScreenOn()) {
            mElectronBeamOffAnimator.start();
          }
 else {
            mElectronBeamOffAnimator.end();
          }
        }
      }
    }
  }
  if (mustNotify && !mScreenOnWasBlocked && !mElectronBeamOnAnimator.isStarted()&& !mElectronBeamOffAnimator.isStarted()&& mPowerState.waitUntilClean(mCleanListener)) {
synchronized (mLock) {
      if (!mPendingRequestChangedLocked) {
        mDisplayReadyLocked=true;
        if (DEBUG) {
          Slog.d(TAG,"String_Node_Str");
        }
      }
    }
    sendOnStateChangedWithWakelock();
  }
}

Fixed version (ground truth):
private void updatePowerState(){
  final boolean mustNotify;
  boolean mustInitialize=false;
  boolean updateAutoBrightness=mTwilightChanged;
  boolean wasDim=false;
  mTwilightChanged=false;
synchronized (mLock) {
    mPendingUpdatePowerStateLocked=false;
    if (mPendingRequestLocked == null) {
      return;
    }
    if (mPowerRequest == null) {
      mPowerRequest=new DisplayPowerRequest(mPendingRequestLocked);
      mWaitingForNegativeProximity=mPendingWaitForNegativeProximityLocked;
      mPendingWaitForNegativeProximityLocked=false;
      mPendingRequestChangedLocked=false;
      mustInitialize=true;
    }
 else     if (mPendingRequestChangedLocked) {
      if (mPowerRequest.screenAutoBrightnessAdjustment != mPendingRequestLocked.screenAutoBrightnessAdjustment) {
        updateAutoBrightness=true;
      }
      wasDim=(mPowerRequest.screenState == DisplayPowerRequest.SCREEN_STATE_DIM);
      mPowerRequest.copyFrom(mPendingRequestLocked);
      mWaitingForNegativeProximity|=mPendingWaitForNegativeProximityLocked;
      mPendingWaitForNegativeProximityLocked=false;
      mPendingRequestChangedLocked=false;
      mDisplayReadyLocked=false;
    }
    mustNotify=!mDisplayReadyLocked;
  }
  if (mustInitialize) {
    initialize();
  }
  if (mProximitySensor != null) {
    if (mPowerRequest.useProximitySensor && mPowerRequest.screenState != DisplayPowerRequest.SCREEN_STATE_OFF) {
      setProximitySensorEnabled(true);
      if (!mScreenOffBecauseOfProximity && mProximity == PROXIMITY_POSITIVE) {
        mScreenOffBecauseOfProximity=true;
        sendOnProximityPositiveWithWakelock();
      }
    }
 else     if (mWaitingForNegativeProximity && mScreenOffBecauseOfProximity && mProximity == PROXIMITY_POSITIVE && mPowerRequest.screenState != DisplayPowerRequest.SCREEN_STATE_OFF) {
      setProximitySensorEnabled(true);
    }
 else {
      setProximitySensorEnabled(false);
      mWaitingForNegativeProximity=false;
    }
    if (mScreenOffBecauseOfProximity && mProximity != PROXIMITY_POSITIVE) {
      mScreenOffBecauseOfProximity=false;
      sendOnProximityNegativeWithWakelock();
    }
  }
 else {
    mWaitingForNegativeProximity=false;
  }
  if (mLightSensor != null) {
    setLightSensorEnabled(mPowerRequest.useAutoBrightness && wantScreenOn(mPowerRequest.screenState),updateAutoBrightness);
  }
  if (wantScreenOn(mPowerRequest.screenState)) {
    int target;
    boolean slow;
    if (mScreenAutoBrightness >= 0 && mLightSensorEnabled) {
      target=mScreenAutoBrightness;
      slow=mUsingScreenAutoBrightness;
      mUsingScreenAutoBrightness=true;
    }
 else {
      target=mPowerRequest.screenBrightness;
      slow=false;
      mUsingScreenAutoBrightness=false;
    }
    if (mPowerRequest.screenState == DisplayPowerRequest.SCREEN_STATE_DIM) {
      target=Math.min(target - SCREEN_DIM_MINIMUM_REDUCTION,mScreenBrightnessDimConfig);
      slow=false;
    }
 else     if (wasDim) {
      slow=false;
    }
    animateScreenBrightness(clampScreenBrightness(target),slow ? BRIGHTNESS_RAMP_RATE_SLOW : BRIGHTNESS_RAMP_RATE_FAST);
  }
 else {
    mUsingScreenAutoBrightness=false;
  }
  if (mScreenOffBecauseOfProximity) {
    setScreenOn(false);
    unblockScreenOn();
  }
 else   if (wantScreenOn(mPowerRequest.screenState)) {
    if (!mElectronBeamOffAnimator.isStarted()) {
      setScreenOn(true);
      if (mPowerRequest.blockScreenOn && mPowerState.getElectronBeamLevel() == 0.0f) {
        blockScreenOn();
      }
 else {
        unblockScreenOn();
        if (USE_ELECTRON_BEAM_ON_ANIMATION) {
          if (!mElectronBeamOnAnimator.isStarted()) {
            if (mPowerState.getElectronBeamLevel() == 1.0f) {
              mPowerState.dismissElectronBeam();
            }
 else             if (mPowerState.prepareElectronBeam(mElectronBeamFadesConfig ? ElectronBeam.MODE_FADE : ElectronBeam.MODE_WARM_UP)) {
              mElectronBeamOnAnimator.start();
            }
 else {
              mElectronBeamOnAnimator.end();
            }
          }
        }
 else {
          mPowerState.setElectronBeamLevel(1.0f);
          mPowerState.dismissElectronBeam();
        }
      }
    }
  }
 else {
    unblockScreenOn();
    if (!mElectronBeamOnAnimator.isStarted()) {
      if (!mElectronBeamOffAnimator.isStarted()) {
        if (mPowerState.getElectronBeamLevel() == 0.0f) {
          setScreenOn(false);
        }
 else         if (mPowerState.prepareElectronBeam(mElectronBeamFadesConfig ? ElectronBeam.MODE_FADE : ElectronBeam.MODE_COOL_DOWN) && mPowerState.isScreenOn()) {
          mElectronBeamOffAnimator.start();
        }
 else {
          mElectronBeamOffAnimator.end();
        }
      }
    }
  }
  if (mustNotify && !mScreenOnWasBlocked && !mElectronBeamOnAnimator.isStarted()&& !mElectronBeamOffAnimator.isStarted()&& mPowerState.waitUntilClean(mCleanListener)) {
synchronized (mLock) {
      if (!mPendingRequestChangedLocked) {
        mDisplayReadyLocked=true;
        if (DEBUG) {
          Slog.d(TAG,"String_Node_Str");
        }
      }
    }
    sendOnStateChangedWithWakelock();
  }
}