Merge branch 'develop' into operations_service
[pub/Android/ownCloud.git] / src / com / owncloud / android / authentication / AuthenticatorActivity.java
index 8cbe7ad..69eea77 100644 (file)
@@ -25,8 +25,11 @@ import android.accounts.AccountManager;
 import android.app.AlertDialog;\r
 import android.app.Dialog;\r
 import android.app.ProgressDialog;\r
+import android.content.ComponentName;\r
+import android.content.Context;\r
 import android.content.DialogInterface;\r
 import android.content.Intent;\r
+import android.content.ServiceConnection;\r
 import android.content.SharedPreferences;\r
 import android.graphics.Rect;\r
 import android.graphics.drawable.Drawable;\r
@@ -34,6 +37,7 @@ import android.net.Uri;
 import android.net.http.SslError;\r
 import android.os.Bundle;\r
 import android.os.Handler;\r
+import android.os.IBinder;\r
 import android.preference.PreferenceManager;\r
 import android.support.v4.app.Fragment;\r
 import android.support.v4.app.FragmentManager;\r
@@ -41,6 +45,7 @@ import android.support.v4.app.FragmentTransaction;
 import android.text.Editable;\r
 import android.text.InputType;\r
 import android.text.TextWatcher;\r
+import android.util.Log;\r
 import android.view.KeyEvent;\r
 import android.view.MotionEvent;\r
 import android.view.View;\r
@@ -54,6 +59,7 @@ import android.widget.CheckBox;
 import android.widget.EditText;\r
 import android.widget.TextView;\r
 import android.widget.TextView.OnEditorActionListener;\r
+import android.widget.Toast;\r
 \r
 import com.actionbarsherlock.app.SherlockDialogFragment;\r
 import com.owncloud.android.MainApp;\r
@@ -76,6 +82,8 @@ import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCo
 import com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation;\r
 import com.owncloud.android.lib.resources.users.GetRemoteUserNameOperation;\r
 \r
+import com.owncloud.android.services.OperationsService;\r
+import com.owncloud.android.services.OperationsService.OperationsServiceBinder;\r
 import com.owncloud.android.ui.dialog.SamlWebViewDialog;\r
 import com.owncloud.android.ui.dialog.SslUntrustedCertDialog;\r
 import com.owncloud.android.ui.dialog.SslUntrustedCertDialog.OnSslUntrustedCertListener;\r
@@ -116,6 +124,9 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
     private static final String KEY_AUTH_STATUS_ICON = "AUTH_STATUS_ICON";\r
     private static final String KEY_REFRESH_BUTTON_ENABLED = "KEY_REFRESH_BUTTON_ENABLED";\r
     //private static final String KEY_IS_SHARED_SUPPORTED = "KEY_IS_SHARE_SUPPORTED";\r
+    private static final String KEY_SERVER_AUTH_METHOD = "KEY_SERVER_AUTH_METHOD";\r
+    private static final String KEY_DETECT_AUTH_OP_ID = "KEY_DETECT_AUTH_OP_ID";\r
+\r
 \r
     private static final String AUTH_ON = "on";\r
     private static final String AUTH_OFF = "off";\r
@@ -136,6 +147,9 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
     private String mAuthMessageText;\r
     private int mAuthMessageVisibility, mServerStatusText, mServerStatusIcon;\r
     private boolean mServerIsChecked, mServerIsValid, mIsSslConn;\r
+    private AuthenticationMethod mServerAuthMethod = AuthenticationMethod.UNKNOWN;\r
+    private int mDetectAuthOpId = -1;\r
+\r
     private int mAuthStatusText, mAuthStatusIcon;    \r
     private TextView mAuthStatusLayout;\r
 \r
@@ -176,9 +190,10 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
     private boolean mResumed; // Control if activity is resumed\r
 \r
     public static String DIALOG_UNTRUSTED_CERT = "DIALOG_UNTRUSTED_CERT";\r
