k9mail/k-9 — pull request #193 by zjw (2012-10-12 23:50:28+00:00)
Title: Restore saved TCP port setting

This bug occurs with Android 4.1 (Jelly Bean). Saved TCP port values are no longer being restored when entering the server setup screens. To reproduce:

Go into Settings -> Account Settings -> Fetching mail -> Incoming server. Change the port to a non-standard number (e.g., 1234). Click Next. You will likely get a 'cannot connect' error -- ignore it. Click 'continue'. The port number has now been saved. Go back into 'Incoming server' again. Prior to Jelly Bean, you would see the saved port number (1234). With Jelly Bean, the port number has been reset to a default value.

The cause:

Before this patch, the code in AccountSetupIncoming.onCreate() did the following:

1) Create mSecurityTypeView, a spinner widget for selecting the server security protocol.

2) Set an onItemSelected() listener for mSecurityTypeView so that whenever a spinner option value is selected, a call is made to updatePortFromSecurityType() (which sets a default port value based on the selected security protocol).

3) Initialize the mSecurityTypeView spinner with the saved security protocol option value for the account.

4) Initialize mPortView (a text widget) with the saved TCP port setting for the account.

On Android versions prior to Jelly Bean, the execution of step (3) immediately resulted in triggering the onItemSelected() listener, resulting in an immediate call to updatePortFromSecurityType() which initialized mPortView with a default value. However, this default initialization was irrelevant, because mPortView was subsequently reinitialized in step (4).

With Jelly Bean, step (3) still results in triggering the onItemSelected() listener. However, the subsequent call to updatePortFromSecurityType() is no longer immediate; instead, the call is posted to the UI message queue to be invoked later -- i.e., the call to updatePortFromSecurityType() is delayed until after step (4), so the saved port value ends up being wiped out and replaced with a default value in the UI.

The problem occurs for both the incoming and outgoing server settings.

This patch moves step (4) from inside of onCreate() to inside of updatePortFromSecurityType().

Tested on Gingerbread, ICS, and Jelly Bean.