1 /* 2 * Copyright (C) 2022 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 com.android.internal.net.utils; 17 18 import android.annotation.NonNull; 19 import android.annotation.Nullable; 20 import android.provider.DeviceConfig; 21 22 /** Utilities to query {@link DeviceConfig} and flags. */ 23 // TODO: Remove this class and statically include DeviceConfigUtils instead 24 public final class IkeDeviceConfigUtils { 25 /** 26 * Look up the value of a property for a particular namespace from {@link DeviceConfig}. 27 * 28 * @param namespace The namespace containing the property to look up. 29 * @param name The name of the property to look up. 30 * @param defaultValue The value to return if the property does not exist or its value is null. 31 * @return the corresponding value, or defaultValue if none exists. 32 */ getDeviceConfigPropertyInt( @onNull String namespace, @NonNull String name, int defaultValue)33 public static int getDeviceConfigPropertyInt( 34 @NonNull String namespace, @NonNull String name, int defaultValue) { 35 String value = getDeviceConfigProperty(namespace, name, null /* defaultValue */); 36 try { 37 return (value != null) ? Integer.parseInt(value) : defaultValue; 38 } catch (NumberFormatException e) { 39 return defaultValue; 40 } 41 } 42 43 /** 44 * Look up the value of a property for a particular namespace from {@link DeviceConfig}. 45 * 46 * <p>Flags like timeouts should use this method and set an appropriate min/max range: if 47 * invalid values like "0" or "1" are pushed to devices, everything would timeout. The min/max 48 * range protects against this kind of breakage. 49 * 50 * @param namespace The namespace containing the property to look up. 51 * @param name The name of the property to look up. 52 * @param minimumValue The minimum value of a property. 53 * @param maximumValue The maximum value of a property. 54 * @param defaultValue The value to return if the property does not exist or its value is null. 55 * @return the corresponding value, or defaultValue if none exists or the fetched value is not 56 * in the provided range. 57 */ getDeviceConfigPropertyInt( @onNull String namespace, @NonNull String name, int minimumValue, int maximumValue, int defaultValue)58 public static int getDeviceConfigPropertyInt( 59 @NonNull String namespace, 60 @NonNull String name, 61 int minimumValue, 62 int maximumValue, 63 int defaultValue) { 64 int value = getDeviceConfigPropertyInt(namespace, name, defaultValue); 65 if (value < minimumValue || value > maximumValue) return defaultValue; 66 return value; 67 } 68 /** 69 * Look up the value of a property for a particular namespace from {@link DeviceConfig}. 70 * 71 * @param namespace The namespace containing the property to look up. 72 * @param name The name of the property to look up. 73 * @param defaultValue The value to return if the property does not exist or its value is null. 74 * @return the corresponding value, or defaultValue if none exists. 75 */ getDeviceConfigPropertyBoolean( @onNull String namespace, @NonNull String name, boolean defaultValue)76 public static boolean getDeviceConfigPropertyBoolean( 77 @NonNull String namespace, @NonNull String name, boolean defaultValue) { 78 String value = getDeviceConfigProperty(namespace, name, null /* defaultValue */); 79 return (value != null) ? Boolean.parseBoolean(value) : defaultValue; 80 } 81 82 /** 83 * Look up the value of a property for a particular namespace from {@link DeviceConfig}. 84 * 85 * @param namespace The namespace containing the property to look up. 86 * @param name The name of the property to look up. 87 * @param defaultValue The value to return if the property does not exist or has no valid value. 88 * @return the corresponding value, or defaultValue if none exists. 89 */ 90 @Nullable getDeviceConfigProperty( @onNull String namespace, @NonNull String name, @Nullable String defaultValue)91 public static String getDeviceConfigProperty( 92 @NonNull String namespace, @NonNull String name, @Nullable String defaultValue) { 93 String value = DeviceConfig.getProperty(namespace, name); 94 return value != null ? value : defaultValue; 95 } 96 } 97