-\r
-    private DetectAuthenticationMethodOperation mDetectAuthenticationOperation;\r
-\r
+    \r
+    private ServiceConnection mOperationsServiceConnection = null;\r
+    \r
+    private OperationsServiceBinder mOperationsServiceBinder = null;\r
 \r
     /**\r
      * {@inheritDoc}\r
@@ -190,6 +205,18 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         super.onCreate(savedInstanceState);\r
         getWindow().requestFeature(Window.FEATURE_NO_TITLE);\r
 \r
+        // bind to Operations Service\r
+        mOperationsServiceConnection = new OperationsServiceConnection();\r
+        if (!bindService(new Intent(this, OperationsService.class), \r
+                mOperationsServiceConnection, \r
+                Context.BIND_AUTO_CREATE)) {\r
+            Toast.makeText(this, \r
+                    R.string.error_cant_bind_to_operations_service, \r
+                    Toast.LENGTH_LONG)\r
+                        .show();\r
+            finish();\r
+        }\r
+                \r
         /// set view and get references to view elements\r
         setContentView(R.layout.account_setup);\r
         mAuthMessage = (TextView) findViewById(R.id.auth_message);\r
@@ -306,6 +333,10 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
             refreshButtonEnabled = savedInstanceState.getBoolean(KEY_REFRESH_BUTTON_ENABLED);\r
 \r
 \r
+            mServerAuthMethod = AuthenticationMethod.valueOf(\r
+                    savedInstanceState.getString(KEY_SERVER_AUTH_METHOD));\r
+            mDetectAuthOpId = savedInstanceState.getInt(KEY_DETECT_AUTH_OP_ID);\r
+\r
         }\r
 \r
         if (mAuthMessageVisibility== View.VISIBLE) {\r
@@ -391,6 +422,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
                 return false;\r
             }\r
         });\r
+        \r
     }\r
 \r
 \r
@@ -443,6 +475,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
      */\r
     @Override\r
     protected void onSaveInstanceState(Bundle outState) {\r
+        //Log.wtf(TAG, "onSaveInstanceState init" );\r
         super.onSaveInstanceState(outState);\r
 \r
         /// connection state and info\r
@@ -472,8 +505,10 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
 \r
         // refresh button enabled\r
         outState.putBoolean(KEY_REFRESH_BUTTON_ENABLED, (mRefreshButton.getVisibility() == View.VISIBLE));\r
-\r
-\r
+        \r
+        outState.putString(KEY_SERVER_AUTH_METHOD, mServerAuthMethod.name());\r
+        outState.putInt(KEY_DETECT_AUTH_OP_ID, mDetectAuthOpId);\r
+        //Log.wtf(TAG, "onSaveInstanceState end" );\r
     }\r
 \r
 \r
@@ -500,7 +535,9 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
      */\r
     @Override\r
     protected void onResume() {\r
+        //Log.wtf(TAG, "onResume init" );\r
         super.onResume();\r
+        \r
         if (mAction == ACTION_UPDATE_TOKEN && mJustCreated && getIntent().getBooleanExtra(EXTRA_ENFORCED_UPDATE, false)) {\r
             if (AccountTypeUtils.getAuthTokenTypeAccessToken(MainApp.getAccountType()).equals(mAuthTokenType)) {\r
                 //Toast.makeText(this, R.string.auth_expired_oauth_token_toast, Toast.LENGTH_LONG).show();\r
@@ -517,9 +554,35 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         if (mNewCapturedUriFromOAuth2Redirection != null) {\r
             getOAuth2AccessTokenFromCapturedRedirection();            \r
         }\r
-\r
+        \r
         mJustCreated = false;\r
 \r
+        if (mOperationsServiceBinder != null) {\r
+            doOnResumeAndBound();\r
+        }\r
+        \r
+        //Log.wtf(TAG, "onResume end" );\r
+    }\r
+\r
+    \r
+    @Override\r
+    protected void onPause() {\r
+        //Log.wtf(TAG, "onPause init" );\r
+        if (mOperationsServiceBinder != null) {\r
+            //Log.wtf(TAG, "unregistering to listen for operation callbacks" );\r
+            mOperationsServiceBinder.removeOperationListener(this);\r
+        }\r
+        super.onPause();\r
+        //Log.wtf(TAG, "onPause end" );\r
+    }\r
+    \r
+    @Override\r
+    protected void onDestroy() {\r
+        if (mOperationsServiceConnection != null) {\r
+            unbindService(mOperationsServiceConnection);\r
+            mOperationsServiceBinder = null;\r
+        }\r
+        super.onDestroy();\r
     }\r
 \r
 \r
@@ -811,13 +874,15 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
             onGetUserNameFinish((GetRemoteUserNameOperation) operation, result);\r
 \r
         } else if (operation instanceof DetectAuthenticationMethodOperation) {\r
-            onDetectAutheticationFinish((DetectAuthenticationMethodOperation) operation, result);\r
+            Log.wtf(TAG, "received detection response through callback" );\r
+            onDetectAuthenticationFinish(result);\r
         }\r
 \r
     }\r
 \r
