redirect on empty conversation list

This commit is contained in:
Daniel Gultsch 2018-02-24 20:19:35 +01:00
parent 7bc7853d1a
commit e93b06f45c
3 changed files with 78 additions and 3 deletions

View File

@ -1648,6 +1648,19 @@ public class XmppConnectionService extends Service {
return null; return null;
} }
public boolean isConversationsListEmpty(final Conversation ignore) {
synchronized (this.conversations) {
final int size = this.conversations.size();
if (size == 0) {
return true;
} else if (size == 1) {
return this.conversations.get(0) == ignore;
} else {
return false;
}
}
}
public Conversation findOrCreateConversation(Account account, Jid jid, boolean muc, final boolean async) { public Conversation findOrCreateConversation(Account account, Jid jid, boolean muc, final boolean async) {
return this.findOrCreateConversation(account, jid, muc, false, async); return this.findOrCreateConversation(account, jid, muc, false, async);
} }
@ -3771,7 +3784,7 @@ public class XmppConnectionService extends Service {
public Account getPendingAccount() { public Account getPendingAccount() {
Account pending = null; Account pending = null;
for (Account account : getAccounts()) { for (Account account : getAccounts()) {
if (account.isOptionSet(Account.OPTION_REGISTER)) { if (!account.isOptionSet(Account.OPTION_LOGGED_IN_SUCCESSFULLY)) {
pending = account; pending = account;
} else { } else {
return null; return null;

View File

@ -44,9 +44,12 @@ import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.widget.Toast; import android.widget.Toast;
import java.util.concurrent.atomic.AtomicBoolean;
import eu.siacs.conversations.Config; import eu.siacs.conversations.Config;
import eu.siacs.conversations.R; import eu.siacs.conversations.R;
import eu.siacs.conversations.databinding.ActivityConversationsBinding; import eu.siacs.conversations.databinding.ActivityConversationsBinding;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.ui.interfaces.OnConversationArchived; import eu.siacs.conversations.ui.interfaces.OnConversationArchived;
@ -73,6 +76,7 @@ public class ConversationActivity extends XmppActivity implements OnConversation
private final PendingItem<Intent> pendingViewIntent = new PendingItem<>(); private final PendingItem<Intent> pendingViewIntent = new PendingItem<>();
private ActivityConversationsBinding binding; private ActivityConversationsBinding binding;
private boolean mActivityPaused = true; private boolean mActivityPaused = true;
private AtomicBoolean mRedirectInProcess = new AtomicBoolean(false);
private static boolean isViewIntent(Intent i) { private static boolean isViewIntent(Intent i) {
return i != null && ACTION_VIEW_CONVERSATION.equals(i.getAction()) && i.hasExtra(EXTRA_CONVERSATION); return i != null && ACTION_VIEW_CONVERSATION.equals(i.getAction()) && i.hasExtra(EXTRA_CONVERSATION);
@ -94,6 +98,9 @@ public class ConversationActivity extends XmppActivity implements OnConversation
@Override @Override
void onBackendConnected() { void onBackendConnected() {
if (performRedirectIfNecessary(true)) {
return;
}
for (@IdRes int id : FRAGMENT_ID_NOTIFICATION_ORDER) { for (@IdRes int id : FRAGMENT_ID_NOTIFICATION_ORDER) {
notifyFragmentOfBackendConnected(id); notifyFragmentOfBackendConnected(id);
} }
@ -113,6 +120,55 @@ public class ConversationActivity extends XmppActivity implements OnConversation
} }
} }
private boolean performRedirectIfNecessary(boolean noAnimation) {
return performRedirectIfNecessary(null, noAnimation);
}
private boolean performRedirectIfNecessary(final Conversation ignore, final boolean noAnimation) {
if (xmppConnectionService == null) {
return false;
}
boolean isConversationsListEmpty = xmppConnectionService.isConversationsListEmpty(ignore);
if (isConversationsListEmpty && mRedirectInProcess.compareAndSet(false, true)) {
final Intent intent = getRedirectionIntent(noAnimation);
runOnUiThread(() -> {
startActivity(intent);
if (noAnimation) {
overridePendingTransition(0, 0);
}
});
}
return mRedirectInProcess.get();
}
private Intent getRedirectionIntent(boolean noAnimation) {
Account pendingAccount = xmppConnectionService.getPendingAccount();
Intent intent;
if (pendingAccount != null) {
intent = new Intent(this, EditAccountActivity.class);
intent.putExtra("jid", pendingAccount.getJid().toBareJid().toString());
} else {
if (xmppConnectionService.getAccounts().size() == 0) {
if (Config.X509_VERIFICATION) {
intent = new Intent(this, ManageAccountActivity.class);
} else if (Config.MAGIC_CREATE_DOMAIN != null) {
intent = new Intent(this, WelcomeActivity.class);
WelcomeActivity.addInviteUri(intent, getIntent());
} else {
intent = new Intent(this, EditAccountActivity.class);
}
} else {
intent = new Intent(this, StartConversationActivity.class);
}
}
intent.putExtra("init", true);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
if (noAnimation) {
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
}
return intent;
}
private void notifyFragmentOfBackendConnected(@IdRes int id) { private void notifyFragmentOfBackendConnected(@IdRes int id) {
final Fragment fragment = getFragmentManager().findFragmentById(id); final Fragment fragment = getFragmentManager().findFragmentById(id);
if (fragment != null && fragment instanceof XmppFragment) { if (fragment != null && fragment instanceof XmppFragment) {
@ -211,6 +267,7 @@ public class ConversationActivity extends XmppActivity implements OnConversation
if (this.mTheme != theme) { if (this.mTheme != theme) {
recreate(); recreate();
} }
mRedirectInProcess.set(false);
super.onStart(); super.onStart();
} }
@ -297,7 +354,9 @@ public class ConversationActivity extends XmppActivity implements OnConversation
@Override @Override
public void onConversationArchived(Conversation conversation) { public void onConversationArchived(Conversation conversation) {
//TODO; check if nothing more left; if (performRedirectIfNecessary(conversation, false)) {
return;
}
Fragment mainFragment = getFragmentManager().findFragmentById(R.id.main_fragment); Fragment mainFragment = getFragmentManager().findFragmentById(R.id.main_fragment);
if (mainFragment != null && mainFragment instanceof ConversationFragment) { if (mainFragment != null && mainFragment instanceof ConversationFragment) {
getFragmentManager().popBackStack(); getFragmentManager().popBackStack();
@ -337,6 +396,9 @@ public class ConversationActivity extends XmppActivity implements OnConversation
@Override @Override
public void onConversationUpdate() { public void onConversationUpdate() {
if (performRedirectIfNecessary(false)) {
return;
}
this.refreshUi(); this.refreshUi();
} }

View File

@ -96,7 +96,7 @@ public class WelcomeActivity extends XmppActivity {
public static void launch(AppCompatActivity activity) { public static void launch(AppCompatActivity activity) {
Intent intent = new Intent(activity, WelcomeActivity.class); Intent intent = new Intent(activity, WelcomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
activity.startActivity(intent); activity.startActivity(intent);
activity.overridePendingTransition(0,0); activity.overridePendingTransition(0,0);
} }