1 package com
.owncloud
.android
.oc_framework
.sampleclient
;
4 import java
.io
.FileOutputStream
;
5 import java
.io
.IOException
;
6 import java
.io
.InputStream
;
9 import com
.owncloud
.android
.oc_framework
.accounts
.AccountUtils
;
10 import com
.owncloud
.android
.oc_framework
.network
.webdav
.OnDatatransferProgressListener
;
11 import com
.owncloud
.android
.oc_framework
.network
.webdav
.OwnCloudClientFactory
;
12 import com
.owncloud
.android
.oc_framework
.network
.webdav
.WebdavClient
;
13 import com
.owncloud
.android
.oc_framework
.operations
.OnRemoteOperationListener
;
14 import com
.owncloud
.android
.oc_framework
.operations
.RemoteFile
;
15 import com
.owncloud
.android
.oc_framework
.operations
.RemoteOperation
;
16 import com
.owncloud
.android
.oc_framework
.operations
.RemoteOperationResult
;
17 import com
.owncloud
.android
.oc_framework
.operations
.remote
.ReadRemoteFolderOperation
;
18 import com
.owncloud
.android
.oc_framework
.operations
.remote
.UploadRemoteFileOperation
;
19 import com
.owncloud
.android
.oc_framework
.utils
.FileUtils
;
21 import android
.app
.Activity
;
22 import android
.content
.res
.AssetManager
;
23 import android
.net
.Uri
;
24 import android
.os
.Bundle
;
25 import android
.os
.Handler
;
26 import android
.util
.Log
;
27 import android
.view
.View
;
28 import android
.widget
.ListView
;
29 import android
.widget
.TextView
;
30 import android
.widget
.Toast
;
32 public class MainActivity
extends Activity
implements OnRemoteOperationListener
, OnDatatransferProgressListener
{
34 private static String LOG_TAG
= MainActivity
.class.getCanonicalName();
36 private Handler mHandler
;
38 private WebdavClient mClient
;
40 private FilesArrayAdapter mFilesAdapter
;
42 /** Called when the activity is first created. */
44 public void onCreate(Bundle savedInstanceState
) {
45 super.onCreate(savedInstanceState
);
46 setContentView(R
.layout
.main
);
48 mHandler
= new Handler();
50 Uri serverUri
= Uri
.parse(getString(R
.string
.server_base_url
) + AccountUtils
.WEBDAV_PATH_4_0
);
51 mClient
= OwnCloudClientFactory
.createOwnCloudClient(serverUri
, this, true
);
52 mClient
.setBasicCredentials(getString(R
.string
.username
), getString(R
.string
.password
));
54 mFilesAdapter
= new FilesArrayAdapter(this, R
.layout
.file_in_list
);
55 ((ListView
)findViewById(R
.id
.list_view
)).setAdapter(mFilesAdapter
);
57 // TODO move to background thread or task
58 AssetManager assets
= getAssets();
60 String sampleFileName
= getString(R
.string
.sample_file_name
);
61 File upFolder
= new File(getCacheDir(), getString(R
.string
.upload_folder_path
));
63 File upFile
= new File(upFolder
, sampleFileName
);
64 FileOutputStream fos
= new FileOutputStream(upFile
);
65 InputStream is
= assets
.open(sampleFileName
);
67 byte[] buffer
= new byte[1024];
68 while ((count
= is
.read(buffer
, 0, buffer
.length
)) >= 0) {
69 fos
.write(buffer
, 0, count
);
73 } catch (IOException e
) {
74 Toast
.makeText(this, R
.string
.error_copying_sample_file
, Toast
.LENGTH_SHORT
).show();
75 Log
.e(LOG_TAG
, getString(R
.string
.error_copying_sample_file
), e
);
81 public void onDestroy() {
82 File upFolder
= new File(getCacheDir(), getString(R
.string
.upload_folder_path
));
83 File upFile
= upFolder
.listFiles()[0];
90 public void onClickHandler(View button
) {
91 switch (button
.getId()) {
92 case R
.id
.button_refresh
:
95 case R
.id
.button_upload
:
98 case R
.id
.button_delete_remote
:
99 startRemoteDeletion();
101 case R
.id
.button_download
:
104 case R
.id
.button_delete_local
:
105 startLocalDeletion();
108 Toast
.makeText(this, R
.string
.youre_doing_it_wrong
, Toast
.LENGTH_SHORT
).show();
112 private void startRefresh() {
113 ReadRemoteFolderOperation refreshOperation
= new ReadRemoteFolderOperation(FileUtils
.PATH_SEPARATOR
);
114 refreshOperation
.execute(mClient
, this, mHandler
);
117 private void startUpload() {
118 File upFolder
= new File(getCacheDir(), getString(R
.string
.upload_folder_path
));
119 File fileToUpload
= upFolder
.listFiles()[0];
120 String remotePath
= FileUtils
.PATH_SEPARATOR
+ fileToUpload
.getName();
121 String mimeType
= getString(R
.string
.sample_file_mimetype
);
122 UploadRemoteFileOperation uploadOperation
= new UploadRemoteFileOperation(fileToUpload
.getAbsolutePath(), remotePath
, mimeType
);
123 uploadOperation
.addDatatransferProgressListener(this);
124 uploadOperation
.execute(mClient
, this, mHandler
);
127 private void startRemoteDeletion() {
128 Toast
.makeText(this, R
.string
.todo_start_remote_deletion
, Toast
.LENGTH_SHORT
).show();
131 private void startDownload() {
132 Toast
.makeText(this, R
.string
.todo_start_download
, Toast
.LENGTH_SHORT
).show();
135 private void startLocalDeletion() {
136 Toast
.makeText(this, R
.string
.todo_start_local_deletion
, Toast
.LENGTH_SHORT
).show();
140 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
141 if (!result
.isSuccess()) {
142 Toast
.makeText(this, R
.string
.todo_operation_finished_in_fail
, Toast
.LENGTH_SHORT
).show();
143 Log
.e(LOG_TAG
, result
.getLogMessage(), result
.getException());
145 } else if (operation
instanceof ReadRemoteFolderOperation
) {
146 onSuccessfulRefresh((ReadRemoteFolderOperation
)operation
, result
);
148 } else if (operation
instanceof UploadRemoteFileOperation
) {
149 onSuccessfulUpload((UploadRemoteFileOperation
)operation
, result
);
152 Toast
.makeText(this, R
.string
.todo_operation_finished_in_success
, Toast
.LENGTH_SHORT
).show();
156 private void onSuccessfulRefresh(ReadRemoteFolderOperation operation
, RemoteOperationResult result
) {
157 mFilesAdapter
.clear();
158 List
<RemoteFile
> files
= result
.getData();
159 if (files
!= null
&& files
.size() > 0) {
160 mFilesAdapter
.addAll(files
);
161 mFilesAdapter
.remove(mFilesAdapter
.getItem(0));
163 mFilesAdapter
.notifyDataSetChanged();
166 private void onSuccessfulUpload(UploadRemoteFileOperation operation
, RemoteOperationResult result
) {
172 public void onTransferProgress(long progressRate
, long totalTransferredSoFar
, long totalToTransfer
, String fileName
) {
173 final long percentage
= totalTransferredSoFar
* 100 / totalToTransfer
;
174 final boolean upload
= fileName
.contains(getString(R
.string
.upload_folder_path
));
175 //Log.d(LOG_TAG, "progressRate " + percentage);
176 mHandler
.post(new Runnable() {
179 TextView progressView
= null
;
181 progressView
= (TextView
) findViewById(R
.id
.upload_progress
);
183 progressView
= (TextView
) findViewById(R
.id
.download_progress
);
185 if (progressView
!= null
) {
186 progressView
.setText(Long
.toString(percentage
) + "%");
194 public void onTransferProgress(long arg0
) {
195 // TODO Remove from library