sm8350-common: parts: restore brightness after HBM usage
after disabling HBM, the brightness would rise back up, save brightness before enabling HBM and restore it after disabling
This commit is contained in:
parent
4708d51a81
commit
9590e9c1e8
3 changed files with 51 additions and 21 deletions
|
|
@ -13,11 +13,11 @@ import android.hardware.SensorEventListener;
|
|||
import android.hardware.SensorManager;
|
||||
import android.os.IBinder;
|
||||
import android.os.PowerManager;
|
||||
import androidx.preference.PreferenceManager;
|
||||
import android.provider.Settings;
|
||||
|
||||
import androidx.preference.PreferenceManager;
|
||||
|
||||
import org.lineageos.settings.utils.FileUtils;
|
||||
import org.lineageos.settings.display.*;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
|
@ -31,11 +31,13 @@ public class AutoHBMService extends Service {
|
|||
private ExecutorService mExecutorService;
|
||||
|
||||
private SensorManager mSensorManager;
|
||||
Sensor mLightSensor;
|
||||
private Sensor mLightSensor;
|
||||
|
||||
private SharedPreferences mSharedPrefs;
|
||||
private boolean dcDimmingEnabled;
|
||||
|
||||
private int mStoredBrightness = -1;
|
||||
|
||||
public void activateLightSensorRead() {
|
||||
submit(() -> {
|
||||
mSensorManager = (SensorManager) getApplicationContext().getSystemService(Context.SENSOR_SERVICE);
|
||||
|
|
@ -54,11 +56,23 @@ public class AutoHBMService extends Service {
|
|||
|
||||
private void enableHBM(boolean enable) {
|
||||
if (enable) {
|
||||
// Store current brightness before enabling HBM
|
||||
if (mStoredBrightness == -1) {
|
||||
mStoredBrightness = Settings.System.getInt(getContentResolver(),
|
||||
Settings.System.SCREEN_BRIGHTNESS, 255);
|
||||
}
|
||||
FileUtils.writeLine(HBM, "1");
|
||||
FileUtils.writeLine(BACKLIGHT, "2047");
|
||||
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 255);
|
||||
} else {
|
||||
FileUtils.writeLine(HBM, "0");
|
||||
// Restore brightness when disabling HBM
|
||||
if (mStoredBrightness != -1) {
|
||||
FileUtils.writeLine(BACKLIGHT, String.valueOf(mStoredBrightness));
|
||||
Settings.System.putInt(getContentResolver(),
|
||||
Settings.System.SCREEN_BRIGHTNESS, mStoredBrightness);
|
||||
mStoredBrightness = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -67,12 +81,11 @@ public class AutoHBMService extends Service {
|
|||
}
|
||||
|
||||
private SensorEventListener mSensorEventListener = new SensorEventListener() {
|
||||
|
||||
@Override
|
||||
public void onSensorChanged(SensorEvent event) {
|
||||
float lux = event.values[0];
|
||||
KeyguardManager km =
|
||||
(KeyguardManager) getSystemService(getApplicationContext().KEYGUARD_SERVICE);
|
||||
(KeyguardManager) getSystemService(getApplicationContext().KEYGUARD_SERVICE);
|
||||
boolean keyguardShowing = km.inKeyguardRestrictedInputMode();
|
||||
float luxThreshold = Float.parseFloat(mSharedPrefs.getString(HBMFragment.KEY_AUTO_HBM_THRESHOLD, "20000"));
|
||||
long timeToDisableHBM = Long.parseLong(mSharedPrefs.getString(HBMFragment.KEY_HBM_DISABLE_TIME, "1"));
|
||||
|
|
@ -88,7 +101,7 @@ public class AutoHBMService extends Service {
|
|||
mExecutorService.submit(() -> {
|
||||
try {
|
||||
Thread.sleep(timeToDisableHBM * 1000);
|
||||
} catch (InterruptedException e) {
|
||||
} catch (InterruptedException ignored) {
|
||||
}
|
||||
if (lux < luxThreshold) {
|
||||
mAutoHBMActive = false;
|
||||
|
|
@ -108,9 +121,9 @@ public class AutoHBMService extends Service {
|
|||
private BroadcastReceiver mScreenStateReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
|
||||
if (Intent.ACTION_SCREEN_ON.equals(intent.getAction())) {
|
||||
activateLightSensorRead();
|
||||
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
|
||||
} else if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
|
||||
deactivateLightSensorRead();
|
||||
}
|
||||
}
|
||||
|
|
@ -129,7 +142,7 @@ public class AutoHBMService extends Service {
|
|||
}
|
||||
}
|
||||
|
||||
private Future < ? > submit(Runnable runnable) {
|
||||
private Future<?> submit(Runnable runnable) {
|
||||
return mExecutorService.submit(runnable);
|
||||
}
|
||||
|
||||
|
|
@ -142,10 +155,7 @@ public class AutoHBMService extends Service {
|
|||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
unregisterReceiver(mScreenStateReceiver);
|
||||
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
|
||||
if (pm.isInteractive()) {
|
||||
deactivateLightSensorRead();
|
||||
}
|
||||
deactivateLightSensorRead();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -16,14 +16,12 @@
|
|||
*
|
||||
*/
|
||||
package org.lineageos.settings.hbm;
|
||||
|
||||
import android.provider.Settings;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.Preference.OnPreferenceChangeListener;
|
||||
import androidx.preference.PreferenceManager;
|
||||
|
||||
import org.lineageos.settings.utils.FileUtils;
|
||||
import org.lineageos.settings.display.*;
|
||||
|
||||
|
|
@ -54,13 +52,25 @@ public class HBMModeSwitch implements OnPreferenceChangeListener {
|
|||
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||
Boolean enabled = (Boolean) newValue;
|
||||
boolean dcDimmingEnabled = PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean(DcDimmingTileService.DC_DIMMING_ENABLE_KEY, false);
|
||||
if (dcDimmingEnabled) {
|
||||
if (dcDimmingEnabled) {
|
||||
return false;
|
||||
}
|
||||
FileUtils.writeLine(getHBM(), enabled ? "1" : "0");
|
||||
|
||||
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
|
||||
if (enabled) {
|
||||
// Save current brightness level
|
||||
int currentBrightness = Settings.System.getInt(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 128);
|
||||
sharedPrefs.edit().putInt("last_brightness", currentBrightness).apply();
|
||||
|
||||
FileUtils.writeLine(getHBM(), "1");
|
||||
FileUtils.writeLine(getBACKLIGHT(), "2047");
|
||||
Settings.System.putInt(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 255);
|
||||
} else {
|
||||
FileUtils.writeLine(getHBM(), "0");
|
||||
|
||||
// Restore last brightness level
|
||||
int lastBrightness = sharedPrefs.getInt("last_brightness", 128);
|
||||
Settings.System.putInt(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, lastBrightness);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,6 @@
|
|||
*/
|
||||
|
||||
package org.lineageos.settings.hbm;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
|
|
@ -28,12 +27,10 @@ import android.service.quicksettings.Tile;
|
|||
import android.service.quicksettings.TileService;
|
||||
import androidx.preference.PreferenceManager;
|
||||
import android.provider.Settings;
|
||||
|
||||
import org.lineageos.settings.utils.FileUtils;
|
||||
import org.lineageos.settings.display.*;
|
||||
|
||||
public class HBMModeTileService extends TileService {
|
||||
|
||||
private static final String HBM = "/sys/devices/platform/soc/soc:qcom,dsi-display-primary/hbm_enabled";
|
||||
private static final String HBM_KEY = "hbm";
|
||||
private static final String BACKLIGHT = "/sys/class/backlight/panel0-backlight/brightness";
|
||||
|
|
@ -80,6 +77,7 @@ public class HBMModeTileService extends TileService {
|
|||
public void onStopListening() {
|
||||
super.onStopListening();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick() {
|
||||
super.onClick();
|
||||
|
|
@ -88,12 +86,24 @@ public class HBMModeTileService extends TileService {
|
|||
if (dcDimmingEnabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
final boolean enabled = !(sharedPrefs.getBoolean(HBM_KEY, false));
|
||||
FileUtils.writeLine(HBM, enabled ? "1" : "0");
|
||||
if (enabled) {
|
||||
// Save current brightness level
|
||||
int currentBrightness = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 128);
|
||||
sharedPrefs.edit().putInt("last_brightness", currentBrightness).apply();
|
||||
|
||||
FileUtils.writeLine(HBM, "1");
|
||||
FileUtils.writeLine(BACKLIGHT, "2047");
|
||||
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 255);
|
||||
} else {
|
||||
FileUtils.writeLine(HBM, "0");
|
||||
|
||||
// Restore last brightness level
|
||||
int lastBrightness = sharedPrefs.getInt("last_brightness", 128);
|
||||
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, lastBrightness);
|
||||
}
|
||||
|
||||
sharedPrefs.edit().putBoolean(HBM_KEY, enabled).commit();
|
||||
updateUI(enabled);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue