1 /*
2  * Copyright (C) 2015 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 
17 package com.android.dialer.app.contactinfo;
18 
19 import android.support.annotation.IntDef;
20 import android.text.TextUtils;
21 import com.android.dialer.phonenumbercache.ContactInfo;
22 import java.lang.annotation.Retention;
23 import java.lang.annotation.RetentionPolicy;
24 import java.util.Objects;
25 import java.util.concurrent.atomic.AtomicLong;
26 
27 /** A request for contact details for the given number, used by the ContactInfoCache. */
28 public final class ContactInfoRequest implements Comparable<ContactInfoRequest> {
29 
30   private static final AtomicLong NEXT_SEQUENCE_NUMBER = new AtomicLong(0);
31 
32   private final long sequenceNumber;
33 
34   /** The number to look-up. */
35   public final String number;
36   /** The country in which a call to or from this number was placed or received. */
37   public final String countryIso;
38   /** The cached contact information stored in the call log. */
39   public final ContactInfo callLogInfo;
40 
41   /** Is the request a remote lookup. Remote requests are treated as lower priority. */
42   @TYPE public final int type;
43 
44   /** Specifies the type of the request is. */
45   @IntDef(
46     value = {
47       TYPE_LOCAL,
48       TYPE_LOCAL_AND_REMOTE,
49       TYPE_REMOTE,
50     }
51   )
52   @Retention(RetentionPolicy.SOURCE)
53   public @interface TYPE {}
54 
55   public static final int TYPE_LOCAL = 0;
56   /** If cannot find the contact locally, do remote lookup later. */
57   public static final int TYPE_LOCAL_AND_REMOTE = 1;
58 
59   public static final int TYPE_REMOTE = 2;
60 
ContactInfoRequest( String number, String countryIso, ContactInfo callLogInfo, @TYPE int type)61   public ContactInfoRequest(
62       String number, String countryIso, ContactInfo callLogInfo, @TYPE int type) {
63     this.sequenceNumber = NEXT_SEQUENCE_NUMBER.getAndIncrement();
64     this.number = number;
65     this.countryIso = countryIso;
66     this.callLogInfo = callLogInfo;
67     this.type = type;
68   }
69 
70   @Override
equals(Object obj)71   public boolean equals(Object obj) {
72     if (this == obj) {
73       return true;
74     }
75     if (obj == null) {
76       return false;
77     }
78     if (!(obj instanceof ContactInfoRequest)) {
79       return false;
80     }
81 
82     ContactInfoRequest other = (ContactInfoRequest) obj;
83 
84     if (!TextUtils.equals(number, other.number)) {
85       return false;
86     }
87     if (!TextUtils.equals(countryIso, other.countryIso)) {
88       return false;
89     }
90     if (!Objects.equals(callLogInfo, other.callLogInfo)) {
91       return false;
92     }
93 
94     if (type != other.type) {
95       return false;
96     }
97 
98     return true;
99   }
100 
isLocalRequest()101   public boolean isLocalRequest() {
102     return type == TYPE_LOCAL || type == TYPE_LOCAL_AND_REMOTE;
103   }
104 
105   @Override
hashCode()106   public int hashCode() {
107     return Objects.hash(sequenceNumber, number, countryIso, callLogInfo, type);
108   }
109 
110   @Override
compareTo(ContactInfoRequest other)111   public int compareTo(ContactInfoRequest other) {
112     // Local query always comes first.
113     if (isLocalRequest() && !other.isLocalRequest()) {
114       return -1;
115     }
116     if (!isLocalRequest() && other.isLocalRequest()) {
117       return 1;
118     }
119     // First come first served.
120     return sequenceNumber < other.sequenceNumber ? -1 : 1;
121   }
122 }
123