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