mirror of
https://github.com/owncloud/android-library.git
synced 2026-08-11 16:33:03 +00:00
add dav4droid support part1
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
/* ownCloud Android Library is available under MIT license
|
||||
* Copyright (C) 2018 ownCloud GmbH.
|
||||
*
|
||||
* @author David González Verdugo
|
||||
* Copyright (C) 2016 ownCloud GmbH.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
@@ -26,35 +24,6 @@
|
||||
|
||||
package com.owncloud.android.lib.sampleclient;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.res.AssetManager;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.util.Log;
|
||||
import android.view.Gravity;
|
||||
import android.view.View;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.owncloud.android.lib.common.OwnCloudClient;
|
||||
import com.owncloud.android.lib.common.OwnCloudClientFactory;
|
||||
import com.owncloud.android.lib.common.authentication.OwnCloudCredentialsFactory;
|
||||
import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
|
||||
import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
|
||||
import com.owncloud.android.lib.common.operations.RemoteOperation;
|
||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
|
||||
import com.owncloud.android.lib.resources.files.DownloadRemoteFileOperation;
|
||||
import com.owncloud.android.lib.resources.files.FileUtils;
|
||||
import com.owncloud.android.lib.resources.files.ReadRemoteFolderOperation;
|
||||
import com.owncloud.android.lib.resources.files.RemoteFile;
|
||||
import com.owncloud.android.lib.resources.files.RemoveRemoteFileOperation;
|
||||
import com.owncloud.android.lib.resources.files.UploadRemoteFileOperation;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
@@ -63,30 +32,35 @@ import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import okhttp3.Call;
|
||||
import okhttp3.Callback;
|
||||
import okhttp3.Credentials;
|
||||
import okhttp3.Headers;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.Response;
|
||||
import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
|
||||
import com.owncloud.android.lib.common.OwnCloudClientFactory;
|
||||
import com.owncloud.android.lib.common.OwnCloudClient;
|
||||
import com.owncloud.android.lib.common.authentication.OwnCloudCredentialsFactory;
|
||||
import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
|
||||
import com.owncloud.android.lib.resources.files.RemoteFile;
|
||||
import com.owncloud.android.lib.common.operations.RemoteOperation;
|
||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
|
||||
import com.owncloud.android.lib.resources.files.DownloadRemoteFileOperation;
|
||||
import com.owncloud.android.lib.resources.files.ReadRemoteFolderOperation;
|
||||
import com.owncloud.android.lib.resources.files.RemoveRemoteFileOperation;
|
||||
import com.owncloud.android.lib.resources.files.UploadRemoteFileOperation;
|
||||
import com.owncloud.android.lib.resources.files.FileUtils;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.res.AssetManager;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.ListView;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
public class MainActivity extends Activity implements OnRemoteOperationListener, OnDatatransferProgressListener {
|
||||
|
||||
private static String LOG_TAG = MainActivity.class.getCanonicalName();
|
||||
|
||||
private static final String NODE_VERSION = "version";
|
||||
private static final String WEBDAV_PATH_4_0 = "/remote.php/webdav/";
|
||||
private static final String NEW_WEBDAV_PATH = "/remote.php/dav/files/";
|
||||
private static final String OC_TOTAL_LENGTH_HEADER = "OC-Total-Length";
|
||||
private static final String OC_X_OC_MTIME_HEADER = "X-OC-Mtime";
|
||||
private static final String AUTHORIZATION_HEADER = "Authorization";
|
||||
private static final String USER_AGENT_HEADER = "User-Agent";
|
||||
private static final String CONTENT_TYPE_HEADER = "Content-Type";
|
||||
private static final String USER_AGENT_VALUE = "Mozilla/5.0 (Android) ownCloud-android/2.7.0";
|
||||
private static final String CONTENT_TYPE_VALUE = "multipart/form-data";
|
||||
|
||||
private Handler mHandler;
|
||||
|
||||
@@ -95,10 +69,6 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
|
||||
private FilesArrayAdapter mFilesAdapter;
|
||||
|
||||
private View mFrame;
|
||||
|
||||
private OkHttpClient mOkHttpClient;
|
||||
|
||||
private String mCredentials;
|
||||
|
||||
/** Called when the activity is first created. */
|
||||
@Override
|
||||
@@ -118,7 +88,7 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
|
||||
);
|
||||
|
||||
mFilesAdapter = new FilesArrayAdapter(this, R.layout.file_in_list);
|
||||
// ((ListView)findViewById(R.id.list_view)).setAdapter(mFilesAdapter);
|
||||
((ListView)findViewById(R.id.list_view)).setAdapter(mFilesAdapter);
|
||||
|
||||
// TODO move to background thread or task
|
||||
AssetManager assets = getAssets();
|
||||
@@ -140,14 +110,8 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
|
||||
Toast.makeText(this, R.string.error_copying_sample_file, Toast.LENGTH_SHORT).show();
|
||||
Log.e(LOG_TAG, getString(R.string.error_copying_sample_file), e);
|
||||
}
|
||||
|
||||
((TextView) findViewById(R.id.server_address)).setText(getString(R.string.server_base_url));
|
||||
|
||||
mOkHttpClient = new OkHttpClient();
|
||||
|
||||
mCredentials = Credentials.basic(getString(R.string.username), getString(R.string.password));
|
||||
|
||||
// mFrame = findViewById(R.id.frame);
|
||||
mFrame = findViewById(R.id.frame);
|
||||
}
|
||||
|
||||
|
||||
@@ -163,264 +127,63 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
|
||||
|
||||
public void onClickHandler(View button) {
|
||||
switch (button.getId()) {
|
||||
case R.id.button_check_server:
|
||||
startCheck();
|
||||
break;
|
||||
case R.id.button_refresh:
|
||||
startRefresh();
|
||||
break;
|
||||
case R.id.button_upload:
|
||||
startUpload();
|
||||
break;
|
||||
case R.id.button_download:
|
||||
startDownload();
|
||||
break;
|
||||
case R.id.button_delete_remote:
|
||||
startRemoteDeletion();
|
||||
break;
|
||||
// case R.id.button_delete_local:
|
||||
// startLocalDeletion();
|
||||
// break;
|
||||
case R.id.button_download:
|
||||
startDownload();
|
||||
break;
|
||||
case R.id.button_delete_local:
|
||||
startLocalDeletion();
|
||||
break;
|
||||
default:
|
||||
Toast.makeText(this, R.string.youre_doing_it_wrong, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
private void startCheck() {
|
||||
|
||||
if (!validServerAddress()) return;
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url(getString(R.string.server_base_url) + "/status.php")
|
||||
.get()
|
||||
.build();
|
||||
|
||||
mOkHttpClient.newCall(request).enqueue(new Callback() {
|
||||
|
||||
@Override public void onResponse(Call call, final Response response) throws IOException {
|
||||
|
||||
if (!response.isSuccessful()) {
|
||||
|
||||
showMessage("Response not successful with code: " + response.code());
|
||||
|
||||
throw new IOException("Unexpected code " + response);
|
||||
}
|
||||
|
||||
try { // Successful response
|
||||
|
||||
String jsonData = response.body().string();
|
||||
|
||||
JSONObject Jobject = new JSONObject(jsonData);
|
||||
|
||||
final String serverVersion = Jobject.get("version").toString();
|
||||
|
||||
showMessage("Server with version " + serverVersion + " detected");
|
||||
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
Headers responseHeaders = response.headers();
|
||||
for (int i = 0, size = responseHeaders.size(); i < size; i++) {
|
||||
System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void onFailure(Call call, final IOException e) {
|
||||
e.printStackTrace();
|
||||
showMessage("Something was wrong: " + e.toString());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void startRefresh() {
|
||||
|
||||
// Let's first use OKHttp with the new endpoint without depending on our library operations
|
||||
|
||||
// ReadRemoteFolderOperation refreshOperation = new ReadRemoteFolderOperation(FileUtils.PATH_SEPARATOR);
|
||||
// refreshOperation.execute(mClient, this, mHandler);
|
||||
|
||||
if (!validServerAddress()) return;
|
||||
|
||||
final Request request = new Request.Builder()
|
||||
.url(getString(R.string.server_base_url) + NEW_WEBDAV_PATH + getString(R.string.username))
|
||||
.addHeader(AUTHORIZATION_HEADER, mCredentials)
|
||||
.addHeader(USER_AGENT_HEADER, USER_AGENT_VALUE)
|
||||
.method("PROPFIND", null)
|
||||
.build();
|
||||
|
||||
mOkHttpClient.newCall(request).enqueue(new Callback() {
|
||||
|
||||
@Override public void onResponse(Call call, final Response response) throws IOException {
|
||||
|
||||
if (!response.isSuccessful()) {
|
||||
|
||||
showMessage("Response not successful with code: " + response.code());
|
||||
|
||||
throw new IOException("Unexpected code " + response);
|
||||
|
||||
} else { // Successful response
|
||||
|
||||
final String propFindResult = response.body().string();
|
||||
|
||||
showMessage(propFindResult);
|
||||
|
||||
Headers responseHeaders = response.headers();
|
||||
for (int i = 0, size = responseHeaders.size(); i < size; i++) {
|
||||
System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void onFailure(Call call, IOException e) {
|
||||
showMessage("Something was wrong: " + e.toString());
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
private void startRefresh() {
|
||||
ReadRemoteFolderOperation refreshOperation = new ReadRemoteFolderOperation(FileUtils.PATH_SEPARATOR);
|
||||
refreshOperation.execute(mClient, this, mHandler);
|
||||
}
|
||||
|
||||
private void startUpload() {
|
||||
|
||||
File upFolder = new File(getCacheDir(), getString(R.string.upload_folder_path));
|
||||
final File fileToUpload = upFolder.listFiles()[0];
|
||||
File fileToUpload = upFolder.listFiles()[0];
|
||||
String remotePath = FileUtils.PATH_SEPARATOR + fileToUpload.getName();
|
||||
String mimeType = getString(R.string.sample_file_mimetype);
|
||||
|
||||
MediaType mediaType = MediaType.parse(mimeType);
|
||||
|
||||
// Get the last modification date of the file from the file system
|
||||
Long timeStampLong = fileToUpload.lastModified()/1000;
|
||||
String timeStamp = timeStampLong.toString();
|
||||
|
||||
// Let's first use OKHttp with the new endpoint without depending on our library operations
|
||||
|
||||
// UploadRemoteFileOperation uploadOperation = new UploadRemoteFileOperation(fileToUpload.getAbsolutePath(), remotePath, mimeType, timeStamp);
|
||||
// uploadOperation.addDatatransferProgressListener(this);
|
||||
// uploadOperation.execute(mClient, this, mHandler);
|
||||
|
||||
if (!validServerAddress()) return;
|
||||
|
||||
RequestBody requestBody = RequestBody.create(mediaType, fileToUpload);
|
||||
|
||||
final Request request = new Request.Builder()
|
||||
.url(getString(R.string.server_base_url) + NEW_WEBDAV_PATH + getString(R.string.username) + remotePath)
|
||||
.addHeader(AUTHORIZATION_HEADER, mCredentials)
|
||||
.addHeader(USER_AGENT_HEADER, USER_AGENT_VALUE)
|
||||
.addHeader(CONTENT_TYPE_HEADER, CONTENT_TYPE_VALUE)
|
||||
.addHeader(OC_TOTAL_LENGTH_HEADER, String.valueOf(fileToUpload.length()))
|
||||
.addHeader(OC_X_OC_MTIME_HEADER, timeStamp)
|
||||
.put(requestBody)
|
||||
.build();
|
||||
|
||||
mOkHttpClient.newCall(request).enqueue(new Callback() {
|
||||
|
||||
@Override public void onResponse(Call call, final Response response) throws IOException {
|
||||
|
||||
if (!response.isSuccessful()) {
|
||||
|
||||
showMessage("Response not successful with code: " + response.code());
|
||||
|
||||
throw new IOException("Unexpected code " + response);
|
||||
|
||||
} else { // Successful response
|
||||
|
||||
showMessage("Successful upload of " + fileToUpload.getName());
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void onFailure(Call call, IOException e) {
|
||||
e.printStackTrace();
|
||||
showMessage("Something was wrong: " + e.toString());
|
||||
}
|
||||
});
|
||||
UploadRemoteFileOperation uploadOperation = new UploadRemoteFileOperation(fileToUpload.getAbsolutePath(), remotePath, mimeType, timeStamp);
|
||||
uploadOperation.addDatatransferProgressListener(this);
|
||||
uploadOperation.execute(mClient, this, mHandler);
|
||||
}
|
||||
|
||||
private void startDownload() {
|
||||
File downFolder = new File(getCacheDir(), getString(R.string.download_folder_path));
|
||||
downFolder.mkdir();
|
||||
File upFolder = new File(getCacheDir(), getString(R.string.upload_folder_path));
|
||||
final File fileToUpload = upFolder.listFiles()[0];
|
||||
String remotePath = FileUtils.PATH_SEPARATOR + fileToUpload.getName();
|
||||
|
||||
// Let's first use OKHttp with the new endpoint without depending on our library operations
|
||||
|
||||
// DownloadRemoteFileOperation downloadOperation = new DownloadRemoteFileOperation(remotePath, downFolder.getAbsolutePath());
|
||||
// downloadOperation.addDatatransferProgressListener(this);
|
||||
// downloadOperation.execute(mClient, this, mHandler);
|
||||
|
||||
if (!validServerAddress()) return;
|
||||
|
||||
final Request request = new Request.Builder()
|
||||
.url(getString(R.string.server_base_url) + NEW_WEBDAV_PATH + getString(R.string.username) + remotePath)
|
||||
.addHeader(AUTHORIZATION_HEADER, mCredentials)
|
||||
.addHeader(USER_AGENT_HEADER, USER_AGENT_VALUE)
|
||||
.get()
|
||||
.build();
|
||||
|
||||
|
||||
mOkHttpClient.newCall(request).enqueue(new Callback() {
|
||||
|
||||
@Override public void onResponse(Call call, final Response response) throws IOException {
|
||||
|
||||
if (!response.isSuccessful()) {
|
||||
|
||||
showMessage("Response not successful with code: " + response.code());
|
||||
|
||||
throw new IOException("Unexpected code " + response);
|
||||
|
||||
} else { // Successful response
|
||||
|
||||
showMessage("Successful download of " + fileToUpload.getName() + " although local file " +
|
||||
"won't be created in this stage");
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void onFailure(Call call, IOException e) {
|
||||
e.printStackTrace();
|
||||
showMessage("Something was wrong: " + e.toString());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void startRemoteDeletion() {
|
||||
private void startRemoteDeletion() {
|
||||
File upFolder = new File(getCacheDir(), getString(R.string.upload_folder_path));
|
||||
final File fileToUpload = upFolder.listFiles()[0];
|
||||
File fileToUpload = upFolder.listFiles()[0];
|
||||
String remotePath = FileUtils.PATH_SEPARATOR + fileToUpload.getName();
|
||||
RemoveRemoteFileOperation removeOperation = new RemoveRemoteFileOperation(remotePath);
|
||||
removeOperation.execute(mClient, this, mHandler);
|
||||
}
|
||||
|
||||
// Let's first use OKHttp with the new endpoint without depending on our library operations
|
||||
// RemoveRemoteFileOperation removeOperation = new RemoveRemoteFileOperation(remotePath);
|
||||
// removeOperation.execute(mClient, this, mHandler);
|
||||
|
||||
if (!validServerAddress()) return;
|
||||
|
||||
final Request request = new Request.Builder()
|
||||
.url(getString(R.string.server_base_url) + NEW_WEBDAV_PATH + getString(R.string.username) + remotePath)
|
||||
.addHeader(AUTHORIZATION_HEADER, mCredentials)
|
||||
.addHeader(USER_AGENT_HEADER, USER_AGENT_VALUE)
|
||||
.delete()
|
||||
.build();
|
||||
|
||||
mOkHttpClient.newCall(request).enqueue(new Callback() {
|
||||
|
||||
@Override public void onResponse(Call call, final Response response) throws IOException {
|
||||
|
||||
if (!response.isSuccessful()) {
|
||||
|
||||
showMessage("Response not successful with code: " + response.code());
|
||||
|
||||
throw new IOException("Unexpected code " + response);
|
||||
|
||||
} else { // Successful response
|
||||
|
||||
showMessage("Successful deletion of " + fileToUpload.getName());
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void onFailure(Call call, IOException e) {
|
||||
e.printStackTrace();
|
||||
showMessage("Something was wrong: " + e.toString());
|
||||
}
|
||||
});
|
||||
private void startDownload() {
|
||||
File downFolder = new File(getCacheDir(), getString(R.string.download_folder_path));
|
||||
downFolder.mkdir();
|
||||
File upFolder = new File(getCacheDir(), getString(R.string.upload_folder_path));
|
||||
File fileToUpload = upFolder.listFiles()[0];
|
||||
String remotePath = FileUtils.PATH_SEPARATOR + fileToUpload.getName();
|
||||
DownloadRemoteFileOperation downloadOperation = new DownloadRemoteFileOperation(remotePath, downFolder.getAbsolutePath());
|
||||
downloadOperation.addDatatransferProgressListener(this);
|
||||
downloadOperation.execute(mClient, this, mHandler);
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@@ -430,8 +193,8 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
|
||||
if (!downloadedFile.delete() && downloadedFile.exists()) {
|
||||
Toast.makeText(this, R.string.error_deleting_local_file, Toast.LENGTH_SHORT).show();
|
||||
} else {
|
||||
// ((TextView) findViewById(R.id.download_progress)).setText("0%");
|
||||
// findViewById(R.id.frame).setBackgroundDrawable(null);
|
||||
((TextView) findViewById(R.id.download_progress)).setText("0%");
|
||||
findViewById(R.id.frame).setBackgroundDrawable(null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -480,10 +243,10 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
|
||||
|
||||
private void onSuccessfulRemoteDeletion(RemoveRemoteFileOperation operation, RemoteOperationResult result) {
|
||||
startRefresh();
|
||||
// TextView progressView = (TextView) findViewById(R.id.upload_progress);
|
||||
// if (progressView != null) {
|
||||
// progressView.setText("0%");
|
||||
// }
|
||||
TextView progressView = (TextView) findViewById(R.id.upload_progress);
|
||||
if (progressView != null) {
|
||||
progressView.setText("0%");
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@@ -503,11 +266,11 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
|
||||
@Override
|
||||
public void run() {
|
||||
TextView progressView = null;
|
||||
// if (upload) {
|
||||
// progressView = (TextView) findViewById(R.id.upload_progress);
|
||||
// } else {
|
||||
// progressView = (TextView) findViewById(R.id.download_progress);
|
||||
// }
|
||||
if (upload) {
|
||||
progressView = (TextView) findViewById(R.id.upload_progress);
|
||||
} else {
|
||||
progressView = (TextView) findViewById(R.id.download_progress);
|
||||
}
|
||||
if (progressView != null) {
|
||||
progressView.setText(Long.toString(percentage) + "%");
|
||||
}
|
||||
@@ -515,32 +278,4 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
|
||||
});
|
||||
}
|
||||
|
||||
private boolean validServerAddress() {
|
||||
|
||||
String serverAddress = ((TextView) findViewById(R.id.server_address)).getText().toString();
|
||||
|
||||
if (serverAddress.equals("") || (!serverAddress.contains("http://") && !serverAddress.contains("https://"))) {
|
||||
showToastMessage("Introduce a proper server address with http/https");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void showMessage(final String message) {
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
showToastMessage(message);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void showToastMessage(String message) {
|
||||
Toast toast = Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG);
|
||||
|
||||
toast.setGravity(Gravity.CENTER, 0, 0);
|
||||
|
||||
toast.show();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user