Search in sources :

Example 1 with Account

use of de.pixart.messenger.entities.Account in project Pix-Art-Messenger by kriztan.

the class MessageArchiveService method execute.

private void execute(final Query query) {
    final Account account = query.getAccount();
    if (account.getStatus() == Account.State.ONLINE) {
        Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": running mam query " + query.toString());
        IqPacket packet = this.mXmppConnectionService.getIqGenerator().queryMessageArchiveManagement(query);
        this.mXmppConnectionService.sendIqPacket(account, packet, (a, p) -> {
            Element fin = p.findChild("fin", Namespace.MAM);
            if (p.getType() == IqPacket.TYPE.TIMEOUT) {
                synchronized (MessageArchiveService.this.queries) {
                    MessageArchiveService.this.queries.remove(query);
                    if (query.hasCallback()) {
                        query.callback(false);
                    }
                }
            } else if (p.getType() == IqPacket.TYPE.RESULT && fin != null) {
                processFin(query, fin);
            } else if (p.getType() == IqPacket.TYPE.RESULT && query.isLegacy()) {
            // do nothing
            } else {
                Log.d(Config.LOGTAG, a.getJid().toBareJid().toString() + ": error executing mam: " + p.toString());
                finalizeQuery(query, true);
            }
        });
    } else {
        synchronized (this.pendingQueries) {
            this.pendingQueries.add(query);
        }
    }
}
Also used : Account(de.pixart.messenger.entities.Account) Element(de.pixart.messenger.xml.Element) IqPacket(de.pixart.messenger.xmpp.stanzas.IqPacket)

Example 2 with Account

use of de.pixart.messenger.entities.Account in project Pix-Art-Messenger by kriztan.

the class NotificationService method createForegroundNotification.

public Notification createForegroundNotification() {
    final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mXmppConnectionService);
    List<Account> accounts = mXmppConnectionService.getAccounts();
    String status;
    Account mAccount = null;
    Log.d(Config.LOGTAG, "Accounts size " + accounts.size());
    if (accounts.size() == 1) {
        mAccount = accounts.get(0);
        if (mAccount.getStatus() == Account.State.ONLINE) {
            status = "(" + mXmppConnectionService.getString(R.string.account_status_online) + ")";
        } else if (mAccount.getStatus() == Account.State.CONNECTING) {
            status = "(" + mXmppConnectionService.getString(R.string.account_status_connecting) + ")";
        } else {
            status = "(" + mXmppConnectionService.getString(R.string.account_status_offline) + ")";
        }
    } else if (accounts.size() > 1) {
        // todo: status for multiple accounts???
        status = "";
    } else {
        status = "(" + mXmppConnectionService.getString(R.string.account_status_offline) + ")";
    }
    status = " " + status;
    Log.d(Config.LOGTAG, "Status: " + status);
    mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.conversations_foreground_service) + status);
    if (Config.SHOW_CONNECTED_ACCOUNTS) {
        int enabled = 0;
        int connected = 0;
        for (Account account : accounts) {
            if (account.isOnlineAndConnected()) {
                connected++;
                enabled++;
            } else if (account.isEnabled()) {
                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);
    if (accounts.size() == 1 && accounts.get(0).getStatus() == Account.State.ONLINE) {
        mBuilder.setSmallIcon(R.drawable.ic_link_white_24dp);
    } else if (accounts.size() > 1) {
        // todo: status for multiple accounts???
        mBuilder.setSmallIcon(R.drawable.ic_link_white_24dp);
    } else {
        mBuilder.setSmallIcon(R.drawable.ic_unlink_white_24dp);
    }
    return mBuilder.build();
}
Also used : Account(de.pixart.messenger.entities.Account) Builder(android.support.v4.app.NotificationCompat.Builder) Builder(android.support.v4.app.NotificationCompat.Builder) NotificationCompat(android.support.v4.app.NotificationCompat) SpannableString(android.text.SpannableString)

Example 3 with Account

use of de.pixart.messenger.entities.Account in project Pix-Art-Messenger by kriztan.

the class NotificationService method updateErrorNotification.

public void updateErrorNotification() {
    final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(mXmppConnectionService);
    final List<Account> errors = new ArrayList<>();
    for (final Account account : mXmppConnectionService.getAccounts()) {
        if (account.hasErrorStatus() && account.showErrorNotification()) {
            errors.add(account);
        }
    }
    if (mXmppConnectionService.showForegroundService()) {
        notificationManager.notify(FOREGROUND_NOTIFICATION_ID, createForegroundNotification());
    }
    final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mXmppConnectionService);
    if (errors.size() == 0) {
        notificationManager.cancel(ERROR_NOTIFICATION_ID);
        return;
    } else if (errors.size() == 1) {
        mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.problem_connecting_to_account));
        mBuilder.setContentText(errors.get(0).getJid().toBareJid().toString());
    } else {
        mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.problem_connecting_to_accounts));
        mBuilder.setContentText(mXmppConnectionService.getString(R.string.touch_to_fix));
    }
    mBuilder.addAction(R.drawable.ic_autorenew_white_24dp, mXmppConnectionService.getString(R.string.try_again), createTryAgainIntent());
    mBuilder.setDeleteIntent(createDismissErrorIntent());
    mBuilder.setVisibility(NotificationCompat.VISIBILITY_PRIVATE);
    mBuilder.setSmallIcon(R.drawable.ic_warning_white_24dp);
    mBuilder.setLocalOnly(true);
    mBuilder.setPriority(NotificationCompat.PRIORITY_LOW);
    if (errors.size() == 1) {
        Intent intent = new Intent(mXmppConnectionService, EditAccountActivity.class);
        Account mAccount = mXmppConnectionService.getAccounts().get(0);
        intent.putExtra("jid", mAccount.getJid().toBareJid().toString());
        intent.putExtra("init", false);
        mBuilder.setContentIntent(PendingIntent.getActivity(mXmppConnectionService, 145, intent, PendingIntent.FLAG_UPDATE_CURRENT));
    } else {
        mBuilder.setContentIntent(PendingIntent.getActivity(mXmppConnectionService, 145, new Intent(mXmppConnectionService, ManageAccountActivity.class), PendingIntent.FLAG_UPDATE_CURRENT));
    }
    notificationManager.notify(ERROR_NOTIFICATION_ID, mBuilder.build());
}
Also used : Account(de.pixart.messenger.entities.Account) Builder(android.support.v4.app.NotificationCompat.Builder) NotificationManagerCompat(android.support.v4.app.NotificationManagerCompat) ArrayList(java.util.ArrayList) Builder(android.support.v4.app.NotificationCompat.Builder) NotificationCompat(android.support.v4.app.NotificationCompat) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent)

