SipProfile.java revision 363c2ab82cca4f095e9e0c8465e28f6d27a24bf8
1/* 2 * Copyright (C) 2010 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.net.sip; 18 19import android.os.Parcel; 20import android.os.Parcelable; 21import android.text.TextUtils; 22 23import java.io.Serializable; 24import java.text.ParseException; 25import javax.sip.InvalidArgumentException; 26import javax.sip.ListeningPoint; 27import javax.sip.PeerUnavailableException; 28import javax.sip.SipFactory; 29import javax.sip.address.Address; 30import javax.sip.address.AddressFactory; 31import javax.sip.address.SipURI; 32import javax.sip.address.URI; 33 34/** 35 * Class containing a SIP account, domain and server information. 36 * @hide 37 */ 38public class SipProfile implements Parcelable, Serializable { 39 private static final long serialVersionUID = 1L; 40 private static final int DEFAULT_PORT = 5060; 41 private Address mAddress; 42 private String mProxyAddress; 43 private String mPassword; 44 private String mDomain; 45 private String mProtocol = ListeningPoint.UDP; 46 private String mProfileName; 47 private boolean mSendKeepAlive = false; 48 private boolean mAutoRegistration = true; 49 50 /** @hide */ 51 public static final Parcelable.Creator<SipProfile> CREATOR = 52 new Parcelable.Creator<SipProfile>() { 53 public SipProfile createFromParcel(Parcel in) { 54 return new SipProfile(in); 55 } 56 57 public SipProfile[] newArray(int size) { 58 return new SipProfile[size]; 59 } 60 }; 61 62 /** 63 * Class to help create a {@link SipProfile}. 64 */ 65 public static class Builder { 66 private AddressFactory mAddressFactory; 67 private SipProfile mProfile = new SipProfile(); 68 private SipURI mUri; 69 private String mDisplayName; 70 private String mProxyAddress; 71 72 { 73 try { 74 mAddressFactory = 75 SipFactory.getInstance().createAddressFactory(); 76 } catch (PeerUnavailableException e) { 77 throw new RuntimeException(e); 78 } 79 } 80 81 /** 82 * Constructor. 83 * 84 * @param uriString the URI string as "sip:<user_name>@<domain>" 85 * @throws ParseException if the string is not a valid URI 86 */ 87 public Builder(String uriString) throws ParseException { 88 if (uriString == null) { 89 throw new NullPointerException("uriString cannot be null"); 90 } 91 URI uri = mAddressFactory.createURI(fix(uriString)); 92 if (uri instanceof SipURI) { 93 mUri = (SipURI) uri; 94 } else { 95 throw new ParseException(uriString + " is not a SIP URI", 0); 96 } 97 mProfile.mDomain = mUri.getHost(); 98 } 99 100 /** 101 * Constructor. 102 * 103 * @param username username of the SIP account 104 * @param serverDomain the SIP server domain; if the network address 105 * is different from the domain, use 106 * {@link #setOutboundProxy(String)} to set server address 107 * @throws ParseException if the parameters are not valid 108 */ 109 public Builder(String username, String serverDomain) 110 throws ParseException { 111 if ((username == null) || (serverDomain == null)) { 112 throw new NullPointerException( 113 "username and serverDomain cannot be null"); 114 } 115 mUri = mAddressFactory.createSipURI(username, serverDomain); 116 mProfile.mDomain = serverDomain; 117 } 118 119 private String fix(String uriString) { 120 return (uriString.trim().toLowerCase().startsWith("sip:") 121 ? uriString 122 : "sip:" + uriString); 123 } 124 125 /** 126 * Sets the name of the profile. This name is given by user. 127 * 128 * @param name name of the profile 129 * @return this builder object 130 */ 131 public Builder setProfileName(String name) { 132 mProfile.mProfileName = name; 133 return this; 134 } 135 136 /** 137 * Sets the password of the SIP account 138 * 139 * @param password password of the SIP account 140 * @return this builder object 141 */ 142 public Builder setPassword(String password) { 143 mUri.setUserPassword(password); 144 return this; 145 } 146 147 /** 148 * Sets the port number of the server. By default, it is 5060. 149 * 150 * @param port port number of the server 151 * @return this builder object 152 * @throws InvalidArgumentException if the port number is out of range 153 */ 154 public Builder setPort(int port) throws InvalidArgumentException { 155 mUri.setPort(port); 156 return this; 157 } 158 159 /** 160 * Sets the protocol used to connect to the SIP server. Currently, 161 * only "UDP" and "TCP" are supported. 162 * 163 * @param protocol the protocol string 164 * @return this builder object 165 * @throws InvalidArgumentException if the protocol is not recognized 166 */ 167 public Builder setProtocol(String protocol) 168 throws InvalidArgumentException { 169 if (protocol == null) { 170 throw new NullPointerException("protocol cannot be null"); 171 } 172 protocol = protocol.toUpperCase(); 173 if (!protocol.equals("UDP") && !protocol.equals("TCP")) { 174 throw new InvalidArgumentException( 175 "unsupported protocol: " + protocol); 176 } 177 mProfile.mProtocol = protocol; 178 return this; 179 } 180 181 /** 182 * Sets the outbound proxy of the SIP server. 183 * 184 * @param outboundProxy the network address of the outbound proxy 185 * @return this builder object 186 */ 187 public Builder setOutboundProxy(String outboundProxy) { 188 mProxyAddress = outboundProxy; 189 return this; 190 } 191 192 /** 193 * Sets the display name of the user. 194 * 195 * @param displayName display name of the user 196 * @return this builder object 197 */ 198 public Builder setDisplayName(String displayName) { 199 mDisplayName = displayName; 200 return this; 201 } 202 203 /** 204 * Sets the send keep-alive flag. 205 * 206 * @param flag true if sending keep-alive message is required, 207 * false otherwise 208 * @return this builder object 209 */ 210 public Builder setSendKeepAlive(boolean flag) { 211 mProfile.mSendKeepAlive = flag; 212 return this; 213 } 214 215 216 /** 217 * Sets the auto. registration flag. 218 * 219 * @param flag true if the profile will be registered automatically, 220 * false otherwise 221 * @return this builder object 222 */ 223 public Builder setAutoRegistration(boolean flag) { 224 mProfile.mAutoRegistration = flag; 225 return this; 226 } 227 228 /** 229 * Builds and returns the SIP profile object. 230 * 231 * @return the profile object created 232 */ 233 public SipProfile build() { 234 // remove password from URI 235 mProfile.mPassword = mUri.getUserPassword(); 236 mUri.setUserPassword(null); 237 try { 238 mProfile.mAddress = mAddressFactory.createAddress( 239 mDisplayName, mUri); 240 if (!TextUtils.isEmpty(mProxyAddress)) { 241 SipURI uri = (SipURI) 242 mAddressFactory.createURI(fix(mProxyAddress)); 243 mProfile.mProxyAddress = uri.getHost(); 244 } 245 } catch (ParseException e) { 246 // must not occur 247 throw new RuntimeException(e); 248 } 249 return mProfile; 250 } 251 } 252 253 private SipProfile() { 254 } 255 256 private SipProfile(Parcel in) { 257 mAddress = (Address) in.readSerializable(); 258 mProxyAddress = in.readString(); 259 mPassword = in.readString(); 260 mDomain = in.readString(); 261 mProtocol = in.readString(); 262 mProfileName = in.readString(); 263 mSendKeepAlive = (in.readInt() == 0) ? false : true; 264 mAutoRegistration = (in.readInt() == 0) ? false : true; 265 } 266 267 /** @hide */ 268 public void writeToParcel(Parcel out, int flags) { 269 out.writeSerializable(mAddress); 270 out.writeString(mProxyAddress); 271 out.writeString(mPassword); 272 out.writeString(mDomain); 273 out.writeString(mProtocol); 274 out.writeString(mProfileName); 275 out.writeInt(mSendKeepAlive ? 1 : 0); 276 out.writeInt(mAutoRegistration ? 1 : 0); 277 } 278 279 /** @hide */ 280 public int describeContents() { 281 return 0; 282 } 283 284 /** 285 * Gets the SIP URI of this profile. 286 * 287 * @return the SIP URI of this profile 288 */ 289 public SipURI getUri() { 290 return (SipURI) mAddress.getURI(); 291 } 292 293 /** 294 * Gets the SIP URI string of this profile. 295 * 296 * @return the SIP URI string of this profile 297 */ 298 public String getUriString() { 299 return mAddress.getURI().toString(); 300 } 301 302 /** 303 * Gets the SIP address of this profile. 304 * 305 * @return the SIP address of this profile 306 */ 307 public Address getSipAddress() { 308 return mAddress; 309 } 310 311 /** 312 * Gets the display name of the user. 313 * 314 * @return the display name of the user 315 */ 316 public String getDisplayName() { 317 return mAddress.getDisplayName(); 318 } 319 320 /** 321 * Gets the username. 322 * 323 * @return the username 324 */ 325 public String getUserName() { 326 return getUri().getUser(); 327 } 328 329 /** 330 * Gets the password. 331 * 332 * @return the password 333 */ 334 public String getPassword() { 335 return mPassword; 336 } 337 338 /** 339 * Gets the SIP domain. 340 * 341 * @return the SIP domain 342 */ 343 public String getSipDomain() { 344 return mDomain; 345 } 346 347 /** 348 * Gets the port number of the SIP server. 349 * 350 * @return the port number of the SIP server 351 */ 352 public int getPort() { 353 int port = getUri().getPort(); 354 return (port == -1) ? DEFAULT_PORT : port; 355 } 356 357 /** 358 * Gets the protocol used to connect to the server. 359 * 360 * @return the protocol 361 */ 362 public String getProtocol() { 363 return mProtocol; 364 } 365 366 /** 367 * Gets the network address of the server outbound proxy. 368 * 369 * @return the network address of the server outbound proxy 370 */ 371 public String getProxyAddress() { 372 return mProxyAddress; 373 } 374 375 /** 376 * Gets the (user-defined) name of the profile. 377 * 378 * @return name of the profile 379 */ 380 public String getProfileName() { 381 return mProfileName; 382 } 383 384 /** 385 * Gets the flag of 'Sending keep-alive'. 386 * 387 * @return the flag of sending SIP keep-alive messages. 388 */ 389 public boolean getSendKeepAlive() { 390 return mSendKeepAlive; 391 } 392 393 /** 394 * Gets the flag of 'Auto Registration'. 395 * 396 * @return the flag of registering the profile automatically. 397 */ 398 public boolean getAutoRegistration() { 399 return mAutoRegistration; 400 } 401} 402