diff options
Diffstat (limited to 'src/main/java/eu/siacs/conversations/services/XmppConnectionService.java')
-rw-r--r-- | src/main/java/eu/siacs/conversations/services/XmppConnectionService.java | 59 |
1 files changed, 49 insertions, 10 deletions
diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 52a2fc221..7eebe0696 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -1,6 +1,7 @@ package eu.siacs.conversations.services; import android.annotation.SuppressLint; +import android.annotation.TargetApi; import android.app.AlarmManager; import android.app.PendingIntent; import android.app.Service; @@ -128,6 +129,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa public static final String ACTION_DISABLE_FOREGROUND = "disable_foreground"; public static final String ACTION_TRY_AGAIN = "try_again"; public static final String ACTION_DISABLE_ACCOUNT = "disable_account"; + public static final String ACTION_IDLE_PING = "idle_ping"; private static final String ACTION_MERGE_PHONE_CONTACTS = "merge_phone_contacts"; public static final String ACTION_GCM_TOKEN_REFRESH = "gcm_token_refresh"; public static final String ACTION_GCM_MESSAGE_RECEIVED = "gcm_message_received"; @@ -551,8 +553,14 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa 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()); + break; } } this.wakeLock.acquire(); @@ -627,7 +635,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa if (pingNow) { for (Account account : pingCandidates) { account.getXmppConnection().sendPing(); - Log.d(Config.LOGTAG, account.getJid().toBareJid() + " send ping"); + Log.d(Config.LOGTAG, account.getJid().toBareJid() + " send ping (action="+action+")"); this.scheduleWakeUpCall(Config.PING_TIMEOUT, account.getUuid().hashCode()); } } @@ -759,6 +767,9 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa toggleForegroundService(); updateUnreadCountBadge(); toggleScreenEventReceiver(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + scheduleNextIdlePing(); + } } @Override @@ -841,16 +852,25 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa public void scheduleWakeUpCall(int seconds, int requestCode) { final long timeToWake = SystemClock.elapsedRealtime() + (seconds < 0 ? 1 : seconds + 1) * 1000; - - Context context = getApplicationContext(); - AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); - - Intent intent = new Intent(context, EventReceiver.class); + AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); + Intent intent = new Intent(this, EventReceiver.class); intent.setAction("ping"); - PendingIntent alarmIntent = PendingIntent.getBroadcast(context, requestCode, intent, 0); + PendingIntent alarmIntent = PendingIntent.getBroadcast(this, requestCode, intent, 0); alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, timeToWake, alarmIntent); } + @TargetApi(Build.VERSION_CODES.M) + private void scheduleNextIdlePing() { + Log.d(Config.LOGTAG,"schedule next idle ping"); + AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); + Intent intent = new Intent(this, EventReceiver.class); + intent.setAction(ACTION_IDLE_PING); + alarmManager.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, + SystemClock.elapsedRealtime()+(Config.IDLE_PING_INTERVAL * 1000), + PendingIntent.getBroadcast(this,0,intent,0) + ); + } + public XmppConnection createConnection(final Account account) { final SharedPreferences sharedPref = getPreferences(); account.setResource(sharedPref.getString("resource", getString(R.string.default_resource)) @@ -1831,6 +1851,9 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa account.pendingConferenceLeaves.remove(conversation); if (account.getStatus() == Account.State.ONLINE) { conversation.resetMucOptions(); + if (onConferenceJoined != null) { + conversation.getMucOptions().flagNoAutoPushConfiguration(); + } conversation.setHasMessagesLeftOnServer(false); fetchConferenceConfiguration(conversation, new OnConferenceConfigurationFetched() { @@ -2048,8 +2071,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa } return; } - String localpart = new BigInteger(75, getRNG()).toString(32); - Jid jid = Jid.fromParts(localpart, server, null); + final Jid jid = Jid.fromParts(new BigInteger(64, getRNG()).toString(Character.MAX_RADIX), server, null); final Conversation conversation = findOrCreateConversation(account, jid, true); joinMuc(conversation, new OnConferenceJoined() { @Override @@ -2058,7 +2080,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa @Override public void onPushSucceeded() { if (subject != null && !subject.trim().isEmpty()) { - pushSubjectToConference(conversation, subject); + pushSubjectToConference(conversation, subject.trim()); } for (Jid invite : jids) { invite(conversation, invite); @@ -2066,6 +2088,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa if (account.countPresences() > 1) { directInvite(conversation, account.getJid().toBareJid()); } + saveConversationAsBookmark(conversation, subject); if (callback != null) { callback.success(conversation); } @@ -2073,6 +2096,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa @Override public void onPushFailed() { + archiveConversation(conversation); if (callback != null) { callback.error(R.string.conference_creation_failed, conversation); } @@ -3306,6 +3330,21 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa return templates; } + public void saveConversationAsBookmark(Conversation conversation, String name) { + Account account = conversation.getAccount(); + Bookmark bookmark = new Bookmark(account, conversation.getJid().toBareJid()); + if (!conversation.getJid().isBareJid()) { + bookmark.setNick(conversation.getJid().getResourcepart()); + } + if (name != null && !name.trim().isEmpty()) { + bookmark.setBookmarkName(name.trim()); + } + bookmark.setAutojoin(getPreferences().getBoolean("autojoin",true)); + account.getBookmarks().add(bookmark); + pushBookmarks(account); + conversation.setBookmark(bookmark); + } + public interface OnMamPreferencesFetched { void onPreferencesFetched(Element prefs); void onPreferencesFetchFailed(); |