blob: 71fd71b87619efede0b140ebf969ff0b76dcc602 [file] [log] [blame]
Fyodor Kupolov77bf7202018-01-02 18:26:37 -08001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package com.android.server.am;
18
19import android.content.ContentResolver;
20import android.database.ContentObserver;
21import android.net.Uri;
22import android.os.SystemProperties;
23import android.provider.Settings;
24import android.text.TextUtils;
25import android.util.Slog;
Makoto Onuki3c9c9982018-01-17 10:46:48 -080026import android.view.ThreadedRenderer;
Fyodor Kupolov77bf7202018-01-02 18:26:37 -080027
28import com.android.internal.annotations.VisibleForTesting;
29import com.android.internal.util.Preconditions;
30
31/**
32 * Maps global system settings to system properties.
33 * <p>The properties are dynamically updated when settings change.
34 */
35class GlobalSettingsToPropertiesMapper {
36
37 private static final String TAG = "GlobalSettingsToPropertiesMapper";
38
39 private static final String[][] sGlobalSettingsMapping = new String[][] {
40 // List mapping entries in the following format:
Shikhar Srivastav9a896a6c2018-01-09 16:58:36 -080041 // {Settings.Global.SETTING_NAME, "system_property_name"},
42 {Settings.Global.SYS_VDSO, "sys.vdso"},
Makoto Onuki3c9c9982018-01-17 10:46:48 -080043 {Settings.Global.FPS_DEVISOR, ThreadedRenderer.DEBUG_FPS_DIVISOR},
Ajay Dudanibcfcd6c2018-02-05 10:44:27 -080044 {Settings.Global.DISPLAY_PANEL_LPM, "sys.display_panel_lpm"},
Fyodor Kupolov77bf7202018-01-02 18:26:37 -080045 };
46
47
48 private final ContentResolver mContentResolver;
49 private final String[][] mGlobalSettingsMapping;
50
51 @VisibleForTesting
52 GlobalSettingsToPropertiesMapper(ContentResolver contentResolver,
53 String[][] globalSettingsMapping) {
54 mContentResolver = contentResolver;
55 mGlobalSettingsMapping = globalSettingsMapping;
56 }
57
58 void updatePropertiesFromGlobalSettings() {
59 for (String[] entry : mGlobalSettingsMapping) {
60 final String settingName = entry[0];
61 final String propName = entry[1];
62 Uri settingUri = Settings.Global.getUriFor(settingName);
63 Preconditions.checkNotNull(settingUri, "Setting " + settingName + " not found");
64 ContentObserver co = new ContentObserver(null) {
65 @Override
66 public void onChange(boolean selfChange) {
67 updatePropertyFromSetting(settingName, propName);
68 }
69 };
70 updatePropertyFromSetting(settingName, propName);
71 mContentResolver.registerContentObserver(settingUri, false, co);
72 }
73 }
74
75 public static void start(ContentResolver contentResolver) {
76 new GlobalSettingsToPropertiesMapper(contentResolver, sGlobalSettingsMapping)
77 .updatePropertiesFromGlobalSettings();
78 }
79
80 private String getGlobalSetting(String name) {
81 return Settings.Global.getString(mContentResolver, name);
82 }
83
84 private void setProperty(String key, String value) {
85 // Check if need to clear the property
86 if (value == null) {
87 // It's impossible to remove system property, therefore we check previous value to
88 // avoid setting an empty string if the property wasn't set.
89 if (TextUtils.isEmpty(systemPropertiesGet(key))) {
90 return;
91 }
92 value = "";
93 }
94 try {
95 systemPropertiesSet(key, value);
Fyodor Kupolov0fc5b0e2018-02-07 10:50:23 -080096 } catch (Exception e) {
Fyodor Kupolov77bf7202018-01-02 18:26:37 -080097 Slog.e(TAG, "Unable to set property " + key + " value '" + value + "'", e);
98 }
99 }
100
101 @VisibleForTesting
102 protected String systemPropertiesGet(String key) {
103 return SystemProperties.get(key);
104 }
105
106 @VisibleForTesting
107 protected void systemPropertiesSet(String key, String value) {
108 SystemProperties.set(key, value);
109 }
110
111 @VisibleForTesting
112 void updatePropertyFromSetting(String settingName, String propName) {
113 String settingValue = getGlobalSetting(settingName);
114 setProperty(propName, settingValue);
115 }
116}