-
-
Save anonymous/e3170f5f1c5918dc65b6 to your computer and use it in GitHub Desktop.
SettingsDialog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.apache.android.xmpp; | |
import android.app.Dialog; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import org.jivesoftware.smack.ConnectionConfiguration; | |
import org.jivesoftware.smack.XMPPConnection; | |
import org.jivesoftware.smack.XMPPException; | |
import org.jivesoftware.smack.packet.Presence; | |
/** | |
* Gather the xmpp settings and create an XMPPConnection | |
*/ | |
public class SettingsDialog extends Dialog implements android.view.View.OnClickListener { | |
private XMPPClient xmppClient; | |
public SettingsDialog(XMPPClient xmppClient) { | |
super(xmppClient); | |
this.xmppClient = xmppClient; | |
} | |
protected void onStart() { | |
super.onStart(); | |
setContentView(R.layout.settings); | |
getWindow().setFlags(4, 4); | |
setTitle("XMPP Settings"); | |
Button ok = (Button) findViewById(R.id.ok); | |
ok.setOnClickListener(this); | |
} | |
public void onClick(View v) { | |
String host = getText(R.id.host); | |
String port = getText(R.id.port); | |
String service = getText(R.id.service); | |
String username = getText(R.id.userid); | |
String password = getText(R.id.password); | |
// Create a connection | |
ConnectionConfiguration connConfig = | |
new ConnectionConfiguration(host, Integer.parseInt(port), service); | |
XMPPConnection connection = new XMPPConnection(connConfig); | |
try { | |
connection.connect(); | |
Log.i("XMPPClient", "[SettingsDialog] Connected to " + connection.getHost()); | |
} catch (XMPPException ex) { | |
Log.e("XMPPClient", "[SettingsDialog] Failed to connect to " + connection.getHost()); | |
Log.e("XMPPClient", ex.toString()); | |
xmppClient.setConnection(null); | |
} | |
try { | |
connection.login(username, password); | |
Log.i("XMPPClient", "Logged in as " + connection.getUser()); | |
// Set the status to available | |
Presence presence = new Presence(Presence.Type.available); | |
connection.sendPacket(presence); | |
xmppClient.setConnection(connection); | |
} catch (XMPPException ex) { | |
Log.e("XMPPClient", "[SettingsDialog] Failed to log in as " + username); | |
Log.e("XMPPClient", ex.toString()); | |
xmppClient.setConnection(null); | |
} | |
dismiss(); | |
} | |
private String getText(int id) { | |
EditText widget = (EditText) this.findViewById(id); | |
return widget.getText().toString(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.apache.android.xmpp; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.os.Handler; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.ArrayAdapter; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.ListView; | |
import org.jivesoftware.smack.PacketListener; | |
import org.jivesoftware.smack.XMPPConnection; | |
import org.jivesoftware.smack.filter.MessageTypeFilter; | |
import org.jivesoftware.smack.filter.PacketFilter; | |
import org.jivesoftware.smack.packet.Message; | |
import org.jivesoftware.smack.packet.Packet; | |
import org.jivesoftware.smack.util.StringUtils; | |
import java.util.ArrayList; | |
public class XMPPClient extends Activity { | |
private ArrayList<String> messages = new ArrayList(); | |
private Handler mHandler = new Handler(); | |
private SettingsDialog mDialog; | |
private EditText mRecipient; | |
private EditText mSendText; | |
private ListView mList; | |
private XMPPConnection connection; | |
/** | |
* Called with the activity is first created. | |
*/ | |
@Override | |
public void onCreate(Bundle icicle) { | |
super.onCreate(icicle); | |
Log.i("XMPPClient", "onCreate called"); | |
setContentView(R.layout.main); | |
mRecipient = (EditText) this.findViewById(R.id.recipient); | |
Log.i("XMPPClient", "mRecipient = " + mRecipient); | |
mSendText = (EditText) this.findViewById(R.id.sendText); | |
Log.i("XMPPClient", "mSendText = " + mSendText); | |
mList = (ListView) this.findViewById(R.id.listMessages); | |
Log.i("XMPPClient", "mList = " + mList); | |
setListAdapter(); | |
// Dialog for getting the xmpp settings | |
mDialog = new SettingsDialog(this); | |
// Set a listener to show the settings dialog | |
Button setup = (Button) this.findViewById(R.id.setup); | |
setup.setOnClickListener(new View.OnClickListener() { | |
public void onClick(View view) { | |
mHandler.post(new Runnable() { | |
public void run() { | |
mDialog.show(); | |
} | |
}); | |
} | |
}); | |
// Set a listener to send a chat text message | |
Button send = (Button) this.findViewById(R.id.send); | |
send.setOnClickListener(new View.OnClickListener() { | |
public void onClick(View view) { | |
String to = mRecipient.getText().toString(); | |
String text = mSendText.getText().toString(); | |
Log.i("XMPPClient", "Sending text [" + text + "] to [" + to + "]"); | |
Message msg = new Message(to, Message.Type.chat); | |
msg.setBody(text); | |
connection.sendPacket(msg); | |
messages.add(connection.getUser() + ":"); | |
messages.add(text); | |
setListAdapter(); | |
} | |
}); | |
} | |
/** | |
* Called by Settings dialog when a connection is establised with the XMPP server | |
* | |
* @param connection | |
*/ | |
public void setConnection | |
(XMPPConnection | |
connection) { | |
this.connection = connection; | |
if (connection != null) { | |
// Add a packet listener to get messages sent to us | |
PacketFilter filter = new MessageTypeFilter(Message.Type.chat); | |
connection.addPacketListener(new PacketListener() { | |
public void processPacket(Packet packet) { | |
Message message = (Message) packet; | |
if (message.getBody() != null) { | |
String fromName = StringUtils.parseBareAddress(message.getFrom()); | |
Log.i("XMPPClient", "Got text [" + message.getBody() + "] from [" + fromName + "]"); | |
messages.add(fromName + ":"); | |
messages.add(message.getBody()); | |
// Add the incoming message to the list view | |
mHandler.post(new Runnable() { | |
public void run() { | |
setListAdapter(); | |
} | |
}); | |
} | |
} | |
}, filter); | |
} | |
} | |
private void setListAdapter | |
() { | |
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, | |
R.layout.multi_line_list_item, | |
messages); | |
mList.setAdapter(adapter); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.apache.android.xmpp; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.os.Handler; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.ArrayAdapter; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.ListView; | |
import org.jivesoftware.smack.PacketListener; | |
import org.jivesoftware.smack.XMPPConnection; | |
import org.jivesoftware.smack.filter.MessageTypeFilter; | |
import org.jivesoftware.smack.filter.PacketFilter; | |
import org.jivesoftware.smack.packet.Message; | |
import org.jivesoftware.smack.packet.Packet; | |
import org.jivesoftware.smack.util.StringUtils; | |
import java.util.ArrayList; | |
public class XMPPClient extends Activity { | |
private ArrayList<String> messages = new ArrayList(); | |
private Handler mHandler = new Handler(); | |
private SettingsDialog mDialog; | |
private EditText mRecipient; | |
private EditText mSendText; | |
private ListView mList; | |
private XMPPConnection connection; | |
/** | |
* Called with the activity is first created. | |
*/ | |
@Override | |
public void onCreate(Bundle icicle) { | |
super.onCreate(icicle); | |
Log.i("XMPPClient", "onCreate called"); | |
setContentView(R.layout.main); | |
mRecipient = (EditText) this.findViewById(R.id.recipient); | |
Log.i("XMPPClient", "mRecipient = " + mRecipient); | |
mSendText = (EditText) this.findViewById(R.id.sendText); | |
Log.i("XMPPClient", "mSendText = " + mSendText); | |
mList = (ListView) this.findViewById(R.id.listMessages); | |
Log.i("XMPPClient", "mList = " + mList); | |
setListAdapter(); | |
// Dialog for getting the xmpp settings | |
mDialog = new SettingsDialog(this); | |
// Set a listener to show the settings dialog | |
Button setup = (Button) this.findViewById(R.id.setup); | |
setup.setOnClickListener(new View.OnClickListener() { | |
public void onClick(View view) { | |
mHandler.post(new Runnable() { | |
public void run() { | |
mDialog.show(); | |
} | |
}); | |
} | |
}); | |
// Set a listener to send a chat text message | |
Button send = (Button) this.findViewById(R.id.send); | |
send.setOnClickListener(new View.OnClickListener() { | |
public void onClick(View view) { | |
String to = mRecipient.getText().toString(); | |
String text = mSendText.getText().toString(); | |
Log.i("XMPPClient", "Sending text [" + text + "] to [" + to + "]"); | |
Message msg = new Message(to, Message.Type.chat); | |
msg.setBody(text); | |
connection.sendPacket(msg); | |
messages.add(connection.getUser() + ":"); | |
messages.add(text); | |
setListAdapter(); | |
} | |
}); | |
} | |
/** | |
* Called by Settings dialog when a connection is establised with the XMPP server | |
* | |
* @param connection | |
*/ | |
public void setConnection | |
(XMPPConnection | |
connection) { | |
this.connection = connection; | |
if (connection != null) { | |
// Add a packet listener to get messages sent to us | |
PacketFilter filter = new MessageTypeFilter(Message.Type.chat); | |
connection.addPacketListener(new PacketListener() { | |
public void processPacket(Packet packet) { | |
Message message = (Message) packet; | |
if (message.getBody() != null) { | |
String fromName = StringUtils.parseBareAddress(message.getFrom()); | |
Log.i("XMPPClient", "Got text [" + message.getBody() + "] from [" + fromName + "]"); | |
messages.add(fromName + ":"); | |
messages.add(message.getBody()); | |
// Add the incoming message to the list view | |
mHandler.post(new Runnable() { | |
public void run() { | |
setListAdapter(); | |
} | |
}); | |
} | |
} | |
}, filter); | |
} | |
} | |
private void setListAdapter | |
() { | |
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, | |
R.layout.multi_line_list_item, | |
messages); | |
mList.setAdapter(adapter); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment