new stuff for account authenticator
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / ui / activity / AuthenticatorActivity.java
index c62290b..3ecd3e7 100644 (file)
@@ -21,8 +21,6 @@ package eu.alefzero.owncloud.ui.activity;
 import java.net.MalformedURLException;\r
 import java.net.URL;\r
 \r
-import com.actionbarsherlock.ActionBarSherlock;\r
-\r
 import android.accounts.Account;\r
 import android.accounts.AccountAuthenticatorActivity;\r
 import android.accounts.AccountManager;\r
@@ -40,202 +38,225 @@ import android.text.InputType;
 import android.util.Log;\r
 import android.view.View;\r
 import android.view.Window;\r
-import android.widget.CheckBox;\r
 import android.widget.TextView;\r
 import android.widget.Toast;\r
+import eu.alefzero.owncloud.AccountUtils;\r
 import eu.alefzero.owncloud.R;\r
 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;\r
-import eu.alefzero.owncloud.authenticator.AuthUtils;\r
+import eu.alefzero.owncloud.authenticator.AuthenticationRunnable;\r
+import eu.alefzero.owncloud.authenticator.OnAuthenticationResultListener;\r
 import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;\r
 \r
 /**\r
  * This Activity is used to add an ownCloud account to the App\r
+ * \r
  * @author Bartek Przybylski\r
- *\r
+ * \r
  */\r