Example 4 with Account

use of de.pixart.messenger.entities.Account in project Pix-Art-Messenger by kriztan.

the class XmppConnectionService method publishDisplayName.

public void publishDisplayName(Account account) {
    String displayName = account.getDisplayName();
    if (displayName != null && !displayName.isEmpty()) {
        IqPacket publish = mIqGenerator.publishNick(displayName);
        sendIqPacket(account, publish, new OnIqPacketReceived() {

            @Override
            public void onIqPacketReceived(Account account, IqPacket packet) {
                if (packet.getType() == IqPacket.TYPE.ERROR) {
                    Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": could not publish nick");
                }
            }
        });
    }
}
Also used : Account(de.pixart.messenger.entities.Account) OnIqPacketReceived(de.pixart.messenger.xmpp.OnIqPacketReceived) IqPacket(de.pixart.messenger.xmpp.stanzas.IqPacket)

Example 5 with Account

use of de.pixart.messenger.entities.Account in project Pix-Art-Messenger by kriztan.

the class XmppConnectionService method onStartCommand.

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    final String action = intent == null ? null : intent.getAction();
    String pushedAccountHash = null;
    boolean interactive = false;
    if (action != null) {
        final String uuid = intent.getStringExtra("uuid");
        switch(action) {
            case ConnectivityManager.CONNECTIVITY_ACTION:
                if (hasInternetConnection() && Config.RESET_ATTEMPT_COUNT_ON_NETWORK_CHANGE) {
                    resetAllAttemptCounts(true, false);
                }
                break;
            case ACTION_MERGE_PHONE_CONTACTS:
                if (restoredFromDatabaseLatch.getCount() == 0) {
                    loadPhoneContacts();
                }
                return START_STICKY;
            case Intent.ACTION_SHUTDOWN:
                logoutAndSave(true);
                return START_NOT_STICKY;
            case ACTION_CLEAR_NOTIFICATION:
                mNotificationExecutor.execute(() -> {
                    try {
                        final Conversation c = findConversationByUuid(uuid);
                        if (c != null) {
                            mNotificationService.clear(c);
                        } else {
                            mNotificationService.clear();
                        }
                        restoredFromDatabaseLatch.await();
                    } catch (InterruptedException e) {
                        Log.d(Config.LOGTAG, "unable to process clear notification");
                    }
                });
                break;
            case ACTION_DISMISS_ERROR_NOTIFICATIONS:
                dismissErrorNotifications();
                break;
            case ACTION_TRY_AGAIN:
                resetAllAttemptCounts(false, true);
                interactive = true;
                break;
            case ACTION_REPLY_TO_CONVERSATION:
                Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
                if (remoteInput == null) {
                    break;
                }
                final CharSequence body = remoteInput.getCharSequence("text_reply");
                final boolean dismissNotification = intent.getBooleanExtra("dismiss_notification", false);
                if (body == null || body.length() <= 0) {
                    break;
                }
                mNotificationExecutor.execute(() -> {
                    try {
                        restoredFromDatabaseLatch.await();
                        final Conversation c = findConversationByUuid(uuid);
                        if (c != null) {
                            directReply(c, body.toString(), dismissNotification);
                        }
                    } catch (InterruptedException e) {
                        Log.d(Config.LOGTAG, "unable to process direct reply");
                    }
                });
                break;
            case ACTION_MARK_AS_READ:
                mNotificationExecutor.execute(() -> {
                    final Conversation c = findConversationByUuid(uuid);
                    if (c == null) {
                        Log.d(Config.LOGTAG, "received mark read intent for unknown conversation (" + uuid + ")");
                        return;
                    }
                    try {
                        restoredFromDatabaseLatch.await();
                        sendReadMarker(c);
                    } catch (InterruptedException e) {
                        Log.d(Config.LOGTAG, "unable to process notification read marker for conversation " + c.getName());
                    }
                });
                break;
            case ACTION_SNOOZE:
                mNotificationExecutor.execute(() -> {
                    final Conversation c = findConversationByUuid(uuid);
                    if (c == null) {
                        Log.d(Config.LOGTAG, "received snooze intent for unknown conversation (" + uuid + ")");
                        return;
                    }
                    c.setMutedTill(System.currentTimeMillis() + 30 * 60 * 1000);
                    mNotificationService.clear(c);
                    updateConversation(c);
                });
            case AudioManager.RINGER_MODE_CHANGED_ACTION:
                if (dndOnSilentMode()) {
                    refreshAllPresences();
                }
                break;
            case Intent.ACTION_SCREEN_ON:
                deactivateGracePeriod();
            case Intent.ACTION_SCREEN_OFF:
                if (awayWhenScreenOff()) {
                    refreshAllPresences();
                }
                break;
            case ACTION_GCM_TOKEN_REFRESH:
                refreshAllGcmTokens();
                break;
            case ACTION_IDLE_PING:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    scheduleNextIdlePing();
                }
                break;
            case ACTION_GCM_MESSAGE_RECEIVED:
                Log.d(Config.LOGTAG, "gcm push message arrived in service. extras=" + intent.getExtras());
                pushedAccountHash = intent.getStringExtra("account");
                break;
            case Intent.ACTION_SEND:
                Uri uri = intent.getData();
                if (uri != null) {
                    Log.d(Config.LOGTAG, "received uri permission for " + uri.toString());
                }
                return START_STICKY;
        }
    }
    synchronized (this) {
        this.wakeLock.acquire();
        boolean pingNow = ConnectivityManager.CONNECTIVITY_ACTION.equals(action);
        HashSet<Account> pingCandidates = new HashSet<>();
        for (Account account : accounts) {
            pingNow |= processAccountState(account, interactive, "ui".equals(action), CryptoHelper.getAccountFingerprint(account).equals(pushedAccountHash), pingCandidates);
        }
        if (pingNow) {
            for (Account account : pingCandidates) {
                List<Conversation> conversations = getConversations();
                for (Conversation conversation : conversations) {
                    if (conversation.getAccount() == account && !account.pendingConferenceJoins.contains(conversation)) {
                        resendFailedFileMessages(conversation);
                    }
                }
                final boolean lowTimeout = isInLowPingTimeoutMode(account);
                account.getXmppConnection().sendPing();
                Log.d(Config.LOGTAG, account.getJid().toBareJid() + " send ping (action=" + action + ", lowTimeout=" + Boolean.toString(lowTimeout) + ")");
                scheduleWakeUpCall(lowTimeout ? Config.LOW_PING_TIMEOUT : Config.PING_TIMEOUT, account.getUuid().hashCode());
            }
        }
        if (wakeLock.isHeld()) {
            try {
                wakeLock.release();
            } catch (final RuntimeException ignored) {
            }
        }
    }
    if (SystemClock.elapsedRealtime() - mLastExpiryRun.get() >= Config.EXPIRY_INTERVAL) {
        expireOldMessages();
    }
    return START_STICKY;
}
Also used : Account(de.pixart.messenger.entities.Account) Bundle(android.os.Bundle) Conversation(de.pixart.messenger.entities.Conversation) Uri(android.net.Uri) XmppUri(de.pixart.messenger.utils.XmppUri) HashSet(java.util.HashSet)

Aggregations

Account (de.pixart.messenger.entities.Account)104 IqPacket (de.pixart.messenger.xmpp.stanzas.IqPacket)39 OnIqPacketReceived (de.pixart.messenger.xmpp.OnIqPacketReceived)31 Jid (de.pixart.messenger.xmpp.jid.Jid)26 Element (de.pixart.messenger.xml.Element)23 InvalidJidException (de.pixart.messenger.xmpp.jid.InvalidJidException)19 Conversation (de.pixart.messenger.entities.Conversation)18 ArrayList (java.util.ArrayList)14 Intent (android.content.Intent)12 Contact (de.pixart.messenger.entities.Contact)12 Bookmark (de.pixart.messenger.entities.Bookmark)10 PendingIntent (android.app.PendingIntent)9 Message (de.pixart.messenger.entities.Message)8 Bundle (android.os.Bundle)7 AlertDialog (android.support.v7.app.AlertDialog)7 MucOptions (de.pixart.messenger.entities.MucOptions)7 List (java.util.List)7 SuppressLint (android.annotation.SuppressLint)6 Bitmap (android.graphics.Bitmap)6 View (android.view.View)6