OLD | NEW |
| (Empty) |
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 from pylib import constants | |
6 | |
7 | |
8 class ContentSettings(dict): | |
9 | |
10 """A dict interface to interact with device content settings. | |
11 | |
12 System properties are key/value pairs as exposed by adb shell content. | |
13 """ | |
14 | |
15 def __init__(self, table, device): | |
16 super(ContentSettings, self).__init__() | |
17 self._table = table | |
18 self._device = device | |
19 | |
20 @staticmethod | |
21 def _GetTypeBinding(value): | |
22 if isinstance(value, bool): | |
23 return 'b' | |
24 if isinstance(value, float): | |
25 return 'f' | |
26 if isinstance(value, int): | |
27 return 'i' | |
28 if isinstance(value, long): | |
29 return 'l' | |
30 if isinstance(value, str): | |
31 return 's' | |
32 raise ValueError('Unsupported type %s' % type(value)) | |
33 | |
34 def iteritems(self): | |
35 # Example row: | |
36 # 'Row: 0 _id=13, name=logging_id2, value=-1fccbaa546705b05' | |
37 for row in self._device.RunShellCommand( | |
38 'content query --uri content://%s' % self._table, as_root=True): | |
39 fields = row.split(', ') | |
40 key = None | |
41 value = None | |
42 for field in fields: | |
43 k, _, v = field.partition('=') | |
44 if k == 'name': | |
45 key = v | |
46 elif k == 'value': | |
47 value = v | |
48 if not key: | |
49 continue | |
50 if not value: | |
51 value = '' | |
52 yield key, value | |
53 | |
54 def __getitem__(self, key): | |
55 return self._device.RunShellCommand( | |
56 'content query --uri content://%s --where "name=\'%s\'" ' | |
57 '--projection value' % (self._table, key), as_root=True).strip() | |
58 | |
59 def __setitem__(self, key, value): | |
60 if key in self: | |
61 self._device.RunShellCommand( | |
62 'content update --uri content://%s ' | |
63 '--bind value:%s:%s --where "name=\'%s\'"' % ( | |
64 self._table, | |
65 self._GetTypeBinding(value), value, key), | |
66 as_root=True) | |
67 else: | |
68 self._device.RunShellCommand( | |
69 'content insert --uri content://%s ' | |
70 '--bind name:%s:%s --bind value:%s:%s' % ( | |
71 self._table, | |
72 self._GetTypeBinding(key), key, | |
73 self._GetTypeBinding(value), value), | |
74 as_root=True) | |
75 | |
76 def __delitem__(self, key): | |
77 self._device.RunShellCommand( | |
78 'content delete --uri content://%s ' | |
79 '--bind name:%s:%s' % ( | |
80 self._table, | |
81 self._GetTypeBinding(key), key), | |
82 as_root=True) | |
OLD | NEW |