⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 windowsconnector.java

📁 基于SKYPE API 控件的开发示例 JSkype is an JNI implementation which enables Java clients to use the Skyp API
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            OS.RegCloseKey(phkResult[0]);
        }
        return result;
    }

    /**
     * Initialize the connector.
     * @param timeout Maximum amout of time in millieseconds to initialize.
     * @throws ConnectorException when initialization cannot be completed.
     */
    protected void initialize(final int timeout) throws ConnectorException {
        final Object object = new Object();
        Thread thread = new Thread("SkypeEventDispatcher") {
            public void run() {
                display = new Display();
                windowClass = new TCHAR(0, "" + System.currentTimeMillis() + (int) (Math.random() * 1000), true);
                int messageReceived = new Callback(WindowsConnector.this, "messageReceived", 4).getAddress();
                if (messageReceived == 0) {
                    SWT.error(SWT.ERROR_NO_MORE_CALLBACKS);
                }
                int hHeap = OS.GetProcessHeap();
                int hInstance = OS.GetModuleHandle(null);
                WNDCLASS lpWndClass = new WNDCLASS();
                lpWndClass.hInstance = hInstance;
                lpWndClass.lpfnWndProc = messageReceived;
                lpWndClass.style = OS.CS_BYTEALIGNWINDOW | OS.CS_DBLCLKS;
                lpWndClass.hCursor = OS.LoadCursor(0, OS.IDC_ARROW);
                int byteCount = windowClass.length() * TCHAR.sizeof;
                lpWndClass.lpszClassName = OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, byteCount);
                OS.MoveMemory(lpWndClass.lpszClassName, windowClass, byteCount);
                OS.RegisterClass(lpWndClass);
                windowHandle = OS.CreateWindowEx(0, windowClass, null, OS.WS_OVERLAPPED, 0, 0, 0, 0, 0, 0, hInstance, null);
                synchronized (object) {
                    object.notify();
                }
                while (true) {
                    if (!display.readAndDispatch()) {
                        display.sleep();
                    }
                }
            };
        };
        thread.setDaemon(true);
        thread.start();
        synchronized (object) {
            try {
                long start = System.currentTimeMillis();
                object.wait(timeout);
                if (timeout <= System.currentTimeMillis() - start) {
                    throw new ConnectorException("The Windows connector couldn't be initialized by timeout.");
                }
            } catch (InterruptedException e) {
                throw new ConnectorException("The Windows connector initialization was interrupted.", e);
            }
        }
    }

    /**
     * Implementation of the connect method for this connector.
     * @param timeout maximum amout of time to connect.
     * @return Status after connecting.
     * @throws ConnectorException when connection could not be established.
     */
    protected Status connect(final int timeout) throws ConnectorException {
        final Object object = new Object();
        ConnectorListener listener = new AbstractConnectorListener() {
            @Override
            public void statusChanged(ConnectorStatusEvent event) {
                synchronized (object) {
                    object.notify();
                }
            }
        };
        try {
            addConnectorListener(listener, false);
        } catch (ConnectorException e) {
            throw new InternalError("The listener couldn't be added.");
        }
        synchronized (object) {
            try {
                while (true) {
                    OS.SendMessage(HWND_BROADCAST, DISCOVER_MESSAGE_ID, windowHandle, 0);
                    long start = System.currentTimeMillis();
                    object.wait(timeout);
                    if (timeout <= System.currentTimeMillis() - start) {
                        setStatus(Status.NOT_RUNNING);
                    }
                    Status status = getStatus();
                    if (status != Status.PENDING_AUTHORIZATION) {
                        return status;
                    }
                    Thread.sleep(1000);
                }
            } catch (InterruptedException e) {
                throw new ConnectorException("Trying to connect was interrupted.", e);
            } finally {
                removeConnectorListener(listener);
            }
        }
    }

    /**
     * Send the application name to the Skype Client.
     * @param applicationName the new applicationname.
     * @throws ConnectorException when connection to Skype client has gone bad.
     */
    protected void sendApplicationName(final String applicationName) throws ConnectorException {
        String command = "NAME " + applicationName;
        execute(command, new String[] {command}, false);
    }
    
    /**
     * Gets called when a message is received.
     * @param hwnd Skype client window handle.
     * @param msg The message received.
     * @param wParam The window parameter.
     * @param lParam The lparam.
     * @return Status value.
     */
    int messageReceived(final int hwnd, final int msg, final int wParam, final int lParam) {
        if (msg == ATTACH_MESSAGE_ID) {
            switch (lParam) {
            case ATTACH_PENDING_AUTHORIZATION:
                setStatus(Status.PENDING_AUTHORIZATION);
                break;
            case ATTACH_SUCCESS:
                skypeWindowHandle = wParam;
                setStatus(Status.ATTACHED);
                break;
            case ATTACH_REFUSED:
                setStatus(Status.REFUSED);
                break;
            case ATTACH_NOT_AVAILABLE:
                setStatus(Status.NOT_AVAILABLE);
                break;
            case ATTACH_API_AVAILABLE:
                setStatus(Status.API_AVAILABLE);
                break;
            default:
                setStatus(Status.NOT_RUNNING);
                break;
            }
            return 1;
        } else if (msg == WM_COPYDATA) {
            if (wParam == skypeWindowHandle) {
                int[] data = new int[3];
                OS.MoveMemory(data, lParam, 12);
                int cbData = data[1];
                int lpData = data[2];
                int length = cbData;
                byte[] buffer = new byte[length];
                OS.MoveMemory(buffer, lpData, length);
                byte[] string = new byte[buffer.length - 1];
                System.arraycopy(buffer, 0, string, 0, string.length);
                try {
                    String message = new String(string, "UTF-8");
                    fireMessageReceived(message);
                    return 1;
                } catch (UnsupportedEncodingException e) {
                }
            }
        }
        return OS.DefWindowProc(hwnd, msg, wParam, lParam);
    }

    /**
     * Clean up and disconnect.
     */
    protected void disposeImpl() {
        // TODO WindowsConnector#disposeImpl()
        throw new UnsupportedOperationException("WindowsConnector#disposeImpl() is not implemented yet.");
    }

    /**
     * Send a command to the Skype client.
     * @param command The command to send.
     */
    protected void sendCommand(final String command) {
        display.asyncExec(new Runnable() {
            public void run() {
                try {
                    byte[] data = (command + "\u0000").getBytes("UTF-8");
                    int hHeap = OS.GetProcessHeap();
                    int pMessage = OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, data.length);
                    OS.MoveMemory(pMessage, data, data.length);
                    OS.SendMessage(skypeWindowHandle, WM_COPYDATA, windowHandle, new int[] { 0, data.length, pMessage });
                    OS.HeapFree(hHeap, 0, pMessage);
                } catch (UnsupportedEncodingException e) {
                }
            }
        });
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -