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        if (!mResDir.equals(peer.mResDir)) {
66            return false;
67        }
68        if (mDisplayId != peer.mDisplayId) {
69            return false;
70        }
71        if (mOverrideConfiguration != peer.mOverrideConfiguration) {
72            if (mOverrideConfiguration == null || peer.mOverrideConfiguration == null) {
73                return false;
74            }
75            if (!mOverrideConfiguration.equals(peer.mOverrideConfiguration)) {
76                return false;
77            }
78        }
79        if (mScale != peer.mScale) {
80            return false;
81        }
82        return true;
83    }
84
85    @Override
86    public String toString() {
87        return Integer.toHexString(mHash);
88    }
89}
90