Automatically limit (stop) battery charging at a given percentage


Question

Device : Moto X Play ( Rooted, Stock, Android 6.0.1, msm8916 Board, XT1562)



Why do I want to limit charging ?



( Let's leave out whether my reasons are valid or not and accept that I want to, if you disagree with my reasons)



To improve the longevity of battery , I wish to limit charging when it reaches 90%, as explained here
Ideal charging / discharging percentage for maximum battery life?



Is it possible?



Yes, at least on my previous device Honor 6, as brought out here How do I override charging current on Huawei Honor 6?, with two different ways of doing it



I have tried unsuccessfully to identify the file responsible in which changing values would limit charging



I am looking for the system file responsible for stopping charging, with it's true path (without symlinks)



(as I saw on on my previous device, locating the path of file was a big challenge after identifying it)





Answer

Okay.. Got it :)



  • The first thing to note is on Honor 6, my previous device , there was a single file I could play around with to stop charging automatically



  • On Moto X play, there are two distinct files - one for disconnecting the charger and other for removing charging symbol ( In other words, you could stop charging and still have the charger connected symbol showing). Till I realised this, I was running around in circles, since I took charging symbol to show that charging was happening ( and assuming I was dealing with the wrong file ). While experimenting, when I noticed charge drop despite charging symbol showing, I verified with Ampere app and wisdom dawned !




What are the files?


(Edit:30 Mar 20 Jackpot! Found this awesome repository
of files for various devices by the developer of Advanced Charging Controller
Magisk Module. You are better off using that versatile module but in case you want to DIY, it's a great start) .


There may be other files, but the files I homed on too are :



  1. For Charging Icon : /sys/devices/soc.0/78d9000.usb/power_supply/usb/online ( For disabling icon change value of file from 0 to 1)



  2. For charging : /sys/devices/soc.0/78b5000.i2c/i2c-1/1-001c/power_supply/battery/charging_enabled ( For disabling charging change value of file from 1 to 0)




For automating this, approach was similar to my answer here using Terminal Emulator and Macrodroid as explained here - How to automate command line when specific application is launched?


How did I find the files?


Inelegantly, and I am hoping that somebody posts a good way. Here is a way that may work for you in identifying the file and path


Assumptions



  • Long hours on the net and some coding on GitHub ( which I don't understand at all ) pointed me to look at i2c and usb related files with names like charger_enabled or enable_charger. This seems to be true for Honor 6, Moto X and some Nexus devices. I have no clue whether it is chipset or device dependent



  • The true path of this file is in /sys. Again, may vary




Next Steps



  • Do a search for charger enabled or enable_charger or a wild card search with similar words in /sys



  • Change rw permissions to enable changing the content from 1to 0 or vice versa



  • Connect your charger and see the value in file, disconnect and repeat. If the value changes, it looks like the right file. While the charger is connected, change the value in file and if charging stops, you have hit it (More so, if the path is in i2c directory). Verify with Ampere app to confirm charging status



  • While you are at it, may as well copy original and modified files to a separate directory in your storage. Needed for automation



  • If your device is like mine where charging can be stopped but charging icon, shows, more grind to identify that file by keywords like online or connected or ???




If you are lucky, above should work for you




Just as additional information for posterity the Terminal Emulator shortcuts are :



  • For disabling charging and removing the charging icon


su –c "cp /storage/emulated/0/moto_charging/disable_charging/charging_enabled /sys/devices/soc.0/78b5000.i2c/i2c-1/1-001c/power_supply/battery/" ; su -c "cp /storage/emulated/0/moto_charging/no_symbol_charging/online /sys/devices/soc.0/78d9000.usb/power_supply/usb/"



  • For enabling charger and charging icon


su -c "cp /storage/emulated/0/moto_charging/show_charging/online /sys/devices/soc.0/78d9000.usb/power_supply/usb/" ; su –c "cp /storage/emulated/0/moto_charging/enable_charging/charging_enabled /sys/devices/soc.0/78b5000.i2c/i2c-1/1-001c/power_supply/battery/"


moto_charging is a folder created in Internal SD that has original and modified copies ( to enable and disable charging), and these files are copied into the system. Of course, requires root


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