Home | History | Annotate | Download | only in android
      1 # SPDX-License-Identifier: Apache-2.0
      2 #
      3 # Copyright (C) 2015, ARM Limited and contributors.
      4 #
      5 # Licensed under the Apache License, Version 2.0 (the "License"); you may
      6 # not use this file except in compliance with the License.
      7 # You may obtain a copy of the License at
      8 #
      9 # http://www.apache.org/licenses/LICENSE-2.0
     10 #
     11 # Unless required by applicable law or agreed to in writing, software
     12 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     13 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 # See the License for the specific language governing permissions and
     15 # limitations under the License.
     16 #
     17 
     18 import logging
     19 from system import System
     20 from time import sleep
     21 
     22 class Screen(object):
     23     """
     24     Set of utility functions to control an Android Screen
     25     """
     26 
     27     @staticmethod
     28     def set_orientation(target, auto=True, portrait=None):
     29         """
     30         Set screen orientation mode
     31         """
     32         log = logging.getLogger('Screen')
     33         acc_mode = 1 if auto else 0
     34         # Force manual orientation of portrait specified
     35         if portrait is not None:
     36             acc_mode = 0
     37             log.info('Force manual orientation')
     38         if acc_mode == 0:
     39             usr_mode = 0 if portrait else 1
     40             usr_mode_str = 'PORTRAIT' if portrait else 'LANDSCAPE'
     41             log.info('Set orientation: %s', usr_mode_str)
     42         else:
     43             usr_mode = 0
     44             log.info('Set orientation: AUTO')
     45 
     46         if acc_mode == 0:
     47             target.execute('content insert '\
     48                            '--uri content://settings/system '\
     49                            '--bind name:s:accelerometer_rotation '\
     50                            '--bind value:i:{}'.format(acc_mode))
     51             target.execute('content insert '\
     52                            '--uri content://settings/system '\
     53                            '--bind name:s:user_rotation '\
     54                            '--bind value:i:{}'.format(usr_mode))
     55         else:
     56             # Force PORTRAIT mode when activation AUTO rotation
     57             target.execute('content insert '\
     58                            '--uri content://settings/system '\
     59                            '--bind name:s:user_rotation '\
     60                            '--bind value:i:{}'.format(usr_mode))
     61             target.execute('content insert '\
     62                            '--uri content://settings/system '\
     63                            '--bind name:s:accelerometer_rotation '\
     64                            '--bind value:i:{}'.format(acc_mode))
     65 
     66     @staticmethod
     67     def set_brightness(target, auto=True, percent=None):
     68         """
     69         Set screen brightness percentage
     70         """
     71         log = logging.getLogger('Screen')
     72         bri_mode = 1 if auto else 0
     73         # Force manual brightness if a percent specified
     74         if percent:
     75             bri_mode = 0
     76         target.execute('content insert '\
     77                        '--uri content://settings/system '\
     78                        '--bind name:s:screen_brightness_mode '\
     79                        '--bind value:i:{}'.format(bri_mode))
     80         if bri_mode == 0:
     81             if percent<0 or percent>100:
     82                 msg = "Screen brightness {} out of range (0,100)"\
     83                       .format(percent)
     84                 raise ValueError(msg)
     85             value = 255 * percent / 100
     86             target.execute('content insert '\
     87                            '--uri content://settings/system '\
     88                            '--bind name:s:screen_brightness '\
     89                            '--bind value:i:{}'.format(value))
     90             log.info('Set brightness: %d%%', percent)
     91         else:
     92             log.info('Set brightness: AUTO')
     93 
     94     @staticmethod
     95     def set_dim(target, auto=True):
     96         """
     97         Set screen dimming mode
     98         """
     99         log = logging.getLogger('Screen')
    100         dim_mode = 1 if auto else 0
    101         dim_mode_str = 'ON' if auto else 'OFF'
    102         target.execute('content insert '\
    103                        '--uri content://settings/system '\
    104                        '--bind name:s:dim_screen '\
    105                        '--bind value:i:{}'.format(dim_mode))
    106         log.info('Dim screen mode: %s', dim_mode_str)
    107 
    108     @staticmethod
    109     def set_timeout(target, seconds=30):
    110         """
    111         Set screen off timeout in seconds
    112         """
    113         log = logging.getLogger('Screen')
    114         if seconds<0:
    115             msg = "Screen timeout {}: cannot be negative".format(seconds)
    116             raise ValueError(msg)
    117         value = seconds * 1000
    118         target.execute('content insert '\
    119                        '--uri content://settings/system '\
    120                        '--bind name:s:screen_off_timeout '\
    121                        '--bind value:i:{}'.format(value))
    122         log.info('Screen timeout: %d [s]', seconds)
    123 
    124     @staticmethod
    125     def set_defaults(target):
    126         """
    127         Reset screen settings to a reasonable default
    128         """
    129         Screen.set_orientation(target)
    130         Screen.set_brightness(target)
    131         Screen.set_dim(target)
    132         Screen.set_timeout(target)
    133         Screen.set_doze_always_on(target)
    134 
    135     @staticmethod
    136     def get_screen_density(target):
    137         """
    138         Get screen density of the device.
    139         """
    140         return target.execute('getprop ro.sf.lcd_density')
    141 
    142     @staticmethod
    143     def set_screen(target, on=True):
    144         log = logging.getLogger('Screen')
    145         if not on:
    146             log.info('Setting screen OFF')
    147             System.sleep(target)
    148             return
    149         log.info('Setting screen ON')
    150         System.wakeup(target)
    151 
    152     @staticmethod
    153     def unlock(target):
    154        Screen.set_screen(target, on=True)
    155        sleep(1)
    156        System.menu(target)
    157        System.home(target)
    158 
    159     @staticmethod
    160     def set_doze_always_on(target, on=True):
    161         log = logging.getLogger('Screen')
    162         if not on:
    163             log.info('Setting doze always on OFF')
    164             target.execute('settings put secure doze_always_on 0')
    165             return
    166         log.info('Setting doze always on ON')
    167         target.execute('settings put secure doze_always_on 1')
    168 
    169 # vim :set tabstop=4 shiftwidth=4 expandtab
    170