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