Merge remote-tracking branch 'origin/operations_service' into operations_service
authorDavid A. Velasco <dvelasco@solidgear.es>
Fri, 28 Mar 2014 11:57:44 +0000 (12:57 +0100)
committerDavid A. Velasco <dvelasco@solidgear.es>
Fri, 28 Mar 2014 11:57:44 +0000 (12:57 +0100)
1  2 
src/com/owncloud/android/authentication/AuthenticatorActivity.java
src/com/owncloud/android/services/OperationsService.java

@@@ -58,7 -58,6 +58,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
@@@ -147,9 -146,6 +147,9 @@@ SsoWebViewClientListener, OnSslUntruste
      private int mAuthStatusText, mAuthStatusIcon;    \r
      private TextView mAuthStatusLayout;\r
  \r
 +    private ServiceConnection mOperationsConnection = null;\r
 +    private OperationsServiceBinder mOperationsBinder = null;\r
 +    \r
      private final Handler mHandler = new Handler();\r
      private Thread mOperationThread;\r
      private GetRemoteStatusOperation mOcServerChkOperation;\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
+     private ServiceConnection mOperationsServiceConnection = null;\r
+     \r
+     private OperationsServiceBinder mOperationsServiceBinder = null;\r
  \r
  \r
      /**\r
          super.onCreate(savedInstanceState);\r
          getWindow().requestFeature(Window.FEATURE_NO_TITLE);\r
  \r
 +        // bind to Operations Service\r
 +        mOperationsConnection = new ServiceConnection() {\r
 +\r
 +            @Override\r
 +            public void onServiceConnected(ComponentName name, IBinder service) {\r
 +                Log_OC.d(TAG, "Operations service connected");\r
 +                mOperationsBinder = (OperationsServiceBinder) service;\r
 +            }\r
 +\r
 +            @Override\r
 +            public void onServiceDisconnected(ComponentName name) {\r
 +                Log_OC.d(TAG, "Operations service crashed");\r
 +                mOperationsBinder = null;\r
 +            }\r
 +            \r
 +        };\r
 +        if (!bindService(new Intent(this, OperationsService.class), \r
 +                        mOperationsConnection, \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
                  return false;\r
              }\r
          });\r
+         \r
+         mOperationsServiceConnection = new OperationsServiceConnection();\r
+         bindService(new Intent(this, OperationsService.class), mOperationsServiceConnection, Context.BIND_AUTO_CREATE);\r
      }\r
  \r
  \r
          mJustCreated = false;\r
  \r
      }\r
 +    \r
 +    \r
 +    @Override\r
 +    protected void onDestroy() {\r
 +        if (mOperationsConnection != null) {\r
 +            unbindService(mOperationsConnection);\r
 +            mOperationsBinder = null;\r
 +        }\r
 +        super.onDestroy();\r
 +    }\r
  \r
  \r
      /**\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
-         String webdav_path = AccountUtils.getWebdavPath(mDiscoveredVersion, mAuthTokenType);\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 service = new Intent(this, OperationsService.class);\r
+         service.setAction(OperationsService.ACTION_DETECT_AUTHENTICATION_METHOD);\r
+         service.putExtra(OperationsService.EXTRA_SERVER_URL, mHostBaseUrl);\r
+         startService(service);\r
      }\r
  \r
  \r
  \r
      }\r
  \r
+     /** \r
+      * Implements callback methods for service binding. Passed as a parameter to { \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.d(TAG, "Operations service connected");\r
+                 mOperationsServiceBinder = (OperationsServiceBinder) service;\r
+                 mOperationsServiceBinder.addOperationListener(AuthenticatorActivity.this, mHandler);\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.d(TAG, "Operations service disconnected");\r
+                 mOperationsServiceBinder = null;\r
+                 // TODO whatever could be waiting for the service is unbound\r
+             }\r
+         }\r
+     \r
+     }\r
  }\r
@@@ -1,5 -1,5 +1,5 @@@
  /* ownCloud Android client application
 - *   Copyright (C) 2012-2013 ownCloud Inc.
 + *   Copyright (C) 2012-2014 ownCloud Inc.
   *
   *   This program is free software: you can redistribute it and/or modify
   *   it under the terms of the GNU General Public License version 2,
@@@ -32,6 -32,7 +32,7 @@@ import com.owncloud.android.lib.common.
  import com.owncloud.android.lib.resources.shares.ShareType;
  import com.owncloud.android.operations.common.SyncOperation;
  import com.owncloud.android.operations.CreateShareOperation;
+ import com.owncloud.android.operations.DetectAuthenticationMethodOperation;
  import com.owncloud.android.operations.UnshareLinkOperation;
  import com.owncloud.android.utils.Log_OC;
  
@@@ -47,7 -48,6 +48,6 @@@ import android.os.IBinder
  import android.os.Looper;
  import android.os.Message;
  import android.os.Process;
- //import android.support.v4.content.LocalBroadcastManager;
  import android.util.Pair;
  
  public class OperationsService extends Service {
@@@ -62,6 -62,7 +62,7 @@@
      
      public static final String ACTION_CREATE_SHARE = "CREATE_SHARE";
      public static final String ACTION_UNSHARE = "UNSHARE";
+     public static final String ACTION_DETECT_AUTHENTICATION_METHOD = "DETECT_AUTHENTICATION_METHOD";
      
      public static final String ACTION_OPERATION_ADDED = OperationsService.class.getName() + ".OPERATION_ADDED";
      public static final String ACTION_OPERATION_FINISHED = OperationsService.class.getName() + ".OPERATION_FINISHED";
                  if (remotePath.length() > 0) {
                      operation = new UnshareLinkOperation(remotePath, this.getApplicationContext());
                  }
+             } else if (action.equals(ACTION_DETECT_AUTHENTICATION_METHOD)) { // Detect Authentication Method
+                 operation = new DetectAuthenticationMethodOperation(this.getApplicationContext());
+             
              } else {
                  // nothing we are going to handle
                  return START_NOT_STICKY;