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