OLD | NEW |
---|---|
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 | 5 |
6 class ContentSettings(dict): | 6 class ContentSettings(dict): |
7 | 7 |
8 """A dict interface to interact with device content settings. | 8 """A dict interface to interact with device content settings. |
9 | 9 |
10 System properties are key/value pairs as exposed by adb shell content. | 10 System properties are key/value pairs as exposed by adb shell content. |
11 """ | 11 """ |
12 | 12 |
13 def __init__(self, table, adb): | 13 def __init__(self, table, device): |
14 super(ContentSettings, self).__init__() | 14 super(ContentSettings, self).__init__() |
15 sdk_version_string = device.old_interface.system_properties[ | |
16 'ro.build.version.sdk'] | |
15 try: | 17 try: |
16 sdk_version = int(adb.system_properties['ro.build.version.sdk']) | 18 sdk_version = int(sdk_version_string) |
17 assert sdk_version >= 16, ( | 19 assert sdk_version >= 16, ( |
18 'ContentSettings supported only on SDK 16 and later') | 20 'ContentSettings supported only on SDK 16 and later') |
19 except ValueError: | 21 except ValueError: |
20 assert False, ('Unknown SDK version %s' % | 22 assert False, ('Unknown SDK version %s' % sdk_version_string) |
21 adb.system_properties['ro.build.version.sdk']) | |
22 self._table = table | 23 self._table = table |
23 self._adb = adb | 24 # TODO check if this is used. |
craigdh
2014/04/09 15:55:02
TODO should have a name. But why not do it now?
| |
25 self._device = device | |
24 | 26 |
25 @staticmethod | 27 @staticmethod |
26 def _GetTypeBinding(value): | 28 def _GetTypeBinding(value): |
27 if isinstance(value, bool): | 29 if isinstance(value, bool): |
28 return 'b' | 30 return 'b' |
29 if isinstance(value, float): | 31 if isinstance(value, float): |
30 return 'f' | 32 return 'f' |
31 if isinstance(value, int): | 33 if isinstance(value, int): |
32 return 'i' | 34 return 'i' |
33 if isinstance(value, long): | 35 if isinstance(value, long): |
34 return 'l' | 36 return 'l' |
35 if isinstance(value, str): | 37 if isinstance(value, str): |
36 return 's' | 38 return 's' |
37 raise ValueError('Unsupported type %s' % type(value)) | 39 raise ValueError('Unsupported type %s' % type(value)) |
38 | 40 |
39 def iteritems(self): | 41 def iteritems(self): |
40 # Example row: | 42 # Example row: |
41 # 'Row: 0 _id=13, name=logging_id2, value=-1fccbaa546705b05' | 43 # 'Row: 0 _id=13, name=logging_id2, value=-1fccbaa546705b05' |
42 for row in self._adb.RunShellCommandWithSU( | 44 for row in self._device.old_interface.RunShellCommandWithSU( |
43 'content query --uri content://%s' % self._table): | 45 'content query --uri content://%s' % self._table): |
44 fields = row.split(', ') | 46 fields = row.split(', ') |
45 key = None | 47 key = None |
46 value = None | 48 value = None |
47 for field in fields: | 49 for field in fields: |
48 k, _, v = field.partition('=') | 50 k, _, v = field.partition('=') |
49 if k == 'name': | 51 if k == 'name': |
50 key = v | 52 key = v |
51 elif k == 'value': | 53 elif k == 'value': |
52 value = v | 54 value = v |
53 assert key, value | 55 assert key, value |
54 yield key, value | 56 yield key, value |
55 | 57 |
56 def __getitem__(self, key): | 58 def __getitem__(self, key): |
57 return self._adb.RunShellCommandWithSU( | 59 return self._device.old_interface.RunShellCommandWithSU( |
58 'content query --uri content://%s --where "name=\'%s\'" ' | 60 'content query --uri content://%s --where "name=\'%s\'" ' |
59 '--projection value' % (self._table, key)).strip() | 61 '--projection value' % (self._table, key)).strip() |
60 | 62 |
61 def __setitem__(self, key, value): | 63 def __setitem__(self, key, value): |
62 if key in self: | 64 if key in self: |
63 self._adb.RunShellCommandWithSU( | 65 self._device.old_interface.RunShellCommandWithSU( |
64 'content update --uri content://%s ' | 66 'content update --uri content://%s ' |
65 '--bind value:%s:%s --where "name=\'%s\'"' % ( | 67 '--bind value:%s:%s --where "name=\'%s\'"' % ( |
66 self._table, | 68 self._table, |
67 self._GetTypeBinding(value), value, key)) | 69 self._GetTypeBinding(value), value, key)) |
68 else: | 70 else: |
69 self._adb.RunShellCommandWithSU( | 71 self._device.old_interface.RunShellCommandWithSU( |
70 'content insert --uri content://%s ' | 72 'content insert --uri content://%s ' |
71 '--bind name:%s:%s --bind value:%s:%s' % ( | 73 '--bind name:%s:%s --bind value:%s:%s' % ( |
72 self._table, | 74 self._table, |
73 self._GetTypeBinding(key), key, | 75 self._GetTypeBinding(key), key, |
74 self._GetTypeBinding(value), value)) | 76 self._GetTypeBinding(value), value)) |
75 | 77 |
76 def __delitem__(self, key): | 78 def __delitem__(self, key): |
77 self._adb.RunShellCommandWithSU( | 79 self._device.old_interface.RunShellCommandWithSU( |
78 'content delete --uri content://%s ' | 80 'content delete --uri content://%s ' |
79 '--bind name:%s:%s' % ( | 81 '--bind name:%s:%s' % ( |
80 self._table, | 82 self._table, |
81 self._GetTypeBinding(key), key)) | 83 self._GetTypeBinding(key), key)) |
OLD | NEW |