ResourcesKey.java revision 082614c6a57a115ee0c5975e3579bf34a178c0f8
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
17package android.content.res;
18
19import android.annotation.NonNull;
20import android.annotation.Nullable;
21import android.text.TextUtils;
22
23import java.util.Arrays;
24import java.util.Objects;
25
26/** @hide */
27public final class ResourcesKey {
28    @Nullable
29    public final String mResDir;
30
31    @Nullable
32    public final String[] mSplitResDirs;
33
34    @Nullable
35    public final String[] mOverlayDirs;
36
37    @Nullable
38    public final String[] mLibDirs;
39
40    public final int mDisplayId;
41
42    @NonNull
43    public final Configuration mOverrideConfiguration;
44
45    @NonNull
46    public final CompatibilityInfo mCompatInfo;
47
48    private final int mHash;
49
50    public ResourcesKey(@Nullable String resDir,
51                        @Nullable String[] splitResDirs,
52                        @Nullable String[] overlayDirs,
53                        @Nullable String[] libDirs,
54                        int displayId,
55                        @Nullable Configuration overrideConfig,
56                        @Nullable CompatibilityInfo compatInfo) {
57        mResDir = resDir;
58        mSplitResDirs = splitResDirs;
59        mOverlayDirs = overlayDirs;
60        mLibDirs = libDirs;
61        mDisplayId = displayId;
62        mOverrideConfiguration = overrideConfig != null ? overrideConfig : Configuration.EMPTY;
63        mCompatInfo = compatInfo != null ? compatInfo : CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO;
64
65        int hash = 17;
66        hash = 31 * hash + Objects.hashCode(mResDir);
67        hash = 31 * hash + Arrays.hashCode(mSplitResDirs);
68        hash = 31 * hash + Arrays.hashCode(mOverlayDirs);
69        hash = 31 * hash + Arrays.hashCode(mLibDirs);
70        hash = 31 * hash + mDisplayId;
71        hash = 31 * hash + Objects.hashCode(mOverrideConfiguration);
72        hash = 31 * hash + Objects.hashCode(mCompatInfo);
73        mHash = hash;
74    }
75
76    public boolean hasOverrideConfiguration() {
77        return !Configuration.EMPTY.equals(mOverrideConfiguration);
78    }
79
80    @Override
81    public int hashCode() {
82        return mHash;
83    }
84
85    @Override
86    public boolean equals(Object obj) {
87        if (!(obj instanceof ResourcesKey)) {
88            return false;
89        }
90
91        ResourcesKey peer = (ResourcesKey) obj;
92        if (mHash != peer.mHash) {
93            // If the hashes don't match, the objects can't match.
94            return false;
95        }
96
97        if (!Objects.equals(mResDir, peer.mResDir)) {
98            return false;
99        }
100        if (!Arrays.equals(mSplitResDirs, peer.mSplitResDirs)) {
101            return false;
102        }
103        if (!Arrays.equals(mOverlayDirs, peer.mOverlayDirs)) {
104            return false;
105        }
106        if (!Arrays.equals(mLibDirs, peer.mLibDirs)) {
107            return false;
108        }
109        if (mDisplayId != peer.mDisplayId) {
110            return false;
111        }
112        if (!Objects.equals(mOverrideConfiguration, peer.mOverrideConfiguration)) {
113            return false;
114        }
115        if (!Objects.equals(mCompatInfo, peer.mCompatInfo)) {
116            return false;
117        }
118        return true;
119    }
120
121    @Override
122    public String toString() {
123        StringBuilder builder = new StringBuilder().append("ResourcesKey{");
124        builder.append(" mHash=").append(Integer.toHexString(mHash));
125        builder.append(" mResDir=").append(mResDir);
126        builder.append(" mSplitDirs=[");
127        if (mSplitResDirs != null) {
128            builder.append(TextUtils.join(",", mSplitResDirs));
129        }
130        builder.append("]");
131        builder.append(" mOverlayDirs=[");
132        if (mOverlayDirs != null) {
133            builder.append(TextUtils.join(",", mOverlayDirs));
134        }
135        builder.append("]");
136        builder.append(" mLibDirs=[");
137        if (mLibDirs != null) {
138            builder.append(TextUtils.join(",", mLibDirs));
139        }
140        builder.append("]");
141        builder.append(" mDisplayId=").append(mDisplayId);
142        builder.append(" mOverrideConfig=").append(Configuration.resourceQualifierString(
143                mOverrideConfiguration));
144        builder.append(" mCompatInfo=").append(mCompatInfo);
145        builder.append("}");
146        return builder.toString();
147    }
148}
149