Skip to content

Commit

Permalink
Make reconnections actually work
Browse files Browse the repository at this point in the history
At the moment, the reconnect feature is somewhat glitchy, popping up
multiple reconnect prompts even if a reconnection succeeds, and
occasionally causing crashes.  A successful reconnection results in the
conversation history being cleared, which is an annoying outcome when
connected over an unreliable network.

This patch does the following:

* Keep track of whether a reconnect dialog is active, to prevent
  multiple dialogs from opening.
* Introduce a new field to the Server object, mayReconnect, which is
  used to keep track of whether a reconnection should be attempted in
  the event of a disconnection.  It's set to "true" when we connect to a
  server, and "false" if the user asks for a disconnection.
* Prevent the clearing of active conversations and conversation history
  on disconnect, unless the user specifically asked for the disconnect.
* Keep the IRCService running even when no servers are connected, unless
  the user has disconnected from all servers herself.  This is needed
  for reliable auto-reconnects (see next patch), but has the side effect
  of keeping conversation history around even if the activity isn't open
  when a disconnect happens.
  • Loading branch information
steven676 authored and pocmo committed Jun 29, 2011
1 parent a69fafc commit e765131
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 14 deletions.
25 changes: 15 additions & 10 deletions application/src/org/yaaic/activity/ConversationActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ public class ConversationActivity extends Activity implements ServiceConnection,

private int historySize;

private boolean reconnectDialogActive = false;

OnKeyListener inputKeyListener = new OnKeyListener() {
/**
* On key pressed (input line)
Expand Down Expand Up @@ -392,6 +394,8 @@ public void onServiceConnected(ComponentName name, IBinder service)
if (server.getStatus() == Status.PRE_CONNECTING && getIntent().hasExtra("connect")) {
server.setStatus(Status.CONNECTING);
binder.connect(server);
} else {
onStatusUpdate();
}
}

Expand Down Expand Up @@ -441,6 +445,7 @@ public boolean onMenuItemSelected(int featureId, MenuItem item)
switch (item.getItemId()) {
case R.id.disconnect:
server.setStatus(Status.DISCONNECTED);
server.setMayReconnect(false);
binder.getService().getConnection(serverId).quitServer();
server.clearConversations();
setResult(RESULT_OK);
Expand Down Expand Up @@ -604,10 +609,6 @@ public void onStatusUpdate()
input.setEnabled(false);

if (server.getStatus() == Status.CONNECTING) {
deckAdapter.clearConversations();
Conversation serverInfo = server.getConversation(ServerInfo.DEFAULT_NAME);
serverInfo.setHistorySize(historySize);
deckAdapter.addItem(serverInfo);
return;
}

Expand All @@ -616,27 +617,31 @@ public void onStatusUpdate()
return;
}

if (!binder.getService().getSettings().isReconnectEnabled()) {
if (!binder.getService().getSettings().isReconnectEnabled() && !reconnectDialogActive) {
reconnectDialogActive = true;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(getResources().getString(R.string.reconnect_after_disconnect, server.getTitle()))
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
if (!server.isDisconnected()) {
reconnectDialogActive = false;
return;
}
binder.getService().getConnection(server.getId()).setAutojoinChannels(
server.getCurrentChannelNames()
);
server.clearConversations();
deckAdapter.clearConversations();
Conversation serverInfo = server.getConversation(ServerInfo.DEFAULT_NAME);
serverInfo.setHistorySize(historySize);
deckAdapter.addItem(serverInfo);
server.setStatus(Status.CONNECTING);
binder.connect(server);
reconnectDialogActive = false;
}
})
.setNegativeButton(getString(R.string.negative_button), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
server.setMayReconnect(false);
reconnectDialogActive = false;
dialog.cancel();
}
});
Expand Down
4 changes: 3 additions & 1 deletion application/src/org/yaaic/activity/ServersActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ protected void onListItemClick(ListView l, View v, int position, long id) {

Intent intent = new Intent(this, ConversationActivity.class);

if (server.getStatus() == Status.DISCONNECTED) {
if (server.getStatus() == Status.DISCONNECTED && !server.mayReconnect()) {
server.setStatus(Status.PRE_CONNECTING);
intent.putExtra("connect", true);
}
Expand Down Expand Up @@ -220,6 +220,7 @@ public void onClick(DialogInterface dialog, int item) {
case 1: // Disconnect
server.clearConversations();
server.setStatus(Status.DISCONNECTED);
server.setMayReconnect(false);
binder.getService().getConnection(server.getId()).quitServer();
break;
case 2: // Edit
Expand Down Expand Up @@ -292,6 +293,7 @@ public boolean onMenuItemSelected(int featureId, MenuItem item)
for (Server server : mServers) {
if (binder.getService().hasConnection(server.getId())) {
server.setStatus(Status.DISCONNECTED);
server.setMayReconnect(false);
binder.getService().getConnection(server.getId()).quitServer();
}
}
Expand Down
4 changes: 2 additions & 2 deletions application/src/org/yaaic/irc/IRCConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ public void setIdent(String ident)
public void onConnect()
{
server.setStatus(Status.CONNECTED);
server.setMayReconnect(true);

service.sendBroadcast(
Broadcast.createServerIntent(Broadcast.SERVER_UPDATE, server.getId())
Expand Down Expand Up @@ -372,7 +373,7 @@ protected void onInvite(String targetNick, String sourceNick, String sourceLogin
@Override
protected void onJoin(String target, String sender, String login, String hostname)
{
if (sender.equalsIgnoreCase(getNick())) {
if (sender.equalsIgnoreCase(getNick()) && server.getConversation(target) == null) {
// We joined a new channel
Conversation conversation = new Channel(target);
conversation.setHistorySize(service.getSettings().getHistorySize());
Expand Down Expand Up @@ -1132,7 +1133,6 @@ public void onDisconnect()
if (service.getSettings().isReconnectEnabled() && server.getStatus() != Status.DISCONNECTED) {
setAutojoinChannels(server.getCurrentChannelNames());

server.clearConversations();
server.setStatus(Status.CONNECTING);
service.connect(server);
} else {
Expand Down
2 changes: 1 addition & 1 deletion application/src/org/yaaic/irc/IRCService.java
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ public void checkServiceStatus()

for (int i = 0; i < mSize; i++) {
server = mServers.get(i);
if (server.isDisconnected()) {
if (server.isDisconnected() && !server.mayReconnect()) {
int serverId = server.getId();
synchronized(this) {
IRCConnection connection = connections.get(serverId);
Expand Down
17 changes: 17 additions & 0 deletions application/src/org/yaaic/model/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class Server
private int status = Status.DISCONNECTED;
private String selected = "";
private boolean isForeground = false;
private boolean mayReconnect = false;

/**
* Create a new server object
Expand Down Expand Up @@ -443,4 +444,20 @@ public void setIsForeground(boolean isForeground)
{
this.isForeground = isForeground;
}

/**
* Get whether a reconnect may be attempted if we're disconnected.
*/
public boolean mayReconnect()
{
return mayReconnect;
}

/**
* Set whether a reconnect may be attempted if we're disconnected.
*/
public void setMayReconnect(boolean mayReconnect)
{
this.mayReconnect = mayReconnect;
}
}

0 comments on commit e765131

Please sign in to comment.