Merge branch 'develop' into operations_service
authormasensio <masensio@solidgear.es>
Tue, 1 Apr 2014 12:18:32 +0000 (14:18 +0200)
committermasensio <masensio@solidgear.es>
Tue, 1 Apr 2014 12:18:32 +0000 (14:18 +0200)
1  2 
src/com/owncloud/android/authentication/AuthenticatorActivity.java

@@@ -25,11 -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
@@@ -37,7 -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
@@@ -45,7 -41,6 +45,7 @@@ import android.support.v4.app.FragmentT
  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
@@@ -59,7 -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
@@@ -82,8 -76,6 +82,8 @@@ import com.owncloud.android.lib.common.
  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
@@@ -112,7 -104,6 +112,6 @@@ SsoWebViewClientListener, OnSslUntruste
      private static final String KEY_AUTH_MESSAGE_TEXT = "AUTH_MESSAGE_TEXT";\r
      private static final String KEY_HOST_URL_TEXT = "HOST_URL_TEXT";\r
      private static final String KEY_OC_VERSION = "OC_VERSION";\r
-     private static final String KEY_OC_VERSION_STRING = "OC_VERSION_STRING";\r
      private static final String KEY_ACCOUNT = "ACCOUNT";\r
      private static final String KEY_SERVER_VALID = "SERVER_VALID";\r
      private static final String KEY_SERVER_CHECKED = "SERVER_CHECKED";\r
      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
      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
      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
          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
              /// retrieve extras from intent\r
              mAccount = getIntent().getExtras().getParcelable(EXTRA_ACCOUNT);\r
              if (mAccount != null) {
-                 String ocVersion = mAccountMgr.getUserData(mAccount, Constants.KEY_OC_VERSION);\r
-                 String ocVersionString = mAccountMgr.getUserData(mAccount, Constants.KEY_OC_VERSION_STRING);
+                 String ocVersion = mAccountMgr.getUserData(mAccount, Constants.KEY_OC_VERSION);
                  if (ocVersion != null) {\r
-                     mDiscoveredVersion = new OwnCloudVersion(ocVersion, ocVersionString);\r
+                     mDiscoveredVersion = new OwnCloudVersion(ocVersion);\r
                  }\r
                  mHostBaseUrl = normalizeUrl(mAccountMgr.getUserData(mAccount, Constants.KEY_OC_BASE_URL));\r
                  mHostUrlInput.setText(mHostBaseUrl);\r
  \r
              /// server data\r
              String ocVersion = savedInstanceState.getString(KEY_OC_VERSION);\r
-             String ocVersionString = savedInstanceState.getString(KEY_OC_VERSION_STRING);\r
              if (ocVersion != null) {\r
-                 mDiscoveredVersion = new OwnCloudVersion(ocVersion, ocVersionString);\r
+                 mDiscoveredVersion = new OwnCloudVersion(ocVersion);\r
              }\r
              mHostBaseUrl = savedInstanceState.getString(KEY_HOST_URL_TEXT);\r
  \r
              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
                  return false;\r
              }\r
          });\r
 +        \r
      }\r
  \r
  \r
       */\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
          /// server data\r
          if (mDiscoveredVersion != null) {\r
              outState.putString(KEY_OC_VERSION, mDiscoveredVersion.getVersion());\r
-             outState.putString(KEY_OC_VERSION_STRING, mDiscoveredVersion.getVersionString());\r
          }\r
          outState.putString(KEY_HOST_URL_TEXT, mHostBaseUrl);\r
  \r
  \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
       */\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
          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
              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
      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
              }\r
              /// add user data to the new account; TODO probably can be done in the last parameter addAccountExplicitly, or in KEY_USERDATA
              mAccountMgr.setUserData(mAccount, Constants.KEY_OC_VERSION,         mDiscoveredVersion.getVersion());\r
-             mAccountMgr.setUserData(mAccount, Constants.KEY_OC_VERSION_STRING,  mDiscoveredVersion.getVersionString());\r
              mAccountMgr.setUserData(mAccount, Constants.KEY_OC_BASE_URL,   mHostBaseUrl);\r
  
              if (isSaml) {\r
          }\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