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