/* ChatActivity.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.app;
import ru.chatty.db.ChattyDatabase.Contacts;
import ru.chatty.db.ChattyDatabase.History;
import ru.chatty.engine.Message;
import ru.chatty.im.Account;
import ru.chatty.im.Contact;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.text.Spannable;
import android.text.format.Time;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class ChatActivity extends Activity {
private static final String LOG_TAG = "Chatty:ChatActivity";
public static final String CONTACT_ID_EXTRA = "ContactID";
public static final String ACCOUNT_ID_EXTRA = "AccountID";
/** GUI */
private static final int ADD_HISTORY_ITEM = 0;
private EditText message;
private ArrayAdapter<Spannable> historyListAdapter;
private String contactId; //
private long contactDBId; //
private Account account;
private Contact contact;
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case ADD_HISTORY_ITEM:
historyListAdapter.insert(
ChatMessageFormatter.getFormattedText(
ChatActivity.this, (Message) msg.obj), 0);
break;
}
}
};
private final Contact.Listener contactListener = new Contact.Listener() {
@Override
public void onIncomingMessage(String message) {
contact.setMessagesIsReaded();
mHandler.sendMessage(mHandler.obtainMessage(ADD_HISTORY_ITEM,
new Message(message, null, true)));
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chat);
Intent intent = getIntent();
contactId = intent.getStringExtra(CONTACT_ID_EXTRA);
String accountId = intent.getStringExtra(ACCOUNT_ID_EXTRA);
/*
* contactCursor = managedQuery(Contacts.CONTENT_URI, null, Contacts._ID
* + " = " + contactDBId, null, null); if (!contactCursor.moveToFirst())
* { Log.e(LOG_TAG, "Can't find contact"); finish(); return; }
*/
// contactId = contactCursor.getString(contactCursor
// .getColumnIndex(Contacts.CONTACT_ID));
account = ChattyApplication.getApplication(this).getAccountsManager()
.getAccount(accountId);
if (account == null) {
Log.e(LOG_TAG, "Can't find account");
finish();
return;
}
contact = account.getContact(contactId);
if (contact == null) {
Log.e(LOG_TAG, "contact not found");
finish();
return;
}
Cursor c = managedQuery(Contacts.CONTENT_URI,
new String[] { Contacts._ID }, Contacts.CONTACT_ID + " = ?",
new String[] { contactId }, null);
if (!c.moveToFirst()) {
Log.e(LOG_TAG, "Contact not found in DB [contactId = " + contactId + "]");
finish();
return;
}
contactDBId = c.getLong(0);
contact.registerListener(contactListener);
contact.setMessagesIsReaded();
setTitle(contact.getNick());
initHistory();
message = (EditText) findViewById(R.id.InputMessage);
Button sendButton = (Button) findViewById(R.id.SendButton);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (account.isConnected()) {
String msg = message.getText().toString().trim();
if (msg.length() > 0) {
sendMessage(msg);
message.setText("");
}
}
}
});
}
@Override
public void onDestroy() {
super.onDestroy();
if (contact != null)
contact.unregisterListener(contactListener);
}
private void initHistory() {
historyListAdapter = new ArrayAdapter<Spannable>(this,
R.layout.chat_history_item);
Cursor c = managedQuery(
History.CONTENT_URI,
null,
History.CONTACT
+ " = "
+ contactDBId, null,
History.DATE + " DESC");
if (c.moveToFirst()) {
do {
String text = c.getString(c.getColumnIndex(History.MESSAGE));
short flags = c.getShort(c.getColumnIndex(History.FLAGS));
long millis = c.getLong(c.getColumnIndex(History.DATE));
Time time = new Time();
time.set(millis);
Message msg = new Message(text, time,
(flags & History.FLAG_RECEIVED_MESSAGE) != 0);
historyListAdapter.add(ChatMessageFormatter.getFormattedText(
ChatActivity.this, msg));
} while (c.moveToNext());
}
ListView history = (ListView) findViewById(R.id.History);
history.setAdapter(historyListAdapter);
}
void sendMessage(String message) {
contact.sendMessage(message);
ContentValues values = new ContentValues();
values.put(History.MESSAGE, message);
values.put(History.CONTACT, contactDBId);
getContentResolver().insert(History.CONTENT_URI, values);
Message msg = new Message(message, null, false);
mHandler.sendMessage(mHandler.obtainMessage(ADD_HISTORY_ITEM, msg));
}
}
|