Tony Mak | fc37457 | 2019-03-05 14:46:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | package android.view.textclassifier; |
| 17 | |
| 18 | import android.annotation.Nullable; |
| 19 | import android.provider.DeviceConfig; |
| 20 | import android.util.KeyValueListParser; |
| 21 | |
| 22 | import com.android.internal.annotations.VisibleForTesting; |
| 23 | |
| 24 | /** |
| 25 | * Retrieves settings from {@link DeviceConfig} and {@link android.provider.Settings}. |
| 26 | * It will try DeviceConfig first and then Settings. |
| 27 | * |
| 28 | * @hide |
| 29 | */ |
| 30 | @VisibleForTesting |
| 31 | public final class ConfigParser { |
| 32 | private static final String TAG = "ConfigParser"; |
| 33 | |
| 34 | private final KeyValueListParser mParser; |
| 35 | |
| 36 | public ConfigParser(@Nullable String textClassifierConstants) { |
| 37 | final KeyValueListParser parser = new KeyValueListParser(','); |
| 38 | try { |
| 39 | parser.setString(textClassifierConstants); |
| 40 | } catch (IllegalArgumentException e) { |
| 41 | // Failed to parse the settings string, log this and move on with defaults. |
| 42 | Log.w(TAG, "Bad text_classifier_constants: " + textClassifierConstants); |
| 43 | } |
| 44 | mParser = parser; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Reads a boolean flag. |
| 49 | */ |
| 50 | public boolean getBoolean(String key, boolean defaultValue) { |
| 51 | return DeviceConfig.getBoolean( |
| 52 | DeviceConfig.NAMESPACE_TEXTCLASSIFIER, |
| 53 | key, |
| 54 | mParser.getBoolean(key, defaultValue)); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Reads an integer flag. |
| 59 | */ |
| 60 | public int getInt(String key, int defaultValue) { |
| 61 | return DeviceConfig.getInt( |
| 62 | DeviceConfig.NAMESPACE_TEXTCLASSIFIER, |
| 63 | key, |
| 64 | mParser.getInt(key, defaultValue)); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Reads a float flag. |
| 69 | */ |
| 70 | public float getFloat(String key, float defaultValue) { |
| 71 | return DeviceConfig.getFloat( |
| 72 | DeviceConfig.NAMESPACE_TEXTCLASSIFIER, |
| 73 | key, |
| 74 | mParser.getFloat(key, defaultValue)); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Reads a string flag. |
| 79 | */ |
| 80 | public String getString(String key, String defaultValue) { |
| 81 | return DeviceConfig.getString( |
| 82 | DeviceConfig.NAMESPACE_TEXTCLASSIFIER, |
| 83 | key, |
| 84 | mParser.getString(key, defaultValue)); |
| 85 | } |
| 86 | } |