Search in sources :

Example 1 with ImConnectionAdapter

use of org.awesomeapp.messenger.service.adapters.ImConnectionAdapter 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)

Example 2 with ImConnectionAdapter

use of org.awesomeapp.messenger.service.adapters.ImConnectionAdapter in project Zom-Android by zom.

the class RemoteImService method networkStateChanged.

void networkStateChanged(NetworkInfo networkInfo, NetworkConnectivityReceiver.State networkState) {
    int networkType = networkInfo != null ? networkInfo.getType() : -1;
    debug("networkStateChanged: type=" + networkInfo + " state=" + networkState);
    boolean networkChanged = false;
    if (mNetworkType != networkType || mNetworkState != networkState) {
        mNetworkState = networkState;
        mNetworkType = networkType;
        networkChanged = true;
        if (isNetworkAvailable()) {
            if (mNeedCheckAutoLogin) {
                mNeedCheckAutoLogin = !autoLogin();
            }
            for (ImConnectionAdapter conn : mConnections.values()) {
                conn.networkTypeChanged();
            }
        } else {
            suspendConnections();
        }
        // update the notification
        if (mNotifyBuilder != null) {
            String message = "";
            if (!isNetworkAvailable()) {
                message = getString(R.string.error_suspended_connection);
                mNotifyBuilder.setSmallIcon(R.drawable.notify_zom);
            } else {
                message = getString(R.string.app_unlocked);
                mNotifyBuilder.setSmallIcon(R.drawable.notify_zom);
            }
            mNotifyBuilder.setContentText(message);
            // Because the ID remains unchanged, the existing notification is
            // updated.
            mNotifyManager.notify(notifyId, mNotifyBuilder.build());
        }
    }
}
Also used : ImConnectionAdapter(org.awesomeapp.messenger.service.adapters.ImConnectionAdapter)

Example 3 with ImConnectionAdapter

use of org.awesomeapp.messenger.service.adapters.ImConnectionAdapter in project Zom-Android by zom.

the class RemoteImService method reestablishConnections.

// package private for inner class access
boolean reestablishConnections() {
    // restart transports if needed
    if (Preferences.useAdvancedNetworking()) {
        if (aNetworking != null) {
            aNetworking.stopTransport();
        }
        aNetworking = new AdvancedNetworking();
        aNetworking.installTransport(this, "ss");
        aNetworking.startTransport();
    }
    for (ImConnectionAdapter conn : mConnections.values()) {
        int connState = conn.getState();
        if (connState == ImConnection.SUSPENDED) {
            conn.reestablishSession();
        }
    }
    return mConnections.values().size() > 0;
}
Also used : ImConnectionAdapter(org.awesomeapp.messenger.service.adapters.ImConnectionAdapter)

Example 4 with ImConnectionAdapter

use of org.awesomeapp.messenger.service.adapters.ImConnectionAdapter in project Zom-Android by zom.

the class RemoteImService method shutdown.

private void shutdown() {
    Debug.recordTrail(this, SERVICE_DESTROY_TRAIL_TAG, new Date());
    HeartbeatService.stopBeating(getApplicationContext());
    debug("ImService stopped.");
    for (ImConnectionAdapter conn : mConnections.values()) {
        if (conn.getState() == ImConnection.LOGGED_IN)
            conn.logout();
    }
    stopForeground(true);
    /* ignore unmount errors and quit ASAP. Threads actively using the VFS will
             * cause IOCipher's VirtualFileSystem.unmount() to throw an IllegalStateException */
    try {
        if (SecureMediaStore.isMounted())
            SecureMediaStore.unmount();
    } catch (IllegalStateException e) {
        debug("there was a problem unmount secure media store: " + e.getMessage());
    }
    if (mCacheWord != null && (!mCacheWord.isLocked())) {
        mCacheWord.lock();
        mCacheWord.disconnectFromService();
    }
}
Also used : Date(java.util.Date) ImConnectionAdapter(org.awesomeapp.messenger.service.adapters.ImConnectionAdapter)

Example 5 with ImConnectionAdapter

use of org.awesomeapp.messenger.service.adapters.ImConnectionAdapter in project Zom-Android by zom.

the class RemoteImService method onCreate.

@Override
public void onCreate() {
    debug("ImService started");
    final String prev = Debug.getTrail(this, SERVICE_CREATE_TRAIL_KEY);
    if (prev != null)
        Debug.recordTrail(this, PREV_SERVICE_CREATE_TRAIL_TAG, prev);
    Debug.recordTrail(this, SERVICE_CREATE_TRAIL_KEY, new Date());
    final String prevConnections = Debug.getTrail(this, CONNECTIONS_TRAIL_TAG);
    if (prevConnections != null)
        Debug.recordTrail(this, PREV_CONNECTIONS_TRAIL_TAG, prevConnections);
    Debug.recordTrail(this, CONNECTIONS_TRAIL_TAG, "0");
    mConnections = new Hashtable<String, ImConnectionAdapter>();
    mConnectionsByUser = new Hashtable<String, ImConnectionAdapter>();
    mHandler = new Handler();
    Debug.onServiceStart();
    PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
    mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "IM_WAKELOCK");
    // Clear all account statii to logged-out, since we just got started and we don't want
    // leftovers from any previous crash.
    clearConnectionStatii();
    mStatusBarNotifier = new StatusBarNotifier(this);
    mServiceHandler = new ServiceHandler();
    mPluginHelper = ImPluginHelper.getInstance(this);
    mPluginHelper.loadAvailablePlugins();
    // Have the heartbeat start autoLogin, unless onStart turns this off
    mNeedCheckAutoLogin = true;
    HeartbeatService.startBeating(getApplicationContext());
    if (Preferences.getUseForegroundPriority()) {
        startForeground(notifyId, getForegroundNotification());
    }
    installTransports(this);
}
Also used : PowerManager(android.os.PowerManager) Handler(android.os.Handler) CacheWordHandler(info.guardianproject.cacheword.CacheWordHandler) Date(java.util.Date) ImConnectionAdapter(org.awesomeapp.messenger.service.adapters.ImConnectionAdapter)

Aggregations

ImConnectionAdapter (org.awesomeapp.messenger.service.adapters.ImConnectionAdapter)8 Date (java.util.Date)2 ContentResolver (android.content.ContentResolver)1 Intent (android.content.Intent)1 Cursor (android.database.Cursor)1 Handler (android.os.Handler)1 PowerManager (android.os.PowerManager)1 RemoteException (android.os.RemoteException)1 CacheWordHandler (info.guardianproject.cacheword.CacheWordHandler)1 Address (org.awesomeapp.messenger.model.Address)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 Message (org.awesomeapp.messenger.model.Message)1 XmppAddress (org.awesomeapp.messenger.plugin.xmpp.XmppAddress)1 Imps (org.awesomeapp.messenger.provider.Imps)1 ChatSessionAdapter (org.awesomeapp.messenger.service.adapters.ChatSessionAdapter)1 ChatSessionManagerAdapter (org.awesomeapp.messenger.service.adapters.ChatSessionManagerAdapter)1