5642554bc4169dc6330fad32f3e13593895fdbc1
[pub/Android/ownCloud.git] / oc_framework / sample_client / src / com / owncloud / android / oc_framework / sampleclient / MainActivity.java
1 package com.owncloud.android.oc_framework.sampleclient;
2
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.util.Iterator;
8 import java.util.List;
9
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;
23
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;
35
36 public class MainActivity extends Activity implements OnRemoteOperationListener, OnDatatransferProgressListener {
37
38 private static String LOG_TAG = MainActivity.class.getCanonicalName();
39
40 private Handler mHandler;
41
42 private WebdavClient mClient;
43
44 private FilesArrayAdapter mFilesAdapter;
45
46 private View mFrame;
47
48 /** Called when the activity is first created. */
49 @Override
50 public void onCreate(Bundle savedInstanceState) {
51 super.onCreate(savedInstanceState);
52 setContentView(R.layout.main);
53
54 mHandler = new Handler();
55
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));
59
60 mFilesAdapter = new FilesArrayAdapter(this, R.layout.file_in_list);
61 ((ListView)findViewById(R.id.list_view)).setAdapter(mFilesAdapter);
62
63 // TODO move to background thread or task
64 AssetManager assets = getAssets();
65 try {
66 String sampleFileName = getString(R.string.sample_file_name);
67 File upFolder = new File(getCacheDir(), getString(R.string.upload_folder_path));
68 upFolder.mkdir();
69 File upFile = new File(upFolder, sampleFileName);
70 FileOutputStream fos = new FileOutputStream(upFile);
71 InputStream is = assets.open(sampleFileName);
72 int count = 0;
73 byte[] buffer = new byte[1024];
74 while ((count = is.read(buffer, 0, buffer.length)) >= 0) {
75 fos.write(buffer, 0, count);
76 }
77 is.close();
78 fos.close();
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);
82 }
83
84 mFrame = findViewById(R.id.frame);
85 }
86
87
88 @Override
89 public void onDestroy() {
90 File upFolder = new File(getCacheDir(), getString(R.string.upload_folder_path));
91 File upFile = upFolder.listFiles()[0];
92 upFile.delete();
93 upFolder.delete();
94 super.onDestroy();
95 }
96
97
98 public void onClickHandler(View button) {
99 switch (button.getId()) {
100 case R.id.button_refresh:
101 startRefresh();
102 break;
103 case R.id.button_upload:
104 startUpload();
105 break;
106 case R.id.button_delete_remote:
107 startRemoteDeletion();
108 break;
109 case R.id.button_download:
110 startDownload();
111 break;
112 case R.id.button_delete_local:
113 startLocalDeletion();
114 break;
115 default:
116 Toast.makeText(this, R.string.youre_doing_it_wrong, Toast.LENGTH_SHORT).show();
117 }
118 }
119
120 private void startRefresh() {
121 ReadRemoteFolderOperation refreshOperation = new ReadRemoteFolderOperation(FileUtils.PATH_SEPARATOR);
122 refreshOperation.execute(mClient, this, mHandler);
123 }
124
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);
133 }
134
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);
141 }
142
143 private void startDownload() {
144 File downFolder = new File(getCacheDir(), getString(R.string.download_folder_path));
145 downFolder.mkdir();
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);
154 }
155
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();
162 } else {
163 ((TextView) findViewById(R.id.download_progress)).setText("0%");
164 findViewById(R.id.frame).setBackgroundDrawable(null);
165 }
166 }
167
168 @Override
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());
173
174 } else if (operation instanceof ReadRemoteFolderOperation) {
175 onSuccessfulRefresh((ReadRemoteFolderOperation)operation, result);
176
177 } else if (operation instanceof UploadRemoteFileOperation ) {
178 onSuccessfulUpload((UploadRemoteFileOperation)operation, result);
179
180 } else if (operation instanceof RemoveRemoteFileOperation ) {
181 onSuccessfulRemoteDeletion((RemoveRemoteFileOperation)operation, result);
182
183 } else if (operation instanceof DownloadRemoteFileOperation ) {
184 onSuccessfulDownload((DownloadRemoteFileOperation)operation, result);
185
186 } else {
187 Toast.makeText(this, R.string.todo_operation_finished_in_success, Toast.LENGTH_SHORT).show();
188 }
189 }
190
191 private void onSuccessfulRefresh(ReadRemoteFolderOperation operation, RemoteOperationResult result) {
192 mFilesAdapter.clear();
193 List<RemoteFile> files = result.getData();
194 if (files != null) {
195 Iterator<RemoteFile> it = files.iterator();
196 while (it.hasNext()) {
197 mFilesAdapter.add(it.next());
198 }
199 mFilesAdapter.remove(mFilesAdapter.getItem(0));
200 }
201 mFilesAdapter.notifyDataSetChanged();
202 }
203
204 private void onSuccessfulUpload(UploadRemoteFileOperation operation, RemoteOperationResult result) {
205 startRefresh();
206 }
207
208 private void onSuccessfulRemoteDeletion(RemoveRemoteFileOperation operation, RemoteOperationResult result) {
209 startRefresh();
210 TextView progressView = (TextView) findViewById(R.id.upload_progress);
211 if (progressView != null) {
212 progressView.setText("0%");
213 }
214 }
215
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);
222 }
223
224 @Override
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() {
230 @Override
231 public void run() {
232 TextView progressView = null;
233 if (upload) {
234 progressView = (TextView) findViewById(R.id.upload_progress);
235 } else {
236 progressView = (TextView) findViewById(R.id.download_progress);
237 }
238 if (progressView != null) {
239 progressView.setText(Long.toString(percentage) + "%");
240 }
241 }
242 });
243 }
244
245
246 @Override
247 public void onTransferProgress(long arg0) {
248 // TODO Remove from library
249 }
250
251 }