1 /* 2 * Copyright (C) 2013 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 android.content.res; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.compat.annotation.UnsupportedAppUsage; 22 import android.content.res.loader.ResourcesLoader; 23 import android.text.TextUtils; 24 25 import java.util.Arrays; 26 import java.util.Objects; 27 28 /** @hide */ 29 public final class ResourcesKey { 30 @Nullable 31 @UnsupportedAppUsage 32 public final String mResDir; 33 34 @Nullable 35 @UnsupportedAppUsage 36 public final String[] mSplitResDirs; 37 38 @Nullable 39 public final String[] mOverlayDirs; 40 41 @Nullable 42 public final String[] mLibDirs; 43 44 public final int mDisplayId; 45 46 @NonNull 47 public final Configuration mOverrideConfiguration; 48 49 @NonNull 50 public final CompatibilityInfo mCompatInfo; 51 52 @Nullable 53 public final ResourcesLoader[] mLoaders; 54 55 private final int mHash; 56 ResourcesKey(@ullable String resDir, @Nullable String[] splitResDirs, @Nullable String[] overlayDirs, @Nullable String[] libDirs, int displayId, @Nullable Configuration overrideConfig, @Nullable CompatibilityInfo compatInfo, @Nullable ResourcesLoader[] loader)57 public ResourcesKey(@Nullable String resDir, 58 @Nullable String[] splitResDirs, 59 @Nullable String[] overlayDirs, 60 @Nullable String[] libDirs, 61 int displayId, 62 @Nullable Configuration overrideConfig, 63 @Nullable CompatibilityInfo compatInfo, 64 @Nullable ResourcesLoader[] loader) { 65 mResDir = resDir; 66 mSplitResDirs = splitResDirs; 67 mOverlayDirs = overlayDirs; 68 mLibDirs = libDirs; 69 mLoaders = (loader != null && loader.length == 0) ? null : loader; 70 mDisplayId = displayId; 71 mOverrideConfiguration = new Configuration(overrideConfig != null 72 ? overrideConfig : Configuration.EMPTY); 73 mCompatInfo = compatInfo != null ? compatInfo : CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO; 74 75 int hash = 17; 76 hash = 31 * hash + Objects.hashCode(mResDir); 77 hash = 31 * hash + Arrays.hashCode(mSplitResDirs); 78 hash = 31 * hash + Arrays.hashCode(mOverlayDirs); 79 hash = 31 * hash + Arrays.hashCode(mLibDirs); 80 hash = 31 * hash + mDisplayId; 81 hash = 31 * hash + Objects.hashCode(mOverrideConfiguration); 82 hash = 31 * hash + Objects.hashCode(mCompatInfo); 83 hash = 31 * hash + Arrays.hashCode(mLoaders); 84 mHash = hash; 85 } 86 87 @UnsupportedAppUsage ResourcesKey(@ullable String resDir, @Nullable String[] splitResDirs, @Nullable String[] overlayDirs, @Nullable String[] libDirs, int displayId, @Nullable Configuration overrideConfig, @Nullable CompatibilityInfo compatInfo)88 public ResourcesKey(@Nullable String resDir, 89 @Nullable String[] splitResDirs, 90 @Nullable String[] overlayDirs, 91 @Nullable String[] libDirs, 92 int displayId, 93 @Nullable Configuration overrideConfig, 94 @Nullable CompatibilityInfo compatInfo) { 95 this(resDir, splitResDirs, overlayDirs, libDirs, displayId, overrideConfig, compatInfo, 96 null); 97 } 98 hasOverrideConfiguration()99 public boolean hasOverrideConfiguration() { 100 return !Configuration.EMPTY.equals(mOverrideConfiguration); 101 } 102 isPathReferenced(String path)103 public boolean isPathReferenced(String path) { 104 if (mResDir != null && mResDir.startsWith(path)) { 105 return true; 106 } else { 107 return anyStartsWith(mSplitResDirs, path) || anyStartsWith(mOverlayDirs, path) 108 || anyStartsWith(mLibDirs, path); 109 } 110 } 111 anyStartsWith(String[] list, String prefix)112 private static boolean anyStartsWith(String[] list, String prefix) { 113 if (list != null) { 114 for (String s : list) { 115 if (s != null && s.startsWith(prefix)) { 116 return true; 117 } 118 } 119 } 120 return false; 121 } 122 123 @Override hashCode()124 public int hashCode() { 125 return mHash; 126 } 127 128 @Override equals(Object obj)129 public boolean equals(Object obj) { 130 if (!(obj instanceof ResourcesKey)) { 131 return false; 132 } 133 134 ResourcesKey peer = (ResourcesKey) obj; 135 if (mHash != peer.mHash) { 136 // If the hashes don't match, the objects can't match. 137 return false; 138 } 139 140 if (!Objects.equals(mResDir, peer.mResDir)) { 141 return false; 142 } 143 if (!Arrays.equals(mSplitResDirs, peer.mSplitResDirs)) { 144 return false; 145 } 146 if (!Arrays.equals(mOverlayDirs, peer.mOverlayDirs)) { 147 return false; 148 } 149 if (!Arrays.equals(mLibDirs, peer.mLibDirs)) { 150 return false; 151 } 152 if (mDisplayId != peer.mDisplayId) { 153 return false; 154 } 155 if (!Objects.equals(mOverrideConfiguration, peer.mOverrideConfiguration)) { 156 return false; 157 } 158 if (!Objects.equals(mCompatInfo, peer.mCompatInfo)) { 159 return false; 160 } 161 if (!Arrays.equals(mLoaders, peer.mLoaders)) { 162 return false; 163 } 164 return true; 165 } 166 167 @Override toString()168 public String toString() { 169 StringBuilder builder = new StringBuilder().append("ResourcesKey{"); 170 builder.append(" mHash=").append(Integer.toHexString(mHash)); 171 builder.append(" mResDir=").append(mResDir); 172 builder.append(" mSplitDirs=["); 173 if (mSplitResDirs != null) { 174 builder.append(TextUtils.join(",", mSplitResDirs)); 175 } 176 builder.append("]"); 177 builder.append(" mOverlayDirs=["); 178 if (mOverlayDirs != null) { 179 builder.append(TextUtils.join(",", mOverlayDirs)); 180 } 181 builder.append("]"); 182 builder.append(" mLibDirs=["); 183 if (mLibDirs != null) { 184 builder.append(TextUtils.join(",", mLibDirs)); 185 } 186 builder.append("]"); 187 builder.append(" mDisplayId=").append(mDisplayId); 188 builder.append(" mOverrideConfig=").append(Configuration.resourceQualifierString( 189 mOverrideConfiguration)); 190 builder.append(" mCompatInfo=").append(mCompatInfo); 191 builder.append(" mLoaders=["); 192 if (mLoaders != null) { 193 builder.append(TextUtils.join(",", mLoaders)); 194 } 195 builder.append("]}"); 196 return builder.toString(); 197 } 198 } 199