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.
6 class ContentSettings(dict):
8 """A dict interface to interact with device content settings.
10 System properties are key/value pairs as exposed by adb shell content.
13 def __init__(self, table, device):
14 super(ContentSettings, self).__init__()
15 sdk_version_string = device.old_interface.system_properties[
16 'ro.build.version.sdk']
18 sdk_version = int(sdk_version_string)
19 assert sdk_version >= 16, (
20 'ContentSettings supported only on SDK 16 and later')
22 assert False, ('Unknown SDK version %s' % sdk_version_string)
27 def _GetTypeBinding(value):
28 if isinstance(value, bool):
30 if isinstance(value, float):
32 if isinstance(value, int):
34 if isinstance(value, long):
36 if isinstance(value, str):
38 raise ValueError('Unsupported type %s' % type(value))
42 # 'Row: 0 _id=13, name=logging_id2, value=-1fccbaa546705b05'
43 for row in self._device.old_interface.RunShellCommandWithSU(
44 'content query --uri content://%s' % self._table):
45 fields = row.split(', ')
49 k, _, v = field.partition('=')
57 def __getitem__(self, key):
58 return self._device.old_interface.RunShellCommandWithSU(
59 'content query --uri content://%s --where "name=\'%s\'" '
60 '--projection value' % (self._table, key)).strip()
62 def __setitem__(self, key, value):
64 self._device.old_interface.RunShellCommandWithSU(
65 'content update --uri content://%s '
66 '--bind value:%s:%s --where "name=\'%s\'"' % (
68 self._GetTypeBinding(value), value, key))
70 self._device.old_interface.RunShellCommandWithSU(
71 'content insert --uri content://%s '
72 '--bind name:%s:%s --bind value:%s:%s' % (
74 self._GetTypeBinding(key), key,
75 self._GetTypeBinding(value), value))
77 def __delitem__(self, key):
78 self._device.old_interface.RunShellCommandWithSU(
79 'content delete --uri content://%s '
80 '--bind name:%s:%s' % (
82 self._GetTypeBinding(key), key))