tzh
2024-08-22 c7d0944258c7d0943aa7b2211498fd612971ce27
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Copyright 2015 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
 
import re
import subprocess
import sys
import time
 
DISPLAY_LEVEL = 96  # [0:255] Depends on tablet model. Adjust for best result.
DISPLAY_CMD_WAIT = 0.5  # seconds. Screen commands take time to have effect
DISPLAY_TIMEOUT = 1800000  # ms
 
 
def main():
    """Power up and unlock screen as needed."""
    screen_id = None
    display_level = DISPLAY_LEVEL
    for s in sys.argv[1:]:
        if s[:7] == 'screen=' and len(s) > 7:
            screen_id = s[7:]
        if s[:11] == 'brightness=' and len(s) > 11:
            display_level = int(s[11:])
            if display_level < 0 or display_level > 255:
                print 'Invalid brightness value. Range is [0-255]'
                display_level = DISPLAY_LEVEL
 
    if not screen_id:
        print 'Error: need to specify screen serial'
        assert False
 
    # turn on screen if necessary and unlock
    cmd = ('adb -s %s shell dumpsys display | egrep "mScreenState"'
           % screen_id)
    process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
    cmd_ret = process.stdout.read()
    screen_state = re.split(r'[s|=]', cmd_ret)[-1]
    power_event = ('adb -s %s shell input keyevent POWER' % screen_id)
    subprocess.Popen(power_event.split())
    time.sleep(DISPLAY_CMD_WAIT)
    if 'ON' in screen_state:
        print 'Screen was ON. Toggling to refresh.'
        subprocess.Popen(power_event.split())
        time.sleep(DISPLAY_CMD_WAIT)
    else:
        print 'Screen was OFF. Powered ON.'
    unlock = ('adb -s %s wait-for-device shell wm dismiss-keyguard'
              % screen_id)
    subprocess.Popen(unlock.split())
    time.sleep(DISPLAY_CMD_WAIT)
 
    # set to manual mode and set brightness
    manual = ('adb -s %s shell settings put system screen_brightness_mode 0'
              % screen_id)
    subprocess.Popen(manual.split())
    time.sleep(DISPLAY_CMD_WAIT)
    print 'Tablet display brightness set to %d' % display_level
    bright = ('adb -s %s shell settings put system screen_brightness %d'
              % (screen_id, display_level))
    subprocess.Popen(bright.split())
    time.sleep(DISPLAY_CMD_WAIT)
 
    # set screen to dim at max time (30min)
    stay_bright = ('adb -s %s shell settings put system screen_off_timeout %d'
                   % (screen_id, DISPLAY_TIMEOUT))
    subprocess.Popen(stay_bright.split())
    time.sleep(DISPLAY_CMD_WAIT)
 
if __name__ == '__main__':
    main()