diff --git a/universal7885-common/apps/CameraLightSensor/Android.bp b/universal7885-common/apps/CameraLightSensor/Android.bp index fc3d95b..9f4e546 100644 --- a/universal7885-common/apps/CameraLightSensor/Android.bp +++ b/universal7885-common/apps/CameraLightSensor/Android.bp @@ -6,4 +6,5 @@ android_app { platform_apis: true, privileged: true, certificate: "platform", + static_libs: [ "androidx.core_core" ], } diff --git a/universal7885-common/apps/CameraLightSensor/src/com/eurekateam/cameralightsensor/Camera2Service.java b/universal7885-common/apps/CameraLightSensor/src/com/eurekateam/cameralightsensor/Camera2Service.java index 8994e31..704d399 100755 --- a/universal7885-common/apps/CameraLightSensor/src/com/eurekateam/cameralightsensor/Camera2Service.java +++ b/universal7885-common/apps/CameraLightSensor/src/com/eurekateam/cameralightsensor/Camera2Service.java @@ -2,8 +2,8 @@ package com.eurekateam.cameralightsensor; import static com.eurekateam.cameralightsensor.CameraLightSensorService.DEBUG; -import android.Manifest; import android.annotation.NonNull; +import android.annotation.SuppressLint; import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; @@ -11,7 +11,6 @@ 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; @@ -27,10 +26,11 @@ import android.media.ImageReader; import android.os.Handler; import android.os.IBinder; import android.os.Looper; -import android.os.SystemClock; import android.provider.Settings; import android.util.Log; +import androidx.core.app.NotificationCompat; + import java.nio.ByteBuffer; import java.util.Collections; @@ -44,6 +44,8 @@ public class Camera2Service extends Service { protected ImageReader imageReader; private Context mContext; private CameraManager manager; + private long mCaptureTime; + private final int DELAY = 5000; protected CameraDevice.StateCallback cameraStateCallback = new CameraDevice.StateCallback() { @Override @@ -71,16 +73,16 @@ public class Camera2Service extends Service { if (!destroy) { Camera2Service.this.session = session; try { + if (createCaptureRequest() == null) return; try { - if (createCaptureRequest() == null) return; session.capture(createCaptureRequest(), captureCallback, null); } catch (CameraAccessException e) { e.printStackTrace(); - } catch (IllegalStateException e){ + } catch (IllegalStateException e) { Log.w(TAG, "onReady: Session is NULL"); } - } catch (Exception e){ - Log.e(TAG, "Camera is in use"); + } catch (Exception e) { + Log.e(TAG, "Camera is in use"); e.printStackTrace(); } } @@ -115,13 +117,12 @@ public class Camera2Service extends Service { } }; + @SuppressLint("MissingPermission") public void readyCamera() { manager = (CameraManager) getSystemService(CAMERA_SERVICE); + mCaptureTime = System.currentTimeMillis(); 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(250, 250, ImageFormat.JPEG, 2 /* images buffered */); @@ -161,24 +162,25 @@ public class Camera2Service extends Service { { NotificationManager nm = (NotificationManager) mContext.getSystemService(NOTIFICATION_SERVICE); NotificationChannel channel = new NotificationChannel( - mContext.getPackageName(), "CameraLightSensor", + mContext.getBasePackageName(), "CameraLightSensor", NotificationManager.IMPORTANCE_LOW ); 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); - PendingIntent contentIntent = PendingIntent.getActivity(mContext,50,notificationIntent,PendingIntent.FLAG_IMMUTABLE); + 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); + builder.setContentTitle("Camera Light Sensor Service"); + builder.setChannelId(mContext.getBasePackageName()); return builder.build(); } + @Override public void onCreate() { if(DEBUG) Log.d(TAG, "onCreate service"); @@ -190,7 +192,7 @@ public class Camera2Service extends Service { public void actOnReadyCameraDevice() { try { - cameraDevice.createCaptureSession(Collections.singletonList(imageReader.getSurface()), sessionStateCallback, null); + cameraDevice.createCaptureSession(Collections.singletonList(imageReader.getSurface()), sessionStateCallback, null); } catch (CameraAccessException e) { Log.e(TAG, e.getMessage()); } @@ -227,7 +229,7 @@ public class Camera2Service extends Service { protected CaptureRequest createCaptureRequest() { try { - CaptureRequest.Builder builder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); + CaptureRequest.Builder builder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD); builder.addTarget(imageReader.getSurface()); return builder.build(); } catch (CameraAccessException e) { @@ -244,37 +246,47 @@ public class Camera2Service extends Service { if (packageId.equals(mContext.getBasePackageName())) return; 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 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); } @Override public void onCameraAvailable(String cameraId) { if (DEBUG) Log.i(TAG, "CameraManager.AvailabilityCallback : Camera IS Available. "); - isavail = true; 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() { @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); + cameraDevice.close(); + session.close(); } }; @@ -305,12 +317,12 @@ public class Camera2Service extends Service { int oldbrightness = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS); if (DEBUG) Log.i(TAG, "AdjustBrightness: Oldval = " + oldbrightness + " Newval = " + brightness + " Adjusting.."); - int newbrightness = brightness; - if (newbrightness > 255){ - newbrightness = 255; - }else if (newbrightness < 0){ - newbrightness = 0; - } + int newbrightness = brightness; + if (newbrightness > 255){ + newbrightness = 255; + }else if (newbrightness < 0){ + newbrightness = 0; + } Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, newbrightness); } } diff --git a/universal7885-common/apps/CameraLightSensor/src/com/eurekateam/cameralightsensor/CameraLightSensorService.java b/universal7885-common/apps/CameraLightSensor/src/com/eurekateam/cameralightsensor/CameraLightSensorService.java index 56f41a9..d00d06e 100755 --- a/universal7885-common/apps/CameraLightSensor/src/com/eurekateam/cameralightsensor/CameraLightSensorService.java +++ b/universal7885-common/apps/CameraLightSensor/src/com/eurekateam/cameralightsensor/CameraLightSensorService.java @@ -3,10 +3,12 @@ package com.eurekateam.cameralightsensor; import android.Manifest; import android.app.Activity; import android.content.BroadcastReceiver; +import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageManager; +import android.database.ContentObserver; import android.net.Uri; import android.os.Bundle; import android.os.Handler; @@ -20,18 +22,20 @@ public class CameraLightSensorService extends Activity { private static final String TAG = "CameraLightSensor"; static final boolean DEBUG = false; Intent i; + IntentFilter screenStateFilter; private Context mContext; + public ContentResolver contentResolver; + private boolean mRegistered; + private boolean mServiceStarted; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); moveTaskToBack(true); mContext = getApplicationContext(); - IntentFilter screenStateFilter = new IntentFilter(Intent.ACTION_SCREEN_ON); - screenStateFilter.addAction(Intent.ACTION_SCREEN_OFF); + contentResolver = getContentResolver(); BatteryOptimization(mContext); i = new Intent(mContext, Camera2Service.class); - mContext.startForegroundService(i); - registerReceiver(mScreenStateReceiver, screenStateFilter); if (this.checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) { Log.d(TAG, "Permission Granted"); @@ -40,36 +44,70 @@ public class CameraLightSensorService extends Activity { this.requestPermissions(new String[] { Manifest.permission.CAMERA }, 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 public void onDestroy() { if (DEBUG) Log.d(TAG, "Destroying service"); + mRegistered = false; + contentResolver.unregisterContentObserver(observer); super.onDestroy(); } private final BroadcastReceiver mScreenStateReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { - try { - onDisplayOn(); - } catch (Settings.SettingNotFoundException e) { - e.printStackTrace(); - } + onDisplayOn(); + mServiceStarted = true; } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { - onDisplayOff(); + if (mServiceStarted) onDisplayOff(); + mServiceStarted = false; } } }; - private void onDisplayOn() throws Settings.SettingNotFoundException { - if(Settings.System.getInt(getContentResolver(), - Settings.System.SCREEN_BRIGHTNESS_MODE ) == - Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC){ - if(DEBUG) Log.d(TAG, "AutoBrightness Mode Enabled. Starting..."); - i = new Intent(mContext, Camera2Service.class); - mContext.startForegroundService(i); - }else{ - Log.d(TAG, "AutoBrightness Mode Disabled, Not Starting Service..."); + // Make a listener + ContentObserver observer = new ContentObserver(new Handler(Looper.getMainLooper())) { + @Override + public void onChange(boolean selfChange) { + super.onChange(selfChange); + Log.i(TAG, "observer: Brightness Settings Changed"); + try { + if(Settings.System.getInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS_MODE) + == 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(){ if(DEBUG) Log.d(TAG, "Screen is off. Stopping Service..."); diff --git a/universal7885-common/sepolicy/private/property_contexts b/universal7885-common/sepolicy/private/property_contexts index 3324ac8..e38e381 100644 --- a/universal7885-common/sepolicy/private/property_contexts +++ b/universal7885-common/sepolicy/private/property_contexts @@ -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_etcpath 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 ril.NwNmId u:object_r:telephony_config_prop:s0 diff --git a/universal7885-common/sepolicy/vendor/lhd.te b/universal7885-common/sepolicy/vendor/lhd.te deleted file mode 100644 index 12e28c0..0000000 --- a/universal7885-common/sepolicy/vendor/lhd.te +++ /dev/null @@ -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; diff --git a/universal7885-common/sepolicy/vendor/property_contexts b/universal7885-common/sepolicy/vendor/property_contexts index dca0076..070dfd4 100644 --- a/universal7885-common/sepolicy/vendor/property_contexts +++ b/universal7885-common/sepolicy/vendor/property_contexts @@ -52,11 +52,6 @@ vendor.wlan. u:object_r:vendor_wlan_prop:s0 # Hwc 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 vendor.secureos. u:object_r:vendor_secureos_prop:s0 vendor.tzdaemon u:object_r:vendor_tzdaemon_prop:s0 diff --git a/universal7885-common/sepolicy/vendor/rild.te b/universal7885-common/sepolicy/vendor/rild.te index 5f51394..d39e817 100644 --- a/universal7885-common/sepolicy/vendor/rild.te +++ b/universal7885-common/sepolicy/vendor/rild.te @@ -94,4 +94,3 @@ allow rild hal_audio_default:file r_file_perms; # hwservice add_hwservice(rild, rild_hwservice) -get_prop(rild, rild_prop)