-    private void onDetectAutheticationFinish(DetectAuthenticationMethodOperation operation, RemoteOperationResult result) {\r
+    private void onDetectAuthenticationFinish(RemoteOperationResult result) {\r
         // Read authentication method\r
+        mDetectAuthOpId = -1;\r
         if (result.getData().size() > 0) {\r
             AuthenticationMethod authMethod = (AuthenticationMethod) result.getData().get(0);\r
             String basic = AccountTypeUtils.getAuthTokenTypePass(MainApp.getAccountType());\r
@@ -959,14 +1024,23 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
     private void detectAuthorizationMethod() {\r
 \r
         Log_OC.d(TAG, "Trying empty authorization to detect authentication method");\r
-\r
-        /// get the path to the root folder through WebDAV from the version server\r
+        \r
         String webdav_path = AccountUtils.getWebdavPath(mDiscoveredVersion, mAuthTokenType);\r
-\r
+        \r
         /// test credentials \r
-        mDetectAuthenticationOperation = new DetectAuthenticationMethodOperation(this);\r
-        OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(Uri.parse(mHostBaseUrl + webdav_path), this, true);\r
-        mOperationThread = mDetectAuthenticationOperation.execute(client, this, mHandler);\r
+        //Intent detectAuthIntent = new Intent(this, OperationsService.class);\r
+        Intent detectAuthIntent = new Intent();\r
+        detectAuthIntent.setAction(OperationsService.ACTION_DETECT_AUTHENTICATION_METHOD);\r
+        detectAuthIntent.putExtra(OperationsService.EXTRA_SERVER_URL, mHostBaseUrl);\r
+        detectAuthIntent.putExtra(OperationsService.EXTRA_WEBDAV_PATH, webdav_path);\r
+        \r
+        //if (mOperationsBinder != null) {  // let's let it crash to detect if is really possible\r
+        mServerAuthMethod = AuthenticationMethod.UNKNOWN;\r
+        if (mOperationsServiceBinder != null) {\r
+            //Log.wtf(TAG, "starting detection..." );\r
+            mDetectAuthOpId = mOperationsServiceBinder.newOperation(detectAuthIntent);\r
+        }\r
+        //}\r
     }\r
 \r
 \r
@@ -1790,5 +1864,49 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         }\r
 \r
     }\r
+    \r
+    \r
+    private void doOnResumeAndBound() {\r
+        //Log.wtf(TAG, "registering to listen for operation callbacks" );\r
+        mOperationsServiceBinder.addOperationListener(AuthenticatorActivity.this, mHandler);\r
+        \r
+        if (mDetectAuthOpId != -1) {\r
+            RemoteOperationResult result = \r
+                    mOperationsServiceBinder.getOperationResultIfFinished(mDetectAuthOpId);\r
+            if (result != null) {\r
+                //Log.wtf(TAG, "found result of operation finished while rotating");\r
+                onDetectAuthenticationFinish(result);\r
+            }\r
+        }\r
+    }\r
+    \r
+    /** \r
+     * Implements callback methods for service binding. \r
+     */\r
+    private class OperationsServiceConnection implements ServiceConnection {\r
+\r
+        @Override\r
+        public void onServiceConnected(ComponentName component, IBinder service) {\r
+            if (component.equals(new ComponentName(AuthenticatorActivity.this, OperationsService.class))) {\r
+                //Log_OC.wtf(TAG, "Operations service connected");\r
+                mOperationsServiceBinder = (OperationsServiceBinder) service;\r
+                \r
+                doOnResumeAndBound();\r
+                \r
+            } else {\r
+                return;\r
+            }\r
+            \r
+        }\r
 \r
+        @Override\r
+        public void onServiceDisconnected(ComponentName component) {\r
+            if (component.equals(new ComponentName(AuthenticatorActivity.this, OperationsService.class))) {\r
+                Log_OC.e(TAG, "Operations service crashed");\r
+                mOperationsServiceBinder = null;\r
+            }\r
+        }\r
+    \r
+    }\r
+    \r
 }\r