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 15import re 16import subprocess 17import sys 18import time 19 20DISPLAY_LEVEL = 96 # [0:255] Depends on tablet model. Adjust for best result. 21DISPLAY_CMD_WAIT = 0.5 # seconds. Screen commands take time to have effect 22DISPLAY_TIMEOUT = 1800000 # ms 23 24 25def main(): 26 """Power up and unlock screen as needed.""" 27 screen_id = None 28 display_level = DISPLAY_LEVEL 29 for s in sys.argv[1:]: 30 if s[:7] == 'screen=' and len(s) > 7: 31 screen_id = s[7:] 32 if s[:11] == 'brightness=' and len(s) > 11: 33 display_level = int(s[11:]) 34 if display_level < 0 or display_level > 255: 35 print 'Invalid brightness value. Range is [0-255]' 36 display_level = DISPLAY_LEVEL 37 38 if not screen_id: 39 print 'Error: need to specify screen serial' 40 assert False 41 42 # turn on screen if necessary and unlock 43 cmd = ('adb -s %s shell dumpsys display | egrep "mScreenState"' 44 % screen_id) 45 process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE) 46 cmd_ret = process.stdout.read() 47 screen_state = re.split(r'[s|=]', cmd_ret)[-1] 48 power_event = ('adb -s %s shell input keyevent POWER' % screen_id) 49 subprocess.Popen(power_event.split()) 50 time.sleep(DISPLAY_CMD_WAIT) 51 if 'ON' in screen_state: 52 print 'Screen was ON. Toggling to refresh.' 53 subprocess.Popen(power_event.split()) 54 time.sleep(DISPLAY_CMD_WAIT) 55 else: 56 print 'Screen was OFF. Powered ON.' 57 unlock = ('adb -s %s wait-for-device shell wm dismiss-keyguard' 58 % screen_id) 59 subprocess.Popen(unlock.split()) 60 time.sleep(DISPLAY_CMD_WAIT) 61 62 # set to manual mode and set brightness 63 manual = ('adb -s %s shell settings put system screen_brightness_mode 0' 64 % screen_id) 65 subprocess.Popen(manual.split()) 66 time.sleep(DISPLAY_CMD_WAIT) 67 print 'Tablet display brightness set to %d' % display_level 68 bright = ('adb -s %s shell settings put system screen_brightness %d' 69 % (screen_id, display_level)) 70 subprocess.Popen(bright.split()) 71 time.sleep(DISPLAY_CMD_WAIT) 72 73 # set screen to dim at max time (30min) 74 stay_bright = ('adb -s %s shell settings put system screen_off_timeout %d' 75 % (screen_id, DISPLAY_TIMEOUT)) 76 subprocess.Popen(stay_bright.split()) 77 time.sleep(DISPLAY_CMD_WAIT) 78 79if __name__ == '__main__': 80 main() 81