blob: 9e40f46c3c21228c8201e95bc98ade9ac7199b9e [file] [log] [blame]
Craig Mautner88c05892013-06-28 09:47:45 -07001/*
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
17package android.content.res;
18
Filip Gruszczynskia01f4862015-08-04 14:46:33 -070019import android.annotation.NonNull;
Adam Lesinski082614c2016-03-04 14:33:47 -080020import android.annotation.Nullable;
Artur Satayeve23a0eb2019-12-10 17:47:52 +000021import android.compat.annotation.UnsupportedAppUsage;
Ryan Mitchell4579c0a2020-01-08 16:29:11 -080022import android.content.res.loader.ResourcesLoader;
Adam Lesinski082614c2016-03-04 14:33:47 -080023import android.text.TextUtils;
Filip Gruszczynskia01f4862015-08-04 14:46:33 -070024
Adam Lesinski082614c2016-03-04 14:33:47 -080025import java.util.Arrays;
Wale Ogunwale60454db2015-01-23 16:05:07 -080026import java.util.Objects;
Craig Mautner88c05892013-06-28 09:47:45 -070027
28/** @hide */
29public final class ResourcesKey {
Adam Lesinski082614c2016-03-04 14:33:47 -080030 @Nullable
Mathew Inwood5c0d3542018-08-14 13:54:31 +010031 @UnsupportedAppUsage
Adam Lesinski082614c2016-03-04 14:33:47 -080032 public final String mResDir;
33
34 @Nullable
Mathew Inwood5c0d3542018-08-14 13:54:31 +010035 @UnsupportedAppUsage
Adam Lesinski082614c2016-03-04 14:33:47 -080036 public final String[] mSplitResDirs;
37
38 @Nullable
39 public final String[] mOverlayDirs;
40
41 @Nullable
42 public final String[] mLibDirs;
Craig Mautner88c05892013-06-28 09:47:45 -070043
44 public final int mDisplayId;
Adam Lesinski082614c2016-03-04 14:33:47 -080045
Filip Gruszczynskia01f4862015-08-04 14:46:33 -070046 @NonNull
Wale Ogunwale60454db2015-01-23 16:05:07 -080047 public final Configuration mOverrideConfiguration;
Craig Mautner88c05892013-06-28 09:47:45 -070048
Adam Lesinski082614c2016-03-04 14:33:47 -080049 @NonNull
50 public final CompatibilityInfo mCompatInfo;
51
Ryan Mitchell4579c0a2020-01-08 16:29:11 -080052 @Nullable
53 public final ResourcesLoader[] mLoaders;
54
Adam Lesinski082614c2016-03-04 14:33:47 -080055 private final int mHash;
56
Mathew Inwood5c0d3542018-08-14 13:54:31 +010057 @UnsupportedAppUsage
Adam Lesinski082614c2016-03-04 14:33:47 -080058 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 Mitchell4579c0a2020-01-08 16:29:11 -080064 @Nullable CompatibilityInfo compatInfo,
65 @Nullable ResourcesLoader[] loader) {
Craig Mautner88c05892013-06-28 09:47:45 -070066 mResDir = resDir;
Adam Lesinski082614c2016-03-04 14:33:47 -080067 mSplitResDirs = splitResDirs;
68 mOverlayDirs = overlayDirs;
69 mLibDirs = libDirs;
Ryan Mitchell4579c0a2020-01-08 16:29:11 -080070 mLoaders = (loader != null && loader.length == 0) ? null : loader;
Craig Mautner88c05892013-06-28 09:47:45 -070071 mDisplayId = displayId;
Andrii Kuliana8a9bc52016-10-14 11:00:13 -070072 mOverrideConfiguration = new Configuration(overrideConfig != null
73 ? overrideConfig : Configuration.EMPTY);
Adam Lesinski082614c2016-03-04 14:33:47 -080074 mCompatInfo = compatInfo != null ? compatInfo : CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO;
Craig Mautner88c05892013-06-28 09:47:45 -070075
76 int hash = 17;
Adam Lesinski082614c2016-03-04 14:33:47 -080077 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 Mautner88c05892013-06-28 09:47:45 -070081 hash = 31 * hash + mDisplayId;
Adam Lesinski082614c2016-03-04 14:33:47 -080082 hash = 31 * hash + Objects.hashCode(mOverrideConfiguration);
83 hash = 31 * hash + Objects.hashCode(mCompatInfo);
Ryan Mitchell4579c0a2020-01-08 16:29:11 -080084 hash = 31 * hash + Arrays.hashCode(mLoaders);
Craig Mautner88c05892013-06-28 09:47:45 -070085 mHash = hash;
86 }
87
88 public boolean hasOverrideConfiguration() {
Filip Gruszczynskia01f4862015-08-04 14:46:33 -070089 return !Configuration.EMPTY.equals(mOverrideConfiguration);
Craig Mautner88c05892013-06-28 09:47:45 -070090 }
91
Jeff Sharkey98bf12f2016-05-26 11:56:57 -060092 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 Mautner88c05892013-06-28 09:47:45 -0700112 @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 Lesinski082614c2016-03-04 14:33:47 -0800122
Craig Mautner88c05892013-06-28 09:47:45 -0700123 ResourcesKey peer = (ResourcesKey) obj;
Adam Lesinski082614c2016-03-04 14:33:47 -0800124 if (mHash != peer.mHash) {
125 // If the hashes don't match, the objects can't match.
126 return false;
127 }
Wink Saville05e6dde2014-08-31 19:24:10 -0700128
Wale Ogunwale60454db2015-01-23 16:05:07 -0800129 if (!Objects.equals(mResDir, peer.mResDir)) {
Wink Saville05e6dde2014-08-31 19:24:10 -0700130 return false;
131 }
Adam Lesinski082614c2016-03-04 14:33:47 -0800132 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 Mautner88c05892013-06-28 09:47:45 -0700141 if (mDisplayId != peer.mDisplayId) {
142 return false;
143 }
Adam Lesinski082614c2016-03-04 14:33:47 -0800144 if (!Objects.equals(mOverrideConfiguration, peer.mOverrideConfiguration)) {
Filip Gruszczynskia01f4862015-08-04 14:46:33 -0700145 return false;
Craig Mautner88c05892013-06-28 09:47:45 -0700146 }
Adam Lesinski082614c2016-03-04 14:33:47 -0800147 if (!Objects.equals(mCompatInfo, peer.mCompatInfo)) {
Craig Mautner88c05892013-06-28 09:47:45 -0700148 return false;
149 }
Ryan Mitchell4579c0a2020-01-08 16:29:11 -0800150 if (!Arrays.equals(mLoaders, peer.mLoaders)) {
151 return false;
152 }
Craig Mautner88c05892013-06-28 09:47:45 -0700153 return true;
154 }
155
156 @Override
157 public String toString() {
Adam Lesinski082614c2016-03-04 14:33:47 -0800158 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 Mitchell4579c0a2020-01-08 16:29:11 -0800180 builder.append(" mLoaders=[");
181 if (mLoaders != null) {
182 builder.append(TextUtils.join(",", mLoaders));
183 }
184 builder.append("]}");
Adam Lesinski082614c2016-03-04 14:33:47 -0800185 return builder.toString();
Craig Mautner88c05892013-06-28 09:47:45 -0700186 }
187}