• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 ZXing authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.zxing.client.android;
18 
19 import android.content.Context;
20 import android.content.SharedPreferences;
21 import android.hardware.Sensor;
22 import android.hardware.SensorEvent;
23 import android.hardware.SensorEventListener;
24 import android.hardware.SensorManager;
25 import android.preference.PreferenceManager;
26 import com.google.zxing.client.android.camera.CameraManager;
27 import com.google.zxing.client.android.camera.FrontLightMode;
28 
29 /**
30  * Detects ambient light and switches on the front light when very dark, and off again when sufficiently light.
31  *
32  * @author Sean Owen
33  * @author Nikolaus Huber
34  */
35 final class AmbientLightManager implements SensorEventListener {
36 
37   private static final float TOO_DARK_LUX = 45.0f;
38   private static final float BRIGHT_ENOUGH_LUX = 450.0f;
39 
40   private final Context context;
41   private CameraManager cameraManager;
42   private Sensor lightSensor;
43 
AmbientLightManager(Context context)44   AmbientLightManager(Context context) {
45     this.context = context;
46   }
47 
start(CameraManager cameraManager)48   void start(CameraManager cameraManager) {
49     this.cameraManager = cameraManager;
50     SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
51     if (FrontLightMode.readPref(sharedPrefs) == FrontLightMode.AUTO) {
52       SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
53       lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
54       if (lightSensor != null) {
55         sensorManager.registerListener(this, lightSensor, SensorManager.SENSOR_DELAY_NORMAL);
56       }
57     }
58   }
59 
stop()60   void stop() {
61     if (lightSensor != null) {
62       SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
63       sensorManager.unregisterListener(this);
64       cameraManager = null;
65       lightSensor = null;
66     }
67   }
68 
69   @Override
onSensorChanged(SensorEvent sensorEvent)70   public void onSensorChanged(SensorEvent sensorEvent) {
71     float ambientLightLux = sensorEvent.values[0];
72     if (cameraManager != null) {
73       if (ambientLightLux <= TOO_DARK_LUX) {
74         cameraManager.setTorch(true);
75       } else if (ambientLightLux >= BRIGHT_ENOUGH_LUX) {
76         cameraManager.setTorch(false);
77       }
78     }
79   }
80 
81   @Override
onAccuracyChanged(Sensor sensor, int accuracy)82   public void onAccuracyChanged(Sensor sensor, int accuracy) {
83     // do nothing
84   }
85 
86 }
87