ChooseAccountForProfilePictureActivity.java

 1package eu.siacs.conversations.ui;
 2
 3import android.content.Intent;
 4import android.net.Uri;
 5import android.os.Bundle;
 6import android.support.v7.app.ActionBar;
 7import android.widget.ListView;
 8
 9import java.util.ArrayList;
10import java.util.List;
11
12import eu.siacs.conversations.R;
13import eu.siacs.conversations.entities.Account;
14import eu.siacs.conversations.entities.Conversation;
15import eu.siacs.conversations.ui.adapter.AccountAdapter;
16import rocks.xmpp.addr.Jid;
17
18public class ChooseAccountForProfilePictureActivity extends XmppActivity {
19
20    protected final List<Account> accountList = new ArrayList<>();
21    protected ListView accountListView;
22    protected AccountAdapter mAccountAdapter;
23
24    @Override
25    protected void refreshUiReal() {
26        loadEnabledAccounts();
27        mAccountAdapter.notifyDataSetChanged();
28    }
29
30    @Override
31    protected void onCreate(Bundle savedInstanceState) {
32        super.onCreate(savedInstanceState);
33        setContentView(R.layout.activity_manage_accounts);
34        setSupportActionBar(findViewById(R.id.toolbar));
35        configureActionBar(getSupportActionBar(), false);
36        accountListView = findViewById(R.id.account_list);
37        this.mAccountAdapter = new AccountAdapter(this, accountList, false);
38        accountListView.setAdapter(this.mAccountAdapter);
39        accountListView.setOnItemClickListener((arg0, view, position, arg3) -> {
40            final Account account = accountList.get(position);
41            goToProfilePictureActivity(account);
42        });
43    }
44
45    @Override
46    protected void onStart() {
47        super.onStart();
48        final int theme = findTheme();
49        if (this.mTheme != theme) {
50            recreate();
51        }
52    }
53
54    @Override
55    void onBackendConnected() {
56        loadEnabledAccounts();
57        if (accountList.size() == 1) {
58            goToProfilePictureActivity(accountList.get(0));
59            return;
60        }
61        mAccountAdapter.notifyDataSetChanged();
62    }
63
64    private void loadEnabledAccounts() {
65        accountList.clear();
66        for(Account account : xmppConnectionService.getAccounts()) {
67            if (account.isEnabled()) {
68                accountList.add(account);
69            }
70        }
71    }
72
73    private void goToProfilePictureActivity(Account account) {
74        final Intent startIntent = getIntent();
75        final Uri uri = startIntent == null ? null : startIntent.getData();
76        if (uri != null) {
77            Intent intent = new Intent(this, PublishProfilePictureActivity.class);
78            intent.putExtra(EXTRA_ACCOUNT, account.getJid().asBareJid().toString());
79            intent.setData(uri);
80            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
81            startActivity(intent);
82        }
83        finish();
84    }
85}