15912614ddec1d327f9f0e31268ded0713c1c920
[pub/Android/ownCloud.git] / oc_framework-test-project / src / com / owncloud / android / oc_framework_test_project / TestActivity.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18 package com.owncloud.android.oc_framework_test_project;
19
20 import java.io.File;
21
22 import com.owncloud.android.oc_framework.network.webdav.OwnCloudClientFactory;
23 import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
24 import com.owncloud.android.oc_framework.operations.RemoteFile;
25 import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
26 import com.owncloud.android.oc_framework.operations.remote.CreateRemoteFolderOperation;
27 import com.owncloud.android.oc_framework.operations.remote.DownloadRemoteFileOperation;
28 import com.owncloud.android.oc_framework.operations.remote.ReadRemoteFolderOperation;
29 import com.owncloud.android.oc_framework.operations.remote.RemoveRemoteFileOperation;
30 import com.owncloud.android.oc_framework.operations.remote.RenameRemoteFileOperation;
31
32 import android.net.Uri;
33 import android.os.Bundle;
34 import android.os.Environment;
35 import android.app.Activity;
36 import android.view.Menu;
37
38 /**
39 * Activity to test OC framework
40 * @author masensio
41 * @author David A. Velasco
42 */
43 public class TestActivity extends Activity {
44
45 // This account must exists on the simulator / device
46 private static final String mServerUri = "https://beta.owncloud.com/owncloud/remote.php/webdav";
47 private static final String mUser = "testandroid";
48 private static final String mPass = "testandroid";
49
50 //private Account mAccount = null;
51 private WebdavClient mClient;
52
53 @Override
54 protected void onCreate(Bundle savedInstanceState) {
55 super.onCreate(savedInstanceState);
56 setContentView(R.layout.activity_test);
57 Uri uri = Uri.parse(mServerUri);
58 mClient = OwnCloudClientFactory.createOwnCloudClient(uri ,getApplicationContext(), true);
59 mClient.setBasicCredentials(mUser, mPass);
60 }
61
62 @Override
63 public boolean onCreateOptionsMenu(Menu menu) {
64 // Inflate the menu; this adds items to the action bar if it is present.
65 getMenuInflater().inflate(R.menu.test, menu);
66 return true;
67 }
68
69 /**
70 * Access to the library method to Create a Folder
71 * @param remotePath Full path to the new directory to create in the remote server.
72 * @param createFullPath 'True' means that all the ancestor folders should be created if don't exist yet.
73 *
74 * @return
75 */
76 public RemoteOperationResult createFolder(String remotePath, boolean createFullPath) {
77
78 CreateRemoteFolderOperation createOperation = new CreateRemoteFolderOperation(remotePath, createFullPath);
79 RemoteOperationResult result = createOperation.execute(mClient);
80
81 return result;
82 }
83
84 /**
85 * Access to the library method to Rename a File or Folder
86 * @param oldName Old name of the file.
87 * @param oldRemotePath Old remote path of the file. For folders it starts and ends by "/"
88 * @param newName New name to set as the name of file.
89 * @param isFolder 'true' for folder and 'false' for files
90 *
91 * @return
92 */
93
94 public RemoteOperationResult renameFile(String oldName, String oldRemotePath, String newName, boolean isFolder) {
95
96 RenameRemoteFileOperation renameOperation = new RenameRemoteFileOperation(oldName, oldRemotePath, newName, isFolder);
97 RemoteOperationResult result = renameOperation.execute(mClient);
98
99 return result;
100 }
101
102 /**
103 * Access to the library method to Remove a File or Folder
104 *
105 * @param remotePath Remote path of the file or folder in the server.
106 * @return
107 */
108 public RemoteOperationResult removeFile(String remotePath) {
109
110 RemoveRemoteFileOperation removeOperation = new RemoveRemoteFileOperation(remotePath);
111 RemoteOperationResult result = removeOperation.execute(mClient);
112
113 return result;
114 }
115
116 /**
117 * Access to the library method to Read a Folder (PROPFIND DEPTH 1)
118 * @param remotePath
119 *
120 * @return
121 */
122 public RemoteOperationResult readFile(String remotePath) {
123
124 ReadRemoteFolderOperation readOperation= new ReadRemoteFolderOperation(remotePath);
125 RemoteOperationResult result = readOperation.execute(mClient);
126
127 return result;
128 }
129
130 /**
131 * Access to the library method to Download a File
132 * @param remotePath
133 *
134 * @return
135 */
136 public RemoteOperationResult downloadFile(RemoteFile remoteFile, String temporalFolder) {
137 // Create folder
138 String path = "/owncloud/tmp/" + temporalFolder;
139 File sdCard = Environment.getExternalStorageDirectory();
140 File folder = new File(sdCard.getAbsolutePath() + "/" + path);
141 folder.mkdirs();
142
143 DownloadRemoteFileOperation downloadOperation = new DownloadRemoteFileOperation(remoteFile, folder.getAbsolutePath());
144 RemoteOperationResult result = downloadOperation.execute(mClient);
145
146 return result;
147 }
148
149 }