ContactRequest.java revision c7849b23a73d699b5e7f199f0a3afce5b9dee7a6
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 */
16package com.android.mail.bitmap;
17
18import android.content.res.AssetFileDescriptor;
19import android.text.TextUtils;
20
21import com.android.bitmap.DecodeTask;
22import com.android.mail.bitmap.ContactResolver.ContactDrawableInterface;
23
24import java.io.ByteArrayInputStream;
25import java.io.IOException;
26import java.io.InputStream;
27
28/**
29 * A request object for contact images. ContactRequests have a destination because multiple
30 * ContactRequests can share the same decoded data.
31 */
32public class ContactRequest implements DecodeTask.Request {
33
34    private final String mName;
35    private final String mEmail;
36
37    public byte[] bytes;
38
39    public ContactRequest(final String name, final String email) {
40        mName = name;
41        mEmail = normalizeEmail(email);
42    }
43
44    private String normalizeEmail(final String email) {
45        if (TextUtils.isEmpty(email)) {
46            throw new IllegalArgumentException("Email must not be empty.");
47        }
48        // todo: b/10258788
49        return email;
50    }
51
52    @Override
53    public boolean equals(final Object o) {
54        if (this == o) {
55            return true;
56        }
57        if (o == null || getClass() != o.getClass()) {
58            return false;
59        }
60
61        final ContactRequest that = (ContactRequest) o;
62
63        // Only count email, so we can pull results out of the cache that are from other contact
64        // requests.
65        //noinspection RedundantIfStatement
66        if (mEmail != null ? !mEmail.equals(that.mEmail) : that.mEmail != null) {
67            return false;
68        }
69
70        return true;
71    }
72
73    @Override
74    public int hashCode() {
75        // Only count email, so we can pull results out of the cache that are from other contact
76        // requests.
77        return mEmail != null ? mEmail.hashCode() : 0;
78    }
79
80    @Override
81    public String toString() {
82        return "[" + super.toString() + " mName=" + mName + " mEmail=" + mEmail + "]";
83    }
84
85    @Override
86    public AssetFileDescriptor createFd() throws IOException {
87        return null;
88    }
89
90    @Override
91    public InputStream createInputStream() throws IOException {
92        return new ByteArrayInputStream(bytes);
93    }
94
95    @Override
96    public boolean hasOrientationExif() throws IOException {
97        return false;
98    }
99
100    public String getEmail() {
101        return mEmail;
102    }
103
104    public String getDisplayName() {
105        return !TextUtils.isEmpty(mName) ? mName : mEmail;
106    }
107
108    /**
109     * This ContactRequest wrapper provides implementations of equals() and hashcode() that
110     * include the destination. We need to put multiple ContactRequests in a set,
111     * but its implementations of equals() and hashcode() don't include the destination.
112     */
113    public static class ContactRequestHolder {
114
115        public final ContactRequest contactRequest;
116        public final ContactDrawableInterface destination;
117
118        public ContactRequestHolder(final ContactRequest contactRequest,
119                final ContactDrawableInterface destination) {
120            this.contactRequest = contactRequest;
121            this.destination = destination;
122        }
123
124        @Override
125        public boolean equals(final Object o) {
126            if (this == o) {
127                return true;
128            }
129            if (o == null || getClass() != o.getClass()) {
130                return false;
131            }
132
133            final ContactRequestHolder that = (ContactRequestHolder) o;
134
135            if (contactRequest != null ? !contactRequest.equals(that.contactRequest)
136                    : that.contactRequest != null) {
137                return false;
138            }
139            //noinspection RedundantIfStatement
140            if (destination != null ? !destination.equals(that.destination)
141                    : that.destination != null) {
142                return false;
143            }
144
145            return true;
146        }
147
148        @Override
149        public int hashCode() {
150            int result = contactRequest != null ? contactRequest.hashCode() : 0;
151            result = 31 * result + (destination != null ? destination.hashCode() : 0);
152            return result;
153        }
154
155        @Override
156        public String toString() {
157            return contactRequest.toString();
158        }
159
160        public String getEmail() {
161            return contactRequest.getEmail();
162        }
163
164        public String getDisplayName() {
165            return contactRequest.getDisplayName();
166        }
167    }
168}
169