1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 package com.example.android.samplesync.client; 17 18 import android.text.TextUtils; 19 import android.util.Log; 20 21 import org.json.JSONObject; 22 import org.json.JSONException; 23 24 import java.lang.StringBuilder; 25 26 /** 27 * Represents a low-level contacts RawContact - or at least 28 * the fields of the RawContact that we care about. 29 */ 30 final public class RawContact { 31 32 /** The tag used to log to adb console. **/ 33 private static final String TAG = "RawContact"; 34 35 private final String mUserName; 36 37 private final String mFullName; 38 39 private final String mFirstName; 40 41 private final String mLastName; 42 43 private final String mCellPhone; 44 45 private final String mOfficePhone; 46 47 private final String mHomePhone; 48 49 private final String mEmail; 50 51 private final String mStatus; 52 53 private final String mAvatarUrl; 54 55 private final boolean mDeleted; 56 57 private final boolean mDirty; 58 59 private final long mServerContactId; 60 61 private final long mRawContactId; 62 63 private final long mSyncState; 64 getServerContactId()65 public long getServerContactId() { 66 return mServerContactId; 67 } 68 getRawContactId()69 public long getRawContactId() { 70 return mRawContactId; 71 } 72 getUserName()73 public String getUserName() { 74 return mUserName; 75 } 76 getFirstName()77 public String getFirstName() { 78 return mFirstName; 79 } 80 getLastName()81 public String getLastName() { 82 return mLastName; 83 } 84 getFullName()85 public String getFullName() { 86 return mFullName; 87 } 88 getCellPhone()89 public String getCellPhone() { 90 return mCellPhone; 91 } 92 getOfficePhone()93 public String getOfficePhone() { 94 return mOfficePhone; 95 } 96 getHomePhone()97 public String getHomePhone() { 98 return mHomePhone; 99 } 100 getEmail()101 public String getEmail() { 102 return mEmail; 103 } 104 getStatus()105 public String getStatus() { 106 return mStatus; 107 } 108 getAvatarUrl()109 public String getAvatarUrl() { 110 return mAvatarUrl; 111 } 112 isDeleted()113 public boolean isDeleted() { 114 return mDeleted; 115 } 116 isDirty()117 public boolean isDirty() { 118 return mDirty; 119 } 120 getSyncState()121 public long getSyncState() { 122 return mSyncState; 123 } 124 getBestName()125 public String getBestName() { 126 if (!TextUtils.isEmpty(mFullName)) { 127 return mFullName; 128 } else if (TextUtils.isEmpty(mFirstName)) { 129 return mLastName; 130 } else { 131 return mFirstName; 132 } 133 } 134 135 /** 136 * Convert the RawContact object into a JSON string. From the 137 * JSONString interface. 138 * @return a JSON string representation of the object 139 */ toJSONObject()140 public JSONObject toJSONObject() { 141 JSONObject json = new JSONObject(); 142 143 try { 144 if (!TextUtils.isEmpty(mFirstName)) { 145 json.put("f", mFirstName); 146 } 147 if (!TextUtils.isEmpty(mLastName)) { 148 json.put("l", mLastName); 149 } 150 if (!TextUtils.isEmpty(mCellPhone)) { 151 json.put("m", mCellPhone); 152 } 153 if (!TextUtils.isEmpty(mOfficePhone)) { 154 json.put("o", mOfficePhone); 155 } 156 if (!TextUtils.isEmpty(mHomePhone)) { 157 json.put("h", mHomePhone); 158 } 159 if (!TextUtils.isEmpty(mEmail)) { 160 json.put("e", mEmail); 161 } 162 if (mServerContactId > 0) { 163 json.put("i", mServerContactId); 164 } 165 if (mRawContactId > 0) { 166 json.put("c", mRawContactId); 167 } 168 if (mDeleted) { 169 json.put("d", mDeleted); 170 } 171 } catch (final Exception ex) { 172 Log.i(TAG, "Error converting RawContact to JSONObject" + ex.toString()); 173 } 174 175 return json; 176 } 177 RawContact(String name, String fullName, String firstName, String lastName, String cellPhone, String officePhone, String homePhone, String email, String status, String avatarUrl, boolean deleted, long serverContactId, long rawContactId, long syncState, boolean dirty)178 public RawContact(String name, String fullName, String firstName, String lastName, 179 String cellPhone, String officePhone, String homePhone, String email, 180 String status, String avatarUrl, boolean deleted, long serverContactId, 181 long rawContactId, long syncState, boolean dirty) { 182 mUserName = name; 183 mFullName = fullName; 184 mFirstName = firstName; 185 mLastName = lastName; 186 mCellPhone = cellPhone; 187 mOfficePhone = officePhone; 188 mHomePhone = homePhone; 189 mEmail = email; 190 mStatus = status; 191 mAvatarUrl = avatarUrl; 192 mDeleted = deleted; 193 mServerContactId = serverContactId; 194 mRawContactId = rawContactId; 195 mSyncState = syncState; 196 mDirty = dirty; 197 } 198 199 /** 200 * Creates and returns an instance of the RawContact from the provided JSON data. 201 * 202 * @param user The JSONObject containing user data 203 * @return user The new instance of Sample RawContact created from the JSON data. 204 */ valueOf(JSONObject contact)205 public static RawContact valueOf(JSONObject contact) { 206 207 try { 208 final String userName = !contact.isNull("u") ? contact.getString("u") : null; 209 final int serverContactId = !contact.isNull("i") ? contact.getInt("i") : -1; 210 // If we didn't get either a username or serverId for the contact, then 211 // we can't do anything with it locally... 212 if ((userName == null) && (serverContactId <= 0)) { 213 throw new JSONException("JSON contact missing required 'u' or 'i' fields"); 214 } 215 216 final int rawContactId = !contact.isNull("c") ? contact.getInt("c") : -1; 217 final String firstName = !contact.isNull("f") ? contact.getString("f") : null; 218 final String lastName = !contact.isNull("l") ? contact.getString("l") : null; 219 final String cellPhone = !contact.isNull("m") ? contact.getString("m") : null; 220 final String officePhone = !contact.isNull("o") ? contact.getString("o") : null; 221 final String homePhone = !contact.isNull("h") ? contact.getString("h") : null; 222 final String email = !contact.isNull("e") ? contact.getString("e") : null; 223 final String status = !contact.isNull("s") ? contact.getString("s") : null; 224 final String avatarUrl = !contact.isNull("a") ? contact.getString("a") : null; 225 final boolean deleted = !contact.isNull("d") ? contact.getBoolean("d") : false; 226 final long syncState = !contact.isNull("x") ? contact.getLong("x") : 0; 227 return new RawContact(userName, null, firstName, lastName, cellPhone, 228 officePhone, homePhone, email, status, avatarUrl, deleted, 229 serverContactId, rawContactId, syncState, false); 230 } catch (final Exception ex) { 231 Log.i(TAG, "Error parsing JSON contact object" + ex.toString()); 232 } 233 return null; 234 } 235 236 /** 237 * Creates and returns RawContact instance from all the supplied parameters. 238 */ create(String fullName, String firstName, String lastName, String cellPhone, String officePhone, String homePhone, String email, String status, boolean deleted, long rawContactId, long serverContactId)239 public static RawContact create(String fullName, String firstName, String lastName, 240 String cellPhone, String officePhone, String homePhone, 241 String email, String status, boolean deleted, long rawContactId, 242 long serverContactId) { 243 return new RawContact(null, fullName, firstName, lastName, cellPhone, officePhone, 244 homePhone, email, status, null, deleted, serverContactId, rawContactId, 245 -1, true); 246 } 247 248 /** 249 * Creates and returns a User instance that represents a deleted user. 250 * Since the user is deleted, all we need are the client/server IDs. 251 * @param clientUserId The client-side ID for the contact 252 * @param serverUserId The server-side ID for the contact 253 * @return a minimal User object representing the deleted contact. 254 */ createDeletedContact(long rawContactId, long serverContactId)255 public static RawContact createDeletedContact(long rawContactId, long serverContactId) 256 { 257 return new RawContact(null, null, null, null, null, null, null, 258 null, null, null, true, serverContactId, rawContactId, -1, true); 259 } 260 } 261