1 # Copyright 2016 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 re 16 import subprocess 17 import sys 18 import time 19 20 TURN_OFF_DELAY = 1 # seconds. Needed for back to back runs 21 22 23 def main(): 24 """Put screen to sleep.""" 25 screen_id = '' 26 for s in sys.argv[1:]: 27 if s[:7] == 'screen=' and len(s) > 7: 28 screen_id = s[7:] 29 30 if not screen_id: 31 print 'Error: need to specify screen serial' 32 assert False 33 34 cmd = ('adb -s %s shell dumpsys power | egrep "Display Power"' 35 % screen_id) 36 process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE) 37 cmd_ret = process.stdout.read() 38 screen_state = re.split(r'[s|=]', cmd_ret)[-1] 39 if 'OFF' in screen_state: 40 print 'Screen already OFF.' 41 else: 42 pwrdn = ('adb -s %s shell input keyevent POWER' % screen_id) 43 subprocess.Popen(pwrdn.split()) 44 time.sleep(TURN_OFF_DELAY) 45 46 if __name__ == '__main__': 47 main() 48