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, adb):
14 super(ContentSettings, self).__init__()
16 sdk_version = int(adb.system_properties['ro.build.version.sdk'])
17 assert sdk_version >= 16, (
18 'ContentSettings supported only on SDK 16 and later')
20 assert False, ('Unknown SDK version %s' %
21 adb.system_properties['ro.build.version.sdk'])
26 def _GetTypeBinding(value):
27 if isinstance(value, bool):
29 if isinstance(value, float):
31 if isinstance(value, int):
33 if isinstance(value, long):
35 if isinstance(value, str):
37 raise ValueError('Unsupported type %s' % type(value))
41 # 'Row: 0 _id=13, name=logging_id2, value=-1fccbaa546705b05'
42 for row in self._adb.RunShellCommandWithSU(
43 'content query --uri content://%s' % self._table):
44 fields = row.split(', ')
48 k, _, v = field.partition('=')
56 def __getitem__(self, key):
57 return self._adb.RunShellCommandWithSU(
58 'content query --uri content://%s --where "name=\'%s\'" '
59 '--projection value' % (self._table, key)).strip()
61 def __setitem__(self, key, value):
63 self._adb.RunShellCommandWithSU(
64 'content update --uri content://%s '
65 '--bind value:%s:%s --where "name=\'%s\'"' % (
67 self._GetTypeBinding(value), value, key))
69 self._adb.RunShellCommandWithSU(
70 'content insert --uri content://%s '
71 '--bind name:%s:%s --bind value:%s:%s' % (
73 self._GetTypeBinding(key), key,
74 self._GetTypeBinding(value), value))
76 def __delitem__(self, key):
77 self._adb.RunShellCommandWithSU(
78 'content delete --uri content://%s '
79 '--bind name:%s:%s' % (
81 self._GetTypeBinding(key), key))