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