Search in sources :

Example 6 with Account

use of eu.siacs.conversations.entities.Account in project Conversations by siacs.

the class XmppConnectionService method onCreate.

@SuppressLint("TrulyRandom")
@Override
public void onCreate() {
    ExceptionHelper.init(getApplicationContext());
    PRNGFixes.apply();
    this.mRandom = new SecureRandom();
    updateMemorizingTrustmanager();
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
    final int cacheSize = maxMemory / 8;
    this.mBitmapCache = new LruCache<String, Bitmap>(cacheSize) {

        @Override
        protected int sizeOf(final String key, final Bitmap bitmap) {
            return bitmap.getByteCount() / 1024;
        }
    };
    this.databaseBackend = DatabaseBackend.getInstance(getApplicationContext());
    this.accounts = databaseBackend.getAccounts();
    if (Config.FREQUENT_RESTARTS_THRESHOLD != 0 && Config.FREQUENT_RESTARTS_DETECTION_WINDOW != 0 && !keepForegroundService() && databaseBackend.startTimeCountExceedsThreshold()) {
        getPreferences().edit().putBoolean(SettingsActivity.KEEP_FOREGROUND_SERVICE, true).commit();
        Log.d(Config.LOGTAG, "number of restarts exceeds threshold. enabling foreground service");
    }
    restoreFromDatabase();
    getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, contactObserver);
    new Thread(new Runnable() {

        @Override
        public void run() {
            fileObserver.startWatching();
        }
    }).start();
    if (Config.supportOpenPgp()) {
        this.pgpServiceConnection = new OpenPgpServiceConnection(getApplicationContext(), "org.sufficientlysecure.keychain", new OpenPgpServiceConnection.OnBound() {

            @Override
            public void onBound(IOpenPgpService2 service) {
                for (Account account : accounts) {
                    final PgpDecryptionService pgp = account.getPgpDecryptionService();
                    if (pgp != null) {
                        pgp.continueDecryption(true);
                    }
                }
            }

            @Override
            public void onError(Exception e) {
            }
        });
        this.pgpServiceConnection.bindToService();
    }
    this.pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    this.wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "XmppConnectionService");
    toggleForegroundService();
    updateUnreadCountBadge();
    toggleScreenEventReceiver();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        scheduleNextIdlePing();
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        registerReceiver(this.mEventReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
    }
}
Also used : Account(eu.siacs.conversations.entities.Account) IntentFilter(android.content.IntentFilter) IOpenPgpService2(org.openintents.openpgp.IOpenPgpService2) SecureRandom(java.security.SecureRandom) OpenPgpServiceConnection(org.openintents.openpgp.util.OpenPgpServiceConnection) PgpDecryptionService(eu.siacs.conversations.crypto.PgpDecryptionService) SuppressLint(android.annotation.SuppressLint) OtrException(net.java.otr4j.OtrException) FileNotFoundException(java.io.FileNotFoundException) InvalidJidException(eu.siacs.conversations.xmpp.jid.InvalidJidException) CertificateException(java.security.cert.CertificateException) Bitmap(android.graphics.Bitmap) SuppressLint(android.annotation.SuppressLint)

Example 7 with Account

use of eu.siacs.conversations.entities.Account in project Conversations by siacs.

the class NotificationService method createForegroundNotification.

public Notification createForegroundNotification() {
    final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mXmppConnectionService);
    mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.conversations_foreground_service));
    if (Config.SHOW_CONNECTED_ACCOUNTS) {
        List<Account> accounts = mXmppConnectionService.getAccounts();
        int enabled = 0;
        int connected = 0;
        for (Account account : accounts) {
            if (account.isOnlineAndConnected()) {
                connected++;
                enabled++;
            } else if (!account.isOptionSet(Account.OPTION_DISABLED)) {
                enabled++;
            }
        }
        mBuilder.setContentText(mXmppConnectionService.getString(R.string.connected_accounts, connected, enabled));
    } else {
        mBuilder.setContentText(mXmppConnectionService.getString(R.string.touch_to_open_conversations));
    }
    mBuilder.setContentIntent(createOpenConversationsIntent());
    mBuilder.setWhen(0);
    mBuilder.setPriority(Config.SHOW_CONNECTED_ACCOUNTS ? NotificationCompat.PRIORITY_DEFAULT : NotificationCompat.PRIORITY_MIN);
    mBuilder.setSmallIcon(R.drawable.ic_link_white_24dp);
    if (Config.SHOW_DISABLE_FOREGROUND) {
        final int cancelIcon;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mBuilder.setCategory(Notification.CATEGORY_SERVICE);
            cancelIcon = R.drawable.ic_cancel_white_24dp;
        } else {
            cancelIcon = R.drawable.ic_action_cancel;
        }
        mBuilder.addAction(cancelIcon, mXmppConnectionService.getString(R.string.disable_foreground_service), createDisableForeground());
    }
    return mBuilder.build();
}
Also used : Account(eu.siacs.conversations.entities.Account) Builder(android.support.v4.app.NotificationCompat.Builder) Builder(android.support.v4.app.NotificationCompat.Builder) NotificationCompat(android.support.v4.app.NotificationCompat)

Example 8 with Account

use of eu.siacs.conversations.entities.Account in project Conversations by siacs.

the class XmppConnectionService method pushContactToServer.

