OC-2489: Add GetRemoteSharedFilesOperation
authormasensio <masensio@solidgear.es>
Tue, 21 Jan 2014 13:12:46 +0000 (14:12 +0100)
committermasensio <masensio@solidgear.es>
Tue, 21 Jan 2014 13:12:46 +0000 (14:12 +0100)
oc_framework/src/com/owncloud/android/oc_framework/accounts/OwnCloudAccount.java
oc_framework/src/com/owncloud/android/oc_framework/operations/ShareRemoteFile.java
oc_framework/src/com/owncloud/android/oc_framework/operations/remote/GetRemoteSharedFilesOperation.java [new file with mode: 0644]
src/com/owncloud/android/authentication/AuthenticatorActivity.java
src/com/owncloud/android/operations/GetSharedFilesOperation.java [new file with mode: 0644]
src/com/owncloud/android/ui/activity/FileActivity.java
src/com/owncloud/android/ui/activity/FileDisplayActivity.java

index 73fdb65..5c08457 100644 (file)
@@ -58,6 +58,10 @@ public class OwnCloudAccount extends Account {
             * Flag signaling if the ownCloud server can be accessed with session cookies from SAML-based web single-sign-on.
             */
            public static final String KEY_SUPPORTS_SAML_WEB_SSO = "oc_supports_saml_web_sso";
             * Flag signaling if the ownCloud server can be accessed with session cookies from SAML-based web single-sign-on.
             */
            public static final String KEY_SUPPORTS_SAML_WEB_SSO = "oc_supports_saml_web_sso";
+           /**
+            * Flag signaling if the ownCloud server supports Share API"
+            */
+           public static final String KEY_SUPPORTS_SHARE_API = "oc_supports_share_api";
        }
        
        private String mAuthTokenType;
        }
        
        private String mAuthTokenType;
index f599028..a292310 100644 (file)
@@ -31,6 +31,12 @@ import android.util.Log;
 import com.owncloud.android.oc_framework.network.webdav.WebdavEntry;
 import com.owncloud.android.oc_framework.utils.FileUtils;
 
 import com.owncloud.android.oc_framework.network.webdav.WebdavEntry;
 import com.owncloud.android.oc_framework.utils.FileUtils;
 
