79d273a01c1554ca56f93b3098afb1812a08ec2b
[pub/Android/ownCloud.git] / oc_framework / sample_client / src / com / owncloud / android / oc_framework / sampleclient / MainActivity.java
1 /* ownCloud Android Library 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 DownloadRemoteFileOperation downloadOperation = new DownloadRemoteFileOperation(remotePath, downFolder.getAbsolutePath());
174 downloadOperation.addDatatransferProgressListener(this);
175 downloadOperation.execute(mClient, this, mHandler);
176 }
177
178 @SuppressWarnings("deprecation")
179 private void startLocalDeletion() {
180 File downFolder = new File(getCacheDir(), getString(R.string.download_folder_path));
181 File downloadedFile = downFolder.listFiles()[0];
182 if (!downloadedFile.delete() && downloadedFile.exists()) {
183 Toast.makeText(this, R.string.error_deleting_local_file, Toast.LENGTH_SHORT).show();
184 } else {
185 ((TextView) findViewById(R.id.download_progress)).setText("0%");
186 findViewById(R.id.frame).setBackgroundDrawable(null);
187 }
188 }
189
190 @Override
191 public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
192 if (!result.isSuccess()) {
193 Toast.makeText(this, R.string.todo_operation_finished_in_fail, Toast.LENGTH_SHORT).show();
194 Log.e(LOG_TAG, result.getLogMessage(), result.getException());
195
196 } else if (operation instanceof ReadRemoteFolderOperation) {
197 onSuccessfulRefresh((ReadRemoteFolderOperation)operation, result);
198
199 } else if (operation instanceof UploadRemoteFileOperation ) {
200 onSuccessfulUpload((UploadRemoteFileOperation)operation, result);
201
202 } else if (operation instanceof RemoveRemoteFileOperation ) {
203 onSuccessfulRemoteDeletion((RemoveRemoteFileOperation)operation, result);
204
205 } else if (operation instanceof DownloadRemoteFileOperation ) {
206 onSuccessfulDownload((DownloadRemoteFileOperation)operation, result);
207
208 } else {
209 Toast.makeText(this, R.string.todo_operation_finished_in_success, Toast.LENGTH_SHORT).show();
210 }
211 }
212
213 private void onSuccessfulRefresh(ReadRemoteFolderOperation operation, RemoteOperationResult result) {
214 mFilesAdapter.clear();
215 List<RemoteFile> files = result.getData();
216 if (files != null) {
217 Iterator<RemoteFile> it = files.iterator();
218 while (it.hasNext()) {
219 mFilesAdapter.add(it.next());
220 }
221 mFilesAdapter.remove(mFilesAdapter.getItem(0));
222 }
223 mFilesAdapter.notifyDataSetChanged();
224 }
225
226 private void onSuccessfulUpload(UploadRemoteFileOperation operation, RemoteOperationResult result) {
227 startRefresh();
228 }
229
230 private void onSuccessfulRemoteDeletion(RemoveRemoteFileOperation operation, RemoteOperationResult result) {
231 startRefresh();
232 TextView progressView = (TextView) findViewById(R.id.upload_progress);
233 if (progressView != null) {
234 progressView.setText("0%");
235 }
236 }
237
238 @SuppressWarnings("deprecation")
239 private void onSuccessfulDownload(DownloadRemoteFileOperation operation, RemoteOperationResult result) {
240 File downFolder = new File(getCacheDir(), getString(R.string.download_folder_path));
241 File downloadedFile = downFolder.listFiles()[0];
242 BitmapDrawable bDraw = new BitmapDrawable(getResources(), downloadedFile.getAbsolutePath());
243 mFrame.setBackgroundDrawable(bDraw);
244 }
245
246 @Override
247 public void onTransferProgress(long progressRate, long totalTransferredSoFar, long totalToTransfer, String fileName) {
248 final long percentage = (totalToTransfer > 0 ? totalTransferredSoFar * 100 / totalToTransfer : 0);
249 final boolean upload = fileName.contains(getString(R.string.upload_folder_path));
250 Log.d(LOG_TAG, "progressRate " + percentage);
251 mHandler.post(new Runnable() {
252 @Override
253 public void run() {
254 TextView progressView = null;
255 if (upload) {
256 progressView = (TextView) findViewById(R.id.upload_progress);
257 } else {
258 progressView = (TextView) findViewById(R.id.download_progress);
259 }
260 if (progressView != null) {
261 progressView.setText(Long.toString(percentage) + "%");
262 }
263 }
264 });
265 }
266
267 }