universal7885: CameraLightSensor: Correctly monitor brightness settings

* Added an observer
This commit is contained in:
roynatech2544 2021-11-03 22:11:45 +09:00
commit 0d81c5b97d
No known key found for this signature in database
GPG key ID: 9675C32163D88D30
7 changed files with 111 additions and 102 deletions

View file

@ -6,4 +6,5 @@ android_app {
platform_apis: true, platform_apis: true,
privileged: true, privileged: true,
certificate: "platform", certificate: "platform",
static_libs: [ "androidx.core_core" ],
} }

View file

@ -2,8 +2,8 @@ package com.eurekateam.cameralightsensor;
import static com.eurekateam.cameralightsensor.CameraLightSensorService.DEBUG; import static com.eurekateam.cameralightsensor.CameraLightSensorService.DEBUG;
import android.Manifest;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.annotation.SuppressLint;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationChannel; import android.app.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
@ -11,7 +11,6 @@ import android.app.PendingIntent;
import android.app.Service; import android.app.Service;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.graphics.Color; import android.graphics.Color;
@ -27,10 +26,11 @@ import android.media.ImageReader;
import android.os.Handler; import android.os.Handler;
import android.os.IBinder; import android.os.IBinder;
import android.os.Looper; import android.os.Looper;
import android.os.SystemClock;
import android.provider.Settings; import android.provider.Settings;
import android.util.Log; import android.util.Log;
import androidx.core.app.NotificationCompat;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.Collections; import java.util.Collections;
@ -44,6 +44,8 @@ public class Camera2Service extends Service {
protected ImageReader imageReader; protected ImageReader imageReader;
private Context mContext; private Context mContext;
private CameraManager manager; private CameraManager manager;
private long mCaptureTime;
private final int DELAY = 5000;
protected CameraDevice.StateCallback cameraStateCallback = new CameraDevice.StateCallback() { protected CameraDevice.StateCallback cameraStateCallback = new CameraDevice.StateCallback() {
@Override @Override
@ -71,16 +73,16 @@ public class Camera2Service extends Service {
if (!destroy) { if (!destroy) {
Camera2Service.this.session = session; Camera2Service.this.session = session;
try { try {
if (createCaptureRequest() == null) return;
try { try {
if (createCaptureRequest() == null) return;
session.capture(createCaptureRequest(), captureCallback, null); session.capture(createCaptureRequest(), captureCallback, null);
} catch (CameraAccessException e) { } catch (CameraAccessException e) {
e.printStackTrace(); e.printStackTrace();
} catch (IllegalStateException e){ } catch (IllegalStateException e) {
Log.w(TAG, "onReady: Session is NULL"); Log.w(TAG, "onReady: Session is NULL");
} }
} catch (Exception e){ } catch (Exception e) {
Log.e(TAG, "Camera is in use"); Log.e(TAG, "Camera is in use");
e.printStackTrace(); e.printStackTrace();
} }
} }
@ -115,13 +117,12 @@ public class Camera2Service extends Service {
} }
}; };
@SuppressLint("MissingPermission")
public void readyCamera() { public void readyCamera() {
manager = (CameraManager) getSystemService(CAMERA_SERVICE); manager = (CameraManager) getSystemService(CAMERA_SERVICE);
mCaptureTime = System.currentTimeMillis();
try { try {
String pickedCamera = getCamera(manager); String pickedCamera = getCamera(manager);
if (this.checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
return;
}
manager.registerAvailabilityCallback(availabilityCallback, null); manager.registerAvailabilityCallback(availabilityCallback, null);
manager.openCamera(pickedCamera, cameraStateCallback, null); manager.openCamera(pickedCamera, cameraStateCallback, null);
imageReader = ImageReader.newInstance(250, 250, ImageFormat.JPEG, 2 /* images buffered */); imageReader = ImageReader.newInstance(250, 250, ImageFormat.JPEG, 2 /* images buffered */);
@ -161,24 +162,25 @@ public class Camera2Service extends Service {
{ {
NotificationManager nm = (NotificationManager) mContext.getSystemService(NOTIFICATION_SERVICE); NotificationManager nm = (NotificationManager) mContext.getSystemService(NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel( NotificationChannel channel = new NotificationChannel(
mContext.getPackageName(), "CameraLightSensor", mContext.getBasePackageName(), "CameraLightSensor",
NotificationManager.IMPORTANCE_LOW NotificationManager.IMPORTANCE_LOW
); );
nm.createNotificationChannel(channel); nm.createNotificationChannel(channel);
Notification.Builder builder = new Notification.Builder(mContext); NotificationCompat.Builder builder =
new NotificationCompat.Builder(mContext, mContext.getBasePackageName());
Intent notificationIntent = new Intent(mContext, Camera2Service.class); Intent notificationIntent = new Intent(mContext, Camera2Service.class);
PendingIntent contentIntent = PendingIntent.getActivity(mContext,50,notificationIntent,PendingIntent.FLAG_IMMUTABLE); PendingIntent contentIntent = PendingIntent.getActivity(mContext,50,
notificationIntent,PendingIntent.FLAG_IMMUTABLE);
//set //set
builder.setContentIntent(contentIntent); builder.setContentIntent(contentIntent);
builder.setSmallIcon(R.drawable.ic_brightness); builder.setSmallIcon(R.drawable.ic_brightness);
builder.setContentText("Camera Light Sensor is running"); builder.setContentTitle("Camera Light Sensor Service");
builder.setContentTitle("Camera Light Sensor"); builder.setChannelId(mContext.getBasePackageName());
builder.setChannelId(mContext.getPackageName());
builder.setAutoCancel(true);
return builder.build(); return builder.build();
} }
@Override @Override
public void onCreate() { public void onCreate() {
if(DEBUG) Log.d(TAG, "onCreate service"); if(DEBUG) Log.d(TAG, "onCreate service");
@ -190,7 +192,7 @@ public class Camera2Service extends Service {
public void actOnReadyCameraDevice() { public void actOnReadyCameraDevice() {
try { try {
cameraDevice.createCaptureSession(Collections.singletonList(imageReader.getSurface()), sessionStateCallback, null); cameraDevice.createCaptureSession(Collections.singletonList(imageReader.getSurface()), sessionStateCallback, null);
} catch (CameraAccessException e) { } catch (CameraAccessException e) {
Log.e(TAG, e.getMessage()); Log.e(TAG, e.getMessage());
} }
@ -227,7 +229,7 @@ public class Camera2Service extends Service {
protected CaptureRequest createCaptureRequest() { protected CaptureRequest createCaptureRequest() {
try { try {
CaptureRequest.Builder builder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); CaptureRequest.Builder builder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
builder.addTarget(imageReader.getSurface()); builder.addTarget(imageReader.getSurface());
return builder.build(); return builder.build();
} catch (CameraAccessException e) { } catch (CameraAccessException e) {
@ -244,37 +246,47 @@ public class Camera2Service extends Service {
if (packageId.equals(mContext.getBasePackageName())) return; if (packageId.equals(mContext.getBasePackageName())) return;
isavail = false; isavail = false;
} }
@Override
public void onCameraClosed(String cameraId) {
if (DEBUG) Log.d(TAG, "CameraManager.AvailabilityCallback: Camera is closed now, " +
"sleeping for 4000 ms");
Handler mHandler = new Handler(Looper.getMainLooper());
mHandler.postDelayed(() -> {
if(isavail) readyCamera();
},4000);
super.onCameraClosed(cameraId);
}
@Override @Override
public void onCameraUnavailable(String cameraId) { public void onCameraUnavailable(String cameraId) {
if (DEBUG)Log.i(TAG, "CameraManager.AvailabilityCallback : Camera NOT Available. "); if (DEBUG) Log.i(TAG, "CameraManager.AvailabilityCallback : Camera NOT Available. ");
isavail = false;
super.onCameraUnavailable(cameraId); super.onCameraUnavailable(cameraId);
} }
@Override @Override
public void onCameraAvailable(String cameraId) { public void onCameraAvailable(String cameraId) {
if (DEBUG) Log.i(TAG, "CameraManager.AvailabilityCallback : Camera IS Available. "); if (DEBUG) Log.i(TAG, "CameraManager.AvailabilityCallback : Camera IS Available. ");
isavail = true;
super.onCameraAvailable(cameraId); super.onCameraAvailable(cameraId);
} }
@Override
public void onCameraClosed(String cameraId) {
super.onCameraClosed(cameraId);
while (mCaptureTime + DELAY >= System.currentTimeMillis() && isavail) {
try {
Thread.sleep(1000);
if (DEBUG) Log.i(TAG, "CameraManager.AvailabilityCallback: " +
"Thread: Waiting...");
isavail = false;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
isavail = true;
Handler mHandler = new Handler(Looper.getMainLooper());
mHandler.postDelayed(() -> {
if(isavail) readyCamera();
},DELAY);
}
}; };
CameraCaptureSession.CaptureCallback captureCallback = new CameraCaptureSession.CaptureCallback() { CameraCaptureSession.CaptureCallback captureCallback = new CameraCaptureSession.CaptureCallback() {
@Override @Override
public void onCaptureSequenceCompleted(CameraCaptureSession session, int sequenceId, long frameNumber) { public void onCaptureSequenceCompleted(CameraCaptureSession session, int sequenceId, long frameNumber) {
if (DEBUG) Log.d(TAG, "captureCallback: Closing Session"); if (DEBUG) Log.d(TAG, "captureCallback: Closing Session");
Handler mHandler = new Handler(Looper.getMainLooper());
mHandler.postDelayed(() -> readyCamera(),400);
super.onCaptureSequenceCompleted(session, sequenceId, frameNumber); super.onCaptureSequenceCompleted(session, sequenceId, frameNumber);
cameraDevice.close();
session.close();
} }
}; };
@ -305,12 +317,12 @@ public class Camera2Service extends Service {
int oldbrightness = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS); int oldbrightness = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
if (DEBUG) Log.i(TAG, "AdjustBrightness: Oldval = " + oldbrightness + " Newval = " + if (DEBUG) Log.i(TAG, "AdjustBrightness: Oldval = " + oldbrightness + " Newval = " +
brightness + " Adjusting.."); brightness + " Adjusting..");
int newbrightness = brightness; int newbrightness = brightness;
if (newbrightness > 255){ if (newbrightness > 255){
newbrightness = 255; newbrightness = 255;
}else if (newbrightness < 0){ }else if (newbrightness < 0){
newbrightness = 0; newbrightness = 0;
} }
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, newbrightness); Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, newbrightness);
} }
} }

View file

@ -3,10 +3,12 @@ package com.eurekateam.cameralightsensor;
import android.Manifest; import android.Manifest;
import android.app.Activity; import android.app.Activity;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.database.ContentObserver;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
@ -20,18 +22,20 @@ public class CameraLightSensorService extends Activity {
private static final String TAG = "CameraLightSensor"; private static final String TAG = "CameraLightSensor";
static final boolean DEBUG = false; static final boolean DEBUG = false;
Intent i; Intent i;
IntentFilter screenStateFilter;
private Context mContext; private Context mContext;
public ContentResolver contentResolver;
private boolean mRegistered;
private boolean mServiceStarted;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
moveTaskToBack(true); moveTaskToBack(true);
mContext = getApplicationContext(); mContext = getApplicationContext();
IntentFilter screenStateFilter = new IntentFilter(Intent.ACTION_SCREEN_ON); contentResolver = getContentResolver();
screenStateFilter.addAction(Intent.ACTION_SCREEN_OFF);
BatteryOptimization(mContext); BatteryOptimization(mContext);
i = new Intent(mContext, Camera2Service.class); i = new Intent(mContext, Camera2Service.class);
mContext.startForegroundService(i);
registerReceiver(mScreenStateReceiver, screenStateFilter);
if (this.checkSelfPermission(Manifest.permission.CAMERA) == if (this.checkSelfPermission(Manifest.permission.CAMERA) ==
PackageManager.PERMISSION_GRANTED) { PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "Permission Granted"); Log.d(TAG, "Permission Granted");
@ -40,36 +44,70 @@ public class CameraLightSensorService extends Activity {
this.requestPermissions(new String[] { Manifest.permission.CAMERA }, this.requestPermissions(new String[] { Manifest.permission.CAMERA },
1); 1);
} }
mRegistered = false;
screenStateFilter = new IntentFilter(Intent.ACTION_SCREEN_ON);
screenStateFilter.addAction(Intent.ACTION_SCREEN_OFF);
try {
if(Settings.System.getInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS_MODE)
== Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC){
registerReceiver(mScreenStateReceiver, screenStateFilter);
mRegistered = true;
}
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
Uri setting = Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS_MODE);
contentResolver.registerContentObserver(setting, false, observer);
} }
@Override @Override
public void onDestroy() { public void onDestroy() {
if (DEBUG) Log.d(TAG, "Destroying service"); if (DEBUG) Log.d(TAG, "Destroying service");
mRegistered = false;
contentResolver.unregisterContentObserver(observer);
super.onDestroy(); super.onDestroy();
} }
private final BroadcastReceiver mScreenStateReceiver = new BroadcastReceiver() { private final BroadcastReceiver mScreenStateReceiver = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
try { onDisplayOn();
onDisplayOn(); mServiceStarted = true;
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
onDisplayOff(); if (mServiceStarted) onDisplayOff();
mServiceStarted = false;
} }
} }
}; };
private void onDisplayOn() throws Settings.SettingNotFoundException { // Make a listener
if(Settings.System.getInt(getContentResolver(), ContentObserver observer = new ContentObserver(new Handler(Looper.getMainLooper())) {
Settings.System.SCREEN_BRIGHTNESS_MODE ) == @Override
Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC){ public void onChange(boolean selfChange) {
if(DEBUG) Log.d(TAG, "AutoBrightness Mode Enabled. Starting..."); super.onChange(selfChange);
i = new Intent(mContext, Camera2Service.class); Log.i(TAG, "observer: Brightness Settings Changed");
mContext.startForegroundService(i); try {
}else{ if(Settings.System.getInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS_MODE)
Log.d(TAG, "AutoBrightness Mode Disabled, Not Starting Service..."); == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC){
registerReceiver(mScreenStateReceiver, screenStateFilter);
mRegistered = true;
mContext.startForegroundService(i);
}else{
if(mRegistered) unregisterReceiver(mScreenStateReceiver);
mRegistered = false;
mContext.stopService(i);
}
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
} }
@Override
public boolean deliverSelfNotifications() {
return true;
}
};
private void onDisplayOn() {
if(DEBUG) Log.d(TAG, "Screen is on. Starting Service...");
mContext.startForegroundService(i);
} }
private void onDisplayOff(){ private void onDisplayOff(){
if(DEBUG) Log.d(TAG, "Screen is off. Stopping Service..."); if(DEBUG) Log.d(TAG, "Screen is off. Stopping Service...");

View file

@ -24,6 +24,8 @@ mdc. u:object_r:exported_config_prop:s0
persist.sys.omc_support u:object_r:exported_config_prop:s0 persist.sys.omc_support u:object_r:exported_config_prop:s0
persist.sys.omc_etcpath u:object_r:exported_config_prop:s0 persist.sys.omc_etcpath u:object_r:exported_config_prop:s0
ro.omc. u:object_r:exported_config_prop:s0 ro.omc. u:object_r:exported_config_prop:s0
mdc.sys.enable_smff0 u:object_r:exported_config_prop:s0
ro.csc.countryiso_code u:object_r:exported_config_prop:s0
### RADIO ### RADIO
ril.NwNmId u:object_r:telephony_config_prop:s0 ril.NwNmId u:object_r:telephony_config_prop:s0

View file

@ -1,38 +0,0 @@
type lhd, domain;
type lhd_exec, exec_type, vendor_file_type, file_type;
# lhd is started by init, type transit from init domain to lhd domain
init_daemon_domain(lhd)
get_prop(lhd, hwservicemanager_prop)
hwbinder_use(lhd)
allow lhd system_suspend_hwservice:hwservice_manager { find };
binder_call(lhd, system_suspend_server)
allow lhd self:tcp_socket create_stream_socket_perms;
allow lhd port:tcp_socket { name_bind name_connect };
allow lhd node:tcp_socket node_bind;
# /data/vendor/gps
allow lhd gps_vendor_data_file:dir rw_dir_perms;
allow lhd gps_vendor_data_file:file create_file_perms;
allow lhd gps_vendor_data_file:fifo_file create_file_perms;
# /dev/bbd_control
allow lhd bbd_device:chr_file rw_file_perms;
# /dev/socket/fwmarkd
allow lhd fwmarkd_socket:sock_file write;
# /sys/bbd/lk_enable
r_dir_file(lhd, sysfs_bbd)
# /sys/class/sec/gps
allow lhd sysfs_sec_gps:dir r_dir_perms;
allow lhd sysfs_sec_gps:file r_file_perms;
# /sys/devices/virtual/sec/sensorhub/mcu_power
allow lhd sysfs_sensorhub_writable:file rw_file_perms;
# /sys/class/sec/gps/GPS_PWR_EN/value
allow lhd sysfs_gps_writable:file rw_file_perms;

View file

@ -52,11 +52,6 @@ vendor.wlan. u:object_r:vendor_wlan_prop:s0
# Hwc # Hwc
hwc.exynos.vsync_mode u:object_r:vendor_hwc_prop:s0 hwc.exynos.vsync_mode u:object_r:vendor_hwc_prop:s0
# Rild
mdc.sys.enable_smff0 u:object_r:rild_prop:s0
ro.csc.countryiso_code u:object_r:rild_prop:s0
persist.sys.omc_etcpath u:object_r:rild_prop:s0
# TEEGRIS # TEEGRIS
vendor.secureos. u:object_r:vendor_secureos_prop:s0 vendor.secureos. u:object_r:vendor_secureos_prop:s0
vendor.tzdaemon u:object_r:vendor_tzdaemon_prop:s0 vendor.tzdaemon u:object_r:vendor_tzdaemon_prop:s0

View file

@ -94,4 +94,3 @@ allow rild hal_audio_default:file r_file_perms;
# hwservice # hwservice
add_hwservice(rild, rild_hwservice) add_hwservice(rild, rild_hwservice)
get_prop(rild, rild_prop)