-public class AuthenticatorActivity extends AccountAuthenticatorActivity {\r
-    private Thread mAuthThread;\r
-    private final Handler mHandler = new Handler();\r
-    private boolean mUseSSLConnection;\r
+public class AuthenticatorActivity extends AccountAuthenticatorActivity implements OnAuthenticationResultListener {\r
+  private static final int DIALOG_LOGIN_PROGRESS = 0;\r
+  private static final String TAG = "AuthActivity";\r
 \r
-    public static final String PARAM_USERNAME = "param_Username";\r
-    public static final String PARAM_HOSTNAME = "param_Hostname";\r
+  private Thread mAuthThread;\r
+  private AuthenticationRunnable mAuthRunnable;\r
+  private final Handler mHandler = new Handler();\r
+  private boolean mUseSSLConnection;\r
 \r
-    public AuthenticatorActivity() {\r
-      mUseSSLConnection = true;\r
-    }\r
-    \r
-    @Override\r
-    protected void onCreate(Bundle savedInstanceState) {\r
-        super.onCreate(savedInstanceState);\r
-        getWindow().requestFeature(Window.FEATURE_NO_TITLE);\r
-        setContentView(R.layout.account_setup);\r
-        if (getIntent().hasExtra(PARAM_USERNAME)) {\r
-            String username = getIntent().getStringExtra(PARAM_HOSTNAME);\r
-            TextView host_text, user_text;\r
-            host_text = (TextView) findViewById(R.id.host_URL);\r
-            user_text = (TextView) findViewById(R.id.account_username);\r
-            host_text.setText(host_text.getText() + username.substring(username.lastIndexOf('@')));\r
-            user_text.setText(user_text.getText() + username.substring(0, username.lastIndexOf('@') - 1));\r
-        }\r
+  public static final String PARAM_USERNAME = "param_Username";\r
+  public static final String PARAM_HOSTNAME = "param_Hostname";\r
+\r
+  public AuthenticatorActivity() {\r
+    mUseSSLConnection = true;\r
+  }\r
+\r
+  @Override\r
+  protected void onCreate(Bundle savedInstanceState) {\r
+    super.onCreate(savedInstanceState);\r
+    getWindow().requestFeature(Window.FEATURE_NO_TITLE);\r
+    setContentView(R.layout.account_setup);\r
+    if (getIntent().hasExtra(PARAM_USERNAME)) {\r
+      String username = getIntent().getStringExtra(PARAM_HOSTNAME);\r
+      TextView host_text, user_text;\r
+      host_text = (TextView) findViewById(R.id.host_URL);\r
+      user_text = (TextView) findViewById(R.id.account_username);\r
+      host_text.setText(host_text.getText()\r
+          + username.substring(username.lastIndexOf('@')));\r
+      user_text.setText(user_text.getText()\r
+          + username.substring(0, username.lastIndexOf('@') - 1));\r
     }\r
+  }\r
 \r
-    @Override\r
-    protected Dialog onCreateDialog(int id) {\r
-        final ProgressDialog dialog = new ProgressDialog(this);\r
-        dialog.setMessage("Trying to login");\r
-        dialog.setIndeterminate(true);\r
-        dialog.setCancelable(true);\r
-        dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {\r
-            public void onCancel(DialogInterface dialog) {\r
-                Log.i(getClass().getName(), "Login canceled");\r
-                if (mAuthThread != null) {\r
-                    mAuthThread.interrupt();\r
-                    finish();\r
-                }\r
+  @Override\r
+  protected Dialog onCreateDialog(int id) {\r
+    Dialog dialog = null;\r
+    switch (id) {\r
+      case DIALOG_LOGIN_PROGRESS : {\r
+        ProgressDialog working_dialog = new ProgressDialog(this);\r
+        dialog = working_dialog;\r
+        working_dialog.setMessage(getResources().getString(R.string.auth_trying_to_login));\r
+        working_dialog.setIndeterminate(true);\r
+        working_dialog.setCancelable(true);\r
+        working_dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {\r
+          @Override\r
+          public void onCancel(DialogInterface dialog) {\r
+            Log.i(getClass().getName(), "Login canceled");\r
+            if (mAuthThread != null) {\r
+              mAuthThread.interrupt();\r
+              finish();\r
             }\r
+          }\r
         });\r
-        return dialog;\r
+        break;\r
+      }\r
+      default :\r
+        Log.e(TAG, "Incorrect dialog called with id = " + id);\r
     }\r
+    return dialog;\r
+  }\r
 \r
-    public void onAuthenticationResult(boolean result, String message) {\r
-        if (result) {\r
-            TextView username_text = (TextView) findViewById(R.id.account_username),\r
-                    password_text = (TextView) findViewById(R.id.account_password);\r
-\r
-            URL url;\r
-            try {\r
-                url = new URL(message);\r
-            } catch (MalformedURLException e) {\r
-                // should never happen\r
-                Log.e(getClass().getName(), "Malformed URL: " + message);\r
-                return;\r
-            }\r
+  public void onAuthenticationResult(boolean success, String message) {\r
+    if (success) {\r
+      TextView username_text = (TextView) findViewById(R.id.account_username), password_text = (TextView) findViewById(R.id.account_password);\r
 \r
-            String username = username_text.getText().toString().trim();\r
-            String accountName = username + "@" + url.getHost();\r
-            Account account = new Account(accountName, AccountAuthenticator.ACCOUNT_TYPE);\r
-            AccountManager accManager = AccountManager.get(this);\r
-            accManager.addAccountExplicitly(account, password_text.getText().toString(), null);\r
-            \r
-            // Add this account as default in the preferences, if there is none already\r
-            Account defaultAccount = AuthUtils.getCurrentOwnCloudAccount(this);\r
-            if(defaultAccount == null){\r
-               SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();\r
-               editor.putString("select_oc_account", accountName);\r
-               editor.commit();\r
-            }\r
+      URL url;\r
+      try {\r
+        url = new URL(message);\r
+      } catch (MalformedURLException e) {\r
+        // should never happen\r
+        Log.e(getClass().getName(), "Malformed URL: " + message);\r
+        return;\r
+      }\r
+\r
+      String username = username_text.getText().toString().trim();\r
+      String accountName = username + "@" + url.getHost();\r
+      Account account = new Account(accountName,\r
+          AccountAuthenticator.ACCOUNT_TYPE);\r
+      AccountManager accManager = AccountManager.get(this);\r
+      accManager.addAccountExplicitly(account, password_text.getText()\r
+          .toString(), null);\r
+\r
+      // Add this account as default in the preferences, if there is none\r
+      // already\r
+      Account defaultAccount = AccountUtils.getCurrentOwnCloudAccount(this);\r
+      if (defaultAccount == null) {\r
+        SharedPreferences.Editor editor = PreferenceManager\r
+            .getDefaultSharedPreferences(this).edit();\r
+        editor.putString("select_oc_account", accountName);\r
+        editor.commit();\r
+      }\r
+\r
+      final Intent intent = new Intent();\r
+      intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE,\r
+          AccountAuthenticator.ACCOUNT_TYPE);\r
+      intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, account.name);\r
+      intent.putExtra(AccountManager.KEY_AUTHTOKEN,\r
+          AccountAuthenticator.ACCOUNT_TYPE);\r
+      accManager.setUserData(account, AccountAuthenticator.KEY_OC_URL,\r
+          url.toString());\r
+\r
+      // TODO prepare this URL using a central service\r
+      intent.putExtra(AccountManager.KEY_USERDATA, username);\r
+      accManager.setUserData(\r
+          account,\r
+          AccountAuthenticator.KEY_CONTACT_URL,\r
+          url.toString().replace(AccountUtils.WEBDAV_PATH_2_0,\r
+              AccountUtils.CARDDAV_PATH_2_0));\r
+\r
+      setAccountAuthenticatorResult(intent.getExtras());\r
+      setResult(RESULT_OK, intent);\r
+      Bundle bundle = new Bundle();\r
+      bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);\r
+      getContentResolver().startSync(ProviderTableMeta.CONTENT_URI, bundle);\r
 \r
-            final Intent intent = new Intent();\r
-            intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, AccountAuthenticator.ACCOUNT_TYPE);\r
-            intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, account.name);\r
-            intent.putExtra(AccountManager.KEY_AUTHTOKEN, AccountAuthenticator.ACCOUNT_TYPE);\r
-            accManager.setUserData(account, AccountAuthenticator.KEY_OC_URL, url.toString());\r
-\r
-            // TODO prepare this URL using a central service\r
-            intent.putExtra(AccountManager.KEY_USERDATA, username);\r
-            accManager.setUserData(account, AccountAuthenticator.KEY_CONTACT_URL,\r
-                    url.toString().replace(AuthUtils.WEBDAV_PATH_2_0, AuthUtils.CARDDAV_PATH_2_0)\r
-            );\r
-\r
-            setAccountAuthenticatorResult(intent.getExtras());\r
-            setResult(RESULT_OK, intent);\r
-            Bundle bundle = new Bundle();\r
-            bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);\r
-            getContentResolver().startSync(ProviderTableMeta.CONTENT_URI, bundle);\r
-\r
-            dismissDialog(0);\r
-            finish();\r
-        } else {\r
-            Toast.makeText(this, message, Toast.LENGTH_LONG).show();\r
-            dismissDialog(0);\r
-        }\r
+      dismissDialog(0);\r
+      finish();\r
+    } else {\r
+      Toast.makeText(this, message, Toast.LENGTH_LONG).show();\r
+      dismissDialog(0);\r
     }\r
+  }\r
 \r
