1 /* ownCloud Android Library is available under MIT license
2 * Copyright (C) 2014 ownCloud (http://www.owncloud.org/)
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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
25 package com
.owncloud
.android
.oc_framework
.sampleclient
;
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
;
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
;
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
;
60 public class MainActivity
extends Activity
implements OnRemoteOperationListener
, OnDatatransferProgressListener
{
62 private static String LOG_TAG
= MainActivity
.class.getCanonicalName();
64 private Handler mHandler
;
66 private WebdavClient mClient
;
68 private FilesArrayAdapter mFilesAdapter
;
72 /** Called when the activity is first created. */
74 public void onCreate(Bundle savedInstanceState
) {
75 super.onCreate(savedInstanceState
);
76 setContentView(R
.layout
.main
);
78 mHandler
= new Handler();
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
));
84 mFilesAdapter
= new FilesArrayAdapter(this, R
.layout
.file_in_list
);
85 ((ListView
)findViewById(R
.id
.list_view
)).setAdapter(mFilesAdapter
);
87 // TODO move to background thread or task
88 AssetManager assets
= getAssets();
90 String sampleFileName
= getString(R
.string
.sample_file_name
);
91 File upFolder
= new File(getCacheDir(), getString(R
.string
.upload_folder_path
));
93 File upFile
= new File(upFolder
, sampleFileName
);
94 FileOutputStream fos
= new FileOutputStream(upFile
);
95 InputStream is
= assets
.open(sampleFileName
);
97 byte[] buffer
= new byte[1024];
98 while ((count
= is
.read(buffer
, 0, buffer
.length
)) >= 0) {
99 fos
.write(buffer
, 0, count
);
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
);
108 mFrame
= findViewById(R
.id
.frame
);
113 public void onDestroy() {
114 File upFolder
= new File(getCacheDir(), getString(R
.string
.upload_folder_path
));
115 File upFile
= upFolder
.listFiles()[0];
122 public void onClickHandler(View button
) {
123 switch (button
.getId()) {
124 case R
.id
.button_refresh
:
127 case R
.id
.button_upload
:
130 case R
.id
.button_delete_remote
:
131 startRemoteDeletion();
133 case R
.id
.button_download
:
136 case R
.id
.button_delete_local
:
137 startLocalDeletion();
140 Toast
.makeText(this, R
.string
.youre_doing_it_wrong
, Toast
.LENGTH_SHORT
).show();
144 private void startRefresh() {
145 ReadRemoteFolderOperation refreshOperation
= new ReadRemoteFolderOperation(FileUtils
.PATH_SEPARATOR
);
146 refreshOperation
.execute(mClient
, this, mHandler
);
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
);
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
);
167 private void startDownload() {
168 File downFolder
= new File(getCacheDir(), getString(R
.string
.download_folder_path
));
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
);
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();
185 ((TextView
) findViewById(R
.id
.download_progress
)).setText("0%");
186 findViewById(R
.id
.frame
).setBackgroundDrawable(null
);
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());
196 } else if (operation
instanceof ReadRemoteFolderOperation
) {
197 onSuccessfulRefresh((ReadRemoteFolderOperation
)operation
, result
);
199 } else if (operation
instanceof UploadRemoteFileOperation
) {
200 onSuccessfulUpload((UploadRemoteFileOperation
)operation
, result
);
202 } else if (operation
instanceof RemoveRemoteFileOperation
) {
203 onSuccessfulRemoteDeletion((RemoveRemoteFileOperation
)operation
, result
);
205 } else if (operation
instanceof DownloadRemoteFileOperation
) {
206 onSuccessfulDownload((DownloadRemoteFileOperation
)operation
, result
);
209 Toast
.makeText(this, R
.string
.todo_operation_finished_in_success
, Toast
.LENGTH_SHORT
).show();
213 private void onSuccessfulRefresh(ReadRemoteFolderOperation operation
, RemoteOperationResult result
) {
214 mFilesAdapter
.clear();
215 List
<RemoteFile
> files
= result
.getData();
217 Iterator
<RemoteFile
> it
= files
.iterator();
218 while (it
.hasNext()) {
219 mFilesAdapter
.add(it
.next());
221 mFilesAdapter
.remove(mFilesAdapter
.getItem(0));
223 mFilesAdapter
.notifyDataSetChanged();
226 private void onSuccessfulUpload(UploadRemoteFileOperation operation
, RemoteOperationResult result
) {
230 private void onSuccessfulRemoteDeletion(RemoveRemoteFileOperation operation
, RemoteOperationResult result
) {
232 TextView progressView
= (TextView
) findViewById(R
.id
.upload_progress
);
233 if (progressView
!= null
) {
234 progressView
.setText("0%");
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
);
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() {
254 TextView progressView
= null
;
256 progressView
= (TextView
) findViewById(R
.id
.upload_progress
);
258 progressView
= (TextView
) findViewById(R
.id
.download_progress
);
260 if (progressView
!= null
) {
261 progressView
.setText(Long
.toString(percentage
) + "%");