+/**
+ * Contains the data of a Share Remote File from the Share API
+ * 
+ * @author masensio
+ *
+ */
 public class ShareRemoteFile extends RemoteFile {
 
        /**
 public class ShareRemoteFile extends RemoteFile {
 
        /**
diff --git a/oc_framework/src/com/owncloud/android/oc_framework/operations/remote/GetRemoteSharedFilesOperation.java b/oc_framework/src/com/owncloud/android/oc_framework/operations/remote/GetRemoteSharedFilesOperation.java
new file mode 100644 (file)
index 0000000..4140255
--- /dev/null
@@ -0,0 +1,93 @@
+/* ownCloud Android Library is available under MIT license
+ *   Copyright (C) 2014 ownCloud (http://www.owncloud.org/)
+ *   
+ *   Permission is hereby granted, free of charge, to any person obtaining a copy
+ *   of this software and associated documentation files (the "Software"), to deal
+ *   in the Software without restriction, including without limitation the rights
+ *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ *   copies of the Software, and to permit persons to whom the Software is
+ *   furnished to do so, subject to the following conditions:
+ *   
+ *   The above copyright notice and this permission notice shall be included in
+ *   all copies or substantial portions of the Software.
+ *   
+ *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
+ *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
+ *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 
+ *   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 
+ *   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
+ *   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ *   THE SOFTWARE.
+ *
+ */
+
+package com.owncloud.android.oc_framework.operations.remote;
+
+import java.io.IOException;
+import java.util.ArrayList;
+
+import org.apache.commons.httpclient.HttpException;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.http.HttpStatus;
+
+import android.util.Log;
+
+import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
+import com.owncloud.android.oc_framework.operations.RemoteOperation;
+import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
+import com.owncloud.android.oc_framework.operations.ShareRemoteFile;
+
+/** 
+ * Get the data from the server to know shared files/folders
+ * 
+ * @author masensio
+ *
+ */
+
+public class GetRemoteSharedFilesOperation extends RemoteOperation {
+
+       private static final String TAG = GetRemoteSharedFilesOperation.class.getSimpleName();
+
+       // OCS Route
+       private static final String SHAREAPI_ROUTE ="/ocs/v1.php/apps/files/files_sharing/api/v1/shares"; 
+
+       private ArrayList<ShareRemoteFile> mSharedFiles;  // List of files for result
+
+       public GetRemoteSharedFilesOperation() {
+               // TODO Auto-generated constructor stub
+       }
+
+       @Override
+       protected RemoteOperationResult run(WebdavClient client) {
+               RemoteOperationResult result = null;
+               int status = -1;
+
+               // Get Method
+               GetMethod get = new GetMethod(client.getBaseUri() + SHAREAPI_ROUTE);
+               Log.d(TAG, "URL ------> " + client.getBaseUri() + SHAREAPI_ROUTE);
+
+               // Get the response
+               try{
+                       status = client.executeMethod(get);
+                       if(isSuccess(status)) {
+                               Log.d(TAG, "Obtain RESPONSE");
+                               String response = get.getResponseBodyAsString();
+                               Log.d(TAG, response);
+                       }
+               } catch (HttpException e) {
+                       result = new RemoteOperationResult(e);
+                       e.printStackTrace();
+               } catch (IOException e) {
+                       result = new RemoteOperationResult(e);
+                       e.printStackTrace();
+               } finally {
+                       get.releaseConnection();
+               }
+               return result;
+       }
+
+       private boolean isSuccess(int status) {
+               return (status == HttpStatus.SC_OK);
+       }
+}
index 25c338b..2fcb5c8 100644 (file)
@@ -245,6 +245,8 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
                 mHostUrlInput.setText(mHostBaseUrl);\r
                 String userName = mAccount.name.substring(0, mAccount.name.lastIndexOf('@'));\r
                 mUsernameInput.setText(userName);\r
                 mHostUrlInput.setText(mHostBaseUrl);\r
                 String userName = mAccount.name.substring(0, mAccount.name.lastIndexOf('@'));\r
                 mUsernameInput.setText(userName);\r
+                mIsSharedSupported = Boolean.getBoolean(mAccountMgr.getUserData(mAccount, OwnCloudAccount.Constants.KEY_SUPPORTS_SHARE_API));\r
+                \r
             }\r
             initAuthorizationMethod();  // checks intent and setup.xml to determine mCurrentAuthorizationMethod\r
             mJustCreated = true;\r
             }\r
             initAuthorizationMethod();  // checks intent and setup.xml to determine mCurrentAuthorizationMethod\r
             mJustCreated = true;\r
@@ -1292,6 +1294,7 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
             /// add user data to the new account; TODO probably can be done in the last parameter addAccountExplicitly, or in KEY_USERDATA\r
             mAccountMgr.setUserData(mAccount, OwnCloudAccount.Constants.KEY_OC_VERSION,    mDiscoveredVersion.toString());\r
             mAccountMgr.setUserData(mAccount, OwnCloudAccount.Constants.KEY_OC_BASE_URL,   mHostBaseUrl);\r
             /// add user data to the new account; TODO probably can be done in the last parameter addAccountExplicitly, or in KEY_USERDATA\r
             mAccountMgr.setUserData(mAccount, OwnCloudAccount.Constants.KEY_OC_VERSION,    mDiscoveredVersion.toString());\r
             mAccountMgr.setUserData(mAccount, OwnCloudAccount.Constants.KEY_OC_BASE_URL,   mHostBaseUrl);\r
