1 package com
.owncloud
.android
.oc_framework
.sampleclient
;
4 import java
.io
.FileOutputStream
;
5 import java
.io
.IOException
;
6 import java
.io
.InputStream
;
7 import java
.util
.Iterator
;
10 import com
.owncloud
.android
.oc_framework
.accounts
.AccountUtils
;
11 import com
.owncloud
.android
.oc_framework
.network
.webdav
.OnDatatransferProgressListener
;
12 import com
.owncloud
.android
.oc_framework
.network
.webdav
.OwnCloudClientFactory
;
13 import com
.owncloud
.android
.oc_framework
.network
.webdav
.WebdavClient
;
14 import com
.owncloud
.android
.oc_framework
.operations
.OnRemoteOperationListener
;
15 import com
.owncloud
.android
.oc_framework
.operations
.RemoteFile
;
16 import com
.owncloud
.android
.oc_framework
.operations
.RemoteOperation
;
17 import com
.owncloud
.android
.oc_framework
.operations
.RemoteOperationResult
;
18 import com
.owncloud
.android
.oc_framework
.operations
.remote
.DownloadRemoteFileOperation
;
19 import com
.owncloud
.android
.oc_framework
.operations
.remote
.ReadRemoteFolderOperation
;
20 import com
.owncloud
.android
.oc_framework
.operations
.remote
.RemoveRemoteFileOperation
;
21 import com
.owncloud
.android
.oc_framework
.operations
.remote
.UploadRemoteFileOperation
;
22 import com
.owncloud
.android
.oc_framework
.utils
.FileUtils
;
24 import android
.app
.Activity
;
25 import android
.content
.res
.AssetManager
;
26 import android
.graphics
.drawable
.BitmapDrawable
;
27 import android
.net
.Uri
;
28 import android
.os
.Bundle
;
29 import android
.os
.Handler
;
30 import android
.util
.Log
;
31 import android
.view
.View
;
32 import android
.widget
.ListView
;
33 import android
.widget
.TextView
;
34 import android
.widget
.Toast
;
36 public class MainActivity
extends Activity
implements OnRemoteOperationListener
, OnDatatransferProgressListener
{
38 private static String LOG_TAG
= MainActivity
.class.getCanonicalName();
40 private Handler mHandler
;
42 private WebdavClient mClient
;
44 private FilesArrayAdapter mFilesAdapter
;
48 /** Called when the activity is first created. */
50 public void onCreate(Bundle savedInstanceState
) {
51 super.onCreate(savedInstanceState
);
52 setContentView(R
.layout
.main
);
54 mHandler
= new Handler();
56 Uri serverUri
= Uri
.parse(getString(R
.string
.server_base_url
) + AccountUtils
.WEBDAV_PATH_4_0
);
57 mClient
= OwnCloudClientFactory
.createOwnCloudClient(serverUri
, this, true
);
58 mClient
.setBasicCredentials(getString(R
.string
.username
), getString(R
.string
.password
));
60 mFilesAdapter
= new FilesArrayAdapter(this, R
.layout
.file_in_list
);
61 ((ListView
)findViewById(R
.id
.list_view
)).setAdapter(mFilesAdapter
);
63 // TODO move to background thread or task
64 AssetManager assets
= getAssets();
66 String sampleFileName
= getString(R
.string
.sample_file_name
);
67 File upFolder
= new File(getCacheDir(), getString(R
.string
.upload_folder_path
));
69 File upFile
= new File(upFolder
, sampleFileName
);
70 FileOutputStream fos
= new FileOutputStream(upFile
);
71 InputStream is
= assets
.open(sampleFileName
);
73 byte[] buffer
= new byte[1024];
74 while ((count
= is
.read(buffer
, 0, buffer
.length
)) >= 0) {
75 fos
.write(buffer
, 0, count
);
79 } catch (IOException e
) {
80 Toast
.makeText(this, R
.string
.error_copying_sample_file
, Toast
.LENGTH_SHORT
).show();
81 Log
.e(LOG_TAG
, getString(R
.string
.error_copying_sample_file
), e
);
84 mFrame
= findViewById(R
.id
.frame
);
89 public void onDestroy() {
90 File upFolder
= new File(getCacheDir(), getString(R
.string
.upload_folder_path
));
91 File upFile
= upFolder
.listFiles()[0];
98 public void onClickHandler(View button
) {
99 switch (button
.getId()) {
100 case R
.id
.button_refresh
:
103 case R
.id
.button_upload
:
106 case R
.id
.button_delete_remote
:
107 startRemoteDeletion();
109 case R
.id
.button_download
:
112 case R
.id
.button_delete_local
:
113 startLocalDeletion();
116 Toast
.makeText(this, R
.string
.youre_doing_it_wrong
, Toast
.LENGTH_SHORT
).show();
120 private void startRefresh() {
121 ReadRemoteFolderOperation refreshOperation
= new ReadRemoteFolderOperation(FileUtils
.PATH_SEPARATOR
);
122 refreshOperation
.execute(mClient
, this, mHandler
);
125 private void startUpload() {
126 File upFolder
= new File(getCacheDir(), getString(R
.string
.upload_folder_path
));
127 File fileToUpload
= upFolder
.listFiles()[0];
128 String remotePath
= FileUtils
.PATH_SEPARATOR
+ fileToUpload
.getName();
129 String mimeType
= getString(R
.string
.sample_file_mimetype
);
130 UploadRemoteFileOperation uploadOperation
= new UploadRemoteFileOperation(fileToUpload
.getAbsolutePath(), remotePath
, mimeType
);
131 uploadOperation
.addDatatransferProgressListener(this);
132 uploadOperation
.execute(mClient
, this, mHandler
);
135 private void startRemoteDeletion() {
136 File upFolder
= new File(getCacheDir(), getString(R
.string
.upload_folder_path
));
137 File fileToUpload
= upFolder
.listFiles()[0];
138 String remotePath
= FileUtils
.PATH_SEPARATOR
+ fileToUpload
.getName();
139 RemoveRemoteFileOperation removeOperation
= new RemoveRemoteFileOperation(remotePath
);
140 removeOperation
.execute(mClient
, this, mHandler
);
143 private void startDownload() {
144 File downFolder
= new File(getCacheDir(), getString(R
.string
.download_folder_path
));
146 File upFolder
= new File(getCacheDir(), getString(R
.string
.upload_folder_path
));
147 File fileToUpload
= upFolder
.listFiles()[0];
148 String remotePath
= FileUtils
.PATH_SEPARATOR
+ fileToUpload
.getName();
149 RemoteFile rFileToDownload
= new RemoteFile(remotePath
);
150 rFileToDownload
.setLength(fileToUpload
.length());
151 DownloadRemoteFileOperation downloadOperation
= new DownloadRemoteFileOperation(rFileToDownload
, downFolder
.getAbsolutePath());
152 downloadOperation
.addDatatransferProgressListener(this);
153 downloadOperation
.execute(mClient
, this, mHandler
);
156 @SuppressWarnings("deprecation")
157 private void startLocalDeletion() {
158 File downFolder
= new File(getCacheDir(), getString(R
.string
.download_folder_path
));
159 File downloadedFile
= downFolder
.listFiles()[0];
160 if (!downloadedFile
.delete() && downloadedFile
.exists()) {
161 Toast
.makeText(this, R
.string
.error_deleting_local_file
, Toast
.LENGTH_SHORT
).show();
163 ((TextView
) findViewById(R
.id
.download_progress
)).setText("0%");
164 findViewById(R
.id
.frame
).setBackgroundDrawable(null
);
169 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
170 if (!result
.isSuccess()) {
171 Toast
.makeText(this, R
.string
.todo_operation_finished_in_fail
, Toast
.LENGTH_SHORT
).show();
172 Log
.e(LOG_TAG
, result
.getLogMessage(), result
.getException());
174 } else if (operation
instanceof ReadRemoteFolderOperation
) {
175 onSuccessfulRefresh((ReadRemoteFolderOperation
)operation
, result
);
177 } else if (operation
instanceof UploadRemoteFileOperation
) {
178 onSuccessfulUpload((UploadRemoteFileOperation
)operation
, result
);
180 } else if (operation
instanceof RemoveRemoteFileOperation
) {
181 onSuccessfulRemoteDeletion((RemoveRemoteFileOperation
)operation
, result
);
183 } else if (operation
instanceof DownloadRemoteFileOperation
) {
184 onSuccessfulDownload((DownloadRemoteFileOperation
)operation
, result
);
187 Toast
.makeText(this, R
.string
.todo_operation_finished_in_success
, Toast
.LENGTH_SHORT
).show();
191 private void onSuccessfulRefresh(ReadRemoteFolderOperation operation
, RemoteOperationResult result
) {
192 mFilesAdapter
.clear();
193 List
<RemoteFile
> files
= result
.getData();
195 Iterator
<RemoteFile
> it
= files
.iterator();
196 while (it
.hasNext()) {
197 mFilesAdapter
.add(it
.next());
199 mFilesAdapter
.remove(mFilesAdapter
.getItem(0));
201 mFilesAdapter
.notifyDataSetChanged();
204 private void onSuccessfulUpload(UploadRemoteFileOperation operation
, RemoteOperationResult result
) {
208 private void onSuccessfulRemoteDeletion(RemoveRemoteFileOperation operation
, RemoteOperationResult result
) {
210 TextView progressView
= (TextView
) findViewById(R
.id
.upload_progress
);
211 if (progressView
!= null
) {
212 progressView
.setText("0%");
216 @SuppressWarnings("deprecation")
217 private void onSuccessfulDownload(DownloadRemoteFileOperation operation
, RemoteOperationResult result
) {
218 File downFolder
= new File(getCacheDir(), getString(R
.string
.download_folder_path
));
219 File downloadedFile
= downFolder
.listFiles()[0];
220 BitmapDrawable bDraw
= new BitmapDrawable(getResources(), downloadedFile
.getAbsolutePath());
221 mFrame
.setBackgroundDrawable(bDraw
);
225 public void onTransferProgress(long progressRate
, long totalTransferredSoFar
, long totalToTransfer
, String fileName
) {
226 final long percentage
= (totalToTransfer
> 0 ? totalTransferredSoFar
* 100 / totalToTransfer
: 0);
227 final boolean upload
= fileName
.contains(getString(R
.string
.upload_folder_path
));
228 Log
.d(LOG_TAG
, "progressRate " + percentage
);
229 mHandler
.post(new Runnable() {
232 TextView progressView
= null
;
234 progressView
= (TextView
) findViewById(R
.id
.upload_progress
);
236 progressView
= (TextView
) findViewById(R
.id
.download_progress
);
238 if (progressView
!= null
) {
239 progressView
.setText(Long
.toString(percentage
) + "%");
247 public void onTransferProgress(long arg0
) {
248 // TODO Remove from library