How to prevent brightness when battery reaches high?


Question

I leave my phone (Samsung Galaxy 4 GT-I9506, Android 5.0.1 lollipop) charging on my table when going to sleep. It reaches full after some hours often. It is so annoying when the display becomes bright after battery reaching full, awakening me.



I know the thread How to prevent brightness from going to full when the battery low popup appears? about the prevention of bright display when the battery goes low. The rooted device seems to be the only choice to change the default behavior. I have unsuccessfully installed CyanogenMod in the device a few times. I am open about other ways to root this device and change its default behavior.



Settings > Display does not have any features by which you can alter the brightness mode. It would be great to customize the brightness in different modes, etc when the battery is low. In the link, the user did not want to consume extra battery with display when the battery was extremely low.



I installed Firelord proposed, Tasker, where I am working the automation. I probably need to root my phone for sufficient change.






How can you prevent brightness when battery reaches full in Android?


Answer

You may come close but cannot prevent that behavior unless there is a setting unbeknownst to you or the device is rooted. No matter what app you use, the brightness would be set to maximum whenever the battery level reaches 100% and in a matter of few milliseconds, it would revert to whatsoever you want it to be. My solution takes care of that problem. Originally posted here.



The file named max_brightness, a file under /sys, hence a part of Linux kernel and directly managed by it, appears to be the key to the solution. I found that setting the brightness limit in that file is one of the best ways to tackle the issue.



I noticed that once your custom maximum brightness is set, neither Android nor an app was able to set brightness beyond that custom level unless they changed the content of that file, which often an app has no reason to and none of them changed either. Besides, the file is owned by root and is part of group root, so any app or even system_server would require root privilege to make changes into that file.



Finding that file is bit tricky. In my MTK device with Android 4.2, that file appears to be under /sys/devices/platform/leds-mt65xx/leds/lcd-backlight/.



In my Qualcomm device with Android 5.0.2 and above, the file appeared to be in /sys/devices/mdp.0/qcom,mdss_fb_primary.168/leds/lcd-backlight/.



I assume that the best way to find that file is to run a search query (setup for that), such as:



(Requires busybox)



adb shell su -c 'find /sys/ -type f -iname "max_brightness"'


In my Qualcomm device, the output it gives is:




/sys/devices/f9924000.i2c/i2c-2/2-0068/leds/red/max_brightness
/sys/devices/f9924000.i2c/i2c-2/2-0068/leds/blue/max_brightness
/sys/devices/f9924000.i2c/i2c-2/2-0068/leds/green/max_brightness
/sys/devices/mdp.0/qcom,mdss_fb_primary.168/leds/lcd-backlight/max_brightness
/sys/devices/qcom,camera-led-flash.81/leds/torch-light/max_brightness
/sys/devices/msm_sdcc.1/leds/mmc0::/max_brightness
/sys/devices/msm_sdcc.2/leds/mmc1::/max_brightness
/sys/devices/01-qcom,leds-d300/leds/led:flash_torch/max_brightness
/sys/devices/01-qcom,leds-d300/leds/led:flash_0/max_brightness
/sys/devices/01-qcom,leds-d300/leds/led:flash_1/max_brightness
/sys/devices/01-qcom,leds-e200/leds/kpdbl-lut-2/max_brightness
/sys/devices/01-qcom,leds-e200/leds/kpdbl-pwm-3/max_brightness
/sys/devices/01-qcom,leds-e200/leds/kpdbl-pwm-4/max_brightness
/sys/devices/01-qcom,leds-e200/leds/button-backlight/max_brightness


The location where lcd-backlight is mentioned was the only one that made sense to me and this is how I found the file in my device. Don't you worry asking about others. I attempted changing the brightness of the screen and looked for the changes in the file named brightness (also located under those said locations). None of them reflected the correct output other than the one at /sys/devices/mdp.0/qcom,mdss_fb_primary.168/leds/lcd-backlight.



Only for demonstration, try setting a limit in max_brightness



adb shell su -c 'echo LIMIT > PATH_TO_MAX_BRIGHTNESS'
# in my case PATH_TO_MAX_BRIGHTNESS is /sys/devices/mdp.0/qcom,mdss_fb_primary.168/leds/lcd-backlight/ and set LIMIT to a value less than 30 (not limited to). 


Now try maxing the brightness by using brightness slider or a third party app and you would notice that the brightness of your screen doesn't exceed beyond a certain point even when slider or the app's configuration appears to be at maximum. The brightness for that "certain point" would certainly be less than the default one you are used to.



Note that the maximum value in max_brightness is often found to be 255 and the brightness ranges from 0-255. Often, not always true, that is. And fear not from setting the limit to 0. It would cause the screen not to lit until you revert the changes or restart the device.



Find a limit which would not disturb you in your sleep.






Automation



For , use any automation app that can run a command with root privilege and when the context of your preferred trigger is met. I recommend a time based trigger.



Some working examples:




  • In Tasker, setup a profile with your preferred context, create a task and use the action: Code → Run Shell:




    • Command: echo LIMIT > PATH_TO_MAX_BRIGHTNESS

    • check Use Root


  • In MacroDroid, setup your preferred trigger and for action install Secure Settings, choose it → Actions → Run Command:




    • Command: echo LIMIT > PATH_TO_MAX_BRIGHTNESS

    • check Use Root


  • In Automate, as part of your flow, when setting up an action for your trigger, select Shell command superuser under Apps and do:




    • Command Line: echo LIMIT > PATH_TO_MAX_BRIGHTNESS




    Make sure to install all the permissions the flow require.


  • In DroidAutomation, setup your trigger and under task, select Root → Execute a command line and type: echo LIMIT > PATH_TO_MAX_BRIGHTNESS




As you would've guessed, you would have to setup two profiles for every app. One to setup a limit and the other to set the maximum brightness to the default you're used to.






I would like you to keep this under consideration (even though you got a hint at the beginning) that using a 100% battery trigger would defeat the purpose of this answer, which is to override the changes Android want to make. As an example, if you setup LIMIT to 0 whenever the battery reaches 100%, what would happen is your Android would lit the screen to maximum as usual and in few milliseconds (you read correctly) your automation app would run and set the limit. Your sleep may get interrupted in those milliseconds.



The workaround is to set up the battery level to 99 or less for full battery charge.



That said, I wouldn't be surprised if you avoid rooting considering that that the biggest compromise you would be making in using an answer with non-root approach is of not accounting those pesky but only milliseconds.


Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes