Home | History | Annotate | Download | only in tools
      1 # Copyright 2018 The Android Open Source Project
      2 #
      3 # Licensed under the Apache License, Version 2.0 (the "License");
      4 # you may not use this file except in compliance with the License.
      5 # You may obtain a copy of the License at
      6 #
      7 #      http://www.apache.org/licenses/LICENSE-2.0
      8 #
      9 # Unless required by applicable law or agreed to in writing, software
     10 # distributed under the License is distributed on an "AS IS" BASIS,
     11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 # See the License for the specific language governing permissions and
     13 # limitations under the License.
     14 
     15 import os
     16 import subprocess
     17 import sys
     18 
     19 CHARGE_PERCENT_START = 40
     20 CHARGE_PERCENT_STOP = 60
     21 
     22 
     23 def set_device_charging_limits(device_id):
     24     """Set the start/stop percentages for charging.
     25 
     26     This can keep battery from overcharging.
     27     Args:
     28         device_id:  str; device ID to set limits
     29     """
     30     print 'Rooting device %s' % device_id
     31     cmd = ('adb -s %s root' % device_id)
     32     process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE,
     33                                stderr=subprocess.PIPE)
     34     pout, perr = process.communicate()
     35     if 'cannot' in pout.lower() or perr:  # 'cannot root' returns no error
     36         print ' Warning: unable to root %s and set charging limits.' % device_id
     37     else:
     38         print ' Setting charging limits on %s' % device_id
     39         cmd = ('adb -s %s shell setprop persist.vendor.charge.start.level %d' % (
     40                 device_id, CHARGE_PERCENT_START))
     41         process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE,
     42                                    stderr=subprocess.PIPE)
     43         _, perr = process.communicate()
     44         if not perr:
     45             print ' Min: %d%%' % CHARGE_PERCENT_START
     46         else:
     47             print ' Warning: unable to set charging start limit.'
     48 
     49         cmd = ('adb -s %s shell setprop persist.vendor.charge.stop.level %d' % (
     50                 device_id, CHARGE_PERCENT_STOP))
     51         process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE,
     52                                    stderr=subprocess.PIPE)
     53         _, perr = process.communicate()
     54         if not perr:
     55             print ' Max: %d%%' % CHARGE_PERCENT_STOP
     56         else:
     57             print ' Warning: unable to set charging stop limit.'
     58 
     59         print 'Unrooting device %s' % device_id
     60         cmd = ('adb -s %s unroot' % device_id)
     61         subprocess.call(cmd.split(), stdout=subprocess.PIPE)
     62 
     63 
     64 def main():
     65     """Set charging limits for battery."""
     66 
     67     device_id = None
     68     for s in sys.argv[1:]:
     69         if s[:7] == 'device=' and len(s) > 7:
     70             device_id = s[7:]
     71 
     72     if device_id:
     73         set_device_charging_limits(device_id)
     74     else:
     75         print 'Usage: python %s device=$DEVICE_ID' % os.path.basename(__file__)
     76 
     77 if __name__ == '__main__':
     78     main()
     79