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.
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')
24 def _GetTypeBinding(value
):
25 if isinstance(value
, bool):
27 if isinstance(value
, float):
29 if isinstance(value
, int):
31 if isinstance(value
, long):
33 if isinstance(value
, str):
35 raise ValueError('Unsupported type %s' % type(value
))
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(', ')
46 k
, _
, v
= field
.partition('=')
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
):
64 self
._device
.RunShellCommand(
65 'content update --uri content://%s '
66 '--bind value:%s:%s --where "name=\'%s\'"' % (
68 self
._GetTypeBinding
(value
), value
, key
),
71 self
._device
.RunShellCommand(
72 'content insert --uri content://%s '
73 '--bind name:%s:%s --bind value:%s:%s' % (
75 self
._GetTypeBinding
(key
), key
,
76 self
._GetTypeBinding
(value
), value
),
79 def __delitem__(self
, key
):
80 self
._device
.RunShellCommand(
81 'content delete --uri content://%s '
82 '--bind name:%s:%s' % (
84 self
._GetTypeBinding
(key
), key
),