/* AccountsManager.java
*
* Copyright 2011 Aleksey Konovalov
* All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package ru.chatty.engine;
import java.util.ArrayList;
import java.util.HashMap;
import android.content.ContentResolver;
import android.database.ContentObserver;
import android.database.Cursor;
import android.os.Handler;
import ru.chatty.db.ChattyDatabase.Accounts;
import ru.chatty.im.Account;
import ru.chatty.im.AccountsFactory;
import ru.chatty.im.ConnectionListener;
import ru.chatty.im.ContactListListener;
import ru.chatty.im.MessagesListener;
public class AccountsManager extends ContentObserver {
// private static final String LOG_TAG = "Chatty:AccountsManager";
private HashMap<String, Account> accounts = new HashMap<String, Account>();
private ContentResolver contentResolver;
private ArrayList<MessagesListener> messagesListeners = new ArrayList<MessagesListener>();
private ArrayList<ContactListListener> contactListListeners = new ArrayList<ContactListListener>();
private ConnectionListener connectionListener;
public AccountsManager(ContentResolver contentResolver) {
super(new Handler());
this.contentResolver = contentResolver;
contentResolver.registerContentObserver(Accounts.CONTENT_URI, true,
this);
updateFromBD();
}
@Override
public void onChange(boolean selfChange) {
updateFromBD();
}
/**
*
*/
private void updateFromBD() {
Cursor accountsCursor = contentResolver.query(Accounts.CONTENT_URI,
null, null, null, null);
HashMap<String, Account> tmp = new HashMap<String, Account>();
synchronized (this) {
if (accountsCursor.moveToFirst()) {
do {
String login = accountsCursor.getString(accountsCursor
.getColumnIndex(Accounts.LOGIN));
Account acc = accounts.get(login);
if (acc == null) {
acc = AccountsFactory.createAccount(accountsCursor);
for (MessagesListener listener : messagesListeners)
acc.registerMessagesListener(listener);
for (ContactListListener listener : contactListListeners)
acc.registerContactListListener(listener);
acc.setConnectionListener(connectionListener);
}
tmp.put(login, acc);
} while (accountsCursor.moveToNext());
}
accounts = tmp;
}
accountsCursor.close();
}
public synchronized Account getAccount(String accountId) {
return accounts.get(accountId);
}
public Account getAccount(Cursor account) {
String accountId = account.getString(account
.getColumnIndex(Accounts.LOGIN));
return getAccount(accountId);
}
public Account getAccount(long accountId) {
Cursor c = contentResolver.query(Accounts.CONTENT_URI, null,
Accounts._ID + " = " + accountId, null, null);
Account result = c.moveToFirst() ? getAccount(c) : null;
c.close();
return result;
}
public synchronized void connectAll() {
for (Account a : accounts.values())
a.connect();
}
public synchronized void disconnectAll() {
for (Account a : accounts.values()) {
a.disconnect();
}
}
public synchronized void registerMessagesListener(MessagesListener listener) {
if (listener != null) {
messagesListeners.add(listener);
for (Account a : accounts.values())
a.registerMessagesListener(listener);
}
}
public synchronized void unregisterMessagesListener(
MessagesListener listener) {
messagesListeners.remove(listener);
for (Account a : accounts.values())
a.unregisterMessagesListener(listener);
}
public synchronized void registerContactListListener(
ContactListListener listener) {
if (listener != null) {
contactListListeners.add(listener);
for (Account a : accounts.values())
a.registerContactListListener(listener);
}
}
public synchronized void unregisterContactListListener(
ContactListListener listener) {
contactListListeners.remove(listener);
for (Account a : accounts.values())
a.unregisterContactListListener(listener);
}
public synchronized void setConnectionListener(ConnectionListener listener) {
connectionListener = listener;
for (Account a : accounts.values())
a.setConnectionListener(connectionListener);
}
}
|