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 17 package com.android.providers.media.util; 18 19 import static com.android.providers.media.util.Logging.TAG; 20 21 import android.content.Context; 22 import android.content.res.Resources; 23 import android.content.res.Resources.NotFoundException; 24 import android.icu.text.MessageFormat; 25 import android.util.Log; 26 27 import androidx.annotation.Nullable; 28 29 import java.util.ArrayList; 30 import java.util.Arrays; 31 import java.util.HashMap; 32 import java.util.List; 33 import java.util.Locale; 34 import java.util.Map; 35 import java.util.Objects; 36 37 public class StringUtils { 38 39 /** 40 * Returns the formatted ICU format string corresponding to the provided resource ID and count 41 * number of entities in the plural string. 42 */ getICUFormatString(Resources resources, int count, int resourceID)43 public static String getICUFormatString(Resources resources, int count, int resourceID) { 44 MessageFormat msgFormat = new MessageFormat( 45 resources.getString(resourceID), 46 Locale.getDefault()); 47 Map<String, Object> arguments = new HashMap<>(); 48 arguments.put("count", count); 49 return msgFormat.format(arguments); 50 } 51 getStringConfig(Context context, int resId)52 public static String getStringConfig(Context context, int resId) { 53 final Resources res = context.getResources(); 54 try { 55 return res.getString(resId); 56 } catch (NotFoundException e) { 57 return null; 58 } 59 } 60 61 /** 62 * Variant of {@link String#startsWith(String)} but which tests with 63 * case-insensitivity. 64 */ startsWithIgnoreCase(@ullable String target, @Nullable String other)65 public static boolean startsWithIgnoreCase(@Nullable String target, @Nullable String other) { 66 if (target == null || other == null) return false; 67 if (other.length() > target.length()) return false; 68 return target.regionMatches(true, 0, other, 0, other.length()); 69 } 70 71 /** 72 * Variant of {@link Objects#equal(Object, Object)} but which tests with 73 * case-insensitivity. 74 */ equalIgnoreCase(@ullable String a, @Nullable String b)75 public static boolean equalIgnoreCase(@Nullable String a, @Nullable String b) { 76 return (a != null) && a.equalsIgnoreCase(b); 77 } 78 79 /** 80 * Returns a string array config as a {@code List<String>}. 81 */ getStringArrayConfig(Context context, int resId)82 public static List<String> getStringArrayConfig(Context context, int resId) { 83 final Resources res = context.getResources(); 84 try { 85 final String[] configValue = res.getStringArray(resId); 86 return Arrays.asList(configValue); 87 } catch (NotFoundException e) { 88 return new ArrayList<String>(); 89 } 90 } 91 92 /** 93 * Returns the list of uncached relative paths after removing invalid ones. 94 */ verifySupportedUncachedRelativePaths(List<String> unverifiedPaths)95 public static List<String> verifySupportedUncachedRelativePaths(List<String> unverifiedPaths) { 96 final List<String> verifiedPaths = new ArrayList<>(); 97 for (final String path : unverifiedPaths) { 98 if (path == null) { 99 continue; 100 } 101 if (path.startsWith("/")) { 102 Log.w(TAG, "Relative path config must not start with '/'. Ignoring: " + path); 103 continue; 104 } 105 if (!path.endsWith("/")) { 106 Log.w(TAG, "Relative path config must end with '/'. Ignoring: " + path); 107 continue; 108 } 109 110 verifiedPaths.add(path); 111 } 112 113 return verifiedPaths; 114 } 115 } 116