Change adaptive brightness level programmatically - android

How can I change the adaptive brightness level programmatically, on Android Lollipop?
I know how to change the manual brightness level, and to toggle on or off the adaptive brightness. It is done like that:
Settings.System.putInt(cr, Settings.System.SCREEN_BRIGHTNESS, newLevel);
However, with adaptive brightness is enabled, the OS combines it with another brightness level which is different than the manual one.
Is there a way to do this?
Target/min/max SDK is 21.

Don't know why but there is a hidden constant SCREEN_AUTO_BRIGHTNESS_ADJ in Android API to adjust adaptive brightness. But you can pass "screen_auto_brightness_adj" string value instead like I did.
Adaptive brightness adjustment is stored as float value in range [-1;1]. If you use brightness value in range [0;255], you can convert it to proper value as shown below.
float value = (((float)brightness*2)/255) - 1.0f;
Settings.System.putFloat(contentResolver, "screen_auto_brightness_adj", value);

If your app targetSdkVersion is 23+, Settings.System.putFloat(contentResolver, "screen_auto_brightness_adj", value) won't work as Android disables you to modify any "hidden" settings.
Read frameworks\base\packages\SettingsProvider\src\com\android\providers\settings\SettingsProvider.java warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk() for details.

Related

Androidx fontscale not working on HuaWei Mate20

I am using Androidx 1.1.0, when i override the "getResources()" function in my "AppCompatActivity" when i set the fontscale to 1.5f in the "getResources()" function, it does not work. And i change the androidx library to support library it works fine. Anyone get the same problem ?
First step open Huawei mate20 and click system settings
Second step, after entering the mobile phone system setting interface,click display.
Third step,after entering the display interface,click font and display size.
Step four, after entering the font and display size interface, you can see that adjusting the font size only adjust the font,display size adjusts the font,picture and other interface elements.
Step 5,here,adust the font size, and the display size can be set as required.

SetCompoundDrawableTintList for Android < 5

I am trying to change the color of of an EditText to something at runtime (the color is also calculated at runtime).
SetCompoundDrawableTintList seems to do the trick. But it is only available starting from API Level 23.
Can I somehow archive the same effect for API Level < 23? Note that I want to set the color at runtime and do not know them in advance.

Updating the screen brightness in android

here's my code to change the brightness settings of my phone.
After running the app, if I go to the settings>display>brightness of my phone, I see the value changed however not 'updated'. So when I click on the dialog box's ok/cancel button, that's when it gets updated. So am I missing a call that finally after changing the values will update the screen brightness? Thanks.
The code is in C# though, I am using Xamarin and monodroid to develop my App. Thanks people!
Android.Provider.Settings.System.PutInt(ContentResolver, Android.Provider.Settings.System.ScreenBrightnessMode, 0);
int brightnessInt = (int)(1.0 * 255); //To set max brightness
Android.Provider.Settings.System.PutInt(ContentResolver, Android.Provider.Settings.System.ScreenBrightness, brightnessInt);
View v = Window.DecorView.FindViewById(Android.Resource.Id.Content);
v.Invalidate();
Make sure it's not on auto adjust
Android 2.2: Adjusting screen brightness
The last few lines solve your problem.
Also make sure android:minSdkVersion is at least v 2.3.3

Changing screen brightness efficiently

I need to change the screen brightness programmatically. I read multiple solutions like this Can't apply system screen brightness programmatically in Android.
My problem is that those solutions implies changing the activity to be effective (having something like a dummy activity finishing immediately) and I would like to avoid the overhead of an activity switch.
Is there any other solution... maybe using native code so that the screen brightness will change immediately ?
The following affects immediately the single activity, no need to restart it. The activity also remembers the screenBrightness attribute over pause/resume.
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 1; // 0f - no backlight ... 1f - full backlight
getWindow().setAttributes(lp);
But it has no effect if you have automatic backlight level enabled in the system settings. This solution should help to turn off automatic backlight.

Set brightness below the system limit using alpha and save value

I've been trying to set the brightness under the limit of Android settings. There are two similar apps in Google Play doing this:
Shades
Screen Filter
The second one, even allows to use the phone with a black screen, just what I need in my app.
There are a lot of other questions about brightness but not to set it under this system limit, and I've found one question asking almost the same but not helping me:
Brightness Screen Filter
So after a lot of search I've got following code:
int brightness=0; //0 to 255, so I set it to a low level (not enough for me)
ContentResolver cResolver = getContentResolver();
//set the system brightness value, and if active, disable auto mode.
System.putInt(cResolver, System.SCREEN_BRIGHTNESS, brightness);
System.putInt(cResolver, System.SCREEN_BRIGHTNESS_MODE, System.SCREEN_BRIGHTNESS_MODE_MANUAL);
//previous lines don't set the brightness to the current screen so we set it like this:
LayoutParams layoutpars = getWindow().getAttributes();
layoutpars.screenBrightness = brightness / (float)255;
//Now we have brightness to the minimum allowed by Android but
//to achieve what these apps do, I have to reduce alpha of the window to the min
//then the screen is black like I want, and totally usable!
layoutpars.alpha=0.05f;
window.setAttributes(layoutpars);
Here the alpha is the key, this is doing what I want, but only for current window/screen, and I need to save to the system this alpha as default, like I'm doing with the brightness and the Manual mode for applying everywhere.
Do you know how I should do this? I'm unable to find a value "System.SCREEN_ALPHA" to set it with System.putInt or another way to do this.
In the other question I linked before, pheelicks replied with a suggestion of using a transparent non-touchable over screen, but this is not working for me, and the result does not give the feeling of a turned off screen like previous apps.
Thanks.
I decided to implemented this feature just inside my app with alpha. So I couldn't solve the initial question at all....
Anyways, in the end seems that the solution to this question is the one Pheelicks replied here: https://stackoverflow.com/a/4287597/1667990
-This is launching an activity to be always on top,
-with alpha to 0.0f so it be transparent,
-that redirects the touch to the activity behind:
//Let touches go through to apps/activities underneath.
Window window = activity.getWindow();
window.addFlags(FLAG_NOT_TOUCHABLE);
-And most important thing not explained in the previous link, is to set the dim behind our window to 1 (like the dim in a messagebox, but setting to the max to be black behind :D )
window.setDimAmount ((float)1.0) ;
window.setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND,
WindowManager.LayoutParams.FLAG_DIM_BEHIND);
I haven't tried that and decided not to do it in my app as it could bring unexpected behavior in my circumstances, but I think it should definitely work, if anyone try please add your feedback!

Categories

Resources