mirror of
https://github.com/chararomandroid/android_device_samsung_a20
synced 2026-08-22 12:05:32 -04:00
universal7885: CameraLightSensor: Inital Test
Change-Id: I74b97bec8b5e173acda1939896835ac807b83c71 Signed-off-by: roynatech2544 <whiteshell2544@naver.com>
This commit is contained in:
parent
35976c4d3e
commit
e17ee16b93
9 changed files with 489 additions and 2 deletions
9
universal7885-common/apps/CameraLightSensor/Android.bp
Normal file
9
universal7885-common/apps/CameraLightSensor/Android.bp
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
android_app {
|
||||
name: "CameraLightSensor",
|
||||
srcs: [
|
||||
"src/**/*.java",
|
||||
],
|
||||
platform_apis: true,
|
||||
privileged: true,
|
||||
certificate: "platform",
|
||||
}
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.eurekateam.cameralightsensor"
|
||||
android:sharedUserId="android.uid.system">
|
||||
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
|
||||
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
|
||||
<uses-permission android:name="android.permission.CAMERA" />
|
||||
<uses-permission android:name="android.permission.RECORD_AUDIO" />
|
||||
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
|
||||
<uses-permission android:name="android.permission.CAMERA_OPEN_CLOSE_LISTENER"/>
|
||||
<uses-feature android:name="android.hardware.microphone" />
|
||||
<uses-feature android:name="android.hardware.camera" />
|
||||
<uses-feature android:name="android.hardware.camera2.full"/>
|
||||
<uses-feature android:name="android.hardware.camera.autofocus" />
|
||||
<protected-broadcast android:name="com.android.systemui.doze.pulse" />
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:label="@string/app_name"
|
||||
android:persistent="true">
|
||||
<receiver
|
||||
android:name=".BootReceiver"
|
||||
android:defaultToDeviceProtectedStorage="true"
|
||||
android:directBootAware="true"
|
||||
android:enabled="true"
|
||||
android:exported="true">
|
||||
<intent-filter android:priority="1000">
|
||||
<action android:name="android.intent.action.BOOT_COMPLETED" />
|
||||
<category android:name="android.intent.category.DEFAULT" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
<activity
|
||||
android:name=".CameraLightSensorService"
|
||||
android:exported="true">
|
||||
<intent-filter android:priority="1000">
|
||||
<action android:name="android.intent.action.SCREEN_OFF" />
|
||||
<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">
|
||||
</service>
|
||||
</application>
|
||||
</manifest>
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24"
|
||||
android:viewportHeight="24">
|
||||
<path
|
||||
android:fillColor="@android:color/black"
|
||||
android:pathData="M20,8.69L20,4h-4.69L12,0.69 8.69,4L4,4v4.69L0.69,12 4,15.31L4,20h4.69L12,23.31 15.31,20L20,20v-4.69L23.31,12 20,8.69zM18,14.48L18,18h-3.52L12,20.48 9.52,18L6,18v-3.52L3.52,12 6,9.52L6,6h3.52L12,3.52 14.48,6L18,6v3.52L20.48,12 18,14.48zM12,17c2.76,0 5,-2.24 5,-5s-2.24,-5 -5,-5v10z"/>
|
||||
</vector>
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
<resources>
|
||||
<string name="app_name">CameraLightSensor</string>
|
||||
</resources>
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
package com.eurekateam.cameralightsensor;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
|
||||
public class BootReceiver extends BroadcastReceiver {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
|
||||
if (context != null) {
|
||||
final Intent i = new Intent();
|
||||
i.setClassName("com.eurekateam.cameralightsensor",
|
||||
"com.eurekateam.cameralightsensor.CameraLightSensorService");
|
||||
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
context.startActivity(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,271 @@
|
|||
package com.eurekateam.cameralightsensor;
|
||||
|
||||
import static com.eurekateam.cameralightsensor.CameraLightSensorService.DEBUG;
|
||||
|
||||
import android.Manifest;
|
||||
import android.annotation.NonNull;
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationChannel;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.app.Service;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.ImageFormat;
|
||||
import android.hardware.camera2.CameraAccessException;
|
||||
import android.hardware.camera2.CameraCaptureSession;
|
||||
import android.hardware.camera2.CameraCharacteristics;
|
||||
import android.hardware.camera2.CameraDevice;
|
||||
import android.hardware.camera2.CameraManager;
|
||||
import android.hardware.camera2.CaptureRequest;
|
||||
import android.media.Image;
|
||||
import android.media.ImageReader;
|
||||
import android.os.IBinder;
|
||||
import android.provider.Settings;
|
||||
import android.util.Log;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Collections;
|
||||
|
||||
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;
|
||||
|
||||
protected CameraDevice.StateCallback cameraStateCallback = new CameraDevice.StateCallback() {
|
||||
@Override
|
||||
public void onOpened(@NonNull CameraDevice camera) {
|
||||
Log.d(TAG, "CameraDevice.StateCallback onOpened");
|
||||
cameraDevice = camera;
|
||||
actOnReadyCameraDevice();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisconnected(@NonNull CameraDevice camera) {
|
||||
Log.w(TAG, "CameraDevice.StateCallback onDisconnected");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(@NonNull CameraDevice camera, int error) {
|
||||
Log.e(TAG, "CameraDevice.StateCallback onError " + error);
|
||||
}
|
||||
};
|
||||
|
||||
protected CameraCaptureSession.StateCallback sessionStateCallback = new CameraCaptureSession.StateCallback() {
|
||||
|
||||
@Override
|
||||
public void onReady(CameraCaptureSession session) {
|
||||
if (!destroy && isavail) {
|
||||
Camera2Service.this.session = session;
|
||||
isavail = false;
|
||||
try {
|
||||
session.capture(createCaptureRequest(), null, null);
|
||||
} catch (Exception e){
|
||||
Log.e(TAG, "Camera is in use");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onConfigured(CameraCaptureSession session) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConfigureFailed(@NonNull CameraCaptureSession session) {
|
||||
}
|
||||
};
|
||||
|
||||
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();
|
||||
}
|
||||
try {
|
||||
session.stopRepeating();
|
||||
session.close();
|
||||
} catch (CameraAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
public void readyCamera() {
|
||||
CameraManager manager = (CameraManager) getSystemService(CAMERA_SERVICE);
|
||||
try {
|
||||
String pickedCamera = getCamera(manager);
|
||||
if (this.checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
|
||||
return;
|
||||
}
|
||||
manager.openCamera(pickedCamera, cameraStateCallback, null);
|
||||
imageReader = ImageReader.newInstance(720, 720, ImageFormat.JPEG, 2 /* images buffered */);
|
||||
imageReader.setOnImageAvailableListener(onImageAvailableListener, null);
|
||||
if (DEBUG) Log.d(TAG, "imageReader created");
|
||||
} catch (CameraAccessException e) {
|
||||
Log.e(TAG, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public String getCamera(CameraManager manager) {
|
||||
try {
|
||||
for (String cameraId : manager.getCameraIdList()) {
|
||||
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
|
||||
int cOrientation = characteristics.get(CameraCharacteristics.LENS_FACING);
|
||||
if (cOrientation == CAMERACHOICE) {
|
||||
return cameraId;
|
||||
}
|
||||
}
|
||||
} catch (CameraAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
public Notification PushNotification()
|
||||
{
|
||||
NotificationManager nm = (NotificationManager) mContext.getSystemService(NOTIFICATION_SERVICE);
|
||||
NotificationChannel channel = new NotificationChannel(
|
||||
mContext.getPackageName(), "CameraLightSensor",
|
||||
NotificationManager.IMPORTANCE_LOW
|
||||
);
|
||||
nm.createNotificationChannel(channel);
|
||||
Notification.Builder builder = new Notification.Builder(mContext);
|
||||
Intent notificationIntent = new Intent(mContext, Camera2Service.class);
|
||||
PendingIntent contentIntent = PendingIntent.getActivity(mContext,50,notificationIntent,PendingIntent.FLAG_IMMUTABLE);
|
||||
|
||||
//set
|
||||
builder.setContentIntent(contentIntent);
|
||||
builder.setSmallIcon(R.drawable.ic_brightness);
|
||||
builder.setContentText("Camera Light Sensor is running");
|
||||
builder.setContentTitle("Camera Light Sensor");
|
||||
builder.setChannelId(mContext.getPackageName());
|
||||
builder.setAutoCancel(true);
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
@Override
|
||||
public void onCreate() {
|
||||
if(DEBUG) Log.d(TAG, "onCreate service");
|
||||
mContext = getApplicationContext();
|
||||
startForeground(0, PushNotification());
|
||||
super.onCreate();
|
||||
}
|
||||
|
||||
public void actOnReadyCameraDevice() {
|
||||
try {
|
||||
cameraDevice.createCaptureSession(Collections.singletonList(imageReader.getSurface()), sessionStateCallback, null);
|
||||
} catch (CameraAccessException e) {
|
||||
Log.e(TAG, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
if (session != null && isavail) {
|
||||
try {
|
||||
session.abortCaptures();
|
||||
session.close();
|
||||
} catch (CameraAccessException e) {
|
||||
Log.e(TAG, e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
destroy = true;
|
||||
Log.i(TAG, "onDestory: Destory");
|
||||
stopForeground(true);
|
||||
}
|
||||
|
||||
private void processImage(Image image) throws Settings.SettingNotFoundException, InterruptedException {
|
||||
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
|
||||
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);
|
||||
AdjustBrightness(brightness);
|
||||
}
|
||||
|
||||
protected CaptureRequest createCaptureRequest() {
|
||||
try {
|
||||
CaptureRequest.Builder builder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
|
||||
builder.addTarget(imageReader.getSurface());
|
||||
return builder.build();
|
||||
} catch (CameraAccessException e) {
|
||||
Log.e(TAG, e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
public int calculateBrightnessEstimate(android.graphics.Bitmap bitmap, int pixelSpacing) {
|
||||
int R = 0; int G = 0; int B = 0;
|
||||
int height = bitmap.getHeight();
|
||||
int width = bitmap.getWidth();
|
||||
int n = 0;
|
||||
int[] pixels = new int[width * height];
|
||||
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
|
||||
for (int i = 0; i < pixels.length; i += pixelSpacing) {
|
||||
int color = pixels[i];
|
||||
R += Color.red(color);
|
||||
G += Color.green(color);
|
||||
B += Color.blue(color);
|
||||
n++;
|
||||
}
|
||||
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);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,102 @@
|
|||
package com.eurekateam.cameralightsensor;
|
||||
|
||||
import android.Manifest;
|
||||
import android.app.Activity;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.PowerManager;
|
||||
import android.provider.Settings;
|
||||
import android.util.Log;
|
||||
|
||||
|
||||
public class CameraLightSensorService extends Activity {
|
||||
private static final String TAG = "CameraLightSensor";
|
||||
static final boolean DEBUG = true;
|
||||
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);
|
||||
registerReceiver(mScreenStateReceiver, screenStateFilter);
|
||||
if (this.checkSelfPermission(Manifest.permission.CAMERA) ==
|
||||
PackageManager.PERMISSION_GRANTED) {
|
||||
Log.d(TAG, "Permission Granted");
|
||||
} else {
|
||||
// You can directly ask for the permission.
|
||||
this.requestPermissions(new String[] { Manifest.permission.CAMERA },
|
||||
1);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
if (DEBUG) Log.d(TAG, "Destroying service");
|
||||
super.onDestroy();
|
||||
this.unregisterReceiver(mScreenStateReceiver);
|
||||
}
|
||||
private final BroadcastReceiver mScreenStateReceiver = new BroadcastReceiver() {
|
||||
@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();
|
||||
|
||||
}
|
||||
}
|
||||
};
|
||||
private void onDisplayOn() throws Settings.SettingNotFoundException {
|
||||
if(Settings.System.getInt(getContentResolver(),
|
||||
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();
|
||||
}else{
|
||||
Log.d(TAG, "AutoBrightness Mode Disabled, Not Starting Service...");
|
||||
}
|
||||
}
|
||||
private void onDisplayOff(){
|
||||
if(DEBUG) Log.d(TAG, "Screen is off. Stopping Service...");
|
||||
i = new Intent(mContext, Camera2Service.class);
|
||||
mContext.stopService(i);
|
||||
}
|
||||
public static void BatteryOptimization(Context context){
|
||||
Intent intent = new Intent();
|
||||
String packageName = context.getPackageName();
|
||||
PowerManager pm = (PowerManager) context.getSystemService(POWER_SERVICE);
|
||||
if (!pm.isIgnoringBatteryOptimizations(packageName)) {
|
||||
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
|
||||
intent.setData(Uri.parse("package:" + packageName));
|
||||
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
context.startActivity(intent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,8 +1,7 @@
|
|||
android_app {
|
||||
name: "SamsungParts",
|
||||
srcs: [
|
||||
"src/*/*/*/*/*.java",
|
||||
"src/*/*/*/*.java",
|
||||
"src/**/*.java",
|
||||
],
|
||||
platform_apis: true,
|
||||
privileged: true,
|
||||
|
|
|
|||
Loading…
Reference in a new issue