mirror of
https://github.com/chararomandroid/android_device_samsung_a20
synced 2026-08-21 11:55:31 -04:00
universal7885: CameraLightSensor: Finalize code
This commit is contained in:
parent
5649875a43
commit
aa12d2a377
8 changed files with 84 additions and 96 deletions
11
universal7885-common/apps/CameraLightSensor/AndroidManifest.xml
Normal file → Executable file
11
universal7885-common/apps/CameraLightSensor/AndroidManifest.xml
Normal file → Executable file
|
|
@ -38,17 +38,6 @@
|
|||
<action android:name="android.intent.action.SCREEN_ON" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<service
|
||||
android:name=".CameraLightSensorTile"
|
||||
android:enabled="true"
|
||||
android:icon="@drawable/ic_brightness"
|
||||
android:label="@string/app_name"
|
||||
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE"
|
||||
android:exported="true">
|
||||
<intent-filter>
|
||||
<action android:name="android.service.quicksettings.action.QS_TILE"/>
|
||||
</intent-filter>
|
||||
</service>
|
||||
<service
|
||||
android:name=".Camera2Service"
|
||||
android:foregroundServiceType="camera">
|
||||
|
|
|
|||
0
universal7885-common/apps/CameraLightSensor/res/drawable/ic_brightness.xml
Normal file → Executable file
0
universal7885-common/apps/CameraLightSensor/res/drawable/ic_brightness.xml
Normal file → Executable file
0
universal7885-common/apps/CameraLightSensor/res/values/strings.xml
Normal file → Executable file
0
universal7885-common/apps/CameraLightSensor/res/values/strings.xml
Normal file → Executable file
0
universal7885-common/apps/CameraLightSensor/src/com/eurekateam/cameralightsensor/BootReceiver.java
Normal file → Executable file
0
universal7885-common/apps/CameraLightSensor/src/com/eurekateam/cameralightsensor/BootReceiver.java
Normal file → Executable file
123
universal7885-common/apps/CameraLightSensor/src/com/eurekateam/cameralightsensor/Camera2Service.java
Normal file → Executable file
123
universal7885-common/apps/CameraLightSensor/src/com/eurekateam/cameralightsensor/Camera2Service.java
Normal file → Executable file
|
|
@ -24,23 +24,26 @@ import android.hardware.camera2.CameraManager;
|
|||
import android.hardware.camera2.CaptureRequest;
|
||||
import android.media.Image;
|
||||
import android.media.ImageReader;
|
||||
import android.os.Handler;
|
||||
import android.os.IBinder;
|
||||
import android.os.Looper;
|
||||
import android.provider.Settings;
|
||||
import android.util.Log;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Collections;
|
||||
import java.util.Objects;
|
||||
|
||||
public class Camera2Service extends Service {
|
||||
protected static final String TAG = Camera2Service.class.getSimpleName();
|
||||
protected static final int CAMERACHOICE = CameraCharacteristics.LENS_FACING_FRONT;
|
||||
protected static long cameraCaptureStartTime;
|
||||
boolean destroy;
|
||||
protected CameraDevice cameraDevice;
|
||||
protected CameraCaptureSession session;
|
||||
boolean isavail = true;
|
||||
protected ImageReader imageReader;
|
||||
private Context mContext;
|
||||
private CameraManager manager;
|
||||
|
||||
protected CameraDevice.StateCallback cameraStateCallback = new CameraDevice.StateCallback() {
|
||||
@Override
|
||||
|
|
@ -65,11 +68,20 @@ public class Camera2Service extends Service {
|
|||
|
||||
@Override
|
||||
public void onReady(CameraCaptureSession session) {
|
||||
if (!destroy && isavail) {
|
||||
if (!destroy) {
|
||||
Camera2Service.this.session = session;
|
||||
isavail = false;
|
||||
try {
|
||||
session.capture(createCaptureRequest(), null, null);
|
||||
Handler mHandler = new Handler(Looper.getMainLooper());
|
||||
mHandler.postDelayed(() -> {
|
||||
try {
|
||||
if (createCaptureRequest() == null) return;
|
||||
session.capture(createCaptureRequest(), captureCallback, null);
|
||||
} catch (CameraAccessException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalStateException e){
|
||||
Log.w(TAG, "onReady: Session is NULL");
|
||||
}
|
||||
},1500);
|
||||
} catch (Exception e){
|
||||
Log.e(TAG, "Camera is in use");
|
||||
e.printStackTrace();
|
||||
|
|
@ -88,43 +100,34 @@ public class Camera2Service extends Service {
|
|||
}
|
||||
};
|
||||
|
||||
protected ImageReader.OnImageAvailableListener onImageAvailableListener = new ImageReader.OnImageAvailableListener() {
|
||||
@Override
|
||||
public void onImageAvailable(ImageReader reader) {
|
||||
if (DEBUG) Log.d(TAG, "onImageAvailable: Capturing");
|
||||
Image img = reader.acquireLatestImage();
|
||||
cameraCaptureStartTime = System.currentTimeMillis();
|
||||
if (img != null) {
|
||||
try {
|
||||
processImage(img);
|
||||
} catch (Settings.SettingNotFoundException | InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
img.close();
|
||||
}
|
||||
protected ImageReader.OnImageAvailableListener onImageAvailableListener = reader -> {
|
||||
if (DEBUG) Log.d(TAG, "onImageAvailable: Capturing");
|
||||
Image img = reader.acquireLatestImage();
|
||||
if (img != null) {
|
||||
try {
|
||||
session.stopRepeating();
|
||||
session.close();
|
||||
} catch (CameraAccessException e) {
|
||||
processImage(img);
|
||||
} catch (Settings.SettingNotFoundException | InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
img.close();
|
||||
}
|
||||
};
|
||||
|
||||
public void readyCamera() {
|
||||
CameraManager manager = (CameraManager) getSystemService(CAMERA_SERVICE);
|
||||
manager = (CameraManager) getSystemService(CAMERA_SERVICE);
|
||||
try {
|
||||
String pickedCamera = getCamera(manager);
|
||||
if (this.checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
|
||||
return;
|
||||
}
|
||||
manager.registerAvailabilityCallback(availabilityCallback, null);
|
||||
manager.openCamera(pickedCamera, cameraStateCallback, null);
|
||||
imageReader = ImageReader.newInstance(720, 720, ImageFormat.JPEG, 2 /* images buffered */);
|
||||
imageReader = ImageReader.newInstance(500, 500, ImageFormat.JPEG, 2 /* images buffered */);
|
||||
imageReader.setOnImageAvailableListener(onImageAvailableListener, null);
|
||||
if (DEBUG) Log.d(TAG, "imageReader created");
|
||||
} catch (CameraAccessException e) {
|
||||
Log.e(TAG, e.getMessage());
|
||||
isavail = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -142,16 +145,14 @@ public class Camera2Service extends Service {
|
|||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
if(DEBUG) Log.d(TAG, "onStartCommand flags " + flags + " startId " + startId);
|
||||
readyCamera();
|
||||
destroy = false;
|
||||
isavail = true;
|
||||
cameraCaptureStartTime = System.currentTimeMillis();
|
||||
startForeground(50, PushNotification());
|
||||
return START_STICKY;
|
||||
readyCamera();
|
||||
return START_NOT_STICKY;
|
||||
}
|
||||
|
||||
public Notification PushNotification()
|
||||
|
|
@ -180,7 +181,8 @@ public class Camera2Service extends Service {
|
|||
public void onCreate() {
|
||||
if(DEBUG) Log.d(TAG, "onCreate service");
|
||||
mContext = getApplicationContext();
|
||||
startForeground(0, PushNotification());
|
||||
startForeground(50, PushNotification());
|
||||
readyCamera();
|
||||
super.onCreate();
|
||||
}
|
||||
|
||||
|
|
@ -200,11 +202,14 @@ public class Camera2Service extends Service {
|
|||
session.close();
|
||||
} catch (CameraAccessException e) {
|
||||
Log.e(TAG, e.getMessage());
|
||||
} catch (IllegalStateException e2){
|
||||
Log.e(TAG, "Session Already Closed");
|
||||
}
|
||||
|
||||
}
|
||||
manager.unregisterAvailabilityCallback(availabilityCallback);
|
||||
isavail = false;
|
||||
destroy = true;
|
||||
Log.i(TAG, "onDestory: Destory");
|
||||
stopForeground(true);
|
||||
}
|
||||
|
||||
|
|
@ -213,14 +218,13 @@ public class Camera2Service extends Service {
|
|||
byte[] bytes = new byte[buffer.capacity()];
|
||||
buffer.get(bytes);
|
||||
Bitmap bitmapImage = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, null);
|
||||
int brightness = calculateBrightnessEstimate(bitmapImage,10);
|
||||
Log.i(TAG, "brightness: " + brightness);
|
||||
int brightness = calculateBrightnessEstimate(bitmapImage,5);
|
||||
AdjustBrightness(brightness);
|
||||
}
|
||||
|
||||
protected CaptureRequest createCaptureRequest() {
|
||||
try {
|
||||
CaptureRequest.Builder builder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
|
||||
CaptureRequest.Builder builder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
|
||||
builder.addTarget(imageReader.getSurface());
|
||||
return builder.build();
|
||||
} catch (CameraAccessException e) {
|
||||
|
|
@ -228,12 +232,39 @@ public class Camera2Service extends Service {
|
|||
return null;
|
||||
}
|
||||
}
|
||||
CameraManager.AvailabilityCallback availabilityCallback = new CameraManager.AvailabilityCallback() {
|
||||
@Override
|
||||
public void onCameraOpened(String cameraId, String packageId) {
|
||||
super.onCameraOpened(cameraId, packageId);
|
||||
Log.i(TAG, "CameraManager.AvailabilityCallback: Camera " + cameraId
|
||||
+ " Opened by Package " + packageId);
|
||||
isavail = Objects.equals(packageId, mContext.getBasePackageName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCameraClosed(String cameraId) {
|
||||
if(!isavail){
|
||||
Log.i(TAG, "CameraManager.AvailabilityCallback: Camera " + cameraId + " Closed." +
|
||||
"Re-opening Camera");
|
||||
readyCamera();
|
||||
}
|
||||
super.onCameraClosed(cameraId);
|
||||
}
|
||||
};
|
||||
CameraCaptureSession.CaptureCallback captureCallback = new CameraCaptureSession.CaptureCallback() {
|
||||
@Override
|
||||
public void onCaptureSequenceCompleted(CameraCaptureSession session, int sequenceId, long frameNumber) {
|
||||
if (DEBUG) Log.d(TAG, "captureCallback: Closing Session");
|
||||
Handler mHandler = new Handler(Looper.getMainLooper());
|
||||
mHandler.postDelayed(() -> readyCamera(),400);
|
||||
super.onCaptureSequenceCompleted(session, sequenceId, frameNumber);
|
||||
}
|
||||
};
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
public int calculateBrightnessEstimate(android.graphics.Bitmap bitmap, int pixelSpacing) {
|
||||
public int calculateBrightnessEstimate(Bitmap bitmap, int pixelSpacing) {
|
||||
int R = 0; int G = 0; int B = 0;
|
||||
int height = bitmap.getHeight();
|
||||
int width = bitmap.getWidth();
|
||||
|
|
@ -249,23 +280,17 @@ public class Camera2Service extends Service {
|
|||
}
|
||||
return (R + B + G) / (n * 3);
|
||||
}
|
||||
private void AdjustBrightness(int brightness) throws Settings.SettingNotFoundException, InterruptedException {
|
||||
if(DEBUG) Log.i(TAG, "AdjustBrightness: Received Brightness Value " + brightness);
|
||||
private void AdjustBrightness(int brightness) throws Settings.SettingNotFoundException {
|
||||
if (DEBUG) Log.i(TAG, "AdjustBrightness: Received Brightness Value " + brightness);
|
||||
int oldbrightness = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
|
||||
if (DEBUG) Log.i(TAG, "AdjustBrightness: Oldval = " + oldbrightness + " Newval = " +
|
||||
brightness + " Adjusting..");
|
||||
if(oldbrightness > brightness){
|
||||
while (oldbrightness > brightness){
|
||||
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightness);
|
||||
brightness -= 5;
|
||||
Thread.sleep(500);
|
||||
}
|
||||
}else{
|
||||
while (oldbrightness < brightness){
|
||||
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightness);
|
||||
brightness += 5;
|
||||
Thread.sleep(500);
|
||||
}
|
||||
}
|
||||
int newbrightness = brightness;
|
||||
if (newbrightness > 255){
|
||||
newbrightness = 255;
|
||||
}else if (newbrightness < 0){
|
||||
newbrightness = 0;
|
||||
}
|
||||
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, newbrightness);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
26
universal7885-common/apps/CameraLightSensor/src/com/eurekateam/cameralightsensor/CameraLightSensorService.java
Normal file → Executable file
26
universal7885-common/apps/CameraLightSensor/src/com/eurekateam/cameralightsensor/CameraLightSensorService.java
Normal file → Executable file
|
|
@ -9,27 +9,30 @@ import android.content.IntentFilter;
|
|||
import android.content.pm.PackageManager;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.os.PowerManager;
|
||||
import android.provider.Settings;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.internal.graphics.cam.Cam;
|
||||
|
||||
|
||||
public class CameraLightSensorService extends Activity {
|
||||
private static final String TAG = "CameraLightSensor";
|
||||
static final boolean DEBUG = true;
|
||||
static final boolean DEBUG = false;
|
||||
Intent i;
|
||||
private boolean stop;
|
||||
private Context mContext;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
moveTaskToBack(true);
|
||||
mContext = getApplicationContext();
|
||||
i = new Intent(mContext, Camera2Service.class);
|
||||
IntentFilter screenStateFilter = new IntentFilter(Intent.ACTION_SCREEN_ON);
|
||||
screenStateFilter.addAction(Intent.ACTION_SCREEN_OFF);
|
||||
BatteryOptimization(mContext);
|
||||
i = new Intent(mContext, Camera2Service.class);
|
||||
mContext.startForegroundService(i);
|
||||
registerReceiver(mScreenStateReceiver, screenStateFilter);
|
||||
if (this.checkSelfPermission(Manifest.permission.CAMERA) ==
|
||||
PackageManager.PERMISSION_GRANTED) {
|
||||
|
|
@ -50,16 +53,13 @@ public class CameraLightSensorService extends Activity {
|
|||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
|
||||
stop = false;
|
||||
try {
|
||||
onDisplayOn();
|
||||
} catch (Settings.SettingNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
|
||||
stop = true;
|
||||
onDisplayOff();
|
||||
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
@ -68,17 +68,7 @@ public class CameraLightSensorService extends Activity {
|
|||
Settings.System.SCREEN_BRIGHTNESS_MODE ) == 1){
|
||||
if(DEBUG) Log.d(TAG, "AutoBrightness Mode Enabled. Starting...");
|
||||
i = new Intent(mContext, Camera2Service.class);
|
||||
Thread th = new Thread(() -> {
|
||||
do {
|
||||
mContext.startForegroundService(i);
|
||||
try {
|
||||
Thread.sleep(4000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} while (!stop);
|
||||
});
|
||||
th.start();
|
||||
mContext.startForegroundService(i);
|
||||
}else{
|
||||
Log.d(TAG, "AutoBrightness Mode Disabled, Not Starting Service...");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,17 +0,0 @@
|
|||
package com.eurekateam.cameralightsensor;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.service.quicksettings.TileService;
|
||||
|
||||
public class CameraLightSensorTile extends TileService {
|
||||
@Override
|
||||
public void onClick() {
|
||||
try {
|
||||
Intent intent = new Intent(this, CameraLightSensorService.class);
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
startActivity(intent);
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -54,7 +54,8 @@ TARGET_SCREEN_WIDTH := 720
|
|||
|
||||
# Camera
|
||||
PRODUCT_PACKAGES += \
|
||||
android.hardware.camera.provider@2.5-service
|
||||
android.hardware.camera.provider@2.5-service \
|
||||
CameraLightSensor
|
||||
|
||||
# DRM
|
||||
PRODUCT_PACKAGES += \
|
||||
|
|
|
|||
Loading…
Reference in a new issue