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.os.IBinder;
20
21/** @hide */
22public final class ResourcesKey {
23    final String mResDir;
24    final float mScale;
25    private final int mHash;
26    private final IBinder mToken;
27
28    public final int mDisplayId;
29    public final Configuration mOverrideConfiguration = new Configuration();
30
31    public ResourcesKey(String resDir, int displayId, Configuration overrideConfiguration,
32            float scale, IBinder token) {
33        mResDir = resDir;
34        mDisplayId = displayId;
35        if (overrideConfiguration != null) {
36            mOverrideConfiguration.setTo(overrideConfiguration);
37        }
38        mScale = scale;
39        mToken = token;
40
41        int hash = 17;
42        hash = 31 * hash + (mResDir == null ? 0 : mResDir.hashCode());
43        hash = 31 * hash + mDisplayId;
44        hash = 31 * hash + (mOverrideConfiguration != null
45                ? mOverrideConfiguration.hashCode() : 0);
46        hash = 31 * hash + Float.floatToIntBits(mScale);
47        mHash = hash;
48    }
49
50    public boolean hasOverrideConfiguration() {
51        return !Configuration.EMPTY.equals(mOverrideConfiguration);
52    }
53
54    @Override
55    public int hashCode() {
56        return mHash;
57    }
58
59    @Override
60    public boolean equals(Object obj) {
61        if (!(obj instanceof ResourcesKey)) {
62            return false;
63        }
64        ResourcesKey peer = (ResourcesKey) obj;
65
66        if ((mResDir == null) && (peer.mResDir != null)) {
67            return false;
68        }
69        if ((mResDir != null) && (peer.mResDir == null)) {
70            return false;
71        }
72        if ((mResDir != null) && (peer.mResDir != null)) {
73            if (!mResDir.equals(peer.mResDir)) {
74                return false;
75            }
76        }
77        if (mDisplayId != peer.mDisplayId) {
78            return false;
79        }
80        if (mOverrideConfiguration != peer.mOverrideConfiguration) {
81            if (mOverrideConfiguration == null || peer.mOverrideConfiguration == null) {
82                return false;
83            }
84            if (!mOverrideConfiguration.equals(peer.mOverrideConfiguration)) {
85                return false;
86            }
87        }
88        if (mScale != peer.mScale) {
89            return false;
90        }
91        return true;
92    }
93
94    @Override
95    public String toString() {
96        return Integer.toHexString(mHash);
97    }
98}
99