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.app.Activity;
10import android.content.Context;
11import android.content.Intent;
12import android.os.Bundle;
13import android.util.Log;
14import android.view.LayoutInflater;
15import android.view.Menu;
16import android.view.MenuItem;
17import android.view.View;
18import android.view.ViewGroup;
19import android.widget.AdapterView;
20import android.widget.AdapterView.OnItemClickListener;
21import android.widget.ArrayAdapter;
22import android.widget.ListView;
23import android.widget.TextView;
24
25public class ManageAccountActivity extends XmppActivity {
26
27 protected List<Account> accountList = new ArrayList<Account>();
28 protected ListView accountListView;
29 protected ArrayAdapter<Account> accountListViewAdapter;
30 protected OnAccountListChangedListener accountChanged = new OnAccountListChangedListener() {
31
32 @Override
33 public void onAccountListChangedListener() {
34 Log.d("xmppService", "ui on account list changed listener");
35 accountList.clear();
36 accountList.addAll(xmppConnectionService.getAccounts());
37 runOnUiThread(new Runnable() {
38
39 @Override
40 public void run() {
41 if (accountList.size() == 1) {
42 startActivity(new Intent(getApplicationContext(),
43 NewConversationActivity.class));
44 }
45 accountListViewAdapter.notifyDataSetChanged();
46 }
47 });
48 }
49 };
50
51 @Override
52 protected void onCreate(Bundle savedInstanceState) {
53
54 super.onCreate(savedInstanceState);
55
56 setContentView(R.layout.manage_accounts);
57
58 accountListView = (ListView) findViewById(R.id.account_list);
59 accountListViewAdapter = new ArrayAdapter<Account>(
60 getApplicationContext(), R.layout.account_row, this.accountList) {
61 @Override
62 public View getView(int position, View view, ViewGroup parent) {
63 Account account = getItem(position);
64 if (view == null) {
65 LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
66 view = (View) inflater.inflate(R.layout.account_row, null);
67 }
68 ((TextView) view.findViewById(R.id.account_jid))
69 .setText(account.getJid());
70 TextView statusView = (TextView) view
71 .findViewById(R.id.account_status);
72 switch (account.getStatus()) {
73 case Account.STATUS_ONLINE:
74 statusView.setText("online");
75 statusView.setTextColor(0xFF83b600);
76 break;
77 case Account.STATUS_OFFLINE:
78 statusView.setText("offline");
79 statusView.setTextColor(0xFFe92727);
80 break;
81 case Account.STATUS_UNAUTHORIZED:
82 statusView.setText("unauthorized");
83 statusView.setTextColor(0xFFe92727);
84 break;
85 case Account.STATUS_SERVER_NOT_FOUND:
86 statusView.setText("server not found");
87 statusView.setTextColor(0xFFe92727);
88 break;
89 default:
90 break;
91 }
92
93 return view;
94 }
95 };
96 accountListView.setAdapter(this.accountListViewAdapter);
97 accountListView.setOnItemClickListener(new OnItemClickListener() {
98
99 @Override
100 public void onItemClick(AdapterView<?> arg0, View view,
101 int position, long arg3) {
102 EditAccount dialog = new EditAccount();
103 dialog.setAccount(accountList.get(position));
104 dialog.setEditAccountListener(new EditAccountListener() {
105
106 @Override
107 public void onAccountEdited(Account account) {
108 xmppConnectionService.updateAccount(account);
109 }
110
111 @Override
112 public void onAccountDelete(Account account) {
113 xmppConnectionService.deleteAccount(account);
114 }
115 });
116 dialog.show(getFragmentManager(), "edit_account");
117 }
118 });
119 }
120
121 @Override
122 protected void onStop() {
123 super.onStop();
124 if (xmppConnectionServiceBound) {
125 xmppConnectionService.removeOnAccountListChangedListener();
126 unbindService(mConnection);
127 xmppConnectionServiceBound = false;
128 }
129 }
130
131 @Override
132 void onBackendConnected() {
133 xmppConnectionService.setOnAccountListChangedListener(accountChanged);
134 this.accountList.clear();
135 this.accountList.addAll(xmppConnectionService.getAccounts());
136 accountListViewAdapter.notifyDataSetChanged();
137 if (this.accountList.size() == 0) {
138 getActionBar().setDisplayHomeAsUpEnabled(false);
139 addAccount();
140 }
141 }
142
143 @Override
144 public boolean onCreateOptionsMenu(Menu menu) {
145 // Inflate the menu; this adds items to the action bar if it is present.
146 getMenuInflater().inflate(R.menu.manageaccounts, menu);
147 return true;
148 }
149
150 @Override
151 public boolean onOptionsItemSelected(MenuItem item) {
152 switch (item.getItemId()) {
153 case R.id.action_settings:
154 startActivity(new Intent(this, SettingsActivity.class));
155 break;
156 case R.id.action_add_account:
157 addAccount();
158 break;
159 default:
160 break;
161 }
162 return super.onOptionsItemSelected(item);
163 }
164
165 protected void addAccount() {
166 final Activity activity = this;
167 EditAccount dialog = new EditAccount();
168 dialog.setEditAccountListener(new EditAccountListener() {
169
170 @Override
171 public void onAccountEdited(Account account) {
172 xmppConnectionService.createAccount(account);
173 activity.getActionBar().setDisplayHomeAsUpEnabled(true);
174 }
175
176 @Override
177 public void onAccountDelete(Account account) {
178 // this will never be called
179 }
180 });
181 dialog.show(getFragmentManager(), "add_account");
182 }
183}