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