+            mAccountMgr.setUserData(mAccount, OwnCloudAccount.Constants.KEY_SUPPORTS_SHARE_API, Boolean.toString(mIsSharedSupported));\r
             if (isSaml) {\r
                 mAccountMgr.setUserData(mAccount, OwnCloudAccount.Constants.KEY_SUPPORTS_SAML_WEB_SSO, "TRUE"); \r
             } else if (isOAuth) {\r
             if (isSaml) {\r
                 mAccountMgr.setUserData(mAccount, OwnCloudAccount.Constants.KEY_SUPPORTS_SAML_WEB_SSO, "TRUE"); \r
             } else if (isOAuth) {\r
diff --git a/src/com/owncloud/android/operations/GetSharedFilesOperation.java b/src/com/owncloud/android/operations/GetSharedFilesOperation.java
new file mode 100644 (file)
index 0000000..426da31
--- /dev/null
@@ -0,0 +1,52 @@
+/* ownCloud Android client application
+ *   Copyright (C) 2012-2013 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,
+ *   as published by the Free Software Foundation.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+package com.owncloud.android.operations;
+
+import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
+import com.owncloud.android.oc_framework.operations.RemoteOperation;
+import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
+import com.owncloud.android.oc_framework.operations.remote.GetRemoteSharedFilesOperation;
+
+/**
+ * Access to remote operation to get the share files/folders
+ * Save the data in Database
+ * 
+ * @author masensio
+ */
+
+public class GetSharedFilesOperation extends RemoteOperation {
+
+    public GetSharedFilesOperation() {
+        // TODO Auto-generated constructor stub
+    }
+
+    @Override
+    protected RemoteOperationResult run(WebdavClient client) {
+        GetRemoteSharedFilesOperation operation = new GetRemoteSharedFilesOperation();
+        RemoteOperationResult result = operation.execute(client);
+        
+        if (result.isSuccess()) {
+            
+        } else {
+            
+        }
+        
+        return null;
+    }
+
+}
index 937d45d..31e3e86 100644 (file)
@@ -33,6 +33,7 @@ import com.owncloud.android.MainApp;
 import com.owncloud.android.R;
 import com.owncloud.android.authentication.AccountUtils;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.R;
 import com.owncloud.android.authentication.AccountUtils;
 import com.owncloud.android.datamodel.OCFile;
+import com.owncloud.android.oc_framework.accounts.OwnCloudAccount;
 import com.owncloud.android.oc_framework.network.webdav.WebdavUtils;
 import com.owncloud.android.utils.Log_OC;
 
 import com.owncloud.android.oc_framework.network.webdav.WebdavUtils;
 import com.owncloud.android.utils.Log_OC;
 
@@ -69,6 +70,11 @@ public abstract class FileActivity extends SherlockFragmentActivity {
     
     /** Flag to signal if the activity is launched by a notification */
     private boolean mFromNotification;
     
     /** Flag to signal if the activity is launched by a notification */
     private boolean mFromNotification;
+    
+    /** Flag to signal if the server supports the Share API */
+    private boolean mIsSharedSupported;
+    
+    
 
     
     /**
 
     
     /**
@@ -158,6 +164,7 @@ public abstract class FileActivity extends SherlockFragmentActivity {
      */
     private void swapToDefaultAccount() {
         // default to the most recently used account
      */
     private void swapToDefaultAccount() {
         // default to the most recently used account
+        AccountManager accountManager = AccountManager.get(this);
         Account newAccount  = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
         if (newAccount == null) {
             /// no account available: force account creation
         Account newAccount  = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
         if (newAccount == null) {
             /// no account available: force account creation
@@ -171,6 +178,7 @@ public abstract class FileActivity extends SherlockFragmentActivity {
             mAccountWasRestored = (newAccount.equals(mAccount));
             mAccount = newAccount;
         }
             mAccountWasRestored = (newAccount.equals(mAccount));
             mAccount = newAccount;
         }
+        setIsSharedSupported( Boolean.getBoolean(accountManager.getUserData(mAccount, OwnCloudAccount.Constants.KEY_SUPPORTS_SHARE_API)));
     }
 
 
     }
 
 
@@ -246,6 +254,18 @@ public abstract class FileActivity extends SherlockFragmentActivity {
     
     
     /**
     
     
     /**
+     *  @return 'True' if the server supports the Share API
+     */
+    public boolean isSharedSupported() {
+        return mIsSharedSupported;
+    }
+
+
+    public void setIsSharedSupported(boolean mIsSharedSupported) {
+        this.mIsSharedSupported = mIsSharedSupported;
+    }
+    
+    /**
      * Helper class handling a callback from the {@link AccountManager} after the creation of
      * a new ownCloud {@link Account} finished, successfully or not.
      * 
      * Helper class handling a callback from the {@link AccountManager} after the creation of
      * a new ownCloud {@link Account} finished, successfully or not.
      * 
index 04f525e..4135ba3 100644 (file)
@@ -72,6 +72,7 @@ import com.owncloud.android.oc_framework.operations.OnRemoteOperationListener;
 import com.owncloud.android.oc_framework.operations.RemoteOperation;
 import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
 import com.owncloud.android.oc_framework.operations.RemoteOperationResult.ResultCode;
 import com.owncloud.android.oc_framework.operations.RemoteOperation;
 import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
 import com.owncloud.android.oc_framework.operations.RemoteOperationResult.ResultCode;
+import com.owncloud.android.operations.GetSharedFilesOperation;
 import com.owncloud.android.operations.RemoveFileOperation;
 import com.owncloud.android.operations.RenameFileOperation;
 import com.owncloud.android.operations.SynchronizeFileOperation;
 import com.owncloud.android.operations.RemoveFileOperation;
 import com.owncloud.android.operations.RenameFileOperation;
 import com.owncloud.android.operations.SynchronizeFileOperation;
@@ -223,7 +224,7 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa
     protected void onAccountSet(boolean stateWasRecovered) {
         if (getAccount() != null) {
             mStorageManager = new FileDataStorageManager(getAccount(), getContentResolver());
     protected void onAccountSet(boolean stateWasRecovered) {
         if (getAccount() != null) {
             mStorageManager = new FileDataStorageManager(getAccount(), getContentResolver());
-
+                
             /// Check whether the 'main' OCFile handled by the Activity is contained in the current Account
             OCFile file = getFile();
             // get parent from path
             /// Check whether the 'main' OCFile handled by the Activity is contained in the current Account
             OCFile file = getFile();
             // get parent from path
@@ -245,6 +246,12 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa
             }
             setFile(file);
             setNavigationListWithFolder(file);
             }
             setFile(file);
             setNavigationListWithFolder(file);
+            
+            /// get the shared files
+            if (isSharedSupported()) {
+                startGetSharedFiles();
+            }
+            
             if (!stateWasRecovered) {
                 Log_OC.e(TAG, "Initializing Fragments in onAccountChanged..");
                 initFragmentsWithFile();
             if (!stateWasRecovered) {
                 Log_OC.e(TAG, "Initializing Fragments in onAccountChanged..");
                 initFragmentsWithFile();
@@ -1278,10 +1285,24 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa
         } else if (operation instanceof CreateFolderOperation) {
             onCreateFolderOperationFinish((CreateFolderOperation)operation, result);
             
         } else if (operation instanceof CreateFolderOperation) {
             onCreateFolderOperationFinish((CreateFolderOperation)operation, result);
             
-        } 
+        } else if (operation instanceof GetSharedFilesOperation) {
+            onGetSharedFilesOperationFinish((GetSharedFilesOperation) operation, result);
+        }
     }
 
 
     }
 
 
+    /** Updates the data about shared files
+     * 
+     * @param operation     Get Shared Files
+     * @param result        Result of the operation
+     */
+    private void onGetSharedFilesOperationFinish(GetSharedFilesOperation operation, RemoteOperationResult result) {
+        // TODO
+        // Refresh the filelist with the information
+        refeshListOfFilesFragment();
+        
+    }
+
     /**
      * Updates the view associated to the activity after the finish of an operation trying to remove a 
      * file. 
     /**
      * Updates the view associated to the activity after the finish of an operation trying to remove a 
      * file. 
@@ -1491,6 +1512,13 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa
     }
 
     
     }
 
     
+    private void startGetSharedFiles() {
+        // Get shared files/folders
+        RemoteOperation getSharedFiles = new GetSharedFilesOperation();
+        getSharedFiles.execute(getAccount(), this, null, null, this);
+        
+    }
+    
 //    public void enableDisableViewGroup(ViewGroup viewGroup, boolean enabled) {
 //        int childCount = viewGroup.getChildCount();
 //        for (int i = 0; i < childCount; i++) {
 //    public void enableDisableViewGroup(ViewGroup viewGroup, boolean enabled) {
 //        int childCount = viewGroup.getChildCount();
 //        for (int i = 0; i < childCount; i++) {