Craig Mautner | 88c0589 | 2013-06-28 09:47:45 -0700 | [diff] [blame] | 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 | |
Filip Gruszczynski | a01f486 | 2015-08-04 14:46:33 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 20 | import android.annotation.Nullable; |
Artur Satayev | e23a0eb | 2019-12-10 17:47:52 +0000 | [diff] [blame] | 21 | import android.compat.annotation.UnsupportedAppUsage; |
Ryan Mitchell | 4579c0a | 2020-01-08 16:29:11 -0800 | [diff] [blame] | 22 | import android.content.res.loader.ResourcesLoader; |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 23 | import android.text.TextUtils; |
Filip Gruszczynski | a01f486 | 2015-08-04 14:46:33 -0700 | [diff] [blame] | 24 | |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 25 | import java.util.Arrays; |
Wale Ogunwale | 60454db | 2015-01-23 16:05:07 -0800 | [diff] [blame] | 26 | import java.util.Objects; |
Craig Mautner | 88c0589 | 2013-06-28 09:47:45 -0700 | [diff] [blame] | 27 | |
| 28 | /** @hide */ |
| 29 | public final class ResourcesKey { |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 30 | @Nullable |
Mathew Inwood | 5c0d354 | 2018-08-14 13:54:31 +0100 | [diff] [blame] | 31 | @UnsupportedAppUsage |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 32 | public final String mResDir; |
| 33 | |
| 34 | @Nullable |
Mathew Inwood | 5c0d354 | 2018-08-14 13:54:31 +0100 | [diff] [blame] | 35 | @UnsupportedAppUsage |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 36 | public final String[] mSplitResDirs; |
| 37 | |
| 38 | @Nullable |
| 39 | public final String[] mOverlayDirs; |
| 40 | |
| 41 | @Nullable |
| 42 | public final String[] mLibDirs; |
Craig Mautner | 88c0589 | 2013-06-28 09:47:45 -0700 | [diff] [blame] | 43 | |
| 44 | public final int mDisplayId; |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 45 | |
Filip Gruszczynski | a01f486 | 2015-08-04 14:46:33 -0700 | [diff] [blame] | 46 | @NonNull |
Wale Ogunwale | 60454db | 2015-01-23 16:05:07 -0800 | [diff] [blame] | 47 | public final Configuration mOverrideConfiguration; |
Craig Mautner | 88c0589 | 2013-06-28 09:47:45 -0700 | [diff] [blame] | 48 | |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 49 | @NonNull |
| 50 | public final CompatibilityInfo mCompatInfo; |
| 51 | |
Ryan Mitchell | 4579c0a | 2020-01-08 16:29:11 -0800 | [diff] [blame] | 52 | @Nullable |
| 53 | public final ResourcesLoader[] mLoaders; |
| 54 | |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 55 | private final int mHash; |
| 56 | |
Mathew Inwood | 5c0d354 | 2018-08-14 13:54:31 +0100 | [diff] [blame] | 57 | @UnsupportedAppUsage |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 58 | public ResourcesKey(@Nullable String resDir, |
| 59 | @Nullable String[] splitResDirs, |
| 60 | @Nullable String[] overlayDirs, |
| 61 | @Nullable String[] libDirs, |
| 62 | int displayId, |
| 63 | @Nullable Configuration overrideConfig, |
Ryan Mitchell | 4579c0a | 2020-01-08 16:29:11 -0800 | [diff] [blame] | 64 | @Nullable CompatibilityInfo compatInfo, |
| 65 | @Nullable ResourcesLoader[] loader) { |
Craig Mautner | 88c0589 | 2013-06-28 09:47:45 -0700 | [diff] [blame] | 66 | mResDir = resDir; |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 67 | mSplitResDirs = splitResDirs; |
| 68 | mOverlayDirs = overlayDirs; |
| 69 | mLibDirs = libDirs; |
Ryan Mitchell | 4579c0a | 2020-01-08 16:29:11 -0800 | [diff] [blame] | 70 | mLoaders = (loader != null && loader.length == 0) ? null : loader; |
Craig Mautner | 88c0589 | 2013-06-28 09:47:45 -0700 | [diff] [blame] | 71 | mDisplayId = displayId; |
Andrii Kulian | a8a9bc5 | 2016-10-14 11:00:13 -0700 | [diff] [blame] | 72 | mOverrideConfiguration = new Configuration(overrideConfig != null |
| 73 | ? overrideConfig : Configuration.EMPTY); |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 74 | mCompatInfo = compatInfo != null ? compatInfo : CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO; |
Craig Mautner | 88c0589 | 2013-06-28 09:47:45 -0700 | [diff] [blame] | 75 | |
| 76 | int hash = 17; |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 77 | hash = 31 * hash + Objects.hashCode(mResDir); |
| 78 | hash = 31 * hash + Arrays.hashCode(mSplitResDirs); |
| 79 | hash = 31 * hash + Arrays.hashCode(mOverlayDirs); |
| 80 | hash = 31 * hash + Arrays.hashCode(mLibDirs); |
Craig Mautner | 88c0589 | 2013-06-28 09:47:45 -0700 | [diff] [blame] | 81 | hash = 31 * hash + mDisplayId; |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 82 | hash = 31 * hash + Objects.hashCode(mOverrideConfiguration); |
| 83 | hash = 31 * hash + Objects.hashCode(mCompatInfo); |
Ryan Mitchell | 4579c0a | 2020-01-08 16:29:11 -0800 | [diff] [blame] | 84 | hash = 31 * hash + Arrays.hashCode(mLoaders); |
Craig Mautner | 88c0589 | 2013-06-28 09:47:45 -0700 | [diff] [blame] | 85 | mHash = hash; |
| 86 | } |
| 87 | |
| 88 | public boolean hasOverrideConfiguration() { |
Filip Gruszczynski | a01f486 | 2015-08-04 14:46:33 -0700 | [diff] [blame] | 89 | return !Configuration.EMPTY.equals(mOverrideConfiguration); |
Craig Mautner | 88c0589 | 2013-06-28 09:47:45 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Jeff Sharkey | 98bf12f | 2016-05-26 11:56:57 -0600 | [diff] [blame] | 92 | public boolean isPathReferenced(String path) { |
| 93 | if (mResDir != null && mResDir.startsWith(path)) { |
| 94 | return true; |
| 95 | } else { |
| 96 | return anyStartsWith(mSplitResDirs, path) || anyStartsWith(mOverlayDirs, path) |
| 97 | || anyStartsWith(mLibDirs, path); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | private static boolean anyStartsWith(String[] list, String prefix) { |
| 102 | if (list != null) { |
| 103 | for (String s : list) { |
| 104 | if (s != null && s.startsWith(prefix)) { |
| 105 | return true; |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | return false; |
| 110 | } |
| 111 | |
Craig Mautner | 88c0589 | 2013-06-28 09:47:45 -0700 | [diff] [blame] | 112 | @Override |
| 113 | public int hashCode() { |
| 114 | return mHash; |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public boolean equals(Object obj) { |
| 119 | if (!(obj instanceof ResourcesKey)) { |
| 120 | return false; |
| 121 | } |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 122 | |
Craig Mautner | 88c0589 | 2013-06-28 09:47:45 -0700 | [diff] [blame] | 123 | ResourcesKey peer = (ResourcesKey) obj; |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 124 | if (mHash != peer.mHash) { |
| 125 | // If the hashes don't match, the objects can't match. |
| 126 | return false; |
| 127 | } |
Wink Saville | 05e6dde | 2014-08-31 19:24:10 -0700 | [diff] [blame] | 128 | |
Wale Ogunwale | 60454db | 2015-01-23 16:05:07 -0800 | [diff] [blame] | 129 | if (!Objects.equals(mResDir, peer.mResDir)) { |
Wink Saville | 05e6dde | 2014-08-31 19:24:10 -0700 | [diff] [blame] | 130 | return false; |
| 131 | } |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 132 | if (!Arrays.equals(mSplitResDirs, peer.mSplitResDirs)) { |
| 133 | return false; |
| 134 | } |
| 135 | if (!Arrays.equals(mOverlayDirs, peer.mOverlayDirs)) { |
| 136 | return false; |
| 137 | } |
| 138 | if (!Arrays.equals(mLibDirs, peer.mLibDirs)) { |
| 139 | return false; |
| 140 | } |
Craig Mautner | 88c0589 | 2013-06-28 09:47:45 -0700 | [diff] [blame] | 141 | if (mDisplayId != peer.mDisplayId) { |
| 142 | return false; |
| 143 | } |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 144 | if (!Objects.equals(mOverrideConfiguration, peer.mOverrideConfiguration)) { |
Filip Gruszczynski | a01f486 | 2015-08-04 14:46:33 -0700 | [diff] [blame] | 145 | return false; |
Craig Mautner | 88c0589 | 2013-06-28 09:47:45 -0700 | [diff] [blame] | 146 | } |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 147 | if (!Objects.equals(mCompatInfo, peer.mCompatInfo)) { |
Craig Mautner | 88c0589 | 2013-06-28 09:47:45 -0700 | [diff] [blame] | 148 | return false; |
| 149 | } |
Ryan Mitchell | 4579c0a | 2020-01-08 16:29:11 -0800 | [diff] [blame] | 150 | if (!Arrays.equals(mLoaders, peer.mLoaders)) { |
| 151 | return false; |
| 152 | } |
Craig Mautner | 88c0589 | 2013-06-28 09:47:45 -0700 | [diff] [blame] | 153 | return true; |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public String toString() { |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 158 | StringBuilder builder = new StringBuilder().append("ResourcesKey{"); |
| 159 | builder.append(" mHash=").append(Integer.toHexString(mHash)); |
| 160 | builder.append(" mResDir=").append(mResDir); |
| 161 | builder.append(" mSplitDirs=["); |
| 162 | if (mSplitResDirs != null) { |
| 163 | builder.append(TextUtils.join(",", mSplitResDirs)); |
| 164 | } |
| 165 | builder.append("]"); |
| 166 | builder.append(" mOverlayDirs=["); |
| 167 | if (mOverlayDirs != null) { |
| 168 | builder.append(TextUtils.join(",", mOverlayDirs)); |
| 169 | } |
| 170 | builder.append("]"); |
| 171 | builder.append(" mLibDirs=["); |
| 172 | if (mLibDirs != null) { |
| 173 | builder.append(TextUtils.join(",", mLibDirs)); |
| 174 | } |
| 175 | builder.append("]"); |
| 176 | builder.append(" mDisplayId=").append(mDisplayId); |
| 177 | builder.append(" mOverrideConfig=").append(Configuration.resourceQualifierString( |
| 178 | mOverrideConfiguration)); |
| 179 | builder.append(" mCompatInfo=").append(mCompatInfo); |
Ryan Mitchell | 4579c0a | 2020-01-08 16:29:11 -0800 | [diff] [blame] | 180 | builder.append(" mLoaders=["); |
| 181 | if (mLoaders != null) { |
| 182 | builder.append(TextUtils.join(",", mLoaders)); |
| 183 | } |
| 184 | builder.append("]}"); |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 185 | return builder.toString(); |
Craig Mautner | 88c0589 | 2013-06-28 09:47:45 -0700 | [diff] [blame] | 186 | } |
| 187 | } |