-    public void onCancelClick(View view) {\r
-        Log.i(getClass().getName(), "Account creating canceled");\r
-        this.finish();\r
+  public void onOkClick(View view) {\r
+    TextView url_text = (TextView) findViewById(R.id.host_URL);\r
+    TextView username_text = (TextView) findViewById(R.id.account_username);\r
+    TextView password_text = (TextView) findViewById(R.id.account_password);\r
+    Log.i(getClass().getName(), "OK clicked");\r
+    boolean hasErrors = false;\r
+\r
+    URL uri = null;\r
+    if (url_text.getText().toString().trim().length() == 0) {\r
+      url_text.setTextColor(Color.RED);\r
+      hasErrors = true;\r
+    } else {\r
+      url_text.setTextColor(android.R.color.black);\r
+    }\r
+    try {\r
+      String url_str = url_text.getText().toString();\r
+      if (!url_str.startsWith("http://") && !url_str.startsWith("https://")) {\r
+        if (mUseSSLConnection)\r
+          url_str = "https://" + url_str;\r
+        else\r
+          url_str = "http://" + url_str;\r
+      }\r
+      uri = new URL(url_str);\r
+    } catch (MalformedURLException e) {\r
+      url_text.setTextColor(Color.RED);\r
+      e.printStackTrace();\r
+      hasErrors = true;\r
     }\r
 \r
-    public void onOkClick(View view) {\r
-        TextView url_text = (TextView) findViewById(R.id.host_URL);\r
-        TextView username_text = (TextView) findViewById(R.id.account_username);\r
-        TextView password_text = (TextView) findViewById(R.id.account_password);\r
-        Log.i(getClass().getName(), "OK clicked");\r
-        boolean hasErrors = false;\r
-\r
-        URL uri = null;\r
-        if (url_text.getText().toString().trim().length() == 0) {\r
-            url_text.setTextColor(Color.RED);\r
-            hasErrors = true;\r
-        } else {\r
-            url_text.setTextColor(android.R.color.black);\r
-        }\r
-        try {\r
-            String url_str = url_text.getText().toString();\r
-            if (!url_str.startsWith("http://") &&\r
-                    !url_str.startsWith("https://")) {\r
-              if (mUseSSLConnection)\r
-                url_str = "https://" + url_str;\r
-              else\r
-                url_str = "http://" + url_str;\r
-            }\r
-            uri = new URL(url_str);\r
-        } catch (MalformedURLException e) {\r
-            url_text.setTextColor(Color.RED);\r
-            e.printStackTrace();\r
-            hasErrors = true;\r
-        }\r
-\r
-        if (username_text.getText().toString().contains(" ") ||\r
-                username_text.getText().toString().trim().length() == 0) {\r
-            username_text.setTextColor(Color.RED);\r
-            hasErrors = true;\r
-        } else {\r
-            username_text.setTextColor(android.R.color.black);\r
-        }\r
-\r
-        if (password_text.getText().toString().trim().length() == 0) {\r
-            password_text.setTextColor(Color.RED);\r
-            hasErrors = true;\r
-        } else {\r
-            password_text.setTextColor(android.R.color.black);\r
-        }\r
-        if (hasErrors) {\r
-            return;\r
-        }\r
-        \r
-        int new_port = uri.getPort();\r
-        if (new_port == -1) {\r
-          if (mUseSSLConnection)\r
-            new_port = 443;\r
-          else\r
-            new_port = 80;\r
-        }\r
-        \r
-        try {\r
-          uri = new URL(uri.getProtocol(), uri.getHost(), new_port, uri.getPath());\r
-        } catch (MalformedURLException e) {\r
-          e.printStackTrace(); // should not happend\r
-        }\r
-        \r
-        showDialog(0);\r
-        mAuthThread = AuthUtils.attemptAuth(uri,\r
-                username_text.getText().toString(),\r
-                password_text.getText().toString(),\r
-                mHandler,\r
-                AuthenticatorActivity.this);\r
+    if (username_text.getText().toString().contains(" ")\r
+        || username_text.getText().toString().trim().length() == 0) {\r
+      username_text.setTextColor(Color.RED);\r
+      hasErrors = true;\r
+    } else {\r
+      username_text.setTextColor(android.R.color.black);\r
     }\r
-    \r
-    public void sslBadgeClick(View view, String val) {\r
-      mUseSSLConnection = ((TextView)view).getText().equals("SSL");\r
+\r
+    if (password_text.getText().toString().trim().length() == 0) {\r
+      password_text.setTextColor(Color.RED);\r
+      hasErrors = true;\r
+    } else {\r
+      password_text.setTextColor(android.R.color.black);\r
     }\r
-    \r
-    public void passwordBadgeClick(View view, String val) {\r
-      if(val.equals("Hide")) {\r
-        ((TextView)view).setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);\r
-      } else {\r
-        ((TextView)view).setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);\r
-      }\r
+    if (hasErrors) {\r
+      return;\r
+    }\r
+\r
+    int new_port = uri.getPort();\r
+    if (new_port == -1) {\r
+      if (mUseSSLConnection)\r
+        new_port = 443;\r
+      else\r
+        new_port = 80;\r
+    }\r
+\r
+    try {\r
+      uri = new URL(uri.getProtocol(), uri.getHost(), new_port, uri.getPath());\r
+    } catch (MalformedURLException e) {\r
+      e.printStackTrace(); // should not happend\r
     }\r
+\r
+    showDialog(DIALOG_LOGIN_PROGRESS);\r
+    mAuthRunnable = new AuthenticationRunnable(\r
+        uri,\r
+        username_text.getText().toString(),\r
+        password_text.getText().toString());\r
+    mAuthRunnable.setOnAuthenticationResultListener(this, mHandler);\r
+    mAuthThread = new Thread(mAuthRunnable);\r
+    mAuthThread.start();\r
+  }\r
+\r
+  public void sslBadgeClick(View view, String val) {\r
+    mUseSSLConnection = ((TextView) view).getText().equals("SSL");\r
+  }\r
+\r
+  public void passwordBadgeClick(View view, String val) {\r
+    int input_type = InputType.TYPE_CLASS_TEXT;\r
+    input_type |= val.equals("Hide")\r
+                  ? InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD\r
+                  : InputType.TYPE_TEXT_VARIATION_PASSWORD;\r
+\r
+    ((TextView) view).setInputType(input_type);\r
+  }\r
 }\r