Program: 199680
Project: 199680
Buggy source:
private void updatePowerState(){
  final boolean mustNotify;
  boolean mustInitialize=false;
  boolean updateAutoBrightness=mTwilightChanged;
  boolean wasDimOrDoze=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;
      }
      wasDimOrDoze=(mPowerRequest.screenState == DisplayPowerRequest.SCREEN_STATE_DIM || mPowerRequest.screenState == DisplayPowerRequest.SCREEN_STATE_DOZE);
      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.wantLightSensorEnabled(),updateAutoBrightness);
  }
  if (mPowerRequest.wantScreenOnAny()) {
    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_DOZE) {
      target=mScreenBrightnessDozeConfig;
      slow=false;
    }
 else     if (mPowerRequest.screenState == DisplayPowerRequest.SCREEN_STATE_DIM) {
      target=Math.min(target - SCREEN_DIM_MINIMUM_REDUCTION,mScreenBrightnessDimConfig);
      slow=false;
    }
 else     if (wasDimOrDoze) {
      slow=false;
    }
    animateScreenBrightness(clampScreenBrightness(target),slow ? BRIGHTNESS_RAMP_RATE_SLOW : BRIGHTNESS_RAMP_RATE_FAST);
  }
 else {
    mUsingScreenAutoBrightness=false;
  }
  if (!mScreenOffBecauseOfProximity) {
    if (mPowerRequest.wantScreenOnAny()) {
      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 wasDimOrDoze=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;
      }
      wasDimOrDoze=(mPowerRequest.screenState == DisplayPowerRequest.SCREEN_STATE_DIM || mPowerRequest.screenState == DisplayPowerRequest.SCREEN_STATE_DOZE);
      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.wantLightSensorEnabled(),updateAutoBrightness);
  }
  if (mPowerRequest.wantScreenOnAny()) {
    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_DOZE) {
      target=mScreenBrightnessDozeConfig;
      slow=false;
    }
 else     if (mPowerRequest.screenState == DisplayPowerRequest.SCREEN_STATE_DIM) {
      target=Math.min(target - SCREEN_DIM_MINIMUM_REDUCTION,mScreenBrightnessDimConfig);
      slow=false;
    }
 else     if (wasDimOrDoze) {
      slow=false;
    }
    animateScreenBrightness(clampScreenBrightness(target),slow ? BRIGHTNESS_RAMP_RATE_SLOW : BRIGHTNESS_RAMP_RATE_FAST);
  }
 else {
    mUsingScreenAutoBrightness=false;
  }
  if (mScreenOffBecauseOfProximity) {
    setScreenOn(false);
    unblockScreenOn();
  }
 else   if (mPowerRequest.wantScreenOnAny()) {
    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();
  }
}