1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
|
Examples
========
Init the library
----------------
Start using the library; it is needed to init the object mClient that will be
in charge of keeping the communication with the server.
Code example
~~~~~~~~~~~~
.. code-block:: java
public class MainActivity extends Activity
implements OnRemoteOperationListener,
OnDatatransferProgressListener {
private OwnCloudClient mClient;
private Handler mHandler = new Handler();
...
public void onCreate(Bundle savedInstanceState) {
...
// Parse URI to the base URL of the ownCloud server
Uri serverUri = Uri.parse(getString(R.string.server_base_url));
// Create client object to perform remote operations
mClient = OwnCloudClientFactory.createOwnCloudClient(
serverUri,
this,
// Activity or Service context
true);
Set credentials
---------------
Authentication on the app is possible by 3 different methods:
* Basic authentication, user name and password
* Bearer access token (oAuth2)
* Cookie (SAML-based single-sign-on)
Code example
~~~~~~~~~~~~
.. code-block:: java
package com.owncloud.android.lib.common;
public class OwnCloudClient extends HttpClient {
...
// Set basic credentials
client.setCredentials(
OwnCloudCredentialsFactory.newBasicCredentials(username, password)
);
// Set bearer access token
client.setCredentials(
OwnCloudCredentialsFactory.newBearerCredentials(accessToken)
);
// Set SAML2 session token
client.setCredentials(
OwnCloudCredentialsFactory.newSamlSsoCredentials(cookie)
);
}
Create a folder
---------------
Create a new folder on the cloud server, the info needed to be sent is the path
of the new folder.
Code example
~~~~~~~~~~~~
.. code-block:: java
private void startFolderCreation(String newFolderPath) {
CreateRemoteFolderOperation createOperation = new CreateRemoteFolderOperation(newFolderPath, false);
createOperation.execute( mClient , this , mHandler);
}
@Override
public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
if (operation instanceof CreateRemoteFolderOperation) {
if (result.isSuccess()) {
// do your stuff here
}
}
…
}
Read folder
-----------
Get the content of an existing folder on the cloud server, the info needed to
be sent is the path of the folder, in the example shown it has been asked the
content of the root folder. As answer of this method, it will be received an
array with all the files and folders stored in the selected folder.
Code example
~~~~~~~~~~~~
.. code-block:: java
private void startReadRootFolder() {
ReadRemoteFolderOperation refreshOperation = new ReadRemoteFolderOperation(FileUtils.PATH_SEPARATOR);
// root folder
refreshOperation.execute(mClient, this, mHandler);
}
@Override
public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
if (operation instanceof ReadRemoteFolderOperation) {
if (result.isSuccess()) {
List< RemoteFile > files = result.getData();
// do your stuff here
}
}
…
}
Read file
---------
Get information related to a certain file or folder, information obtained is:
``filePath``, ``filename``, ``isDirectory``, ``size`` and ``date``.
Code example
~~~~~~~~~~~~
.. code-block:: java
private void startReadFileProperties(String filePath) {
ReadRemoteFileOperation readOperation = new ReadRemoteFileOperation(filePath);
readOperation.execute(mClient, this, mHandler);
}
@Override
public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
if (operation instanceof ReadRemoteFileOperation) {
if (result.isSuccess()) {
RemoteFile file = result.getData()[0];
// do your stuff here
}
}
…
}
Delete file or folder
---------------------
Delete a file or folder on the cloud server. The info needed is the path of
folder/file to be deleted.
Code example
~~~~~~~~~~~~
.. code-block:: java
private void startRemoveFile(String filePath) {
RemoveRemoteFileOperation removeOperation = new RemoveRemoteFileOperation(remotePath);
removeOperation.execute( mClient , this , mHandler);
}
@Override
public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
if (operation instanceof RemoveRemoteFileOperation) {
if (result.isSuccess()) {
// do your stuff here
}
}
…
}
Download a file
---------------
Download an existing file on the cloud server. The info needed is path of the
file on the server and targetDirectory, path where the file will be stored on
the device.
Code example
~~~~~~~~~~~~
.. code-block:: java
private void startDownload(String filePath, File targetDirectory) {
DownloadRemoteFileOperation downloadOperation = new DownloadRemoteFileOperation(filePath, targetDirectory.getAbsolutePath());
downloadOperation.addDatatransferProgressListener(this);
downloadOperation.execute( mClient, this, mHandler);
}
@Override
public void onRemoteOperationFinish( RemoteOperation operation, RemoteOperationResult result) {
if (operation instanceof DownloadRemoteFileOperation) {
if (result.isSuccess()) {
// do your stuff here
}
}
}
@Override
public void onTransferProgress( long progressRate, long totalTransferredSoFar, long totalToTransfer, String fileName) {
mHandler.post( new Runnable() {
@Override
public void run() {
// do your UI updates about progress here
}
});
}
Upload a file
-------------
Upload a new file to the cloud server. The info needed is fileToUpload, path
where the file is stored on the device, remotePath, path where the file will be
stored on the server and mimeType.
Code example
~~~~~~~~~~~~
.. code-block:: java
private void startUpload (File fileToUpload, String remotePath, String mimeType) {
UploadRemoteFileOperation uploadOperation = new UploadRemoteFileOperation( fileToUpload.getAbsolutePath(), remotePath, mimeType);
uploadOperation.addDatatransferProgressListener(this);
uploadOperation.execute(mClient, this, mHandler);
}
@Override
public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
if (operation instanceof UploadRemoteFileOperation) {
if (result.isSuccess()) {
// do your stuff here
}
}
}
@Override
public void onTransferProgress(long progressRate, long totalTransferredSoFar, long totalToTransfer, String fileName) {
mHandler.post( new Runnable() {
@Override
public void run() {
// do your UI updates about progress here
}
});
}
Move a file or folder
---------------------
Move an exisintg file or folder to a different location in the ownCloud server. Parameters needed are the path
to the file or folder to move, and the new path desired for it. The parent folder of the new path must exist in
the server.
When the parameter 'overwrite' is set to 'true', the file or folder is moved even if the new path is already
used by a different file or folder. This one will be replaced by the former.
Code example
~~~~~~~~~~~~
.. code-block:: java
private void startFileMove(String filePath, String newFilePath, boolean overwrite) {
MoveRemoteFileOperation moveOperation = new MoveRemoteFileOperation(filePath, newFilePath, overwrite);
moveOperation.execute( mClient , this , mHandler);
}
@Override
public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
if (operation instanceof MoveRemoteFileOperation) {
if (result.isSuccess()) {
// do your stuff here
}
}
…
}
Read shared items by link
-------------------------
Get information about what files and folder are shared by link (the object
mClient contains the information about the server url and account)
Code example
~~~~~~~~~~~~
.. code-block:: java
private void startAllSharesRetrieval() {
GetRemoteSharesOperation getSharesOp = new GetRemoteSharesOperation();
getSharesOp.execute( mClient , this , mHandler);
}
@Override
public void onRemoteOperationFinish( RemoteOperation operation, RemoteOperationResult result) {
if (operation instanceof GetRemoteSharesOperation) {
if (result.isSuccess()) {
ArrayList< OCShare > shares = new ArrayList< OCShare >();
for (Object obj: result.getData()) {
shares.add(( OCShare) obj);
}
// do your stuff here
}
}
}
Get the share resources for a given file or folder
--------------------------------------------------
Get information about what files and folder are shared by link on a certain
folder. The info needed is filePath, path of the file/folder on the server, the
Boolean variable, getReshares, come from the Sharing api, from the moment it is
not in use within the ownCloud Android library.
Code example
~~~~~~~~~~~~
.. code-block:: java
private void startSharesRetrievalForFileOrFolder(String filePath, boolean getReshares) {
GeteRemoteSharesForFileOperation operation = new GetRemoteSharesForFileOperation(filePath, getReshares, false);
operation.execute( mClient, this, mHandler);
}
private void startSharesRetrievalForFilesInFolder(String folderPath, boolean getReshares) {
GetRemoteSharesForFileOperation operation = new GetRemoteSharesForFileOperation(folderPath, getReshares, true);
operation.execute( mClient, this, mHandler);
}
@Override
public void onRemoteOperationFinish( RemoteOperation operation, RemoteOperationResult result) {
if (operation instanceof GetRemoteSharesForFileOperation) {
if (result.isSuccess()) {
ArrayList< OCShare > shares = new ArrayList< OCShare >();
for (Object obj: result.getData()) {
shares.add(( OCShare) obj);
}
// do your stuff here
}
}
}
Share link of file or folder
-----------------------------
Share a file or a folder from your cloud server by link.
The info needed is filePath, the path of the item that you want to share and
Password, this comes from the Sharing api, from the moment it is not in use
within the ownCloud Android library.
Code example
~~~~~~~~~~~~
.. code-block:: java
private void startCreationOfPublicShareForFile(String filePath, String password) {
CreateRemoteShareOperation operation = new CreateRemoteShareOperation(filePath, ShareType.PUBLIC_LINK, "", false, password, 1);
operation.execute( mClient , this , mHandler);
}
private void startCreationOfGroupShareForFile(String filePath, String groupId) {
CreateRemoteShareOperation operation = new CreateRemoteShareOperation(filePath, ShareType.GROUP, groupId, false , "", 31);
operation.execute(mClient, this, mHandler);
}
private void startCreationOfUserShareForFile(String filePath, String userId) {
CreateRemoteShareOperation operation = new CreateRemoteShareOperation(filePath, ShareType.USER, userId, false, "", 31);
operation.execute(mClient, this, mHandler);
}
@Override
public void onRemoteOperationFinish( RemoteOperation operation, RemoteOperationResult result) {
if (operation instanceof CreateRemoteShareOperation) {
if (result.isSuccess()) {
OCShare share = (OCShare) result.getData ().get(0);
// do your stuff here
}
}
}
Delete a share resource
-----------------------
Stop sharing by link a file or a folder from your cloud server.
The info needed is the object OCShare that you want to stop sharing by link.
Code example
~~~~~~~~~~~~
.. code-block:: java
private void startShareRemoval(OCShare share) {
RemoveRemoteShareOperation operation = new RemoveRemoteShareOperation((int) share.getIdRemoteShared());
operation.execute( mClient, this, mHandler);
}
@Override
public void onRemoteOperationFinish( RemoteOperation operation, RemoteOperationResult result) {
if (operation instanceof RemoveRemoteShareOperation) {
if (result.isSuccess()) {
// do your stuff here
}
}
}
Tips
----
* Credentials must be set before calling any method
* Paths must not be on URL Encoding
* Correct path: ``http://www.myowncloudserver.com/owncloud/remote.php/webdav/PopMusic``
* Wrong path: ``http://www.myowncloudserver.com/owncloud/remote.php/webdav/Pop%20Music/``
* There are some forbidden characters to be used in folder and files names on the server, same on the ownCloud Android Library "\","/","<",">",":",""","|","?","*"
* Upload and download actions may be cancelled thanks to the objects uploadOperation.cancel(), downloadOperation.cancel()
* Unit tests, before launching unit tests you have to enter your account information (server url, user and password) on TestActivity.java
|