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 5import logging 6 7logger = logging.getLogger(__name__) 8 9_LOCK_SCREEN_SETTINGS_PATH = '/data/system/locksettings.db' 10_ALTERNATE_LOCK_SCREEN_SETTINGS_PATH = ( 11 '/data/data/com.android.providers.settings/databases/settings.db') 12PASSWORD_QUALITY_UNSPECIFIED = '0' 13_COMPATIBLE_BUILD_TYPES = ['userdebug', 'eng'] 14 15 16ENABLE_LOCATION_SETTINGS = [ 17 # Note that setting these in this order is required in order for all of 18 # them to take and stick through a reboot. 19 ('com.google.settings/partner', [ 20 ('use_location_for_services', 1), 21 ]), 22 ('settings/secure', [ 23 # Ensure Geolocation is enabled and allowed for tests. 24 ('location_providers_allowed', 'gps,network'), 25 ]), 26 ('com.google.settings/partner', [ 27 ('network_location_opt_in', 1), 28 ]) 29] 30 31DISABLE_LOCATION_SETTINGS = [ 32 ('com.google.settings/partner', [ 33 ('use_location_for_services', 0), 34 ]), 35 ('settings/secure', [ 36 # Ensure Geolocation is disabled. 37 ('location_providers_allowed', ''), 38 ]), 39] 40 41ENABLE_MOCK_LOCATION_SETTINGS = [ 42 ('settings/secure', [ 43 ('mock_location', 1), 44 ]), 45] 46 47DISABLE_MOCK_LOCATION_SETTINGS = [ 48 ('settings/secure', [ 49 ('mock_location', 0), 50 ]), 51] 52 53DETERMINISTIC_DEVICE_SETTINGS = [ 54 ('settings/global', [ 55 ('assisted_gps_enabled', 0), 56 57 # Disable "auto time" and "auto time zone" to avoid network-provided time 58 # to overwrite the device's datetime and timezone synchronized from host 59 # when running tests later. See b/6569849. 60 ('auto_time', 0), 61 ('auto_time_zone', 0), 62 63 ('development_settings_enabled', 1), 64 65 # Flag for allowing ActivityManagerService to send ACTION_APP_ERROR intents 66 # on application crashes and ANRs. If this is disabled, the crash/ANR dialog 67 # will never display the "Report" button. 68 # Type: int ( 0 = disallow, 1 = allow ) 69 ('send_action_app_error', 0), 70 71 ('stay_on_while_plugged_in', 3), 72 73 ('verifier_verify_adb_installs', 0), 74 ]), 75 ('settings/secure', [ 76 ('allowed_geolocation_origins', 77 'http://www.google.co.uk http://www.google.com'), 78 79 # Ensure that we never get random dialogs like "Unfortunately the process 80 # android.process.acore has stopped", which steal the focus, and make our 81 # automation fail (because the dialog steals the focus then mistakenly 82 # receives the injected user input events). 83 ('anr_show_background', 0), 84 85 ('lockscreen.disabled', 1), 86 87 ('screensaver_enabled', 0), 88 89 ('skip_first_use_hints', 1), 90 ]), 91 ('settings/system', [ 92 # Don't want devices to accidentally rotate the screen as that could 93 # affect performance measurements. 94 ('accelerometer_rotation', 0), 95 96 ('lockscreen.disabled', 1), 97 98 # Turn down brightness and disable auto-adjust so that devices run cooler. 99 ('screen_brightness', 5), 100 ('screen_brightness_mode', 0), 101 102 ('user_rotation', 0), 103 ]), 104] 105 106NETWORK_DISABLED_SETTINGS = [ 107 ('settings/global', [ 108 ('airplane_mode_on', 1), 109 ('wifi_on', 0), 110 ]), 111] 112 113 114class ContentSettings(dict): 115 116 """A dict interface to interact with device content settings. 117 118 System properties are key/value pairs as exposed by adb shell content. 119 """ 120 121 def __init__(self, table, device): 122 super(ContentSettings, self).__init__() 123 self._table = table 124 self._device = device 125 126 @staticmethod 127 def _GetTypeBinding(value): 128 if isinstance(value, bool): 129 return 'b' 130 if isinstance(value, float): 131 return 'f' 132 if isinstance(value, int): 133 return 'i' 134 if isinstance(value, long): 135 return 'l' 136 if isinstance(value, str): 137 return 's' 138 raise ValueError('Unsupported type %s' % type(value)) 139 140 def iteritems(self): 141 # Example row: 142 # 'Row: 0 _id=13, name=logging_id2, value=-1fccbaa546705b05' 143 for row in self._device.RunShellCommand( 144 ['content', 'query', '--uri', 'content://%s' % self._table], 145 check_return=True, as_root=True): 146 fields = row.split(', ') 147 key = None 148 value = None 149 for field in fields: 150 k, _, v = field.partition('=') 151 if k == 'name': 152 key = v 153 elif k == 'value': 154 value = v 155 if not key: 156 continue 157 if not value: 158 value = '' 159 yield key, value 160 161 def __getitem__(self, key): 162 return self._device.RunShellCommand( 163 ['content', 'query', '--uri', 'content://%s' % self._table, 164 '--where', "name='%s'" % key], 165 check_return=True, as_root=True).strip() 166 167 def __setitem__(self, key, value): 168 if key in self: 169 self._device.RunShellCommand( 170 ['content', 'update', '--uri', 'content://%s' % self._table, 171 '--bind', 'value:%s:%s' % (self._GetTypeBinding(value), value), 172 '--where', "name='%s'" % key], 173 check_return=True, as_root=True) 174 else: 175 self._device.RunShellCommand( 176 ['content', 'insert', '--uri', 'content://%s' % self._table, 177 '--bind', 'name:%s:%s' % (self._GetTypeBinding(key), key), 178 '--bind', 'value:%s:%s' % (self._GetTypeBinding(value), value)], 179 check_return=True, as_root=True) 180 181 def __delitem__(self, key): 182 self._device.RunShellCommand( 183 ['content', 'delete', '--uri', 'content://%s' % self._table, 184 '--bind', 'name:%s:%s' % (self._GetTypeBinding(key), key)], 185 check_return=True, as_root=True) 186 187 188def ConfigureContentSettings(device, desired_settings): 189 """Configures device content setings from a list. 190 191 Many settings are documented at: 192 http://developer.android.com/reference/android/provider/Settings.Global.html 193 http://developer.android.com/reference/android/provider/Settings.Secure.html 194 http://developer.android.com/reference/android/provider/Settings.System.html 195 196 Many others are undocumented. 197 198 Args: 199 device: A DeviceUtils instance for the device to configure. 200 desired_settings: A list of (table, [(key: value), ...]) for all 201 settings to configure. 202 """ 203 for table, key_value in desired_settings: 204 settings = ContentSettings(table, device) 205 for key, value in key_value: 206 settings[key] = value 207 logger.info('\n%s %s', table, (80 - len(table)) * '-') 208 for key, value in sorted(settings.iteritems()): 209 logger.info('\t%s: %s', key, value) 210 211 212def SetLockScreenSettings(device): 213 """Sets lock screen settings on the device. 214 215 On certain device/Android configurations we need to disable the lock screen in 216 a different database. Additionally, the password type must be set to 217 DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED. 218 Lock screen settings are stored in sqlite on the device in: 219 /data/system/locksettings.db 220 221 IMPORTANT: The first column is used as a primary key so that all rows with the 222 same value for that column are removed from the table prior to inserting the 223 new values. 224 225 Args: 226 device: A DeviceUtils instance for the device to configure. 227 228 Raises: 229 Exception if the setting was not properly set. 230 """ 231 if device.build_type not in _COMPATIBLE_BUILD_TYPES: 232 logger.warning('Unable to disable lockscreen on %s builds.', 233 device.build_type) 234 return 235 236 def get_lock_settings(table): 237 return [(table, 'lockscreen.disabled', '1'), 238 (table, 'lockscreen.password_type', PASSWORD_QUALITY_UNSPECIFIED), 239 (table, 'lockscreen.password_type_alternate', 240 PASSWORD_QUALITY_UNSPECIFIED)] 241 242 if device.FileExists(_LOCK_SCREEN_SETTINGS_PATH): 243 db = _LOCK_SCREEN_SETTINGS_PATH 244 locksettings = get_lock_settings('locksettings') 245 columns = ['name', 'user', 'value'] 246 generate_values = lambda k, v: [k, '0', v] 247 elif device.FileExists(_ALTERNATE_LOCK_SCREEN_SETTINGS_PATH): 248 db = _ALTERNATE_LOCK_SCREEN_SETTINGS_PATH 249 locksettings = get_lock_settings('secure') + get_lock_settings('system') 250 columns = ['name', 'value'] 251 generate_values = lambda k, v: [k, v] 252 else: 253 logger.warning('Unable to find database file to set lock screen settings.') 254 return 255 256 for table, key, value in locksettings: 257 # Set the lockscreen setting for default user '0' 258 values = generate_values(key, value) 259 260 cmd = """begin transaction; 261delete from '%(table)s' where %(primary_key)s='%(primary_value)s'; 262insert into '%(table)s' (%(columns)s) values (%(values)s); 263commit transaction;""" % { 264 'table': table, 265 'primary_key': columns[0], 266 'primary_value': values[0], 267 'columns': ', '.join(columns), 268 'values': ', '.join(["'%s'" % value for value in values]) 269 } 270 output_msg = device.RunShellCommand( 271 ['sqlite3', db, cmd], check_return=True, as_root=True) 272 if output_msg: 273 logger.info(' '.join(output_msg)) 274