Fyodor Kupolov | 77bf720 | 2018-01-02 18:26:37 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.am; |
| 18 | |
| 19 | import android.content.ContentResolver; |
| 20 | import android.database.ContentObserver; |
| 21 | import android.net.Uri; |
| 22 | import android.os.SystemProperties; |
| 23 | import android.provider.Settings; |
| 24 | import android.text.TextUtils; |
| 25 | import android.util.Slog; |
Makoto Onuki | 3c9c998 | 2018-01-17 10:46:48 -0800 | [diff] [blame] | 26 | import android.view.ThreadedRenderer; |
Fyodor Kupolov | 77bf720 | 2018-01-02 18:26:37 -0800 | [diff] [blame] | 27 | |
| 28 | import com.android.internal.annotations.VisibleForTesting; |
| 29 | import 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 | */ |
| 35 | class 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 Srivastav | 9a896a6c | 2018-01-09 16:58:36 -0800 | [diff] [blame] | 41 | // {Settings.Global.SETTING_NAME, "system_property_name"}, |
| 42 | {Settings.Global.SYS_VDSO, "sys.vdso"}, |
Makoto Onuki | 3c9c998 | 2018-01-17 10:46:48 -0800 | [diff] [blame] | 43 | {Settings.Global.FPS_DEVISOR, ThreadedRenderer.DEBUG_FPS_DIVISOR}, |
Ajay Dudani | bcfcd6c | 2018-02-05 10:44:27 -0800 | [diff] [blame] | 44 | {Settings.Global.DISPLAY_PANEL_LPM, "sys.display_panel_lpm"}, |
Fyodor Kupolov | 77bf720 | 2018-01-02 18:26:37 -0800 | [diff] [blame] | 45 | }; |
| 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 Kupolov | 0fc5b0e | 2018-02-07 10:50:23 -0800 | [diff] [blame] | 96 | } catch (Exception e) { |
Fyodor Kupolov | 77bf720 | 2018-01-02 18:26:37 -0800 | [diff] [blame] | 97 | 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 | } |