public void pushContactToServer(final Contact contact) {
    contact.resetOption(Contact.Options.DIRTY_DELETE);
    contact.setOption(Contact.Options.DIRTY_PUSH);
    final Account account = contact.getAccount();
    if (account.getStatus() == Account.State.ONLINE) {
        final boolean ask = contact.getOption(Contact.Options.ASKING);
        final boolean sendUpdates = contact.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST) && contact.getOption(Contact.Options.PREEMPTIVE_GRANT);
        final IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
        iq.query(Namespace.ROSTER).addChild(contact.asElement());
        account.getXmppConnection().sendIqPacket(iq, mDefaultIqHandler);
        if (sendUpdates) {
            sendPresencePacket(account, mPresenceGenerator.sendPresenceUpdatesTo(contact));
        }
        if (ask) {
            sendPresencePacket(account, mPresenceGenerator.requestPresenceUpdatesFrom(contact));
        }
    }
}
Also used : Account(eu.siacs.conversations.entities.Account) IqPacket(eu.siacs.conversations.xmpp.stanzas.IqPacket)

Example 9 with Account

use of eu.siacs.conversations.entities.Account in project Conversations by siacs.

the class XmppConnectionService method switchToForeground.

private void switchToForeground() {
    final boolean broadcastLastActivity = broadcastLastActivity();
    for (Conversation conversation : getConversations()) {
        if (conversation.getMode() == Conversation.MODE_MULTI) {
            conversation.getMucOptions().resetChatState();
        } else {
            conversation.setIncomingChatState(Config.DEFAULT_CHATSTATE);
        }
    }
    for (Account account : getAccounts()) {
        if (account.getStatus() == Account.State.ONLINE) {
            account.deactivateGracePeriod();
            final XmppConnection connection = account.getXmppConnection();
            if (connection != null) {
                if (connection.getFeatures().csi()) {
                    connection.sendActive();
                }
                if (broadcastLastActivity) {
                    //send new presence but don't include idle because we are not
                    sendPresence(account, false);
                }
            }
        }
    }
    Log.d(Config.LOGTAG, "app switched into foreground");
}
Also used : XmppConnection(eu.siacs.conversations.xmpp.XmppConnection) Account(eu.siacs.conversations.entities.Account) Conversation(eu.siacs.conversations.entities.Conversation)

Example 10 with Account

use of eu.siacs.conversations.entities.Account in project Conversations by siacs.

the class XmppConnectionService method publishAvatar.

public void publishAvatar(Account account, final Avatar avatar, final UiCallback<Avatar> callback) {
    IqPacket packet = this.mIqGenerator.publishAvatar(avatar);
    this.sendIqPacket(account, packet, new OnIqPacketReceived() {

        @Override
        public void onIqPacketReceived(Account account, IqPacket result) {
            if (result.getType() == IqPacket.TYPE.RESULT) {
                final IqPacket packet = XmppConnectionService.this.mIqGenerator.publishAvatarMetadata(avatar);
                sendIqPacket(account, packet, new OnIqPacketReceived() {

                    @Override
                    public void onIqPacketReceived(Account account, IqPacket result) {
                        if (result.getType() == IqPacket.TYPE.RESULT) {
                            if (account.setAvatar(avatar.getFilename())) {
                                getAvatarService().clear(account);
                                databaseBackend.updateAccount(account);
                            }
                            Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": published avatar " + (avatar.size / 1024) + "KiB");
                            if (callback != null) {
                                callback.success(avatar);
                            }
                        } else {
                            if (callback != null) {
                                callback.error(R.string.error_publish_avatar_server_reject, avatar);
                            }
                        }
                    }
                });
            } else {
                Element error = result.findChild("error");
                Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": server rejected avatar " + (avatar.size / 1024) + "KiB " + (error != null ? error.toString() : ""));
                if (callback != null) {
                    callback.error(R.string.error_publish_avatar_server_reject, avatar);
                }
            }
        }
    });
}
Also used : Account(eu.siacs.conversations.entities.Account) OnIqPacketReceived(eu.siacs.conversations.xmpp.OnIqPacketReceived) Element(eu.siacs.conversations.xml.Element) IqPacket(eu.siacs.conversations.xmpp.stanzas.IqPacket)

Aggregations

Account (eu.siacs.conversations.entities.Account)100 IqPacket (eu.siacs.conversations.xmpp.stanzas.IqPacket)41 OnIqPacketReceived (eu.siacs.conversations.xmpp.OnIqPacketReceived)33 Jid (eu.siacs.conversations.xmpp.jid.Jid)22 Element (eu.siacs.conversations.xml.Element)21 InvalidJidException (eu.siacs.conversations.xmpp.jid.InvalidJidException)17 Conversation (eu.siacs.conversations.entities.Conversation)16 Contact (eu.siacs.conversations.entities.Contact)9 Message (eu.siacs.conversations.entities.Message)9 ArrayList (java.util.ArrayList)8 PendingIntent (android.app.PendingIntent)7 Intent (android.content.Intent)7 Bookmark (eu.siacs.conversations.entities.Bookmark)7 SuppressLint (android.annotation.SuppressLint)6 AlertDialog (android.app.AlertDialog)6 TextView (android.widget.TextView)6 MessagePacket (eu.siacs.conversations.xmpp.stanzas.MessagePacket)6 FileNotFoundException (java.io.FileNotFoundException)6 DialogInterface (android.content.DialogInterface)5 View (android.view.View)5