Initial commit
[mustard2:mustard2.git] / src / org / mustard2 / android / activity / Accounts.java
1 /**
2  * MUSTARD: Android's Client for StatusNet
3  * 
4  * Copyright (C) 2009-2010 macno.org, Michele Azzolari
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  * 
20  */
21 package org.mustard2.android.activity;
22
23 import org.mustard2.android.Account;
24 import org.mustard2.android.MustardApp;
25 import org.mustard2.android.R;
26 import org.mustard2.android.activity.setup.AccountSetupOAuth;
27 import org.mustard2.android.view.RemoteImageView;
28 import org.mustard2.manager.DbManager;
29
30 import android.app.AlertDialog;
31 import android.app.Dialog;
32 import android.app.ListActivity;
33 import android.app.NotificationManager;
34 import android.content.Context;
35 import android.content.DialogInterface;
36 import android.content.Intent;
37 import android.os.Bundle;
38 import android.util.Log;
39 import android.view.ContextMenu;
40 import android.view.Menu;
41 import android.view.MenuItem;
42 import android.view.View;
43 import android.view.ViewGroup;
44 import android.view.ContextMenu.ContextMenuInfo;
45 import android.widget.ArrayAdapter;
46 import android.widget.CheckBox;
47 import android.widget.CompoundButton;
48 import android.widget.ListView;
49 import android.widget.RadioButton;
50 import android.widget.TextView;
51 import android.widget.AdapterView.AdapterContextMenuInfo;
52
53 /**
54  * @author macno
55  *
56  */
57 public class Accounts extends ListActivity {
58         
59         private final String TAG = "mustard/Accounts"; 
60         
61         private static final int DIALOG_REMOVE_ACCOUNT = 1;
62         private Account mSelectedContextAccount;
63         
64     public static void actionShowAccounts(Context context) {
65         Intent i = new Intent(context, Accounts.class);
66         i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
67         context.startActivity(i);
68     }
69     
70     private void onAddNewAccount() {
71         AccountSetupOAuth.actionNewAccount(this);
72     }
73     
74     private void onOpenAccount(Account account) {
75         ( (MustardApp) getApplication()).getAccountManager().setDefaultAccount(account);
76         Timeline.actionHandleTimeline(this, DbManager.ROWTYPE_FRIENDS, null);
77         finish();
78     }
79     
80     private void onChangeToMergeAccount(Account account, boolean isToMerge) {
81         Log.i(MustardApp.APPLICATION_NAME," onChangeToMergeAccount " + isToMerge);
82         ( (MustardApp) getApplication()).getAccountManager().setToMerge(account, isToMerge);
83     }
84     
85     @Override
86         public void onCreate(Bundle icicle) {
87         super.onCreate(icicle);
88         setContentView(R.layout.accounts);
89         ListView listView = getListView();
90         
91 //        listView.setItemsCanFocus(false);
92         listView.setEmptyView(findViewById(R.id.empty));
93         findViewById(R.id.add_new_account).setOnClickListener(new View.OnClickListener() {
94                         public void onClick(View v) {
95                                 onAddNewAccount();
96                         }
97                 });
98         registerForContextMenu(listView);
99
100         if (icicle != null && icicle.containsKey("selectedContextAccount")) {
101             mSelectedContextAccount = (Account) icicle.getSerializable("selectedContextAccount");
102         }
103     }
104
105     @Override
106     public void onSaveInstanceState(Bundle outState) {
107         super.onSaveInstanceState(outState);
108         if (mSelectedContextAccount != null) {
109             outState.putSerializable("selectedContextAccount", mSelectedContextAccount);
110         }
111     }
112
113     @Override
114     public void onResume() {
115         super.onResume();
116
117         NotificationManager notifMgr = (NotificationManager)
118                 getSystemService(Context.NOTIFICATION_SERVICE);
119         notifMgr.cancel(1);
120
121         refresh();
122     }
123
124     private void refresh() {
125         Account[] accounts = ((MustardApp)getApplication()).getAccountManager().getAccounts();
126         Log.d(TAG,"Found " + accounts.length + " accounts");
127         getListView().setAdapter(new AccountsAdapter(accounts));
128     }
129
130     private Dialog createRemoveAccountDialog() {
131         return new AlertDialog.Builder(this)
132             .setIcon(android.R.drawable.ic_dialog_alert)
133             .setTitle(R.string.account_delete_dlg_title)
134             .setMessage(getString(R.string.account_delete_dlg_instructions_fmt,
135                     mSelectedContextAccount.getNickname()))
136             .setPositiveButton(R.string.btn_okay, new DialogInterface.OnClickListener() {
137                 public void onClick(DialogInterface dialog, int whichButton) {
138                     dismissDialog(DIALOG_REMOVE_ACCOUNT);
139                     MustardApp mustardApp = (MustardApp)getApplication();
140                     mustardApp.getAccountManager().deleteAccount(mSelectedContextAccount);
141                     mSelectedContextAccount = null;
142                     refresh();
143                 }
144             })
145             .setNegativeButton(R.string.btn_cancel, new DialogInterface.OnClickListener() {
146                 public void onClick(DialogInterface dialog, int whichButton) {
147                     dismissDialog(DIALOG_REMOVE_ACCOUNT);
148                 }
149             })
150             .create();
151     }
152
153     private void onDeleteAccount(Account account) {
154         mSelectedContextAccount = account;
155         showDialog(DIALOG_REMOVE_ACCOUNT);
156     }
157     
158     @Override
159         public Dialog onCreateDialog(int id) {
160         switch (id) {
161             case DIALOG_REMOVE_ACCOUNT:
162                 return createRemoveAccountDialog();
163         }
164         return super.onCreateDialog(id);
165     }
166     
167     public void onClick(View view) {
168         if (view.getId() == R.id.add_new_account) {
169             onAddNewAccount();
170         }
171     }
172         
173     @Override
174         public boolean onCreateOptionsMenu(Menu menu) {
175         super.onCreateOptionsMenu(menu);
176         getMenuInflater().inflate(R.menu.accounts_option, menu);
177         return true;
178     }
179         
180     @Override
181         public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
182         super.onCreateContextMenu(menu, v, menuInfo);
183         menu.setHeaderTitle(R.string.accounts_context_menu_title);
184         getMenuInflater().inflate(R.menu.accounts_context, menu);
185     }
186
187     @Override
188         public boolean onOptionsItemSelected(MenuItem item) {
189         switch (item.getItemId()) {
190             case R.id.add_new_account:
191                 onAddNewAccount();
192                 break;
193
194             default:
195                 return super.onOptionsItemSelected(item);
196         }
197         return true;
198     }
199     
200     @Override
201         public boolean onContextItemSelected(MenuItem item) {
202         AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo)item.getMenuInfo();
203         Account account = (Account)getListView().getItemAtPosition(menuInfo.position);
204         switch (item.getItemId()) {
205             case R.id.delete_account:
206                 onDeleteAccount(account);
207                 break;
208             case R.id.edit_account:
209                 // TODO
210 //                onEditAccount(account);
211                 break;
212             case R.id.open:
213                 onOpenAccount(account);
214                 break;
215         }
216         return true;
217     }
218     
219     class AccountsAdapter extends ArrayAdapter<Account> {
220         
221         public AccountsAdapter(Account[] accounts) {
222             super(Accounts.this, 0, accounts);
223         }
224
225         @Override
226         public View getView(int position, View convertView, ViewGroup parent) {
227             final Account account = getItem(position);
228             View view;
229             if (convertView != null) {
230                 view = convertView;
231             }
232             else {
233                 view = getLayoutInflater().inflate(R.layout.accounts_item, parent, false);
234             }
235             view.setOnClickListener(new View.OnClickListener() {
236                         public void onClick(View v) {
237 //                              onOpenAccount(account);
238                         }
239                 });
240                 view.setOnLongClickListener(new View.OnLongClickListener() {
241                         public boolean onLongClick(View v) {
242                                 openContextMenu(v);
243                                 return true;
244                         }
245                 });
246             AccountViewHolder holder = (AccountViewHolder) view.getTag();
247             if (holder == null) {
248                 holder = new AccountViewHolder();
249                 holder.nickname = (TextView) view.findViewById(R.id.nickname);
250                 holder.instance = (TextView) view.findViewById(R.id.instance);
251                 holder.checkBoxDefault = (CheckBox) view.findViewById(R.id.merge);
252                 holder.radioButtonDefault = (RadioButton)view.findViewById(R.id.radio);
253                 holder.avatar = (RemoteImageView) view.findViewById(R.id.profile_image);
254                 view.setTag(holder);
255             }
256             if (account.getAvatarUrl() != null && !"".equals(account.getAvatarUrl())) {
257                 holder.avatar.setRemoteURI(account.getAvatarUrl());
258                 holder.avatar.loadImage();
259             }
260             holder.nickname.setText(account.getNickname());
261             holder.instance.setText(account.getInstance());
262             holder.radioButtonDefault.setChecked(account.isDefault());
263             holder.checkBoxDefault.setChecked(account.isToMerge());
264             holder.checkBoxDefault.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
265
266                                 public void onCheckedChanged(CompoundButton buttonView,
267                                                 boolean isChecked) {
268                                         onChangeToMergeAccount(account,isChecked);
269                                 }
270
271                         });
272             return view;
273         }
274
275         class AccountViewHolder {
276                 RemoteImageView avatar;
277             TextView nickname;
278             TextView instance;
279             CheckBox checkBoxDefault;
280             RadioButton radioButtonDefault;
281         }
282     }
283
284 }