summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/build/android/pylib/utils/shared_preference_utils.py
blob: 64c4c3f919c69226e16a15625c0d1054227da760 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# Copyright 2017 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Utility functions for modifying an app's settings file using JSON."""

import json
import logging


def UnicodeToStr(data):
  """Recursively converts any Unicode to Python strings.

  Args:
    data: The data to be converted.

  Return:
    A copy of the given data, but with instances of Unicode converted to Python
    strings.
  """
  if isinstance(data, dict):
    return {
        UnicodeToStr(key): UnicodeToStr(value)
        for key, value in data.items()
    }
  elif isinstance(data, list):
    return [UnicodeToStr(element) for element in data]
  try:
    # Python-2 compatibility.
    if isinstance(data, unicode):
      return data.encode('utf-8')
  except NameError:
    # Strings are already unicode in python3.
    pass
  return data


def ExtractSettingsFromJson(filepath):
  """Extracts the settings data from the given JSON file.

  Args:
    filepath: The path to the JSON file to read.

  Return:
    The data read from the JSON file with strings converted to Python strings.
  """
  # json.load() loads strings as unicode, which causes issues when trying
  # to edit string values in preference files, so convert to Python strings
  with open(filepath) as prefs_file:
    return UnicodeToStr(json.load(prefs_file))


def ApplySharedPreferenceSetting(shared_pref, setting):
  """Applies the given app settings to the given device.

  Modifies an installed app's settings by modifying its shared preference
  settings file. Provided settings data must be a settings dictionary,
  which are in the following format:
  {
    "package": "com.example.package",
    "filename": "AppSettingsFile.xml",
    "supports_encrypted_path": true,
    "set": {
      "SomeBoolToSet": true,
      "SomeStringToSet": "StringValue",
    },
    "remove": [
      "list",
      "of",
      "keys",
      "to",
      "remove",
    ]
  }

  Example JSON files that can be read with ExtractSettingsFromJson and passed to
  this function are in //chrome/android/shared_preference_files/test/.

  Args:
    shared_pref: The devil SharedPrefs object for the device the settings will
        be applied to.
    setting: A settings dictionary to apply.
  """
  shared_pref.Load()
  for key in setting.get('remove', []):
    try:
      shared_pref.Remove(key)
    except KeyError:
      logging.warning("Attempted to remove non-existent key %s", key)
  for key, value in setting.get('set', {}).items():
    is_set = False
    if not is_set and isinstance(value, bool):
      shared_pref.SetBoolean(key, value)
      is_set = True
    try:
      # Python-2 compatibility.
      if not is_set and isinstance(value, basestring):
        shared_pref.SetString(key, value)
        is_set = True
      if not is_set and (isinstance(value, long) or isinstance(value, int)):
        shared_pref.SetLong(key, value)
        is_set = True
    except NameError:
      if not is_set and isinstance(value, str):
        shared_pref.SetString(key, value)
        is_set = True
      if not is_set and isinstance(value, int):
        shared_pref.SetLong(key, value)
        is_set = True
    if not is_set and isinstance(value, list):
      shared_pref.SetStringSet(key, value)
      is_set = True
    if not is_set:
      raise ValueError("Given invalid value type %s for key %s" % (
          str(type(value)), key))
  shared_pref.Commit()