1/*
2 * Copyright (C) 2014 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
17package android.bluetooth.client.pbap;
18
19import com.android.vcard.VCardEntry;
20import com.android.vcard.VCardEntry.EmailData;
21import com.android.vcard.VCardEntry.NameData;
22import com.android.vcard.VCardEntry.PhoneData;
23
24import org.json.JSONArray;
25import org.json.JSONException;
26import org.json.JSONObject;
27
28import java.util.List;
29
30/**
31 * Entry representation of folder listing
32 */
33public class BluetoothPbapCard {
34
35    public final String handle;
36
37    public final String N;
38    public final String lastName;
39    public final String firstName;
40    public final String middleName;
41    public final String prefix;
42    public final String suffix;
43
44    public BluetoothPbapCard(String handle, String name) {
45        this.handle = handle;
46
47        N = name;
48
49        /*
50         * format is as for vCard N field, so we have up to 5 tokens: LastName;
51         * FirstName; MiddleName; Prefix; Suffix
52         */
53        String[] parsedName = name.split(";", 5);
54
55        lastName = parsedName.length < 1 ? null : parsedName[0];
56        firstName = parsedName.length < 2 ? null : parsedName[1];
57        middleName = parsedName.length < 3 ? null : parsedName[2];
58        prefix = parsedName.length < 4 ? null : parsedName[3];
59        suffix = parsedName.length < 5 ? null : parsedName[4];
60    }
61
62    @Override
63    public String toString() {
64        JSONObject json = new JSONObject();
65
66        try {
67            json.put("handle", handle);
68            json.put("N", N);
69            json.put("lastName", lastName);
70            json.put("firstName", firstName);
71            json.put("middleName", middleName);
72            json.put("prefix", prefix);
73            json.put("suffix", suffix);
74        } catch (JSONException e) {
75            // do nothing
76        }
77
78        return json.toString();
79    }
80
81    static public String jsonifyVcardEntry(VCardEntry vcard) {
82        JSONObject json = new JSONObject();
83
84        try {
85            NameData name = vcard.getNameData();
86            json.put("formatted", name.getFormatted());
87            json.put("family", name.getFamily());
88            json.put("given", name.getGiven());
89            json.put("middle", name.getMiddle());
90            json.put("prefix", name.getPrefix());
91            json.put("suffix", name.getSuffix());
92        } catch (JSONException e) {
93            // do nothing
94        }
95
96        try {
97            JSONArray jsonPhones = new JSONArray();
98
99            List<PhoneData> phones = vcard.getPhoneList();
100
101            if (phones != null) {
102                for (PhoneData phone : phones) {
103                    JSONObject jsonPhone = new JSONObject();
104                    jsonPhone.put("type", phone.getType());
105                    jsonPhone.put("number", phone.getNumber());
106                    jsonPhone.put("label", phone.getLabel());
107                    jsonPhone.put("is_primary", phone.isPrimary());
108
109                    jsonPhones.put(jsonPhone);
110                }
111
112                json.put("phones", jsonPhones);
113            }
114        } catch (JSONException e) {
115            // do nothing
116        }
117
118        try {
119            JSONArray jsonEmails = new JSONArray();
120
121            List<EmailData> emails = vcard.getEmailList();
122
123            if (emails != null) {
124                for (EmailData email : emails) {
125                    JSONObject jsonEmail = new JSONObject();
126                    jsonEmail.put("type", email.getType());
127                    jsonEmail.put("address", email.getAddress());
128                    jsonEmail.put("label", email.getLabel());
129                    jsonEmail.put("is_primary", email.isPrimary());
130
131                    jsonEmails.put(jsonEmail);
132                }
133
134                json.put("emails", jsonEmails);
135            }
136        } catch (JSONException e) {
137            // do nothing
138        }
139
140        return json.toString();
141    }
142}
143