11f74cf2fa27dd0eb09940a2c37229fd3ebe6eeb
[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.ChunkedUploadRemoteFileOperation;
27 import com.owncloud.android.oc_framework.operations.remote.CreateRemoteFolderOperation;
28 import com.owncloud.android.oc_framework.operations.remote.DownloadRemoteFileOperation;
29 import com.owncloud.android.oc_framework.operations.remote.ReadRemoteFolderOperation;
30 import com.owncloud.android.oc_framework.operations.remote.RemoveRemoteFileOperation;
31 import com.owncloud.android.oc_framework.operations.remote.RenameRemoteFileOperation;
32 import com.owncloud.android.oc_framework.operations.remote.UploadRemoteFileOperation;
33
34 import android.net.Uri;
35 import android.os.Bundle;
36 import android.os.Environment;
37 import android.app.Activity;
38 import android.view.Menu;
39
40 /**
41 * Activity to test OC framework
42 * @author masensio
43 * @author David A. Velasco
44 */
45
46 public class TestActivity extends Activity {
47
48 // This account must exists on the simulator / device
49 private static final String mServerUri = "https://beta.owncloud.com/owncloud/remote.php/webdav";
50 private static final String mUser = "testandroid";
51 private static final String mPass = "testandroid";
52 private static final boolean mChunked = true;
53
54 //private Account mAccount = null;
55 private WebdavClient mClient;
56
57 @Override
58 protected void onCreate(Bundle savedInstanceState) {
59 super.onCreate(savedInstanceState);
60 setContentView(R.layout.activity_test);
61 Uri uri = Uri.parse(mServerUri);
62 mClient = OwnCloudClientFactory.createOwnCloudClient(uri ,getApplicationContext(), true);
63 mClient.setBasicCredentials(mUser, mPass);
64 }
65
66 @Override
67 public boolean onCreateOptionsMenu(Menu menu) {
68 // Inflate the menu; this adds items to the action bar if it is present.
69 getMenuInflater().inflate(R.menu.test, menu);
70 return true;
71 }
72
73 /**
74 * Access to the library method to Create a Folder
75 * @param remotePath Full path to the new directory to create in the remote server.
76 * @param createFullPath 'True' means that all the ancestor folders should be created if don't exist yet.
77 *
78 * @return
79 */
80 public RemoteOperationResult createFolder(String remotePath, boolean createFullPath) {
81
82 CreateRemoteFolderOperation createOperation = new CreateRemoteFolderOperation(remotePath, createFullPath);
83 RemoteOperationResult result = createOperation.execute(mClient);
84
85 return result;
86 }
87
88 /**
89 * Access to the library method to Rename a File or Folder
90 * @param oldName Old name of the file.
91 * @param oldRemotePath Old remote path of the file. For folders it starts and ends by "/"
92 * @param newName New name to set as the name of file.
93 * @param isFolder 'true' for folder and 'false' for files
94 *
95 * @return
96 */
97
98 public RemoteOperationResult renameFile(String oldName, String oldRemotePath, String newName, boolean isFolder) {
99
100 RenameRemoteFileOperation renameOperation = new RenameRemoteFileOperation(oldName, oldRemotePath, newName, isFolder);
101 RemoteOperationResult result = renameOperation.execute(mClient);
102
103 return result;
104 }
105
106 /**
107 * Access to the library method to Remove a File or Folder
108 *
109 * @param remotePath Remote path of the file or folder in the server.
110 * @return
111 */
112 public RemoteOperationResult removeFile(String remotePath) {
113
114 RemoveRemoteFileOperation removeOperation = new RemoveRemoteFileOperation(remotePath);
115 RemoteOperationResult result = removeOperation.execute(mClient);
116
117 return result;
118 }
119
120 /**
121 * Access to the library method to Read a Folder (PROPFIND DEPTH 1)
122 * @param remotePath
123 *
124 * @return
125 */
126 public RemoteOperationResult readFile(String remotePath) {
127
128 ReadRemoteFolderOperation readOperation= new ReadRemoteFolderOperation(remotePath);
129 RemoteOperationResult result = readOperation.execute(mClient);
130
131 return result;
132 }
133
134 /**
135 * Access to the library method to Download a File
136 * @param remotePath
137 *
138 * @return
139 */
140 public RemoteOperationResult downloadFile(RemoteFile remoteFile, String temporalFolder) {
141 // Create folder
142 String path = "/owncloud/tmp/" + temporalFolder;
143 File sdCard = Environment.getExternalStorageDirectory();
144 File folder = new File(sdCard.getAbsolutePath() + "/" + path);
145 folder.mkdirs();
146
147 DownloadRemoteFileOperation downloadOperation = new DownloadRemoteFileOperation(remoteFile, folder.getAbsolutePath());
148 RemoteOperationResult result = downloadOperation.execute(mClient);
149
150 return result;
151 }
152
153 /** Access to the library method to Upload a File
154 * @param storagePath
155 * @param remotePath
156 * @param mimeType
157 *
158 * @return
159 */
160 public RemoteOperationResult uploadFile(String storagePath, String remotePath, String mimeType) {
161
162 UploadRemoteFileOperation uploadOperation;
163 if (mChunked) {
164 uploadOperation = new ChunkedUploadRemoteFileOperation(storagePath, remotePath, mimeType);
165 } else {
166 uploadOperation = new UploadRemoteFileOperation(storagePath, remotePath, mimeType);
167 }
168
169 RemoteOperationResult result = uploadOperation.execute(mClient);
170
171 return result;
172 }
173 }