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