Search in sources :

Example 1 with ConnectionListener

use of org.awesomeapp.messenger.model.ConnectionListener in project Zom-Android by zom.

the class RemoteImService method do_createConnection.

private IImConnection do_createConnection(long providerId, long accountId) {
    if (providerId == -1)
        return null;
    Map<String, String> settings = loadProviderSettings(providerId);
    // make sure OTR is init'd before you create your first connection
    initOtrChatManager();
    ConnectionFactory factory = ConnectionFactory.getInstance();
    try {
        ImConnection conn = factory.createConnection(settings, this);
        conn.initUser(providerId, accountId);
        ImConnectionAdapter imConnectionAdapter = new ImConnectionAdapter(providerId, accountId, conn, this);
        conn.addConnectionListener(new ConnectionListener() {

            @Override
            public void onStateChanged(int state, ImErrorInfo error) {
            }

            @Override
            public void onUserPresenceUpdated() {
            }

            @Override
            public void onUpdatePresenceError(ImErrorInfo error) {
            }
        });
        ContentResolver contentResolver = getContentResolver();
        Cursor cursor = contentResolver.query(Imps.ProviderSettings.CONTENT_URI, new String[] { Imps.ProviderSettings.NAME, Imps.ProviderSettings.VALUE }, Imps.ProviderSettings.PROVIDER + "=?", new String[] { Long.toString(providerId) }, null);
        if (cursor == null)
            throw new ImException("unable to query the provider settings");
        Imps.ProviderSettings.QueryMap providerSettings = new Imps.ProviderSettings.QueryMap(cursor, contentResolver, providerId, false, null);
        String userName = Imps.Account.getUserName(contentResolver, accountId);
        String domain = providerSettings.getDomain();
        providerSettings.close();
        mConnections.put(providerId + "." + accountId, imConnectionAdapter);
        mConnectionsByUser.put(imConnectionAdapter.getLoginUser().getAddress().getBareAddress(), imConnectionAdapter);
        Debug.recordTrail(this, CONNECTIONS_TRAIL_TAG, "" + mConnections.size());
        synchronized (mRemoteListeners) {
            try {
                final int N = mRemoteListeners.beginBroadcast();
                for (int i = 0; i < N; i++) {
                    IConnectionCreationListener listener = mRemoteListeners.getBroadcastItem(i);
                    try {
                        listener.onConnectionCreated(imConnectionAdapter);
                    } catch (RemoteException e) {
                    // The RemoteCallbackList will take care of removing the
                    // dead listeners.
                    }
                }
            } finally {
                mRemoteListeners.finishBroadcast();
            }
        }
        return imConnectionAdapter;
    } catch (ImException e) {
        debug("Error creating connection", e);
        return null;
    }
}
Also used : Imps(org.awesomeapp.messenger.provider.Imps) ConnectionListener(org.awesomeapp.messenger.model.ConnectionListener) Cursor(android.database.Cursor) ContentResolver(android.content.ContentResolver) ConnectionFactory(org.awesomeapp.messenger.model.ConnectionFactory) ImException(org.awesomeapp.messenger.model.ImException) ImConnection(org.awesomeapp.messenger.model.ImConnection) RemoteException(android.os.RemoteException) ImConnectionAdapter(org.awesomeapp.messenger.service.adapters.ImConnectionAdapter) ImErrorInfo(org.awesomeapp.messenger.model.ImErrorInfo)

Aggregations

ContentResolver (android.content.ContentResolver)1 Cursor (android.database.Cursor)1 RemoteException (android.os.RemoteException)1 ConnectionFactory (org.awesomeapp.messenger.model.ConnectionFactory)1 ConnectionListener (org.awesomeapp.messenger.model.ConnectionListener)1 ImConnection (org.awesomeapp.messenger.model.ImConnection)1 ImErrorInfo (org.awesomeapp.messenger.model.ImErrorInfo)1 ImException (org.awesomeapp.messenger.model.ImException)1 Imps (org.awesomeapp.messenger.provider.Imps)1 ImConnectionAdapter (org.awesomeapp.messenger.service.adapters.ImConnectionAdapter)1