b166d2a406b55fbdd9424252ae50ba4d165a4cae
[pub/Android/ownCloud.git] / oc_framework / sample_client / src / com / owncloud / android / oc_framework / sampleclient / MainActivity.java
1 /* ownCloud webDAV Library for Android is available under MIT license
2 * Copyright (C) 2014 ownCloud (http://www.owncloud.org/)
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 *
23 */
24
25 package com.owncloud.android.oc_framework.sampleclient;
26
27 import java.io.File;
28 import java.io.FileOutputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.util.Iterator;
32 import java.util.List;
33
34 import com.owncloud.android.oc_framework.accounts.AccountUtils;
35 import com.owncloud.android.oc_framework.network.webdav.OnDatatransferProgressListener;
36 import com.owncloud.android.oc_framework.network.webdav.OwnCloudClientFactory;
37 import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
38 import com.owncloud.android.oc_framework.operations.OnRemoteOperationListener;
39 import com.owncloud.android.oc_framework.operations.RemoteFile;
40 import com.owncloud.android.oc_framework.operations.RemoteOperation;
41 import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
42 import com.owncloud.android.oc_framework.operations.remote.DownloadRemoteFileOperation;
43 import com.owncloud.android.oc_framework.operations.remote.ReadRemoteFolderOperation;
44 import com.owncloud.android.oc_framework.operations.remote.RemoveRemoteFileOperation;
45 import com.owncloud.android.oc_framework.operations.remote.UploadRemoteFileOperation;
46 import com.owncloud.android.oc_framework.utils.FileUtils;
47
48 import android.app.Activity;
49 import android.content.res.AssetManager;
50 import android.graphics.drawable.BitmapDrawable;
51 import android.net.Uri;
52 import android.os.Bundle;
53 import android.os.Handler;
54 import android.util.Log;
55 import android.view.View;
56 import android.widget.ListView;
57 import android.widget.TextView;
58 import android.widget.Toast;
59
60 public class MainActivity extends Activity implements OnRemoteOperationListener, OnDatatransferProgressListener {
61
62 private static String LOG_TAG = MainActivity.class.getCanonicalName();
63
64 private Handler mHandler;
65
66 private WebdavClient mClient;
67
68 private FilesArrayAdapter mFilesAdapter;
69
70 private View mFrame;
71
72 /** Called when the activity is first created. */
73 @Override
74 public void onCreate(Bundle savedInstanceState) {
75 super.onCreate(savedInstanceState);
76 setContentView(R.layout.main);
77
78 mHandler = new Handler();
79
80 Uri serverUri = Uri.parse(getString(R.string.server_base_url) + AccountUtils.WEBDAV_PATH_4_0);
81 mClient = OwnCloudClientFactory.createOwnCloudClient(serverUri, this, true);
82 mClient.setBasicCredentials(getString(R.string.username), getString(R.string.password));
83
84 mFilesAdapter = new FilesArrayAdapter(this, R.layout.file_in_list);
85 ((ListView)findViewById(R.id.list_view)).setAdapter(mFilesAdapter);
86
87 // TODO move to background thread or task
88 AssetManager assets = getAssets();
89 try {
90 String sampleFileName = getString(R.string.sample_file_name);
91 File upFolder = new File(getCacheDir(), getString(R.string.upload_folder_path));
92 upFolder.mkdir();
93 File upFile = new File(upFolder, sampleFileName);
94 FileOutputStream fos = new FileOutputStream(upFile);
95 InputStream is = assets.open(sampleFileName);
96 int count = 0;
97 byte[] buffer = new byte[1024];
98 while ((count = is.read(buffer, 0, buffer.length)) >= 0) {
99 fos.write(buffer, 0, count);
100 }
101 is.close();
102 fos.close();
103 } catch (IOException e) {
104 Toast.makeText(this, R.string.error_copying_sample_file, Toast.LENGTH_SHORT).show();
105 Log.e(LOG_TAG, getString(R.string.error_copying_sample_file), e);
106 }
107
108 mFrame = findViewById(R.id.frame);
109 }
110
111
112 @Override
113 public void onDestroy() {
114 File upFolder = new File(getCacheDir(), getString(R.string.upload_folder_path));
115 File upFile = upFolder.listFiles()[0];
116 upFile.delete();
117 upFolder.delete();
118 super.onDestroy();
119 }
120
121
122 public void onClickHandler(View button) {
123 switch (button.getId()) {
124 case R.id.button_refresh:
125 startRefresh();
126 break;
127 case R.id.button_upload:
128 startUpload();
129 break;
130 case R.id.button_delete_remote:
131 startRemoteDeletion();
132 break;
133 case R.id.button_download:
134 startDownload();
135 break;
136 case R.id.button_delete_local:
137 startLocalDeletion();
138 break;
139 default:
140 Toast.makeText(this, R.string.youre_doing_it_wrong, Toast.LENGTH_SHORT).show();
141 }
142 }
143
144 private void startRefresh() {
145 ReadRemoteFolderOperation refreshOperation = new ReadRemoteFolderOperation(FileUtils.PATH_SEPARATOR);
146 refreshOperation.execute(mClient, this, mHandler);
147 }
148
149 private void startUpload() {
150 File upFolder = new File(getCacheDir(), getString(R.string.upload_folder_path));
151 File fileToUpload = upFolder.listFiles()[0];
152 String remotePath = FileUtils.PATH_SEPARATOR + fileToUpload.getName();
153 String mimeType = getString(R.string.sample_file_mimetype);
154 UploadRemoteFileOperation uploadOperation = new UploadRemoteFileOperation(fileToUpload.getAbsolutePath(), remotePath, mimeType);
155 uploadOperation.addDatatransferProgressListener(this);
156 uploadOperation.execute(mClient, this, mHandler);
157 }
158
159 private void startRemoteDeletion() {
160 File upFolder = new File(getCacheDir(), getString(R.string.upload_folder_path));
161 File fileToUpload = upFolder.listFiles()[0];
162 String remotePath = FileUtils.PATH_SEPARATOR + fileToUpload.getName();
163 RemoveRemoteFileOperation removeOperation = new RemoveRemoteFileOperation(remotePath);
164 removeOperation.execute(mClient, this, mHandler);
165 }
166
167 private void startDownload() {
168 File downFolder = new File(getCacheDir(), getString(R.string.download_folder_path));
169 downFolder.mkdir();
170 File upFolder = new File(getCacheDir(), getString(R.string.upload_folder_path));
171 File fileToUpload = upFolder.listFiles()[0];
172 String remotePath = FileUtils.PATH_SEPARATOR + fileToUpload.getName();
173 RemoteFile rFileToDownload = new RemoteFile(remotePath);
174 rFileToDownload.setLength(fileToUpload.length());
175 DownloadRemoteFileOperation downloadOperation = new DownloadRemoteFileOperation(rFileToDownload, downFolder.getAbsolutePath());
176 downloadOperation.addDatatransferProgressListener(this);
177 downloadOperation.execute(mClient, this, mHandler);
178 }
179
180 @SuppressWarnings("deprecation")
181 private void startLocalDeletion() {
182 File downFolder = new File(getCacheDir(), getString(R.string.download_folder_path));
183 File downloadedFile = downFolder.listFiles()[0];
184 if (!downloadedFile.delete() && downloadedFile.exists()) {
185 Toast.makeText(this, R.string.error_deleting_local_file, Toast.LENGTH_SHORT).show();
186 } else {
187 ((TextView) findViewById(R.id.download_progress)).setText("0%");
188 findViewById(R.id.frame).setBackgroundDrawable(null);
189 }
190 }
191
192 @Override
193 public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
194 if (!result.isSuccess()) {
195 Toast.makeText(this, R.string.todo_operation_finished_in_fail, Toast.LENGTH_SHORT).show();
196 Log.e(LOG_TAG, result.getLogMessage(), result.getException());
197
198 } else if (operation instanceof ReadRemoteFolderOperation) {
199 onSuccessfulRefresh((ReadRemoteFolderOperation)operation, result);
200
201 } else if (operation instanceof UploadRemoteFileOperation ) {
202 onSuccessfulUpload((UploadRemoteFileOperation)operation, result);
203
204 } else if (operation instanceof RemoveRemoteFileOperation ) {
205 onSuccessfulRemoteDeletion((RemoveRemoteFileOperation)operation, result);
206
207 } else if (operation instanceof DownloadRemoteFileOperation ) {
208 onSuccessfulDownload((DownloadRemoteFileOperation)operation, result);
209
210 } else {
211 Toast.makeText(this, R.string.todo_operation_finished_in_success, Toast.LENGTH_SHORT).show();
212 }
213 }
214
215 private void onSuccessfulRefresh(ReadRemoteFolderOperation operation, RemoteOperationResult result) {
216 mFilesAdapter.clear();
217 List<RemoteFile> files = result.getData();
218 if (files != null) {
219 Iterator<RemoteFile> it = files.iterator();
220 while (it.hasNext()) {
221 mFilesAdapter.add(it.next());
222 }
223 mFilesAdapter.remove(mFilesAdapter.getItem(0));
224 }
225 mFilesAdapter.notifyDataSetChanged();
226 }
227
228 private void onSuccessfulUpload(UploadRemoteFileOperation operation, RemoteOperationResult result) {
229 startRefresh();
230 }
231
232 private void onSuccessfulRemoteDeletion(RemoveRemoteFileOperation operation, RemoteOperationResult result) {
233 startRefresh();
234 TextView progressView = (TextView) findViewById(R.id.upload_progress);
235 if (progressView != null) {
236 progressView.setText("0%");
237 }
238 }
239
240 @SuppressWarnings("deprecation")
241 private void onSuccessfulDownload(DownloadRemoteFileOperation operation, RemoteOperationResult result) {
242 File downFolder = new File(getCacheDir(), getString(R.string.download_folder_path));
243 File downloadedFile = downFolder.listFiles()[0];
244 BitmapDrawable bDraw = new BitmapDrawable(getResources(), downloadedFile.getAbsolutePath());
245 mFrame.setBackgroundDrawable(bDraw);
246 }
247
248 @Override
249 public void onTransferProgress(long progressRate, long totalTransferredSoFar, long totalToTransfer, String fileName) {
250 final long percentage = (totalToTransfer > 0 ? totalTransferredSoFar * 100 / totalToTransfer : 0);
251 final boolean upload = fileName.contains(getString(R.string.upload_folder_path));
252 Log.d(LOG_TAG, "progressRate " + percentage);
253 mHandler.post(new Runnable() {
254 @Override
255 public void run() {
256 TextView progressView = null;
257 if (upload) {
258 progressView = (TextView) findViewById(R.id.upload_progress);
259 } else {
260 progressView = (TextView) findViewById(R.id.download_progress);
261 }
262 if (progressView != null) {
263 progressView.setText(Long.toString(percentage) + "%");
264 }
265 }
266 });
267 }
268
269 }