2018-10-09 17:35:33 +00:00
|
|
|
package eu.siacs.conversations.utils;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.Intent;
|
2018-10-17 20:50:34 +00:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
2018-10-09 17:35:33 +00:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
2019-03-31 15:12:01 +00:00
|
|
|
import java.util.ArrayList;
|
2018-10-09 17:35:33 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
2019-03-31 15:12:01 +00:00
|
|
|
import eu.siacs.conversations.Config;
|
2018-10-09 17:35:33 +00:00
|
|
|
import eu.siacs.conversations.R;
|
|
|
|
import eu.siacs.conversations.entities.Account;
|
|
|
|
import eu.siacs.conversations.services.XmppConnectionService;
|
2018-10-17 20:50:34 +00:00
|
|
|
import eu.siacs.conversations.ui.XmppActivity;
|
2018-10-09 17:35:33 +00:00
|
|
|
|
|
|
|
public class AccountUtils {
|
|
|
|
|
2021-03-13 10:13:19 +00:00
|
|
|
public static final Class<?> MANAGE_ACCOUNT_ACTIVITY;
|
2018-10-09 17:35:33 +00:00
|
|
|
|
|
|
|
static {
|
|
|
|
MANAGE_ACCOUNT_ACTIVITY = getManageAccountActivityClass();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-26 07:30:38 +00:00
|
|
|
public static boolean hasEnabledAccounts(final XmppConnectionService service) {
|
|
|
|
final List<Account> accounts = service.getAccounts();
|
|
|
|
for(Account account : accounts) {
|
|
|
|
if (account.isOptionSet(Account.OPTION_DISABLED)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-31 15:12:01 +00:00
|
|
|
public static List<String> getEnabledAccounts(final XmppConnectionService service) {
|
|
|
|
ArrayList<String> accounts = new ArrayList<>();
|
|
|
|
for (Account account : service.getAccounts()) {
|
|
|
|
if (account.getStatus() != Account.State.DISABLED) {
|
|
|
|
if (Config.DOMAIN_LOCK != null) {
|
2020-06-14 09:28:25 +00:00
|
|
|
accounts.add(account.getJid().getEscapedLocal());
|
2019-03-31 15:12:01 +00:00
|
|
|
} else {
|
2020-05-18 07:35:35 +00:00
|
|
|
accounts.add(account.getJid().asBareJid().toEscapedString());
|
2019-03-31 15:12:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return accounts;
|
|
|
|
}
|
|
|
|
|
2018-10-09 17:35:33 +00:00
|
|
|
public static Account getFirstEnabled(XmppConnectionService service) {
|
|
|
|
final List<Account> accounts = service.getAccounts();
|
|
|
|
for(Account account : accounts) {
|
|
|
|
if (!account.isOptionSet(Account.OPTION_DISABLED)) {
|
|
|
|
return account;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-10-20 14:37:59 +00:00
|
|
|
public static Account getFirst(XmppConnectionService service) {
|
|
|
|
final List<Account> accounts = service.getAccounts();
|
|
|
|
for(Account account : accounts) {
|
|
|
|
return account;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-10-09 17:35:33 +00:00
|
|
|
public static Account getPendingAccount(XmppConnectionService service) {
|
|
|
|
Account pending = null;
|
|
|
|
for (Account account : service.getAccounts()) {
|
|
|
|
if (!account.isOptionSet(Account.OPTION_LOGGED_IN_SUCCESSFULLY)) {
|
|
|
|
pending = account;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pending;
|
|
|
|
}
|
|
|
|
|
2021-03-13 10:13:19 +00:00
|
|
|
public static void launchManageAccounts(final Activity activity) {
|
2018-10-09 17:35:33 +00:00
|
|
|
if (MANAGE_ACCOUNT_ACTIVITY != null) {
|
|
|
|
activity.startActivity(new Intent(activity, MANAGE_ACCOUNT_ACTIVITY));
|
|
|
|
} else {
|
|
|
|
Toast.makeText(activity, R.string.feature_not_implemented, Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-13 10:13:19 +00:00
|
|
|
public static void launchManageAccount(final XmppActivity xmppActivity) {
|
|
|
|
final Account account = getFirst(xmppActivity.xmppConnectionService);
|
2018-10-17 20:50:34 +00:00
|
|
|
xmppActivity.switchToAccount(account);
|
|
|
|
}
|
|
|
|
|
2021-03-13 10:13:19 +00:00
|
|
|
private static Class<?> getManageAccountActivityClass() {
|
2018-10-09 17:35:33 +00:00
|
|
|
try {
|
|
|
|
return Class.forName("eu.siacs.conversations.ui.ManageAccountActivity");
|
2021-03-13 10:13:19 +00:00
|
|
|
} catch (final ClassNotFoundException e) {
|
2018-10-09 17:35:33 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2018-10-17 20:50:34 +00:00
|
|
|
|
|
|
|
public static void showHideMenuItems(final Menu menu) {
|
|
|
|
final MenuItem manageAccounts = menu.findItem(R.id.action_accounts);
|
|
|
|
final MenuItem manageAccount = menu.findItem(R.id.action_account);
|
2019-11-02 08:43:37 +00:00
|
|
|
if (manageAccount != null) {
|
|
|
|
manageAccount.setVisible(MANAGE_ACCOUNT_ACTIVITY == null);
|
|
|
|
}
|
|
|
|
if (manageAccounts != null) {
|
|
|
|
manageAccounts.setVisible(MANAGE_ACCOUNT_ACTIVITY != null);
|
|
|
|
}
|
2018-10-17 20:50:34 +00:00
|
|
|
}
|
2018-10-09 17:35:33 +00:00
|
|
|
}
|