瀏覽代碼

OC-2489: Add GetRemoteSharedFilesOperation

masensio 11 年之前
父節點
當前提交
0368e8fa54

+ 4 - 0
oc_framework/src/com/owncloud/android/oc_framework/accounts/OwnCloudAccount.java

@@ -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 supports Share API"
+	     */
+	    public static final String KEY_SUPPORTS_SHARE_API = "oc_supports_share_api";
 	}
 	
 	private String mAuthTokenType;

+ 6 - 0
oc_framework/src/com/owncloud/android/oc_framework/operations/ShareRemoteFile.java

@@ -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;
 
+/**
+ * Contains the data of a Share Remote File from the Share API
+ * 
+ * @author masensio
+ *
+ */
 public class ShareRemoteFile extends RemoteFile {
 
 	/**

+ 93 - 0
oc_framework/src/com/owncloud/android/oc_framework/operations/remote/GetRemoteSharedFilesOperation.java

@@ -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);
+	}
+}

+ 3 - 0
src/com/owncloud/android/authentication/AuthenticatorActivity.java

@@ -245,6 +245,8 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
                 mHostUrlInput.setText(mHostBaseUrl);
                 String userName = mAccount.name.substring(0, mAccount.name.lastIndexOf('@'));
                 mUsernameInput.setText(userName);
+                mIsSharedSupported = Boolean.getBoolean(mAccountMgr.getUserData(mAccount, OwnCloudAccount.Constants.KEY_SUPPORTS_SHARE_API));
+                
             }
             initAuthorizationMethod();  // checks intent and setup.xml to determine mCurrentAuthorizationMethod
             mJustCreated = true;
@@ -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
             mAccountMgr.setUserData(mAccount, OwnCloudAccount.Constants.KEY_OC_VERSION,    mDiscoveredVersion.toString());
             mAccountMgr.setUserData(mAccount, OwnCloudAccount.Constants.KEY_OC_BASE_URL,   mHostBaseUrl);
+            mAccountMgr.setUserData(mAccount, OwnCloudAccount.Constants.KEY_SUPPORTS_SHARE_API, Boolean.toString(mIsSharedSupported));
             if (isSaml) {
                 mAccountMgr.setUserData(mAccount, OwnCloudAccount.Constants.KEY_SUPPORTS_SAML_WEB_SSO, "TRUE"); 
             } else if (isOAuth) {

+ 52 - 0
src/com/owncloud/android/operations/GetSharedFilesOperation.java

@@ -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;
+    }
+
+}

+ 20 - 0
src/com/owncloud/android/ui/activity/FileActivity.java

@@ -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.oc_framework.accounts.OwnCloudAccount;
 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 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
+        AccountManager accountManager = AccountManager.get(this);
         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;
         }
+        setIsSharedSupported( Boolean.getBoolean(accountManager.getUserData(mAccount, OwnCloudAccount.Constants.KEY_SUPPORTS_SHARE_API)));
     }
 
 
@@ -245,6 +253,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.

+ 30 - 2
src/com/owncloud/android/ui/activity/FileDisplayActivity.java

@@ -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.operations.GetSharedFilesOperation;
 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());
-
+                
             /// 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);
+            
+            /// get the shared files
+            if (isSharedSupported()) {
+                startGetSharedFiles();
+            }
+            
             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 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. 
@@ -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++) {