# HG changeset patch # User nikita@localhost # Date 1245686849 -7200 # Node ID 0548b407992ac1a485131aa0e2391f5b4a92cacc # Parent e082fd525147a40c3a0bcc100e79fbb63cfd68a3# Parent 0ce1e306fdd378d2f20eaab2ba18e528cf8ec63d debut de la gestion de la PrivacyList diff -r e082fd525147 -r 0548b407992a src/com/beem/project/beem/service/XmppConnectionAdapter.java --- a/src/com/beem/project/beem/service/XmppConnectionAdapter.java Mon Jun 22 17:53:11 2009 +0200 +++ b/src/com/beem/project/beem/service/XmppConnectionAdapter.java Mon Jun 22 18:07:29 2009 +0200 @@ -5,6 +5,7 @@ import org.jivesoftware.smack.ConnectionConfiguration; import org.jivesoftware.smack.ConnectionListener; +import org.jivesoftware.smack.PrivacyListManager; import org.jivesoftware.smack.Roster; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPException; @@ -24,14 +25,188 @@ /** * This class implements an adapter for XMPPConnection. - * * @author darisk */ public class XmppConnectionAdapter extends IXmppConnection.Stub { + private static final String TAG = "XMPPConnectionAdapter"; + private XMPPConnection mAdaptee; + private IChatManager mChatManager; + private String mLogin; + private String mPassword; + private RosterAdapter mRoster; + private PrivacyListAdapter mPrivacyList; + private BeemService mService; + private RemoteCallbackList mRemoteConnListeners = new RemoteCallbackList(); + + private ConnexionListenerAdapter mConListener = new ConnexionListenerAdapter(); + + /** + * Constructor. + * @param config Configuration to use in order to connect + * @param login login to use on connect + * @param password password to use on connect + */ + public XmppConnectionAdapter(final ConnectionConfiguration config, final String login, final String password, + BeemService service) { + this(new XMPPConnection(config), login, password, service); + } + + /** + * Constructor. + * @param serviceName name of the service to connect to + * @param login login to use on connect + * @param password password to use on connect + */ + public XmppConnectionAdapter(final String serviceName, final String login, final String password, + BeemService service) { + this(new XMPPConnection(serviceName), login, password, service); + } + + /** + * Constructor. + * @param con The connection to adapt + * @param login The login to use + * @param password The password to use + */ + public XmppConnectionAdapter(final XMPPConnection con, final String login, final String password, + BeemService service) { + mAdaptee = con; + mLogin = login; + mPassword = password; + mService = service; + } + + /** + * {@inheritDoc} + */ + @Override + public void addConnectionListener(IBeemConnectionListener listen) throws RemoteException { + if (listen != null) + mRemoteConnListeners.register(listen); + } + + /** + * {@inheritDoc} + */ + @Override + public final void connectAsync() throws RemoteException { + Thread t = new Thread(new Runnable() { + + @Override + public void run() { + try { + connectSync(); + } catch (RemoteException e) { + Log.e(TAG, "Error while connecting", e); + } + } + }); + t.start(); + } + + /** + * {@inheritDoc} + */ + @Override + public boolean connectSync() throws RemoteException { + try { + mAdaptee.connect(); + mAdaptee.addConnectionListener(mConListener); + mAdaptee.login(mLogin, mPassword, "BEEM"); + + mChatManager = new BeemChatManager(mAdaptee.getChatManager(), mService); + // TODO find why this cause a null pointer exception + this.initFeatures(); // pour declarer les features xmpp qu'on supporte + mPrivacyList = new PrivacyListAdapter(PrivacyListManager.getInstanceFor(mAdaptee)); + ChatStateManager.getInstance(mAdaptee); + triggerAsynchronousConnectEvent(); + return true; + } catch (XMPPException e) { + Log.e(TAG, "Error while connecting", e); + mConListener.connectionFailed("Error On Connection"); + } + return false; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean disconnect() { + if (mAdaptee != null && mAdaptee.isConnected()) + mAdaptee.disconnect(); + return true; + } + + public XMPPConnection getAdaptee() { + return mAdaptee; + } + + /** + * {@inheritDoc} + */ + @Override + public IChatManager getChatManager() throws RemoteException { + return mChatManager; + } + + public BeemService getContext() { + return mService; + } + + /** + * {@inheritDoc} + */ + @Override + public IRoster getRoster() throws RemoteException { + if (mRoster != null) + return mRoster; + Roster adap = mAdaptee.getRoster(); + if (adap == null) + return null; + mRoster = new RosterAdapter(adap); + return mRoster; + } + + /** + * enregistre les features dispo dans notre version Liste de features que Telepathy supporte. + */ + private void initFeatures() { + ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(mAdaptee); + if (sdm == null) + sdm = new ServiceDiscoveryManager(mAdaptee); + sdm.addFeature("http://jabber.org/protocol/disco#info"); + JingleManager.setJingleServiceEnabled(); + // sdm.addFeature("http://jabber.org/protocol/nick"); + + } + + /** + * Returns true if currently authenticated by successfully calling the login method. + * @return true when successfully authenticated + */ + public boolean isAuthentificated() { + return mAdaptee.isAuthenticated(); + } + + /** + * {@inheritDoc} + */ + @Override + public void removeConnectionListener(IBeemConnectionListener listen) throws RemoteException { + if (listen != null) + mRemoteConnListeners.unregister(listen); + } + + /** + * Trigger Connection event. + */ + private void triggerAsynchronousConnectEvent() { + mConListener.onConnect(); + } /** * Listener for XMPP connection events. It will calls the remote listeners for connexion events. - * * @author darisk */ private class ConnexionListenerAdapter implements ConnectionListener { @@ -181,195 +356,4 @@ } } - private static final String TAG = "XMPPConnectionAdapter"; - private XMPPConnection mAdaptee; - private IChatManager mChatManager; - private String mLogin; - private String mPassword; - private RosterAdapter mRoster; - - private BeemService mService; - private RemoteCallbackList mRemoteConnListeners = new RemoteCallbackList(); - - private ConnexionListenerAdapter mConListener = new ConnexionListenerAdapter(); - - /** - * Constructor. - * - * @param config - * Configuration to use in order to connect - * @param login - * login to use on connect - * @param password - * password to use on connect - */ - public XmppConnectionAdapter(final ConnectionConfiguration config, final String login, final String password, - BeemService service) { - this(new XMPPConnection(config), login, password, service); - } - - /** - * Constructor. - * - * @param serviceName - * name of the service to connect to - * @param login - * login to use on connect - * @param password - * password to use on connect - */ - public XmppConnectionAdapter(final String serviceName, final String login, final String password, - BeemService service) { - this(new XMPPConnection(serviceName), login, password, service); - } - - /** - * Constructor. - * - * @param con - * The connection to adapt - * @param login - * The login to use - * @param password - * The password to use - */ - public XmppConnectionAdapter(final XMPPConnection con, final String login, final String password, - BeemService service) { - mAdaptee = con; - mLogin = login; - mPassword = password; - mService = service; - } - - /** - * {@inheritDoc} - */ - @Override - public void addConnectionListener(IBeemConnectionListener listen) throws RemoteException { - if (listen != null) - mRemoteConnListeners.register(listen); - } - - /** - * {@inheritDoc} - */ - @Override - public final void connectAsync() throws RemoteException { - Thread t = new Thread(new Runnable() { - - @Override - public void run() { - try { - connectSync(); - } catch (RemoteException e) { - Log.e(TAG, "Error while connecting", e); - } - } - }); - t.start(); - } - - /** - * {@inheritDoc} - */ - @Override - public boolean connectSync() throws RemoteException { - try { - mAdaptee.connect(); - mAdaptee.addConnectionListener(mConListener); - mAdaptee.login(mLogin, mPassword, "BEEM"); - - mChatManager = new BeemChatManager(mAdaptee.getChatManager(), mService); - // TODO find why this cause a null pointer exception - this.initFeatures(); // pour declarer les features xmpp qu'on supporte - ChatStateManager.getInstance(mAdaptee); - triggerAsynchronousConnectEvent(); - return true; - } catch (XMPPException e) { - Log.e(TAG, "Error while connecting", e); - if (e.getXMPPError() != null) - mConListener.connectionFailed(e.getXMPPError().getMessage()); // - else - mConListener.connectionFailed("Error On Connection"); - } - return false; - } - - /** - * {@inheritDoc} - */ - @Override - public boolean disconnect() { - mAdaptee.disconnect(); - return true; - } - - public XMPPConnection getAdaptee() { - return mAdaptee; - } - - /** - * {@inheritDoc} - */ - @Override - public IChatManager getChatManager() throws RemoteException { - return mChatManager; - } - - public BeemService getContext() { - return mService; - } - - /** - * {@inheritDoc} - */ - @Override - public IRoster getRoster() throws RemoteException { - if (mRoster != null) - return mRoster; - Roster adap = mAdaptee.getRoster(); - if (adap == null) - return null; - mRoster = new RosterAdapter(adap); - return mRoster; - } - - /** - * enregistre les features dispo dans notre version Liste de features que Telepathy supporte. - */ - private void initFeatures() { - ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(mAdaptee); - if (sdm == null) - sdm = new ServiceDiscoveryManager(mAdaptee); - sdm.addFeature("http://jabber.org/protocol/disco#info"); - JingleManager.setJingleServiceEnabled(); - // sdm.addFeature("http://jabber.org/protocol/nick"); - - } - - /** - * Returns true if currently authenticated by successfully calling the login method. - * - * @return true when successfully authenticated - */ - public boolean isAuthentificated() { - return mAdaptee.isAuthenticated(); - } - - /** - * {@inheritDoc} - */ - @Override - public void removeConnectionListener(IBeemConnectionListener listen) throws RemoteException { - if (listen != null) - mRemoteConnListeners.unregister(listen); - } - - /** - * Trigger Connection event. - */ - private void triggerAsynchronousConnectEvent() { - mConListener.onConnect(); - } - } diff -r e082fd525147 -r 0548b407992a src/com/beem/project/beem/ui/ContactList.java diff -r e082fd525147 -r 0548b407992a src/com/beem/project/beem/ui/EditSettings.java --- a/src/com/beem/project/beem/ui/EditSettings.java Mon Jun 22 17:53:11 2009 +0200 +++ b/src/com/beem/project/beem/ui/EditSettings.java Mon Jun 22 18:07:29 2009 +0200 @@ -25,490 +25,469 @@ /** * This class represents an activity which allows the user to change his account or proxy parameters - * * @author dasilvj */ public class EditSettings extends Activity { - private final static String LOG_MSG_SETTINGS_SAVED = "Settings saved successfully."; - private final static String LOG_MSG_XMPP_SETTINGS_REGISTERED = "XMPP Settings has been registered."; - private final static String LOG_MSG_ACCOUNT_SETTINGS_REGISTERED = "Account Settings has been registered."; - private final static String LOG_MSG_PROXY_SETTINGS_REGISTERED = "Proxy Settings has been registered."; + private final static String LOG_MSG_SETTINGS_SAVED = "Settings saved successfully."; + private final static String LOG_MSG_XMPP_SETTINGS_REGISTERED = "XMPP Settings has been registered."; + private final static String LOG_MSG_ACCOUNT_SETTINGS_REGISTERED = "Account Settings has been registered."; + private final static String LOG_MSG_PROXY_SETTINGS_REGISTERED = "Proxy Settings has been registered."; + + private final static boolean DEFAULT_BOOLEAN_VALUE = false; + private final static String DEFAULT_STRING_VALUE = ""; + private final static int DEFAULT_INT_VALUE = 0; + + private final static String DEFAULT_XMPP_PORT = "5222"; - private final static boolean DEFAULT_BOOLEAN_VALUE = false; - private final static String DEFAULT_STRING_VALUE = ""; - private final static int DEFAULT_INT_VALUE = 0; + private final static int NOTIFICATION_DURATION = Toast.LENGTH_SHORT; + + private SharedPreferences settings = null; - private final static String DEFAULT_XMPP_PORT = "5222"; + private EditText accUsernameField = null; + private EditText accPasswordField = null; + + private EditText xmppServerField = null; + private EditText xmppPortField = null; + private CheckBox xmppUseTLSCheckBox = null; - private final static int NOTIFICATION_DURATION = Toast.LENGTH_SHORT; - - private SharedPreferences settings = null; + private CheckBox proxyUseCheckBox = null; + private Spinner proxyTypeSpinner = null; + private EditText proxyServerField = null; + private EditText proxyPortField = null; + private EditText proxyUsernameField = null; + private EditText proxyPasswordField = null; - private EditText accUsernameField = null; - private EditText accPasswordField = null; + /** + * Add a labeled "Account" tab on the tabbed window view passed by parameter + * @param tHost a tabbed window view + */ + private void addAccountTab(TabHost tHost) { + TabHost.TabSpec accountTab = tHost.newTabSpec(getString(R.string.settings_tab_tag_account)); + accountTab.setIndicator(getString(R.string.settings_tab_label_account)); + accountTab.setContent(R.id.settings_account); + tHost.addTab(accountTab); + } - private EditText xmppServerField = null; - private EditText xmppPortField = null; - private CheckBox xmppUseTLSCheckBox = null; + /** + * Add a labeled "Proxy" tab on the tabbed window view passed by parameter + * @param tHost a tabbed window view + */ + private void addProxyTab(TabHost tHost) { + TabHost.TabSpec proxyTab = tHost.newTabSpec(getString(R.string.settings_tab_tag_proxy)); + proxyTab.setIndicator(getString(R.string.settings_tab_label_proxy)); + proxyTab.setContent(R.id.settings_proxy); + tHost.addTab(proxyTab); + } - private CheckBox proxyUseCheckBox = null; - private Spinner proxyTypeSpinner = null; - private EditText proxyServerField = null; - private EditText proxyPortField = null; - private EditText proxyUsernameField = null; - private EditText proxyPasswordField = null; + /** + * Add a labeled "XMPP" tab on the tabbed window view passed by parameter + * @param tHost a tabbed window view + */ + private void addXMPPTab(TabHost tHost) { + TabHost.TabSpec personalTab = tHost.newTabSpec(getString(R.string.settings_tab_tag_xmpp)); + personalTab.setIndicator(getString(R.string.settings_tab_label_xmpp)); + personalTab.setContent(R.id.settings_xmpp); + tHost.addTab(personalTab); + } + + /** + * Disable proxy parameters fields + */ + private void disableProxyParameters() { + proxyTypeSpinner.setEnabled(false); + proxyServerField.setEnabled(false); + proxyPortField.setEnabled(false); + proxyUsernameField.setEnabled(false); + proxyPasswordField.setEnabled(false); + } + + private void displayNotification(CharSequence msg) { + Toast toast = Toast.makeText(getApplicationContext(), msg, NOTIFICATION_DURATION); + toast.show(); + } - /** - * Add a labeled "Account" tab on the tabbed window view passed by parameter - * - * @param tHost - * a tabbed window view - */ - private void addAccountTab(TabHost tHost) { - TabHost.TabSpec accountTab = tHost.newTabSpec(getString(R.string.settings_tab_tag_account)); - accountTab.setIndicator(getString(R.string.settings_tab_label_account)); - accountTab.setContent(R.id.settings_account); - tHost.addTab(accountTab); - } + /** + * Enable proxy parameters fields + */ + private void enableProxyParameters() { + proxyTypeSpinner.setEnabled(true); + proxyServerField.setEnabled(true); + proxyPortField.setEnabled(true); + proxyUsernameField.setEnabled(true); + proxyPasswordField.setEnabled(true); + } - /** - * Add a labeled "Proxy" tab on the tabbed window view passed by parameter - * - * @param tHost - * a tabbed window view - */ - private void addProxyTab(TabHost tHost) { - TabHost.TabSpec proxyTab = tHost.newTabSpec(getString(R.string.settings_tab_tag_proxy)); - proxyTab.setIndicator(getString(R.string.settings_tab_label_proxy)); - proxyTab.setContent(R.id.settings_proxy); - tHost.addTab(proxyTab); - } + /** + * Retrieve the value of a CheckBox + * @param viewId + * @return true if the CheckBox is checked, else false + */ + private boolean getCheckBoxValue(int viewId) { + final CheckBox checkBox = (CheckBox) findViewById(viewId); + if (checkBox.isChecked()) + return (true); + return (false); + } + + /** + * Retrieve account password from the preferences + * @return Registered account password + */ + private String getRegisteredAccountPassword() { + return (settings.getString(getString(R.string.settings_key_account_password), DEFAULT_STRING_VALUE)); + } - /** - * Add a labeled "XMPP" tab on the tabbed window view passed by parameter - * - * @param tHost - * a tabbed window view - */ - private void addXMPPTab(TabHost tHost) { - TabHost.TabSpec personalTab = tHost.newTabSpec(getString(R.string.settings_tab_tag_xmpp)); - personalTab.setIndicator(getString(R.string.settings_tab_label_xmpp)); - personalTab.setContent(R.id.settings_xmpp); - tHost.addTab(personalTab); - } + /** + * Retrieve account username from the preferences + * @return Registered account username + */ + private String getRegisteredAccountUsername() { + return (settings.getString(getString(R.string.settings_key_account_username), DEFAULT_STRING_VALUE)); + } + + /** + * Retrieve proxy password from the preferences + * @return Registered proxy password + */ + private String getRegisteredProxyPassword() { + return (settings.getString(getString(R.string.settings_key_proxy_password), DEFAULT_STRING_VALUE)); + } + + /** + * Retrieve proxy port from the preferences + * @return Registered proxy port + */ + private String getRegisteredProxyPort() { + return (settings.getString(getString(R.string.settings_key_proxy_port), DEFAULT_STRING_VALUE)); + } - /** - * Disable proxy parameters fields - */ - private void disableProxyParameters() { - proxyTypeSpinner.setEnabled(false); - proxyServerField.setEnabled(false); - proxyPortField.setEnabled(false); - proxyUsernameField.setEnabled(false); - proxyPasswordField.setEnabled(false); - } + /** + * Retrieve proxy server from the preferences + * @return Registered proxy server + */ + private String getRegisteredProxyServer() { + return (settings.getString(getString(R.string.settings_key_proxy_server), DEFAULT_STRING_VALUE)); + } + + /** + * Retrieve proxy type from the preferences + * @return Registered proxy type + */ + private int getRegisteredProxyType() { + return (settings.getInt(getString(R.string.settings_key_proxy_type), DEFAULT_INT_VALUE)); + } - private void displayNotification(CharSequence msg) { - Toast toast = Toast.makeText(getApplicationContext(), msg, NOTIFICATION_DURATION); - toast.show(); - } + /** + * Retrieve proxy use from the preferences + * @return Registered proxy use + */ + private boolean getRegisteredProxyUse() { + return (settings.getBoolean(getString(R.string.settings_key_proxy_use), DEFAULT_BOOLEAN_VALUE)); + } + + /** + * Retrieve proxy username from the preferences + * @return Registered proxy username + */ + private String getRegisteredProxyUsername() { + return (settings.getString(getString(R.string.settings_key_proxy_username), DEFAULT_STRING_VALUE)); + } - /** - * Enable proxy parameters fields - */ - private void enableProxyParameters() { - proxyTypeSpinner.setEnabled(true); - proxyServerField.setEnabled(true); - proxyPortField.setEnabled(true); - proxyUsernameField.setEnabled(true); - proxyPasswordField.setEnabled(true); - } + /** + * Retrieve xmpp port from the preferences + * @return Registered xmpp port + */ + private String getRegisteredXMPPPort() { + return (settings.getString(getString(R.string.settings_key_xmpp_port), DEFAULT_XMPP_PORT)); + } + + /** + * Retrieve xmpp server from the preferences + * @return Registered xmpp server + */ + private String getRegisteredXMPPServer() { + return (settings.getString(getString(R.string.settings_key_xmpp_server), DEFAULT_STRING_VALUE)); + } + + /** + * Retrieve TLS use from the preferences + * @return Registered TLS use + */ + private boolean getRegisteredXMPPTLSUse() { + return (settings.getBoolean(getString(R.string.settings_key_xmpp_tls_use), DEFAULT_BOOLEAN_VALUE)); + } - /** - * Retrieve the value of a CheckBox - * - * @param viewId - * @return true if the CheckBox is checked, else false - */ - private boolean getCheckBoxValue(int viewId) { - final CheckBox checkBox = (CheckBox) findViewById(viewId); - if (checkBox.isChecked()) - return (true); - return (false); - } + /** + * Initialize Account tab fields + */ + private void initAccountFields() { + accUsernameField = (EditText) findViewById(R.id.settings_account_username); + accPasswordField = (EditText) findViewById(R.id.settings_account_password); + } + + /** + * Initialize all of the components displayed in tabs (EditText fields, CheckBoxes, Spinners...) + */ + private void initFields() { + initAccountFields(); + initXMPPFields(); + initProxyFields(); + } - /** - * Retrieve account password from the preferences - * - * @return Registered account password - */ - private String getRegisteredAccountPassword() { - return (settings.getString(getString(R.string.settings_key_account_password), DEFAULT_STRING_VALUE)); - } + /** + * Initialize Proxy tab fields + */ + private void initProxyFields() { + proxyUseCheckBox = (CheckBox) findViewById(R.id.settings_proxy_use); + proxyTypeSpinner = (Spinner) findViewById(R.id.settings_proxy_type); + proxyServerField = (EditText) findViewById(R.id.settings_proxy_server); + proxyPortField = (EditText) findViewById(R.id.settings_proxy_port); + proxyUsernameField = (EditText) findViewById(R.id.settings_proxy_username); + proxyPasswordField = (EditText) findViewById(R.id.settings_proxy_password); + } + + /** + * Initialize proxy's types spinner of the proxy tab + */ + private void initProxyTypesSpinner() { + ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.proxy_types, + android.R.layout.simple_spinner_item); + adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + proxyTypeSpinner.setAdapter(adapter); + } - /** - * Retrieve account username from the preferences - * - * @return Registered account username - */ - private String getRegisteredAccountUsername() { - return (settings.getString(getString(R.string.settings_key_account_username), DEFAULT_STRING_VALUE)); - } + /** + * Initialize the checkbox allowing user to use a proxy + */ + private void initProxyUseCheckBox() { + proxyUseCheckBox.setOnClickListener(new OnClickListener() { + + public void onClick(View v) { + if (proxyUseCheckBox.isChecked()) { + enableProxyParameters(); + } else { + disableProxyParameters(); + } + } + }); + } + + /** + * Initialize "save" buttons allowing user to save settings + */ + private void initSaveButtons() { + final ArrayList views = new ArrayList(); + Button saveButton = null; - /** - * Retrieve proxy password from the preferences - * - * @return Registered proxy password - */ - private String getRegisteredProxyPassword() { - return (settings.getString(getString(R.string.settings_key_proxy_password), DEFAULT_STRING_VALUE)); - } + views.add(R.id.settings_account_button_save); + views.add(R.id.settings_proxy_button_save); + views.add(R.id.settings_xmpp_button_save); + + for (int i = 0; i < views.size(); i++) { + saveButton = (Button) findViewById(views.get(i)); + saveButton.setFocusable(true); + saveButton.setOnClickListener(new View.OnClickListener() { - /** - * Retrieve proxy port from the preferences - * - * @return Registered proxy port - */ - private String getRegisteredProxyPort() { - return (settings.getString(getString(R.string.settings_key_proxy_port), DEFAULT_STRING_VALUE)); + public void onClick(View v) { + saveSettings(); + } + }); } + } + + /** + * Initialize tabbed window view by adding tabs and setting the default tab + */ + private void initTabbedWindow() { + TabHost tHost = (TabHost) this.findViewById(R.id.settings_tab_host); + tHost.setup(); + addAccountTab(tHost); + addXMPPTab(tHost); + addProxyTab(tHost); + tHost.setCurrentTab(0); + } - /** - * Retrieve proxy server from the preferences - * - * @return Registered proxy server - */ - private String getRegisteredProxyServer() { - return (settings.getString(getString(R.string.settings_key_proxy_server), DEFAULT_STRING_VALUE)); - } + /** + * Initialize XMPP tab fields + */ + private void initXMPPFields() { + xmppServerField = (EditText) findViewById(R.id.settings_xmpp_server); + xmppPortField = (EditText) findViewById(R.id.settings_xmpp_port); + xmppUseTLSCheckBox = (CheckBox) findViewById(R.id.settings_xmpp_use_tls); + } + + /** + * {@inheritDoc} + */ + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.edit_settings); + initTabbedWindow(); + initFields(); + settings = getSharedPreferences(getString(R.string.settings_filename), MODE_PRIVATE); + } + + /** + * {@inheritDoc} + */ + @Override + public boolean onCreateOptionsMenu(Menu menu) { + MenuInflater mInflater = getMenuInflater(); + mInflater.inflate(R.menu.edit_settings, menu); + return true; + } - /** - * Retrieve proxy type from the preferences - * - * @return Registered proxy type - */ - private int getRegisteredProxyType() { - return (settings.getInt(getString(R.string.settings_key_proxy_type), DEFAULT_INT_VALUE)); + /** + * {@inheritDoc} + */ + @Override + public boolean onOptionsItemSelected(MenuItem item) { + Intent i = null; + switch (item.getItemId()) { + case R.id.settings_menu_create_account: + i = new Intent(this, CreateAccount.class); + startActivity(i); + return true; + case R.id.settings_menu_login: + setResult(69); + finish(); + return true; } + return false; + } - /** - * Retrieve proxy use from the preferences - * - * @return Registered proxy use - */ - private boolean getRegisteredProxyUse() { - return (settings.getBoolean(getString(R.string.settings_key_proxy_use), DEFAULT_BOOLEAN_VALUE)); - } + /** + * {@inheritDoc} + */ + @Override + public void onResume() { + super.onResume(); + refreshAccountTabFields(); + refreshXMPPTabFields(); + refreshProxyTabFields(); + + if (!proxyUseCheckBox.isChecked()) + disableProxyParameters(); + } - /** - * Retrieve proxy username from the preferences - * - * @return Registered proxy username - */ - private String getRegisteredProxyUsername() { - return (settings.getString(getString(R.string.settings_key_proxy_username), DEFAULT_STRING_VALUE)); - } + /** + * {@inheritDoc} + */ + @Override + public void onStart() { + super.onStart(); + initProxyTypesSpinner(); + initProxyUseCheckBox(); + initSaveButtons(); + } + + /** + * Refresh values of "Account" tab fields + */ + private void refreshAccountTabFields() { + accUsernameField.setText(getRegisteredAccountUsername()); + accPasswordField.setText(getRegisteredAccountPassword()); + } - /** - * Retrieve xmpp port from the preferences - * - * @return Registered xmpp port - */ - private String getRegisteredXMPPPort() { - return (settings.getString(getString(R.string.settings_key_xmpp_port), DEFAULT_XMPP_PORT)); - } + /** + * Refresh values of "Account" tab fields + */ + private void refreshProxyTabFields() { + proxyUseCheckBox.setChecked(getRegisteredProxyUse()); + proxyTypeSpinner.setSelection(getRegisteredProxyType()); + proxyServerField.setText(getRegisteredProxyServer()); + proxyPortField.setText(getRegisteredProxyPort()); + proxyUsernameField.setText(getRegisteredProxyUsername()); + proxyPasswordField.setText(getRegisteredProxyPassword()); + } - /** - * Retrieve xmpp server from the preferences - * - * @return Registered xmpp server - */ - private String getRegisteredXMPPServer() { - return (settings.getString(getString(R.string.settings_key_xmpp_server), DEFAULT_STRING_VALUE)); - } + /** + * Refresh values of "Account" tab fields + */ + private void refreshXMPPTabFields() { + xmppServerField.setText(getRegisteredXMPPServer()); + xmppPortField.setText(getRegisteredXMPPPort()); + xmppUseTLSCheckBox.setChecked(getRegisteredXMPPTLSUse()); + } + + /** + * Register account settings changes in SharedPreferences.Editor object passed by parameter + * @param settingsEditor + */ + private void registerAccountSettingsChanges(SharedPreferences.Editor settingsEditor) { + final String usernameFieldValue = accUsernameField.getText().toString(); + final String passwordFieldValue = accPasswordField.getText().toString(); + + if (getRegisteredAccountUsername().equals(usernameFieldValue) == false) + settingsEditor.putString(getString(R.string.settings_key_account_username), usernameFieldValue); + if (getRegisteredAccountPassword().equals(passwordFieldValue) == false) + settingsEditor.putString(getString(R.string.settings_key_account_password), passwordFieldValue); + Log.i(getString(R.string.edit_settings_tag), LOG_MSG_ACCOUNT_SETTINGS_REGISTERED); + } - /** - * Retrieve TLS use from the preferences - * - * @return Registered TLS use - */ - private boolean getRegisteredXMPPTLSUse() { - return (settings.getBoolean(getString(R.string.settings_key_xmpp_tls_use), DEFAULT_BOOLEAN_VALUE)); - } + /** + * Register proxy settings changes in SharedPreferences.Editor object passed by parameter + * @param settingsEditor + */ + private void registerProxySettingsChanges(SharedPreferences.Editor settingsEditor) { + final int proxyTypeFieldValue = proxyTypeSpinner.getSelectedItemPosition(); + final String serverFieldValue = proxyServerField.getText().toString(); + final String portFieldValue = proxyPortField.getText().toString(); + final String usernameFieldValue = proxyUsernameField.getText().toString(); + final String passwordFieldValue = proxyPasswordField.getText().toString(); - /** - * Initialize Account tab fields - */ - private void initAccountFields() { - accUsernameField = (EditText) findViewById(R.id.settings_account_username); - accPasswordField = (EditText) findViewById(R.id.settings_account_password); - } + if (getRegisteredProxyType() != proxyTypeFieldValue) + settingsEditor.putInt(getString(R.string.settings_key_proxy_type), proxyTypeFieldValue); + if (getRegisteredProxyServer().equals(serverFieldValue) == false) + settingsEditor.putString(getString(R.string.settings_key_proxy_server), serverFieldValue); + if (getRegisteredProxyPort().equals(portFieldValue) == false) + settingsEditor.putString(getString(R.string.settings_key_proxy_port), portFieldValue); + if (getRegisteredProxyUsername().equals(usernameFieldValue) == false) + settingsEditor.putString(getString(R.string.settings_key_proxy_username), usernameFieldValue); + if (getRegisteredProxyPassword().equals(passwordFieldValue) == false) + settingsEditor.putString(getString(R.string.settings_key_proxy_password), passwordFieldValue); + Log.i(getString(R.string.edit_settings_tag), LOG_MSG_PROXY_SETTINGS_REGISTERED); + } - /** - * Initialize all of the components displayed in tabs (EditText fields, CheckBoxes, Spinners...) - */ - private void initFields() { - initAccountFields(); - initXMPPFields(); - initProxyFields(); - } + /** + * Register XMPP settings changes in SharedPreferences.Editor object passed by parameter + * @param settingsEditor + */ + private void registerXMPPSettingsChanges(SharedPreferences.Editor settingsEditor) { + final boolean tlsUseCheckBoxValue = getCheckBoxValue(R.id.settings_xmpp_use_tls); + final String serverFieldValue = xmppServerField.getText().toString(); + final String portFieldValue = xmppPortField.getText().toString(); + + if (getRegisteredXMPPTLSUse() != tlsUseCheckBoxValue) + settingsEditor.putBoolean(getString(R.string.settings_key_xmpp_tls_use), tlsUseCheckBoxValue); + if (getRegisteredXMPPServer().equals(serverFieldValue) == false) + settingsEditor.putString(getString(R.string.settings_key_xmpp_server), serverFieldValue); + if (getRegisteredXMPPPort().equals(portFieldValue) == false) + settingsEditor.putString(getString(R.string.settings_key_xmpp_port), portFieldValue); + Log.i(getString(R.string.edit_settings_tag), LOG_MSG_XMPP_SETTINGS_REGISTERED); + } - /** - * Initialize Proxy tab fields - */ - private void initProxyFields() { - proxyUseCheckBox = (CheckBox) findViewById(R.id.settings_proxy_use); - proxyTypeSpinner = (Spinner) findViewById(R.id.settings_proxy_type); - proxyServerField = (EditText) findViewById(R.id.settings_proxy_server); - proxyPortField = (EditText) findViewById(R.id.settings_proxy_port); - proxyUsernameField = (EditText) findViewById(R.id.settings_proxy_username); - proxyPasswordField = (EditText) findViewById(R.id.settings_proxy_password); + /** + * Save settings in shared preferences + */ + private void saveSettings() { + final SharedPreferences.Editor settingsEditor = settings.edit(); + final boolean proxyUseCheckBoxValue = getCheckBoxValue(R.id.settings_proxy_use); + + if (getRegisteredProxyUse() != proxyUseCheckBoxValue) + settingsEditor.putBoolean(getString(R.string.settings_key_proxy_use), proxyUseCheckBoxValue); + if (proxyUseCheckBoxValue) + registerProxySettingsChanges(settingsEditor); + registerAccountSettingsChanges(settingsEditor); + registerXMPPSettingsChanges(settingsEditor); + + if (settingsEditor.commit()) { + displayNotification(getText(R.string.settings_saved_ok)); + Log.i(getString(R.string.edit_settings_tag), LOG_MSG_SETTINGS_SAVED); } - /** - * Initialize proxy's types spinner of the proxy tab - */ - private void initProxyTypesSpinner() { - ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.proxy_types, - android.R.layout.simple_spinner_item); - adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); - proxyTypeSpinner.setAdapter(adapter); - } - - /** - * Initialize the checkbox allowing user to use a proxy - */ - private void initProxyUseCheckBox() { - proxyUseCheckBox.setOnClickListener(new OnClickListener() { - - public void onClick(View v) { - if (proxyUseCheckBox.isChecked()) { - enableProxyParameters(); - } else { - disableProxyParameters(); - } - } - }); - } - - /** - * Initialize "save" buttons allowing user to save settings - */ - private void initSaveButtons() { - final ArrayList views = new ArrayList(); - Button saveButton = null; - - views.add(R.id.settings_account_button_save); - views.add(R.id.settings_proxy_button_save); - views.add(R.id.settings_xmpp_button_save); - - for (int i = 0; i < views.size(); i++) { - saveButton = (Button) findViewById(views.get(i)); - saveButton.setFocusable(true); - saveButton.setOnClickListener(new View.OnClickListener() { - - public void onClick(View v) { - saveSettings(); - } - }); - } - } - - /** - * Initialize tabbed window view by adding tabs and setting the default tab - */ - private void initTabbedWindow() { - TabHost tHost = (TabHost) this.findViewById(R.id.settings_tab_host); - tHost.setup(); - addAccountTab(tHost); - addXMPPTab(tHost); - addProxyTab(tHost); - tHost.setCurrentTab(0); - } - - /** - * Initialize XMPP tab fields - */ - private void initXMPPFields() { - xmppServerField = (EditText) findViewById(R.id.settings_xmpp_server); - xmppPortField = (EditText) findViewById(R.id.settings_xmpp_port); - xmppUseTLSCheckBox = (CheckBox) findViewById(R.id.settings_xmpp_use_tls); - } - - /** - * {@inheritDoc} - */ - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.edit_settings); - initTabbedWindow(); - initFields(); - settings = getSharedPreferences(getString(R.string.settings_filename), MODE_PRIVATE); - } - - /** - * {@inheritDoc} - */ - @Override - public boolean onCreateOptionsMenu(Menu menu) { - MenuInflater mInflater = getMenuInflater(); - mInflater.inflate(R.menu.edit_settings, menu); - return true; - } - - /** - * {@inheritDoc} - */ - @Override - public boolean onOptionsItemSelected(MenuItem item) { - Intent i = null; - switch (item.getItemId()) { - case R.id.settings_menu_create_account: - i = new Intent(this, CreateAccount.class); - startActivity(i); - return true; - case R.id.settings_menu_login: - finish(); - return true; - } - return false; - } - - /** - * {@inheritDoc} - */ - @Override - public void onResume() { - super.onResume(); - refreshAccountTabFields(); - refreshXMPPTabFields(); - refreshProxyTabFields(); - - if (!proxyUseCheckBox.isChecked()) - disableProxyParameters(); - } - - /** - * {@inheritDoc} - */ - @Override - public void onStart() { - super.onStart(); - initProxyTypesSpinner(); - initProxyUseCheckBox(); - initSaveButtons(); - } - - /** - * Refresh values of "Account" tab fields - */ - private void refreshAccountTabFields() { - accUsernameField.setText(getRegisteredAccountUsername()); - accPasswordField.setText(getRegisteredAccountPassword()); - } - - /** - * Refresh values of "Account" tab fields - */ - private void refreshProxyTabFields() { - proxyUseCheckBox.setChecked(getRegisteredProxyUse()); - proxyTypeSpinner.setSelection(getRegisteredProxyType()); - proxyServerField.setText(getRegisteredProxyServer()); - proxyPortField.setText(getRegisteredProxyPort()); - proxyUsernameField.setText(getRegisteredProxyUsername()); - proxyPasswordField.setText(getRegisteredProxyPassword()); - } - - /** - * Refresh values of "Account" tab fields - */ - private void refreshXMPPTabFields() { - xmppServerField.setText(getRegisteredXMPPServer()); - xmppPortField.setText(getRegisteredXMPPPort()); - xmppUseTLSCheckBox.setChecked(getRegisteredXMPPTLSUse()); - } - - /** - * Register account settings changes in SharedPreferences.Editor object passed by parameter - * - * @param settingsEditor - */ - private void registerAccountSettingsChanges(SharedPreferences.Editor settingsEditor) { - final String usernameFieldValue = accUsernameField.getText().toString(); - final String passwordFieldValue = accPasswordField.getText().toString(); - - if (getRegisteredAccountUsername().equals(usernameFieldValue) == false) - settingsEditor.putString(getString(R.string.settings_key_account_username), usernameFieldValue); - if (getRegisteredAccountPassword().equals(passwordFieldValue) == false) - settingsEditor.putString(getString(R.string.settings_key_account_password), passwordFieldValue); - Log.i(getString(R.string.edit_settings_tag), LOG_MSG_ACCOUNT_SETTINGS_REGISTERED); - } - - /** - * Register proxy settings changes in SharedPreferences.Editor object passed by parameter - * - * @param settingsEditor - */ - private void registerProxySettingsChanges(SharedPreferences.Editor settingsEditor) { - final int proxyTypeFieldValue = proxyTypeSpinner.getSelectedItemPosition(); - final String serverFieldValue = proxyServerField.getText().toString(); - final String portFieldValue = proxyPortField.getText().toString(); - final String usernameFieldValue = proxyUsernameField.getText().toString(); - final String passwordFieldValue = proxyPasswordField.getText().toString(); - - if (getRegisteredProxyType() != proxyTypeFieldValue) - settingsEditor.putInt(getString(R.string.settings_key_proxy_type), proxyTypeFieldValue); - if (getRegisteredProxyServer().equals(serverFieldValue) == false) - settingsEditor.putString(getString(R.string.settings_key_proxy_server), serverFieldValue); - if (getRegisteredProxyPort().equals(portFieldValue) == false) - settingsEditor.putString(getString(R.string.settings_key_proxy_port), portFieldValue); - if (getRegisteredProxyUsername().equals(usernameFieldValue) == false) - settingsEditor.putString(getString(R.string.settings_key_proxy_username), usernameFieldValue); - if (getRegisteredProxyPassword().equals(passwordFieldValue) == false) - settingsEditor.putString(getString(R.string.settings_key_proxy_password), passwordFieldValue); - Log.i(getString(R.string.edit_settings_tag), LOG_MSG_PROXY_SETTINGS_REGISTERED); - } - - /** - * Register XMPP settings changes in SharedPreferences.Editor object passed by parameter - * - * @param settingsEditor - */ - private void registerXMPPSettingsChanges(SharedPreferences.Editor settingsEditor) { - final boolean tlsUseCheckBoxValue = getCheckBoxValue(R.id.settings_xmpp_use_tls); - final String serverFieldValue = xmppServerField.getText().toString(); - final String portFieldValue = xmppPortField.getText().toString(); - - if (getRegisteredXMPPTLSUse() != tlsUseCheckBoxValue) - settingsEditor.putBoolean(getString(R.string.settings_key_xmpp_tls_use), tlsUseCheckBoxValue); - if (getRegisteredXMPPServer().equals(serverFieldValue) == false) - settingsEditor.putString(getString(R.string.settings_key_xmpp_server), serverFieldValue); - if (getRegisteredXMPPPort().equals(portFieldValue) == false) - settingsEditor.putString(getString(R.string.settings_key_xmpp_port), portFieldValue); - Log.i(getString(R.string.edit_settings_tag), LOG_MSG_XMPP_SETTINGS_REGISTERED); - } - - /** - * Save settings in shared preferences - */ - private void saveSettings() { - final SharedPreferences.Editor settingsEditor = settings.edit(); - final boolean proxyUseCheckBoxValue = getCheckBoxValue(R.id.settings_proxy_use); - - if (getRegisteredProxyUse() != proxyUseCheckBoxValue) - settingsEditor.putBoolean(getString(R.string.settings_key_proxy_use), proxyUseCheckBoxValue); - if (proxyUseCheckBoxValue) - registerProxySettingsChanges(settingsEditor); - registerAccountSettingsChanges(settingsEditor); - registerXMPPSettingsChanges(settingsEditor); - - if (settingsEditor.commit()) { - displayNotification(getText(R.string.settings_saved_ok)); - Log.i(getString(R.string.edit_settings_tag), LOG_MSG_SETTINGS_SAVED); - } - - stopService(new Intent(this, BeemService.class)); - } + stopService(new Intent(this, BeemService.class)); + } } diff -r e082fd525147 -r 0548b407992a src/com/beem/project/beem/ui/Login.java --- a/src/com/beem/project/beem/ui/Login.java Mon Jun 22 17:53:11 2009 +0200 +++ b/src/com/beem/project/beem/ui/Login.java Mon Jun 22 18:07:29 2009 +0200 @@ -15,6 +15,7 @@ import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; +import android.widget.Toast; import com.beem.project.beem.R; import com.beem.project.beem.service.aidl.IBeemConnectionListener; @@ -41,6 +42,8 @@ private final ServiceConnection mServConn = new BeemServiceConnection(); private IXmppFacade xmppFacade = null; + private int REQUEST_CODE = 1; + /** * Create an about "BEEM" dialog */ @@ -69,7 +72,7 @@ @Override public void onClick(View v) { - startActivity(new Intent(Login.this, EditSettings.class)); + startActivityForResult(new Intent(Login.this, EditSettings.class), REQUEST_CODE); } }); @@ -87,7 +90,8 @@ @Override protected void onDestroy() { super.onDestroy(); - unbindService(mServConn); + if (mIsConnected) + unbindService(mServConn); } /** @@ -121,25 +125,39 @@ @Override public void connectionClosed() throws RemoteException { - mIsConnected = false; - Login.this.unbindService(mServConn); - Login.this.stopService(SERVICE_INTENT); + if (mIsConnected) { + mIsConnected = false; + Login.this.unbindService(mServConn); + Login.this.stopService(SERVICE_INTENT); + } } @Override public void connectionClosedOnError() throws RemoteException { - mIsConnected = false; - Login.this.unbindService(mServConn); - Login.this.stopService(SERVICE_INTENT); + if (mIsConnected) { + mIsConnected = false; + Login.this.unbindService(mServConn); + Login.this.stopService(SERVICE_INTENT); + } } @Override public void connectionFailed(String errorMsg) throws RemoteException { - connectionHandler.post(new ErrorRunnable(errorMsg)); mIsConnected = false; Login.this.unbindService(mServConn); Login.this.stopService(SERVICE_INTENT); + connectionHandler.post(new ErrorRunnable(errorMsg)); dismissProgressDialog(); + showToast(errorMsg); + } + + private void showToast(final String errorMsg) { + connectionHandler.post(new Runnable() { + @Override + public void run() { + Toast.makeText(Login.this, errorMsg, Toast.LENGTH_SHORT).show(); + } + }); } private void dismissProgressDialog() { @@ -192,7 +210,7 @@ @Override public void run() { - progressDialog.setMessage("Loading. Please wait..."); + progressDialog.setMessage("Connecting. Please wait..."); progressDialog.show(); } });