Simplified interface to request 'lost results' during rotation of Activity
authorDavid A. Velasco <dvelasco@solidgear.es>
Mon, 7 Apr 2014 07:24:57 +0000 (09:24 +0200)
committerDavid A. Velasco <dvelasco@solidgear.es>
Mon, 7 Apr 2014 07:24:57 +0000 (09:24 +0200)
src/com/owncloud/android/authentication/AuthenticatorActivity.java
src/com/owncloud/android/services/OperationsService.java

index a9fac0c..ee9d9bc 100644 (file)
@@ -114,12 +114,8 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
     private static final String KEY_PASSWORD_EXPOSED = "PASSWORD_VISIBLE";\r
     private static final String KEY_AUTH_STATUS_TEXT = "AUTH_STATUS_TEXT";\r
     private static final String KEY_AUTH_STATUS_ICON = "AUTH_STATUS_ICON";\r
-    private static final String KEY_REFRESH_BUTTON_ENABLED = "REFRESH_BUTTON_ENABLED";\r
     private static final String KEY_SERVER_AUTH_METHOD = "SERVER_AUTH_METHOD";\r
-    private static final String KEY_GET_SERVER_INFO_OP_ID = "DETECT_AUTH_OP_ID";\r
-    private static final String KEY_EXISTENCE_CHECK_OP_ID = "EXISTENCE_CHECK_OP_ID";\r
-    private static final String KEY_OAUTH2_GET_ACCESS_TOKEN_OP_ID = "OAUTH2_GET_ACCESS_TOKEN";\r
-    private static final String KEY_GET_USER_NAME_OP_ID = "GET_USER_NAME";\r
+    private static final String KEY_WAITING_FOR_OP_ID = "DETECT_AUTH_OP_ID";\r
     private static final String KEY_AUTH_TOKEN = "AUTH_TOKEN";\r
 \r
     private static final String AUTH_ON = "on";\r
@@ -177,11 +173,8 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
     private String mAuthToken = "";\r
 \r
     \r
