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