1package de.gultsch.chat.ui;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import de.gultsch.chat.R;
7import de.gultsch.chat.entities.Account;
8import de.gultsch.chat.ui.EditAccount.EditAccountListener;
9import android.content.Context;
10import android.content.Intent;
11import android.os.Bundle;
12import android.util.Log;
13import android.view.LayoutInflater;
14import android.view.Menu;
15import android.view.MenuItem;
16import android.view.View;
17import android.view.ViewGroup;
18import android.widget.AdapterView;
19import android.widget.AdapterView.OnItemClickListener;
20import android.widget.ArrayAdapter;
21import android.widget.ListView;
22import android.widget.TextView;
23
24public class ManageAccountActivity extends XmppActivity {
25
26
27 protected List<Account> accountList = new ArrayList<Account>();
28 protected ListView accountListView;
29 protected ArrayAdapter<Account> accountListViewAdapter;
30
31 @Override
32 protected void onCreate(Bundle savedInstanceState) {
33
34 super.onCreate(savedInstanceState);
35
36 setContentView(R.layout.manage_accounts);
37
38 accountListView = (ListView) findViewById(R.id.account_list);
39 accountListViewAdapter = new ArrayAdapter<Account>(getApplicationContext(), R.layout.account_row, this.accountList) {
40 @Override
41 public View getView(int position, View view, ViewGroup parent) {
42 if (view == null) {
43 LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
44 view = (View) inflater.inflate(R.layout.account_row, null);
45 }
46 ((TextView) view.findViewById(R.id.account_jid)).setText(getItem(position).getJid());
47
48 return view;
49 }
50 };
51 accountListView.setAdapter(this.accountListViewAdapter);
52 accountListView.setOnItemClickListener(new OnItemClickListener() {
53
54 @Override
55 public void onItemClick(AdapterView<?> arg0, View view, int position,
56 long arg3) {
57 EditAccount dialog = new EditAccount();
58 dialog.setAccount(accountList.get(position));
59 dialog.setEditAccountListener(new EditAccountListener() {
60
61 @Override
62 public void onAccountEdited(Account account) {
63 xmppConnectionService.updateAccount(account);
64 }
65
66 @Override
67 public void onAccountDelete(Account account) {
68
69 Log.d("gultsch","deleting account:"+account.getJid());
70
71 xmppConnectionService.deleteAccount(account);
72
73 //dont bother finding the right account in the frontend list. just reload
74 accountList.clear();
75 accountList.addAll(xmppConnectionService.getAccounts());
76
77 accountListViewAdapter.notifyDataSetChanged();
78
79 }
80 });
81 dialog.show(getFragmentManager(),"edit_account");
82 }
83 });
84 }
85
86 @Override
87 public void onStart() {
88 super.onStart();
89 if (xmppConnectionServiceBound) {
90 Log.d("gultsch","already bound");
91 this.accountList.clear();
92 this.accountList.addAll(xmppConnectionService
93 .getAccounts());
94 accountListViewAdapter.notifyDataSetChanged();
95 }
96 }
97
98 @Override
99 void onBackendConnected() {
100 Log.d("gultsch","called on backend connected");
101 this.accountList.clear();
102 this.accountList.addAll(xmppConnectionService.getAccounts());
103 accountListViewAdapter.notifyDataSetChanged();
104 }
105
106 @Override
107 public boolean onCreateOptionsMenu(Menu menu) {
108 // Inflate the menu; this adds items to the action bar if it is present.
109 getMenuInflater().inflate(R.menu.manageaccounts, menu);
110 return true;
111 }
112
113 @Override
114 public boolean onOptionsItemSelected(MenuItem item) {
115 switch (item.getItemId()) {
116 case R.id.action_settings:
117 startActivity(new Intent(this, SettingsActivity.class));
118 break;
119 case R.id.action_add_account:
120 EditAccount dialog = new EditAccount();
121 dialog.setEditAccountListener(new EditAccountListener() {
122
123 @Override
124 public void onAccountEdited(Account account) {
125 xmppConnectionService.createAccount(account);
126 accountList.add(account);
127 accountListViewAdapter.notifyDataSetChanged();
128 }
129
130 @Override
131 public void onAccountDelete(Account account) {
132 //this will never be called
133 }
134 });
135 dialog.show(getFragmentManager(),"add_account");
136 break;
137 default:
138 break;
139 }
140 return super.onOptionsItemSelected(item);
141 }
142}