-    /// Operation-in-progress identifiers - TODO improve pull-interface with OperationsService \r
-    private int mGetServerInfoOpId = -1;\r
-    private int mOauth2GetAccessTokenOpId = -1;\r
-    private int mExistenceCheckOpId = -1;\r
-    private int mGetUserNameOpId = -1;\r
+    /// Identifier of operation in progress which result shouldn't be lost \r
+    private int mWaitingForOpId = -1;\r
     \r
     \r
     /**\r
@@ -218,6 +211,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
             initAuthTokenType();\r
         } else {\r
             mAuthTokenType = savedInstanceState.getString(KEY_AUTH_TOKEN_TYPE);\r
+            mWaitingForOpId = savedInstanceState.getInt(KEY_WAITING_FOR_OP_ID);\r
         }\r
         \r
         /// load user interface\r
@@ -345,9 +339,6 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
             mServerInfo.mAuthMethod = AuthenticationMethod.valueOf(\r
                     savedInstanceState.getString(KEY_SERVER_AUTH_METHOD));\r
             \r
-            // TODO save and recover any operation in progress, in a reasonable way\r
-            mGetServerInfoOpId = savedInstanceState.getInt(KEY_GET_SERVER_INFO_OP_ID);\r
-            \r
         }\r
         \r
         /// step 2 - set properties of UI elements (text, visibility, enabled...)\r
@@ -364,7 +355,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
             findViewById(R.id.hostUrlFrame).setVisibility(View.GONE);\r
             mRefreshButton = findViewById(R.id.centeredRefreshButton);\r
         }\r
-        showRefreshButton(mServerIsChecked && !mServerIsValid && mGetServerInfoOpId == -1);\r
+        showRefreshButton(mServerIsChecked && !mServerIsValid && mWaitingForOpId == -1);\r
         mServerStatusView = (TextView) findViewById(R.id.server_status_text);\r
         showServerStatus();\r
         \r
@@ -452,9 +443,6 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
             mAuthStatusText = savedInstanceState.getInt(KEY_AUTH_STATUS_TEXT);\r
             mAuthStatusIcon = savedInstanceState.getInt(KEY_AUTH_STATUS_ICON);\r
             mAuthToken = savedInstanceState.getString(KEY_AUTH_TOKEN);\r
-            mExistenceCheckOpId = savedInstanceState.getInt(KEY_EXISTENCE_CHECK_OP_ID);\r
-            mOauth2GetAccessTokenOpId = savedInstanceState.getInt(KEY_OAUTH2_GET_ACCESS_TOKEN_OP_ID);\r
-            mGetUserNameOpId = savedInstanceState.getInt(KEY_GET_USER_NAME_OP_ID);\r
         }\r
         \r
         /// step 2 - set properties of UI elements (text, visibility, enabled...)\r
@@ -553,14 +541,12 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
 \r
         /// global state\r
         outState.putString(KEY_AUTH_TOKEN_TYPE, mAuthTokenType);\r
+        outState.putInt(KEY_WAITING_FOR_OP_ID, mWaitingForOpId);\r
 \r
         /// Server PRE-fragment state\r
-        //outState.putBoolean(KEY_REFRESH_BUTTON_ENABLED, (mRefreshButton.getVisibility() == View.VISIBLE));\r
         outState.putInt(KEY_SERVER_STATUS_TEXT, mServerStatusText);\r
         outState.putInt(KEY_SERVER_STATUS_ICON, mServerStatusIcon);\r
         outState.putBoolean(KEY_SERVER_CHECKED, mServerIsChecked);\r
-        //outState.putBoolean(KEY_SERVER_CHECK_IN_PROGRESS, (!mServerIsValid && mServerInfoOperation != null));\r
-        outState.putInt(KEY_GET_SERVER_INFO_OP_ID, mGetServerInfoOpId);\r
         outState.putBoolean(KEY_SERVER_VALID, mServerIsValid);\r
         outState.putBoolean(KEY_IS_SSL_CONN, mServerInfo.mIsSslConn);\r
         outState.putString(KEY_HOST_URL_TEXT, mServerInfo.mBaseUrl);\r
@@ -574,9 +560,6 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         outState.putInt(KEY_AUTH_STATUS_ICON, mAuthStatusIcon);\r
         outState.putInt(KEY_AUTH_STATUS_TEXT, mAuthStatusText);\r
         outState.putString(KEY_AUTH_TOKEN, mAuthToken);\r
-        outState.putInt(KEY_EXISTENCE_CHECK_OP_ID, mExistenceCheckOpId);\r
-        outState.putInt(KEY_OAUTH2_GET_ACCESS_TOKEN_OP_ID, mOauth2GetAccessTokenOpId);\r
-        outState.putInt(KEY_GET_USER_NAME_OP_ID, mGetUserNameOpId);\r
 \r
         Log.wtf(TAG, "onSaveInstanceState end" );\r
     }\r
@@ -679,8 +662,8 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
                 queryParameters);\r
         \r
         if (mOperationsServiceBinder != null) {\r
-            Log.wtf(TAG, "getting access token..." );\r
-            mOauth2GetAccessTokenOpId = mOperationsServiceBinder.newOperation(getServerInfoIntent);\r
+            //Log_OC.wtf(TAG, "getting access token..." );\r
+            mWaitingForOpId = mOperationsServiceBinder.newOperation(getServerInfoIntent);\r
         }\r
     }\r
 \r
@@ -745,8 +728,8 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
             getServerInfoIntent.putExtra(OperationsService.EXTRA_SERVER_URL, uri);\r
             getServerInfoIntent.putExtra(OperationsService.EXTRA_AUTH_TOKEN_TYPE, mAuthTokenType);\r
             if (mOperationsServiceBinder != null) {\r
-                Log.wtf(TAG, "checking server..." );\r
-                mGetServerInfoOpId = mOperationsServiceBinder.newOperation(getServerInfoIntent);\r
+                //Log_OC.wtf(TAG, "checking server..." );\r
+                mWaitingForOpId = mOperationsServiceBinder.newOperation(getServerInfoIntent);\r
             }\r
             \r
         } else {\r
@@ -883,7 +866,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         \r
         if (mOperationsServiceBinder != null) {\r
             Log_OC.wtf(TAG, "starting existenceCheckRemoteOperation..." );\r
-            mExistenceCheckOpId = mOperationsServiceBinder.newOperation(existenceCheckIntent);\r
+            mWaitingForOpId = mOperationsServiceBinder.newOperation(existenceCheckIntent);\r
         }\r
     }\r
 \r
@@ -946,7 +929,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
     public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {\r
 \r
         if (operation instanceof GetServerInfoOperation) {\r
-            if (operation.hashCode() == mGetServerInfoOpId) {\r
+            if (operation.hashCode() == mWaitingForOpId) {\r
                 onGetServerInfoFinish(result);\r
             }   // else nothing ; only the last check operation is considered; \r
                 // multiple can be started if the user amends a URL quickly\r
@@ -969,7 +952,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
     }\r
 \r
     private void onGetUserNameFinish(RemoteOperationResult result) {\r
-        mGetUserNameOpId = -1;\r
+        mWaitingForOpId = -1;\r
         if (result.isSuccess()) {\r
             boolean success = false;\r
             String username = (String) result.getData().get(0);\r
@@ -1002,7 +985,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
     }\r
 \r
     private void onSamlBasedFederatedSingleSignOnAuthorizationStart(RemoteOperationResult result) {\r
-        mExistenceCheckOpId = -1;\r
+        mWaitingForOpId = -1;\r
         dismissDialog(WAIT_DIALOG_TAG);\r
 
         //if (result.isTemporalRedirection() && result.isIdPRedirection()) {\r
@@ -1037,7 +1020,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
     private void onGetServerInfoFinish(RemoteOperationResult result) {\r
         /// update activity state\r
         mServerIsChecked = true;\r
-        mGetServerInfoOpId = -1;\r
+        mWaitingForOpId = -1;\r
         \r
         // update server status, but don't show it yet\r
         updateServerStatusIconAndText(result);\r
@@ -1304,7 +1287,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
      * @param result        Result of the operation.\r
      */\r
     private void onGetOAuthAccessTokenFinish(RemoteOperationResult result) {\r
-        mOauth2GetAccessTokenOpId = -1;\r
+        mWaitingForOpId = -1;\r
         dismissDialog(WAIT_DIALOG_TAG);\r
 \r
         String webdav_path = AccountUtils.getWebdavPath(mServerInfo.mVersion, mAuthTokenType);\r
@@ -1343,9 +1326,10 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
      * @param result        Result of the operation.\r
      */\r
     private void onAuthorizationCheckFinish(RemoteOperationResult result) {\r
-        mExistenceCheckOpId = -1;\r
+        mWaitingForOpId = -1;\r
         dismissDialog(WAIT_DIALOG_TAG);\r
 \r
+        result = new RemoteOperationResult(new RuntimeException("FAKE"));\r
         if (result.isSuccess()) {\r
             Log_OC.d(TAG, "Successful access - time to save the account");\r
 \r
@@ -1674,8 +1658,8 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         getUserNameIntent.putExtra(OperationsService.EXTRA_FOLLOW_REDIRECTS, followRedirects);\r
         \r
         if (mOperationsServiceBinder != null) {\r
-            Log_OC.wtf(TAG, "starting getRemoteUserNameOperation..." );\r
-            mGetUserNameOpId = mOperationsServiceBinder.newOperation(getUserNameIntent);\r
+            //Log_OC.wtf(TAG, "starting getRemoteUserNameOperation..." );\r
+            mWaitingForOpId = mOperationsServiceBinder.newOperation(getUserNameIntent);\r
         }\r
     }\r
 \r
@@ -1771,49 +1755,9 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
     private void doOnResumeAndBound() {\r
         Log.wtf(TAG, "registering to listen for operation callbacks" );\r
         mOperationsServiceBinder.addOperationListener(AuthenticatorActivity.this, mHandler);\r
-        \r
-        \r
-        \r
-        if (mGetServerInfoOpId != -1) {\r
-            RemoteOperationResult result = \r
-                    mOperationsServiceBinder.getOperationResultIfFinished(mGetServerInfoOpId);\r
-            if (result != null) {\r
-                Log_OC.wtf(TAG, "found result of operation finished while rotating");\r
-                onGetServerInfoFinish(result);\r
-            }\r
-            \r
-        } else if (mOauth2GetAccessTokenOpId != -1) {\r
-            RemoteOperationResult result = \r
-                    mOperationsServiceBinder.getOperationResultIfFinished(\r
-                            mOauth2GetAccessTokenOpId);\r
-            if (result != null) {\r
-                Log_OC.wtf(TAG, "found result of operation finished while rotating");\r
-                onGetOAuthAccessTokenFinish(result);\r
-            }\r
-            \r
-        } else if (mExistenceCheckOpId != -1) {\r
-            RemoteOperationResult result = \r
-                    mOperationsServiceBinder.getOperationResultIfFinished(mExistenceCheckOpId);\r
-            if (result != null) {\r
-                Log_OC.wtf(TAG, "found result of operation finished while rotating");\r
-                if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(\r
-                        MainApp.getAccountType()).equals(mAuthTokenType)) {\r
-                    onSamlBasedFederatedSingleSignOnAuthorizationStart(result);\r
-\r
-                } else {\r
-                    onAuthorizationCheckFinish(result);\r
-                }\r
-            }\r
-        }if (mGetUserNameOpId != -1) {\r
-            RemoteOperationResult result = \r
-                    mOperationsServiceBinder.getOperationResultIfFinished(mGetUserNameOpId);\r
-            if (result != null) {\r
-                Log_OC.wtf(TAG, "found result of operation finished while rotating");\r
-                onGetUserNameFinish(result);\r
-            }\r
-            \r
-        } \r
-        \r
+        if (mWaitingForOpId != -1) {\r
+            mOperationsServiceBinder.dispatchResultIfFinished(mWaitingForOpId, this);\r
+        }\r
     }\r
 \r
     \r
index a4de86a..a9f92ed 100644 (file)
@@ -52,7 +52,6 @@ import android.os.IBinder;
 import android.os.Looper;
 import android.os.Message;
 import android.os.Process;
-import android.util.Log;
 import android.util.Pair;
 
 public class OperationsService extends Service {
@@ -89,8 +88,14 @@ public class OperationsService extends Service {
     private ConcurrentLinkedQueue<Pair<Target, RemoteOperation>> mPendingOperations = 
             new ConcurrentLinkedQueue<Pair<Target, RemoteOperation>>();
     
+    /*
     private ConcurrentMap<Integer, RemoteOperationResult> mOperationResults =
             new ConcurrentHashMap<Integer, RemoteOperationResult>();
+     */
+
+    private ConcurrentMap<Integer, Pair<RemoteOperation, RemoteOperationResult>> 
+        mUndispatchedFinishedOperations =
+            new ConcurrentHashMap<Integer, Pair<RemoteOperation, RemoteOperationResult>>();
     
     private static class Target {
         public Uri mServerUrl = null;
@@ -149,21 +154,21 @@ public class OperationsService extends Service {
      */
     @Override
     public int onStartCommand(Intent intent, int flags, int startId) {
-        Log.wtf(TAG, "onStartCommand init" );
+        Log_OC.wtf(TAG, "onStartCommand init" );
         Message msg = mServiceHandler.obtainMessage();
         msg.arg1 = startId;
         mServiceHandler.sendMessage(msg);
-        Log.wtf(TAG, "onStartCommand end" );
+        Log_OC.wtf(TAG, "onStartCommand end" );
         return START_NOT_STICKY;
     }
 
     @Override
     public void onDestroy() {
-        Log.wtf(TAG, "onDestroy init" );
+        Log_OC.wtf(TAG, "onDestroy init" );
         super.onDestroy();
-        Log.wtf(TAG, "Clear mOperationResults" );
-        mOperationResults.clear();
-        Log.wtf(TAG, "onDestroy end" );
+        Log_OC.wtf(TAG, "Clear mUndispatchedFinisiedOperations" );
+        mUndispatchedFinishedOperations.clear();
+        Log_OC.wtf(TAG, "onDestroy end" );
     }
 
 
@@ -173,7 +178,7 @@ public class OperationsService extends Service {
      */
     @Override
     public IBinder onBind(Intent intent) {
-        Log.wtf(TAG, "onBind" );
+        Log_OC.wtf(TAG, "onBind" );
         return mBinder;
     }
 
@@ -341,16 +346,34 @@ public class OperationsService extends Service {
             if (operation != null) {
                 mPendingOperations.add(new Pair<Target , RemoteOperation>(target, operation));
                 startService(new Intent(OperationsService.this, OperationsService.class));
+                Log_OC.wtf(TAG, "New operation added, opId: " + operation.hashCode());
                 return operation.hashCode();
                 
             } else {
+                Log_OC.wtf(TAG, "New operation failed, returned -1");
                 return -1;
             }
         }
 
         public RemoteOperationResult getOperationResultIfFinished(int operationId) {
-            Log_OC.wtf(TAG, "Searching result for operation with id " + operationId);
-            return mOperationResults.remove(operationId);
+            Pair<RemoteOperation, RemoteOperationResult> undispatched = 
+                    mUndispatchedFinishedOperations.remove(operationId);
+            if (undispatched != null) {
+                return undispatched.second;
+            }
+            return null;
+        }
+
+
+        public void dispatchResultIfFinished(int operationId, OnRemoteOperationListener listener) {
+            Pair<RemoteOperation, RemoteOperationResult> undispatched = 
+                    mUndispatchedFinishedOperations.remove(operationId);
+            if (undispatched != null) {
+                listener.onRemoteOperationFinish(undispatched.first, undispatched.second);
+                Log_OC.wtf(TAG, "Sending callback later");
+            } else {
+                Log_OC.wtf(TAG, "Not finished yet");
+            }
         }
 
     }
@@ -385,7 +408,7 @@ public class OperationsService extends Service {
      */
     private void nextOperation() {
         
-        Log.wtf(TAG, "nextOperation init" );
+        Log_OC.wtf(TAG, "nextOperation init" );
         
         Pair<Target, RemoteOperation> next = null;
         synchronized(mPendingOperations) {
@@ -457,7 +480,7 @@ public class OperationsService extends Service {
             }
             
             //sendBroadcastOperationFinished(mLastTarget, mCurrentOperation, result);
-            dispatchOperationListeners(mLastTarget, mCurrentOperation, result);
+            dispatchResultToOperationListeners(mLastTarget, mCurrentOperation, result);
         }
     }
 
@@ -515,7 +538,7 @@ public class OperationsService extends Service {
      * @param operation         Finished operation.
      * @param result            Result of the operation.
      */
-    private void dispatchOperationListeners(
+    private void dispatchResultToOperationListeners(
             Target target, final RemoteOperation operation, final RemoteOperationResult result) {
         int count = 0;
         Iterator<OnRemoteOperationListener> listeners = mBinder.mBoundListeners.keySet().iterator();
@@ -533,7 +556,10 @@ public class OperationsService extends Service {
             }
         }
         if (count == 0) {
-            mOperationResults.put(operation.hashCode(), result);
+            //mOperationResults.put(operation.hashCode(), result);
+            Pair<RemoteOperation, RemoteOperationResult> undispatched = 
+                    new Pair<RemoteOperation, RemoteOperationResult>(operation, result);
+            mUndispatchedFinishedOperations.put(operation.hashCode(), undispatched);
         }
         Log_OC.d(TAG, "Called " + count + " listeners");
     }