Skip to content

Commit

Permalink
[DNM] sm8150-common: sensors: Adjust RGB colors in night mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Hikari-no-Tenshi committed Mar 19, 2020
1 parent 073d2e3 commit e2e4da0
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,22 @@
import android.Manifest;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.database.ContentObserver;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.media.session.MediaSessionLegacyHelper;
import android.net.Uri;
import android.os.FileObserver;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
Expand All @@ -58,6 +62,8 @@ public class KeyHandler implements DeviceKeyHandler {
private static final boolean DEBUG = false;
private static final int GESTURE_REQUEST = 1;
private static String FPNAV_ENABLED_PROP = "sys.fpnav.enabled";
private static String NIGHT_MODE_ENABLED_PROP = "sys.night_mode.enabled";
private static String NIGHT_MODE_COLOR_TEMPERATURE_PROP = "sys.night_mode.color_temperature";
private static final String DOZE_INTENT = "com.android.systemui.doze.pulse";

private static final SparseIntArray sSupportedSliderZenModes = new SparseIntArray();
Expand Down Expand Up @@ -97,6 +103,7 @@ public class KeyHandler implements DeviceKeyHandler {
private ClientPackageNameObserver mClientObserver;
private IOnePlusCameraProvider mProvider;
private boolean isOPCameraAvail;
private Handler mHandler;

private BroadcastReceiver mSystemStateReceiver = new BroadcastReceiver() {
@Override
Expand All @@ -113,6 +120,7 @@ public void onReceive(Context context, Intent intent) {

public KeyHandler(Context context) {
mContext = context;
mHandler = new Handler(Looper.getMainLooper());
mDispOn = true;
mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
mNotificationManager
Expand All @@ -135,6 +143,59 @@ public KeyHandler(Context context) {
mClientObserver = new ClientPackageNameObserver(CLIENT_PACKAGE_PATH);
mClientObserver.startWatching();
}

mCustomSettingsObserver.observe();
mCustomSettingsObserver.update();
}

private CustomSettingsObserver mCustomSettingsObserver = new CustomSettingsObserver(mHandler);
private class CustomSettingsObserver extends ContentObserver {

CustomSettingsObserver(Handler handler) {
super(handler);
}

void observe() {
ContentResolver resolver = mContext.getContentResolver();
resolver.registerContentObserver(Settings.Secure.getUriFor(
Settings.Secure.NIGHT_DISPLAY_ACTIVATED),
false, this, UserHandle.USER_ALL);
resolver.registerContentObserver(Settings.Secure.getUriFor(
Settings.Secure.NIGHT_DISPLAY_COLOR_TEMPERATURE),
false, this, UserHandle.USER_ALL);
}

@Override
public void onChange(boolean selfChange, Uri uri) {
if (uri.equals(Settings.Secure.getUriFor(
Settings.Secure.NIGHT_DISPLAY_ACTIVATED))) {
updateNightModeStatus();
} else if (uri.equals(Settings.Secure.getUriFor(
Settings.Secure.NIGHT_DISPLAY_COLOR_TEMPERATURE))) {
updateNightModeColorTemperature();
}
}

public void update() {
updateNightModeStatus();
updateNightModeColorTemperature();
}
}

private void updateNightModeStatus() {
boolean nightModeEnabled = Settings.Secure.getIntForUser(
mContext.getContentResolver(), Settings.Secure.NIGHT_DISPLAY_ACTIVATED,
0,
UserHandle.USER_CURRENT) != 0;
SystemProperties.set(NIGHT_MODE_ENABLED_PROP, nightModeEnabled ? "1" : "0");
}

private void updateNightModeColorTemperature() {
int colorTemperature = Settings.Secure.getIntForUser(
mContext.getContentResolver(), Settings.Secure.NIGHT_DISPLAY_COLOR_TEMPERATURE,
-1,
UserHandle.USER_CURRENT);
SystemProperties.set(NIGHT_MODE_COLOR_TEMPERATURE_PROP, String.valueOf(colorTemperature));
}

private boolean hasSetupCompleted() {
Expand Down
11 changes: 11 additions & 0 deletions sensors/multihal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#define LOG_TAG "sensors_multihal"
#define LOG_NDEBUG 1
#include <android-base/properties.h>
#include <cutils/atomic.h>
#include <cutils/properties.h>
#include <hardware/sensors.h>
Expand All @@ -44,6 +45,8 @@

#include "screenshot.h"

using android::base::GetProperty;

static pthread_mutex_t init_modules_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t init_sensors_mutex = PTHREAD_MUTEX_INITIALIZER;

Expand Down Expand Up @@ -581,6 +584,14 @@ void light_sensor_correction(sensors_event_t *ev) {
uint8_t r = buffer[0];
uint8_t g = buffer[1];
uint8_t b = buffer[2];
bool nightModeEnabled = android::base::GetProperty("sys.night_mode.enabled", "0") == "1";
int colorTemperature = (int) stoi(android::base::GetProperty("sys.night_mode.color_temperature", "0"));
if (nightModeEnabled && colorTemperature > 0) {
float squareTemperature = colorTemperature * colorTemperature;
r = r * (squareTemperature * 0.0 + colorTemperature * 0.0 + 1.0);
g = g * (squareTemperature * -0.00000000962353339 + colorTemperature * 0.000153045476 + 0.390782778);
b = b * (squareTemperature * -0.0000000189359041 + colorTemperature * 0.000302412211 + -0.198650895);
}
ALOGV("Screen Color Above Sensor: %d, %d, %d", r, g, b);
ALOGV("Original reading: %f", ev->light);
int screen_brightness = get("/sys/class/backlight/panel0-backlight/brightness", 0);
Expand Down
3 changes: 3 additions & 0 deletions sepolicy/vendor/hal_sensors_default.te
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,6 @@ r_dir_file(hal_sensors_default, sysfs_graphics)

# Allow hal_sensors_default to get vendor_sensors_prop
get_prop(hal_sensors_default, vendor_sensors_prop)

# Allow hal_sensors_default to get night mode prop
get_prop(hal_sensors_default, powerctl_prop)
2 changes: 2 additions & 0 deletions sepolicy/vendor/property_contexts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,5 @@ persist.vendor.sensors.enable.mag_filter u:object_r:vendor_sensors_prop:
persist.vendor.sensors.light.location_x u:object_r:vendor_sensors_prop:s0
persist.vendor.sensors.light.location_y u:object_r:vendor_sensors_prop:s0
persist.vendor.sensors.proximity.using_motion u:object_r:vendor_sensors_prop:s0
sys.night_mode.enabled u:object_r:powerctl_prop:s0
sys.night_mode.color_temperature u:object_r:powerctl_prop:s0
2 changes: 2 additions & 0 deletions sepolicy/vendor/system_app.te
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ allow system_app sysfs_graphics:file r_file_perms;
# Allow DeviceSettings to read and write to proc_touchpanel, to toggle single-tap gesture
allow system_app proc_touchpanel:dir search;
allow system_app proc_touchpanel:file rw_file_perms;
# Allow DeviceSettings to set night mode prop
set_prop(system_app, powerctl_prop)

0 comments on commit e2e4da0

Please sign in to comment.