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