From 876518a8561ed2c46abac8310bb50a10a0705e27 Mon Sep 17 00:00:00 2001 From: srowen Date: Tue, 20 Nov 2012 18:20:32 +0000 Subject: [PATCH] Avoid possible NPE from early callback git-svn-id: https://zxing.googlecode.com/svn/trunk@2528 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- .../zxing/client/android/AmbientLightManager.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/android/src/com/google/zxing/client/android/AmbientLightManager.java b/android/src/com/google/zxing/client/android/AmbientLightManager.java index ebebb627d..ffba63b4c 100644 --- a/android/src/com/google/zxing/client/android/AmbientLightManager.java +++ b/android/src/com/google/zxing/client/android/AmbientLightManager.java @@ -46,13 +46,13 @@ final class AmbientLightManager implements SensorEventListener { } void start(CameraManager cameraManager) { + this.cameraManager = cameraManager; SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); if (FrontLightMode.readPref(sharedPrefs) == FrontLightMode.AUTO) { SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); if (lightSensor != null) { sensorManager.registerListener(this, lightSensor, SensorManager.SENSOR_DELAY_NORMAL); - this.cameraManager = cameraManager; } } } @@ -69,10 +69,12 @@ final class AmbientLightManager implements SensorEventListener { @Override public void onSensorChanged(SensorEvent sensorEvent) { float ambientLightLux = sensorEvent.values[0]; - if (ambientLightLux <= TOO_DARK_LUX) { - cameraManager.setTorch(true); - } else if (ambientLightLux >= BRIGHT_ENOUGH_LUX) { - cameraManager.setTorch(false); + if (cameraManager != null) { + if (ambientLightLux <= TOO_DARK_LUX) { + cameraManager.setTorch(true); + } else if (ambientLightLux >= BRIGHT_ENOUGH_LUX) { + cameraManager.setTorch(false); + } } }