1 # Copyright 2015 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 DISPLAY_LEVEL = 96 # [0:255] Depends on tablet model. Adjust for best result. 21 DISPLAY_WAIT = 0.5 # seconds. Screen commands take time to have effect 22 23 24 def main(): 25 """Power up and unlock screen as needed.""" 26 screen_id = None 27 for s in sys.argv[1:]: 28 if s[:7] == 'screen=' and len(s) > 7: 29 screen_id = s[7:] 30 31 if not screen_id: 32 print 'Error: need to specify screen serial' 33 assert False 34 35 # turn on screen if necessary and unlock 36 cmd = ('adb -s %s shell dumpsys display | egrep "mScreenState"' 37 % screen_id) 38 process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE) 39 cmd_ret = process.stdout.read() 40 screen_state = re.split(r'[s|=]', cmd_ret)[-1] 41 if 'OFF' in screen_state: 42 print 'Screen OFF. Turning ON.' 43 wakeup = ('adb -s %s shell input keyevent POWER' % screen_id) 44 subprocess.Popen(wakeup.split()) 45 time.sleep(DISPLAY_WAIT) 46 unlock = ('adb -s %s wait-for-device shell wm dismiss-keyguard' 47 % screen_id) 48 subprocess.Popen(unlock.split()) 49 time.sleep(DISPLAY_WAIT) 50 51 # set brightness 52 print 'Tablet display brightness set to %d' % DISPLAY_LEVEL 53 bright = ('adb -s %s shell settings put system screen_brightness %d' 54 % (screen_id, DISPLAY_LEVEL)) 55 subprocess.Popen(bright.split()) 56 time.sleep(DISPLAY_WAIT) 57 58 59 if __name__ == '__main__': 60 main() 61