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