1 /*
2  * Copyright (C) 2016 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.bluetooth.pbapclient;
17 
18 import com.android.vcard.VCardEntry;
19 
20 import java.util.ArrayList;
21 import java.util.HashSet;
22 import java.util.List;
23 
24 /**
25  *  A simpler more public version of VCardEntry.
26  */
27 public class PhonebookEntry {
28     public static class Name {
29         public String family;
30         public String given;
31         public String middle;
32         public String prefix;
33         public String suffix;
34 
Name()35         public Name() { }
36 
37         @Override
equals(Object o)38         public boolean equals(Object o) {
39             if (!(o instanceof Name)) {
40                 return false;
41             }
42 
43             Name n = ((Name) o);
44             return (family == n.family || family != null && family.equals(n.family)) &&
45                     (given == n.given ||  given != null && given.equals(n.given)) &&
46                     (middle == n.middle || middle != null && middle.equals(n.middle)) &&
47                     (prefix == n.prefix || prefix != null && prefix.equals(n.prefix)) &&
48                     (suffix == n.suffix || suffix != null && suffix.equals(n.suffix));
49         }
50 
51         @Override
hashCode()52         public int hashCode() {
53             int result = 23 * (family == null ? 0 : family.hashCode());
54             result = 23 * result + (given == null ? 0 : given.hashCode());
55             result = 23 * result + (middle == null ? 0 : middle.hashCode());
56             result = 23 * result + (prefix == null ? 0 : prefix.hashCode());
57             result = 23 * result + (suffix == null ? 0 : suffix.hashCode());
58             return result;
59         }
60 
61         @Override
toString()62         public String toString() {
63             StringBuilder sb = new StringBuilder();
64             sb.append("Name: { family: ");
65             sb.append(family);
66             sb.append(" given: ");
67             sb.append(given);
68             sb.append(" middle: ");
69             sb.append(middle);
70             sb.append(" prefix: ");
71             sb.append(prefix);
72             sb.append(" suffix: ");
73             sb.append(suffix);
74             sb.append(" }");
75             return sb.toString();
76         }
77     }
78 
79     public static class Phone {
80         public int type;
81         public String number;
82 
83         @Override
equals(Object o)84         public boolean equals(Object o) {
85             if (!(o instanceof Phone)) {
86                 return false;
87             }
88 
89             Phone p = (Phone) o;
90             return (number == p.number || number != null && number.equals(p.number))
91                     && type == p.type;
92         }
93 
94         @Override
hashCode()95         public int hashCode() {
96             return 23 * type + number.hashCode();
97         }
98 
99         @Override
toString()100         public String toString() {
101             StringBuilder sb = new StringBuilder();
102             sb.append(" Phone: { number: ");
103             sb.append(number);
104             sb.append(" type: " + type);
105             sb.append(" }");
106             return sb.toString();
107         }
108     }
109 
110     @Override
equals(Object object)111     public boolean equals(Object object) {
112         if (object instanceof PhonebookEntry) {
113             return equals((PhonebookEntry) object);
114         }
115         return false;
116     }
117 
PhonebookEntry()118     public PhonebookEntry() {
119         name = new Name();
120         phones = new ArrayList<Phone>();
121     }
122 
PhonebookEntry(VCardEntry v)123     public PhonebookEntry(VCardEntry v) {
124         name = new Name();
125         phones = new ArrayList<Phone>();
126 
127         VCardEntry.NameData n = v.getNameData();
128         name.family = n.getFamily();
129         name.given = n.getGiven();
130         name.middle = n.getMiddle();
131         name.prefix = n.getPrefix();
132         name.suffix = n.getSuffix();
133 
134         List<VCardEntry.PhoneData> vp = v.getPhoneList();
135         if (vp == null || vp.isEmpty()) {
136             return;
137         }
138 
139         for (VCardEntry.PhoneData p : vp) {
140             Phone phone = new Phone();
141             phone.type = p.getType();
142             phone.number = p.getNumber();
143             phones.add(phone);
144         }
145     }
146 
equals(PhonebookEntry p)147     private boolean equals(PhonebookEntry p) {
148         return name.equals(p.name) && phones.equals(p.phones);
149     }
150 
151     @Override
hashCode()152     public int hashCode() {
153         return name.hashCode() + 23 * phones.hashCode();
154     }
155 
156     @Override
toString()157     public String toString() {
158         StringBuilder sb = new StringBuilder();
159         sb.append("PhonebookEntry { id: ");
160         sb.append(id);
161         sb.append(" ");
162         sb.append(name.toString());
163         sb.append(phones.toString());
164         sb.append(" }");
165         return sb.toString();
166     }
167 
168     public Name name;
169     public List<Phone> phones;
170     public String id;
171 }
172