Back to project page AsmackChat.
The source code is released under:
Apache License
If you think the Android project AsmackChat listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * Licensed under Apache License, Version 2.0 or LGPL 2.1, at your option. * --// w w w . ja v a2 s .com * * Copyright 2010 Rene Treffer * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * -- * * Copyright (C) 2010 Rene Treffer * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ package com.googlecode.asmack.chat; import java.io.IOException; import java.io.StringWriter; import java.util.ArrayList; import java.util.concurrent.atomic.AtomicInteger; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserFactory; import org.xmlpull.v1.XmlSerializer; import android.app.Activity; import android.app.NotificationManager; import android.content.ContentValues; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.net.Uri.Builder; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; import com.googlecode.asmack.Attribute; import com.googlecode.asmack.Stanza; import com.googlecode.asmack.connection.IXmppTransportService; /** * Main chat activity. */ public class Chat extends Activity implements OnClickListener { /** * Logging tag, ChatActivity. */ private static final String TAG = Chat.class.getSimpleName(); /** * ID string used as message stanza prefix (app + random). */ private static final String ID = TAG + "-" + Integer.toHexString((int)(Math.random() * 255.9999)); /** * Stanza unique id. */ private static final AtomicInteger atomicInt = new AtomicInteger(); /** * The text input field. */ private EditText input; /** * XMLPullParser factory to generate a parser for messages. */ private XmlPullParserFactory xmlPullParserFactory; /** * The remote jid of this chat. */ private String to; /** * The local account jid of this chat. */ private String from; /** * Full local user jid, if available. */ public String fullJid; /** * The system wide notification manager. */ private NotificationManager notificationManager; /** * Initialize the members of this activity and bind to the xmpp transport * service. * @param savedInstanceState The save state of this activity. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); notificationManager = (NotificationManager) getApplicationContext() .getSystemService(Context.NOTIFICATION_SERVICE); try { xmlPullParserFactory = XmlPullParserFactory.newInstance(); } catch (XmlPullParserException e) { Log.e(TAG, "Can't intatiate xmlPullParser"); finish(); return; } xmlPullParserFactory.setNamespaceAware(true); xmlPullParserFactory.setValidating(false); Intent serviceIntent = new Intent(IXmppTransportService.class.getCanonicalName()); startService(serviceIntent); serviceIntent = new Intent(IChatService.class.getCanonicalName()); startService(serviceIntent); Intent intent = getIntent(); String toFrom = intent.getData().getPathSegments().get(0); Log.d("Chat", toFrom); String splitToFrom[] = toFrom.split("/"); to = splitToFrom[0]; from = splitToFrom[1]; setContentView(R.layout.main); TextView fromTextView = (TextView) findViewById(R.id.FromTextView); fromTextView.setText(from); TextView toTextView = (TextView) findViewById(R.id.ToTextView); toTextView.setText(to); Database.getDatabase(getApplicationContext(), null); ListView history = (ListView) findViewById(R.id.ChatHistoryList); history.setItemsCanFocus(false); history.setAdapter( new HistoryCursorAdapter( this, from, HistoryCursorAdapter.query(this, from, to), true ) ); Button send = (Button) findViewById(R.id.ChatButtonSend); send.setOnClickListener(this); input = (EditText) findViewById(R.id.ChatInput); input.setHint("Send " + XMPPUtils.getUser(to) + " a message"); input.requestFocus(); notificationManager.cancel(toFrom, 1); } /** * Clicked on button click, creating a new xmpp stanza and sending it * to the remote jid. * @param v The view catching the click, ignored. */ @Override public void onClick(View v) { if (input == null) { Log.d(TAG, "input is null"); return; } String msg = input.getEditableText().toString(); StringWriter xml = new StringWriter(); try { XmlSerializer serializer = xmlPullParserFactory.newSerializer(); serializer.setOutput(xml); serializer.startTag(null, "message"); serializer.startTag(null, "body"); serializer.text(msg); serializer.endTag(null, "body"); serializer.endTag(null, "message"); serializer.flush(); } catch (XmlPullParserException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } ArrayList<Attribute> attributes = new ArrayList<Attribute>(); attributes.add(new Attribute("type", "", "chat")); attributes.add(new Attribute("to", "", this.to)); attributes.add(new Attribute( "id", "", ID + "-" + Integer.toHexString(atomicInt.incrementAndGet())) ); Stanza stanza = new Stanza("message", "", from, xml.toString(), attributes); Intent intent = new Intent(); intent.setAction("com.googlecode.asmack.intent.XMPP.STANZA.SEND"); intent.putExtra("stanza", stanza); intent.addFlags(Intent.FLAG_FROM_BACKGROUND); getApplicationContext().sendBroadcast( intent, "com.googlecode.asmack.intent.XMPP.STANZA.SEND" ); ContentValues values = new ContentValues(); values.put("ts", System.currentTimeMillis()); values.put("via", from); values.put("jid", to); values.put("dst", to); values.put("src", from); values.put("msg", msg); Database.getDatabase(null, null).insert("msg", "_id", values); Builder builder = new Uri.Builder(); builder.scheme("content"); builder.authority("jabber-chat-db"); builder.appendPath(from); builder.appendPath(to); getApplicationContext() .getContentResolver() .notifyChange(builder.build(), null); input.setText(""); } }