1 # Copyright 2014 The Chromium Authors. All rights reserved. 2 # Use of this source code is governed by a BSD-style license that can be 3 # found in the LICENSE file. 4 5 import logging 6 7 from pylib import content_settings 8 9 _LOCK_SCREEN_SETTINGS_PATH = '/data/system/locksettings.db' 10 PASSWORD_QUALITY_UNSPECIFIED = '0' 11 12 13 def ConfigureContentSettings(device, desired_settings): 14 """Configures device content setings from a list. 15 16 Many settings are documented at: 17 http://developer.android.com/reference/android/provider/Settings.Global.html 18 http://developer.android.com/reference/android/provider/Settings.Secure.html 19 http://developer.android.com/reference/android/provider/Settings.System.html 20 21 Many others are undocumented. 22 23 Args: 24 device: A DeviceUtils instance for the device to configure. 25 desired_settings: A list of (table, [(key: value), ...]) for all 26 settings to configure. 27 """ 28 try: 29 sdk_version = int(device.GetProp('ro.build.version.sdk')) 30 except ValueError: 31 logging.error('Skipping content settings configuration, unknown sdk %s', 32 device.GetProp('ro.build.version.sdk')) 33 return 34 35 if sdk_version < 16: 36 logging.error('Skipping content settings configuration due to outdated sdk') 37 return 38 39 if device.GetProp('ro.build.type') == 'userdebug': 40 for table, key_value in desired_settings: 41 settings = content_settings.ContentSettings(table, device) 42 for key, value in key_value: 43 settings[key] = value 44 logging.info('\n%s %s', table, (80 - len(table)) * '-') 45 for key, value in sorted(settings.iteritems()): 46 logging.info('\t%s: %s', key, value) 47 48 49 def SetLockScreenSettings(device): 50 """Sets lock screen settings on the device. 51 52 On certain device/Android configurations we need to disable the lock screen in 53 a different database. Additionally, the password type must be set to 54 DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED. 55 Lock screen settings are stored in sqlite on the device in: 56 /data/system/locksettings.db 57 58 IMPORTANT: The first column is used as a primary key so that all rows with the 59 same value for that column are removed from the table prior to inserting the 60 new values. 61 62 Args: 63 device: A DeviceUtils instance for the device to configure. 64 65 Raises: 66 Exception if the setting was not properly set. 67 """ 68 if (not device.old_interface.FileExistsOnDevice(_LOCK_SCREEN_SETTINGS_PATH) or 69 device.GetProp('ro.build.type') != 'userdebug'): 70 return 71 72 db = _LOCK_SCREEN_SETTINGS_PATH 73 locksettings = [('locksettings', 'lockscreen.disabled', '1'), 74 ('locksettings', 'lockscreen.password_type', 75 PASSWORD_QUALITY_UNSPECIFIED), 76 ('locksettings', 'lockscreen.password_type_alternate', 77 PASSWORD_QUALITY_UNSPECIFIED)] 78 for table, key, value in locksettings: 79 # Set the lockscreen setting for default user '0' 80 columns = ['name', 'user', 'value'] 81 values = [key, '0', value] 82 83 cmd = """begin transaction; 84 delete from '%(table)s' where %(primary_key)s='%(primary_value)s'; 85 insert into '%(table)s' (%(columns)s) values (%(values)s); 86 commit transaction;""" % { 87 'table': table, 88 'primary_key': columns[0], 89 'primary_value': values[0], 90 'columns': ', '.join(columns), 91 'values': ', '.join(["'%s'" % value for value in values]) 92 } 93 output_msg = device.RunShellCommand('sqlite3 %s "%s"' % (db, cmd)) 94 if output_msg: 95 print ' '.join(output_msg) 96 97 98 ENABLE_LOCATION_SETTINGS = [ 99 # Note that setting these in this order is required in order for all of 100 # them to take and stick through a reboot. 101 ('com.google.settings/partner', [ 102 ('use_location_for_services', 1), 103 ]), 104 ('settings/secure', [ 105 # Ensure Geolocation is enabled and allowed for tests. 106 ('location_providers_allowed', 'gps,network'), 107 ]), 108 ('com.google.settings/partner', [ 109 ('network_location_opt_in', 1), 110 ]) 111 ] 112 113 DISABLE_LOCATION_SETTINGS = [ 114 ('com.google.settings/partner', [ 115 ('use_location_for_services', 0), 116 ]), 117 ('settings/secure', [ 118 # Ensure Geolocation is disabled. 119 ('location_providers_allowed', ''), 120 ]), 121 ] 122 123 DETERMINISTIC_DEVICE_SETTINGS = [ 124 ('settings/global', [ 125 ('assisted_gps_enabled', 0), 126 127 # Disable "auto time" and "auto time zone" to avoid network-provided time 128 # to overwrite the device's datetime and timezone synchronized from host 129 # when running tests later. See b/6569849. 130 ('auto_time', 0), 131 ('auto_time_zone', 0), 132 133 ('development_settings_enabled', 1), 134 135 # Flag for allowing ActivityManagerService to send ACTION_APP_ERROR intents 136 # on application crashes and ANRs. If this is disabled, the crash/ANR dialog 137 # will never display the "Report" button. 138 # Type: int ( 0 = disallow, 1 = allow ) 139 ('send_action_app_error', 0), 140 141 ('stay_on_while_plugged_in', 3), 142 143 ('verifier_verify_adb_installs', 0), 144 ]), 145 ('settings/secure', [ 146 ('allowed_geolocation_origins', 147 'http://www.google.co.uk http://www.google.com'), 148 149 # Ensure that we never get random dialogs like "Unfortunately the process 150 # android.process.acore has stopped", which steal the focus, and make our 151 # automation fail (because the dialog steals the focus then mistakenly 152 # receives the injected user input events). 153 ('anr_show_background', 0), 154 155 ('lockscreen.disabled', 1), 156 157 ('screensaver_enabled', 0), 158 ]), 159 ('settings/system', [ 160 # Don't want devices to accidentally rotate the screen as that could 161 # affect performance measurements. 162 ('accelerometer_rotation', 0), 163 164 ('lockscreen.disabled', 1), 165 166 # Turn down brightness and disable auto-adjust so that devices run cooler. 167 ('screen_brightness', 5), 168 ('screen_brightness_mode', 0), 169 170 ('user_rotation', 0), 171 ]), 172 ] 173 174 NETWORK_DISABLED_SETTINGS = [ 175 ('settings/global', [ 176 ('airplane_mode_on', 1), 177 ('wifi_on', 0), 178 ]), 179 ] 180