📄 skype.java
字号:
/*******************************************************************************
* Copyright (c) 2006 Koji Hisano <hisano@gmail.com> - UBION Inc. Developer
* Copyright (c) 2006 UBION Inc. <http://www.ubion.co.jp/>
*
* Copyright (c) 2006 Skype Technologies S.A. <http://www.skype.com/>
*
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Common Public License v1.0 which accompanies
* this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* Koji Hisano - initial API and implementation
* Bart Lamot - good ideas for API and initial javadoc
******************************************************************************/
package com.skype;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.skype.connector.AbstractConnectorListener;
import com.skype.connector.Connector;
import com.skype.connector.ConnectorException;
import com.skype.connector.ConnectorListener;
import com.skype.connector.ConnectorMessageEvent;
/**
* Main model (not view) class of the Skype Java API.
* Use this class staticly to do model actions (send messages, SMS messages or calls, etc).
* @see SkypeClient
* @author Koji Hisano
*/
public final class Skype {
/** library version. **/
public static final String LIBRARY_VERSION = "1.0.0.0";
/** contactList instance. */
private static ContactList contactList;
/** Profile instance for this Skype session. */
private static Profile profile;
/** chatMessageListener lock. */
private static Object chatMessageListenerMutex = new Object();
/** CHATMESSAGE listener. */
private static ConnectorListener chatMessageListener;
/** Collection of listeners. */
private static List<ChatMessageListener> chatMessageListeners = Collections.synchronizedList(new ArrayList<ChatMessageListener>());
/** callListener lock object. */
private static Object callListenerMutex = new Object();
/** CALL listener. */
private static ConnectorListener callListener;
/** Collection of all CALL listeners. */
private static List<CallListener> callListeners = Collections.synchronizedList(new ArrayList<CallListener>());
/** voiceMailListener lock object. */
private static Object voiceMailListenerMutex = new Object();
/** VOICEMAIL listener. */
private static ConnectorListener voiceMailListener;
/** Collection of all VOICEMAIL listeners. */
private static List<VoiceMailListener> voiceMailListeners = Collections.synchronizedList(new ArrayList<VoiceMailListener>());
/** User thread. */
private static Thread userThread;
/** User threading lock object. */
private static Object userThreadFieldMutex = new Object();
/** General exception handler. */
private static SkypeExceptionHandler defaultExceptionHandler = new SkypeExceptionHandler() {
/** Print the non caught exceptions. */
public void uncaughtExceptionHappened(Throwable e) {
e.printStackTrace();
}
};
/** refrence to the default exception handler. */
private static SkypeExceptionHandler exceptionHandler = defaultExceptionHandler;
/**
* Make the main thread of this API a deamon thread.
* @see Thread
* @param on if true the main thread will be a deamon thread.
*/
public static void setDeamon(boolean on) {
synchronized (userThreadFieldMutex) {
if (!on && userThread == null) {
userThread = new Thread("SkypeUserThread") {
public void run() {
Object wait = new Object();
synchronized (wait) {
try {
wait.wait();
} catch (InterruptedException e) {
}
}
};
};
userThread.start();
} else if (on && userThread != null) {
userThread.interrupt();
userThread = null;
}
}
}
/**
* Enable debug logging.
* @param on if true debug logging will be sent to the console.
* @throws SkypeException when the connection has gone bad.
*/
public static void setDebug(boolean on) throws SkypeException {
try {
Connector.getInstance().setDebug(on);
} catch (ConnectorException e) {
Utils.convertToSkypeException(e);
}
}
/**
* Return the version of the Skype client (not this API).
* @return String with version.
* @throws SkypeException when connection has gone bad or ERROR reply.
*/
public static String getVersion() throws SkypeException {
return Utils.getProperty("SKYPEVERSION");
}
/**
* Check if Skype client is installed on this computer.
* WARNING, does not work for all platforms yet.
* @return true if Skype client is installed.
*/
public static boolean isInstalled() {
return getInstalledPath() != null;
}
/**
* Find the install path of the Skype client.
* WARNING, does not work for all platforms yet.
* @return String with the full path to Skype client.
*/
public static String getInstalledPath() {
return Connector.getInstance().getInstalledPath();
}
/**
* Check if Skype client is running.
* WARNING, does not work for all platforms.
* @return true if Skype client is running.
* @throws SkypeException when connection has gone bad or ERROR reply.
*/
public static boolean isRunning() throws SkypeException {
try {
return Connector.getInstance().isRunning();
} catch (ConnectorException e) {
Utils.convertToSkypeException(e);
return false;
}
}
/**
* Get the contactlist instance of this Skype session.
* @return contactlist singleton.
* @throws SkypeException when connection has gone bad or ERROR reply.
*/
public static ContactList getContactList() throws SkypeException {
if (contactList == null) {
contactList = new ContactList();
}
return contactList;
}
/**
* Make a Skype CALL to multiple users.
* Without using the Skype client dialogs.
* @param skypeIds The users to call.
* @return The started Call.
* @throws SkypeException when connection has gone bad or ERROR reply.
*/
public static Call call(String... skypeIds) throws SkypeException {
Utils.checkNotNull("skypeIds", skypeIds);
return call(Utils.convertToCommaSeparatedString(skypeIds));
}
/**
* Make a Skype CALL to one single Skype user.
* Without using the Skype client dialogs.
* @param skypeId The user to call.
* @return The new call object.
* @throws SkypeException when connection has gone bad or ERROR reply.
*/
public static Call call(String skypeId) throws SkypeException {
Utils.checkNotNull("skypeIds", skypeId);
try {
String responseHeader = "CALL ";
String response = Connector.getInstance().executeWithId("CALL " + skypeId, responseHeader);
Utils.checkError(response);
String id = response.substring(responseHeader.length(), response.indexOf(" STATUS "));
return Call.getInstance(id);
} catch (ConnectorException e) {
Utils.convertToSkypeException(e);
return null;
}
}
/**
* Start a chat with multiple Skype users.
* Without using the Skype client dialogs.
* @param skypeIds The users to start a chat with.
* @return The new chat object.
* @throws SkypeException when connection has gone bad or ERROR reply.
*/
public static Chat chat(String[] skypeIds) throws SkypeException {
Utils.checkNotNull("skypeIds", skypeIds);
return chat(Utils.convertToCommaSeparatedString(skypeIds));
}
/**
* Start a chat with a single Skype user.
* Without using the Skype client dialogs.
* @param skypeId The user to start the with.
* @return The new chat.
* @throws SkypeException when connection has gone bad or ERROR reply.
*/
public static Chat chat(String skypeId) throws SkypeException {
try {
String responseHeader = "CHAT ";
String response = Connector.getInstance().executeWithId("CHAT CREATE " + skypeId, responseHeader);
Utils.checkError(response);
String id = response.substring(responseHeader.length(), response.indexOf(" STATUS "));
return Chat.getInstance(id);
} catch (ConnectorException e) {
Utils.convertToSkypeException(e);
return null;
}
}
/**
* Send a SMS confirmation code.
* An outgoing SMS message from Skype lists the reply-to number as the user's
* Skype ID. It is possible to change the reply-to number to a mobile phone number by
* registering the number in the Skype client. Skype validates the number and this
* number then becomes the reply-to number for outgoing SMS messages.
* @param numbers the cell phone numbers to validate.
* @return A new SMS object.
* @throws SkypeException when connection has gone bad or ERROR reply.
*/
public static SMS submitConfirmationCode(String[] numbers) throws SkypeException {
Utils.checkNotNull("numbers", numbers);
return submitConfirmationCode(Utils.convertToCommaSeparatedString(numbers));
}
/**
* Send a SMS confirmation code.
* An outgoing SMS message from Skype lists the reply-to number as the user's
* Skype ID. It is possible to change the reply-to number to a mobile phone number by
* registering the number in the Skype client. Skype validates the number and this
* number then becomes the reply-to number for outgoing SMS messages.
* @param number the cell phone numbers to validate.
* @return A new SMS object.
* @throws SkypeException when connection has gone bad or ERROR reply.
*/
public static SMS submitConfirmationCode(String number) throws SkypeException {
SMS message = createSMS(number, SMS.Type.CONFIRMATION_CODE_REQUEST);
message.send();
return message;
}
/**
* Send a SMS confirmation code.
* An outgoing SMS message from Skype lists the reply-to number as the user's
* Skype ID. It is possible to change the reply-to number to a mobile phone number by
* registering the number in the Skype client. Skype validates the number and this
* number then becomes the reply-to number for outgoing SMS messages.
* @param numbers the cell phone numbers to validate.
* @param code the validation code to send.
* @return A new SMS object.
* @throws SkypeException when connection has gone bad or ERROR reply.
*/
public static SMS submitConfirmationCode(String[] numbers, String code) throws SkypeException {
Utils.checkNotNull("numbers", numbers);
Utils.checkNotNull("code", code);
return submitConfirmationCode(Utils.convertToCommaSeparatedString(numbers), code);
}
/**
* Send a SMS confirmation code.
* An outgoing SMS message from Skype lists the reply-to number as the user's
* Skype ID. It is possible to change the reply-to number to a mobile phone number by
* registering the number in the Skype client. Skype validates the number and this
* number then becomes the reply-to number for outgoing SMS messages.
* @param number the cell phone numbers to validate.
* @param code the validation code to send.
* @return A new SMS object.
* @throws SkypeException when connection has gone bad or ERROR reply.
*/
public static SMS submitConfirmationCode(String number, String code) throws SkypeException {
Utils.checkNotNull("number", number);
Utils.checkNotNull("code", code);
SMS message = createSMS(number, SMS.Type.CONFIRMATION_CODE_REQUEST);
message.setContent(code);
message.send();
return message;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -