Totally *Unofficial* Android GTalk Client (Send/Receive XMPP Messages)

Here is the screen shot

001

Functionality

  • Enter the recipient’s name in the edit box on top and press “setup” button
  • Enter the message text in the edit box on the bottom and press “send” button to send the message to the recipient

Source

package org.apache.gtalk;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.database.Cursor;
import android.os.Bundle;
import android.os.DeadObjectException;
import android.os.IBinder;
import android.provider.Im;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.*;
import com.google.android.xmppService.IXmppService;
import com.google.android.xmppService.IXmppSession;
import com.google.android.xmppService.Presence;

public class GTalkClient extends Activity implements View.OnClickListener {
    private static final String LOG_TAG = "GTalkClient";

    IXmppSession mXmppSession = null;
    EditText mSendText;
    ListView mListMessages;
    EditText mRecipient;
    Button mSend;
    Button mSetup;

    /**
     * Called with the activity is first created.
     */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);

        // gather the troops
        mSendText = (EditText) findViewById(R.id.sendText);
        mListMessages = (ListView) findViewById(R.id.listMessages);
        mRecipient = (EditText) findViewById(R.id.recipient);
        mSend = (Button) findViewById(R.id.send);
        mSetup = (Button) findViewById(R.id.setup);

        // set up handler for on click
        mSetup.setOnClickListener(this);
        mSend.setOnClickListener(this);

        bindService((new Intent()).setComponent(
                com.google.android.xmppService.XmppConstants.XMPP_SERVICE_COMPONENT),
                null, mConnection, 0);
    }

    /**
     * Let the user know there was an issue
     *
     * @param msg
     */
    private void logMessage(CharSequence msg) {
        NotificationManager nm = (NotificationManager) getSystemService(
                Context.NOTIFICATION_SERVICE);

        nm.notifyWithText(123, msg, NotificationManager.LENGTH_LONG, null);
    }

    /**
     * Setup the XMPP Session using a service connection
     */
    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            // This is called when the connection with the XmppService has been
            // established, giving us the service object we can use to
            // interact with the service.  We are communicating with our
            // service through an IDL interface, so get a client-side
            // representation of that from the raw service object.
            IXmppService xmppService = IXmppService.Stub.asInterface(service);

            try {
                mXmppSession = xmppService.getDefaultSession();
                if (mXmppSession == null) {
                    // this should not happen.
                    logMessage(getText(R.string.xmpp_session_not_found));
                    return;
                }
                mXmppSession.setPresence(new Presence(Im.PresenceColumns.AVAILABLE, "Am here now!"));
            } catch (DeadObjectException ex) {
                Log.e(LOG_TAG, "caught " + ex);
                logMessage(getText(R.string.found_stale_xmpp_service));
            }

            mSendText.setEnabled(true);
        }

        public void onServiceDisconnected(ComponentName componentName) {
            // This is called when the connection with the service has been
            // unexpectedly disconnected -- that is, its process crashed.
            mXmppSession = null;
            mSendText.setEnabled(false);
        }
    };

    /**
     * Handle clicks on the 2 buttions
     *
     * @param view
     */
    public void onClick(View view) {
        if (view == mSetup) {
            Log.i(LOG_TAG, "onClick - Setup");
            // Run a query against CONTENT_URI = "content://im/messages"
            Cursor cursor = managedQuery(Im.Messages.CONTENT_URI, null,
                    "contact=\'" + mRecipient.getText().toString() + "\'", null, null);

            // Display the cursor results in a simple list
            // Note that the adapter is dyamic (picks up new entries automatically)
            ListAdapter adapter = new SimpleCursorAdapter(this,
                    android.R.layout.simple_list_item_1,
                    cursor, // Give the cursor to the list adatper
                    new String[]{Im.MessagesColumns.BODY},
                    new int[]{android.R.id.text1});

            this.mListMessages.setAdapter(adapter);
        } else if (view == mSend) {
            // use XmppService to send data message to someone
            String username = mRecipient.getText().toString();
            if (!isValidUsername(username)) {
                logMessage(getText(R.string.invalid_username));
                return;
            }

            if (mXmppSession == null) {
                logMessage(getText(R.string.xmpp_service_not_connected));
                return;
            }

            try {
                mXmppSession.sendTextMessage(username, 0, mSendText.getText().toString());
            } catch (DeadObjectException ex) {
                Log.e(LOG_TAG, “caught ” + ex);
                logMessage(getText(R.string.found_stale_xmpp_service));
                mXmppSession = null;
            }
        }
    }

    private boolean isValidUsername(String username) {
        return !TextUtils.isEmpty(username) && username.indexOf(’@') != -1;
    }
}

Download the sources and Application - GTalkClient.zip


About this entry