mirror of
https://github.com/owncloud/android-library.git
synced 2025-06-07 16:06:08 +00:00
Refactor MainActivity and start implementing upload
This commit is contained in:
parent
d79bdb7893
commit
bdd6a1be0c
@ -64,25 +64,25 @@
|
|||||||
android:text="@string/refresh"
|
android:text="@string/refresh"
|
||||||
android:visibility="visible"/>
|
android:visibility="visible"/>
|
||||||
|
|
||||||
<ListView
|
<Button
|
||||||
android:id="@+id/list_view"
|
android:id="@+id/button_upload"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_alignParentLeft="true"
|
android:layout_alignParentLeft="true"
|
||||||
android:layout_alignParentRight="true"
|
android:layout_below="@id/button_refresh"
|
||||||
android:layout_below="@+id/button_refresh"
|
android:layout_marginTop="10dp"
|
||||||
android:visibility="invisible">
|
android:onClick="onClickHandler"
|
||||||
</ListView>
|
android:text="@string/upload"/>
|
||||||
|
|
||||||
<!--<Button-->
|
<!--<ListView-->
|
||||||
<!--android:id="@+id/button_upload"-->
|
<!--android:id="@+id/list_view"-->
|
||||||
<!--style="@style/ButtonStyle"-->
|
<!--android:layout_width="match_parent"-->
|
||||||
<!--android:layout_above="@+id/frame"-->
|
<!--android:layout_height="wrap_content"-->
|
||||||
<!--android:layout_alignParentLeft="true"-->
|
<!--android:layout_alignParentLeft="true"-->
|
||||||
<!--android:enabled="false"-->
|
<!--android:layout_alignParentRight="true"-->
|
||||||
<!--android:onClick="onClickHandler"-->
|
<!--android:layout_below="@+id/button_refresh"-->
|
||||||
<!--android:text="@string/upload"-->
|
<!--android:visibility="invisible">-->
|
||||||
<!--android:visibility="invisible"/>-->
|
<!--</ListView>-->
|
||||||
|
|
||||||
<!--<TextView-->
|
<!--<TextView-->
|
||||||
<!--android:id="@+id/upload_progress"-->
|
<!--android:id="@+id/upload_progress"-->
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
/* ownCloud Android Library is available under MIT license
|
/* ownCloud Android Library is available under MIT license
|
||||||
* Copyright (C) 2018 ownCloud GmbH.
|
* Copyright (C) 2018 ownCloud GmbH.
|
||||||
|
*
|
||||||
|
* @author David González Verdugo
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -33,13 +35,13 @@ import android.os.Handler;
|
|||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.Gravity;
|
import android.view.Gravity;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.ListView;
|
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import com.owncloud.android.lib.common.OwnCloudClient;
|
import com.owncloud.android.lib.common.OwnCloudClient;
|
||||||
import com.owncloud.android.lib.common.OwnCloudClientFactory;
|
import com.owncloud.android.lib.common.OwnCloudClientFactory;
|
||||||
import com.owncloud.android.lib.common.authentication.OwnCloudCredentialsFactory;
|
import com.owncloud.android.lib.common.authentication.OwnCloudCredentialsFactory;
|
||||||
|
import com.owncloud.android.lib.common.network.FileRequestEntity;
|
||||||
import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
|
import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
|
||||||
import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
|
import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperation;
|
import com.owncloud.android.lib.common.operations.RemoteOperation;
|
||||||
@ -51,6 +53,7 @@ import com.owncloud.android.lib.resources.files.RemoteFile;
|
|||||||
import com.owncloud.android.lib.resources.files.RemoveRemoteFileOperation;
|
import com.owncloud.android.lib.resources.files.RemoveRemoteFileOperation;
|
||||||
import com.owncloud.android.lib.resources.files.UploadRemoteFileOperation;
|
import com.owncloud.android.lib.resources.files.UploadRemoteFileOperation;
|
||||||
|
|
||||||
|
import org.apache.commons.httpclient.methods.RequestEntity;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
@ -66,8 +69,11 @@ import okhttp3.Call;
|
|||||||
import okhttp3.Callback;
|
import okhttp3.Callback;
|
||||||
import okhttp3.Credentials;
|
import okhttp3.Credentials;
|
||||||
import okhttp3.Headers;
|
import okhttp3.Headers;
|
||||||
|
import okhttp3.MediaType;
|
||||||
|
import okhttp3.MultipartBody;
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
|
import okhttp3.RequestBody;
|
||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
|
|
||||||
public class MainActivity extends Activity implements OnRemoteOperationListener, OnDatatransferProgressListener {
|
public class MainActivity extends Activity implements OnRemoteOperationListener, OnDatatransferProgressListener {
|
||||||
@ -93,6 +99,14 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
|
|||||||
private static final String USERNAME = "TO COMPLETE";
|
private static final String USERNAME = "TO COMPLETE";
|
||||||
|
|
||||||
private static final String PASSWORD = "TO COMPLETE";
|
private static final String PASSWORD = "TO COMPLETE";
|
||||||
|
|
||||||
|
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 IF_MATCH_HEADER = "If-Match";
|
||||||
|
|
||||||
|
private OkHttpClient mOkHttpClient;
|
||||||
|
|
||||||
|
private String mCredentials;
|
||||||
|
|
||||||
/** Called when the activity is first created. */
|
/** Called when the activity is first created. */
|
||||||
@Override
|
@Override
|
||||||
@ -112,7 +126,7 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
|
|||||||
);
|
);
|
||||||
|
|
||||||
mFilesAdapter = new FilesArrayAdapter(this, R.layout.file_in_list);
|
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
|
// TODO move to background thread or task
|
||||||
AssetManager assets = getAssets();
|
AssetManager assets = getAssets();
|
||||||
@ -134,6 +148,12 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
|
|||||||
Toast.makeText(this, R.string.error_copying_sample_file, Toast.LENGTH_SHORT).show();
|
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);
|
Log.e(LOG_TAG, getString(R.string.error_copying_sample_file), e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
((TextView) findViewById(R.id.server_address)).setText(URL);
|
||||||
|
|
||||||
|
mOkHttpClient = new OkHttpClient();
|
||||||
|
|
||||||
|
mCredentials = Credentials.basic(USERNAME, PASSWORD);
|
||||||
|
|
||||||
// mFrame = findViewById(R.id.frame);
|
// mFrame = findViewById(R.id.frame);
|
||||||
}
|
}
|
||||||
@ -157,9 +177,9 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
|
|||||||
case R.id.button_refresh:
|
case R.id.button_refresh:
|
||||||
startRefresh();
|
startRefresh();
|
||||||
break;
|
break;
|
||||||
// case R.id.button_upload:
|
case R.id.button_upload:
|
||||||
// startUpload();
|
startUpload();
|
||||||
// break;
|
break;
|
||||||
// case R.id.button_delete_remote:
|
// case R.id.button_delete_remote:
|
||||||
// startRemoteDeletion();
|
// startRemoteDeletion();
|
||||||
// break;
|
// break;
|
||||||
@ -176,30 +196,20 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
|
|||||||
|
|
||||||
private void startCheck() {
|
private void startCheck() {
|
||||||
|
|
||||||
String serverAddress = ((TextView) findViewById(R.id.server_address)).getText().toString();
|
if (!validServerAddress()) return;
|
||||||
|
|
||||||
if (!validServerAddress(serverAddress)) return;
|
|
||||||
|
|
||||||
OkHttpClient client = new OkHttpClient();
|
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
Request request = new Request.Builder()
|
||||||
.url(serverAddress + "/status.php")
|
.url(URL + "/status.php")
|
||||||
.get()
|
.get()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
client.newCall(request).enqueue(new Callback() {
|
mOkHttpClient.newCall(request).enqueue(new Callback() {
|
||||||
|
|
||||||
@Override public void onResponse(Call call, final Response response) throws IOException {
|
@Override public void onResponse(Call call, final Response response) throws IOException {
|
||||||
|
|
||||||
if (!response.isSuccessful()) {
|
if (!response.isSuccessful()) {
|
||||||
|
|
||||||
runOnUiThread(new Runnable() {
|
showUnsuccessfulMessage(response.code());
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
|
|
||||||
showToastMessage("Response not successful with code " + response.code());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
throw new IOException("Unexpected code " + response);
|
throw new IOException("Unexpected code " + response);
|
||||||
}
|
}
|
||||||
@ -212,13 +222,7 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
|
|||||||
|
|
||||||
final String serverVersion = Jobject.get("version").toString();
|
final String serverVersion = Jobject.get("version").toString();
|
||||||
|
|
||||||
runOnUiThread(new Runnable() {
|
showSuccessfulMessage("Server with version " + serverVersion + " detected");
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
|
|
||||||
showToastMessage("Server with version " + serverVersion + " detected");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -255,33 +259,21 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
|
|||||||
// ReadRemoteFolderOperation refreshOperation = new ReadRemoteFolderOperation(FileUtils.PATH_SEPARATOR);
|
// ReadRemoteFolderOperation refreshOperation = new ReadRemoteFolderOperation(FileUtils.PATH_SEPARATOR);
|
||||||
// refreshOperation.execute(mClient, this, mHandler);
|
// refreshOperation.execute(mClient, this, mHandler);
|
||||||
|
|
||||||
String serverAddress = ((TextView) findViewById(R.id.server_address)).getText().toString();
|
if (!validServerAddress()) return;
|
||||||
|
|
||||||
if (!validServerAddress(serverAddress)) return;
|
|
||||||
|
|
||||||
OkHttpClient client = new OkHttpClient();
|
|
||||||
|
|
||||||
String credentials = Credentials.basic(USERNAME, PASSWORD);
|
|
||||||
|
|
||||||
final Request request = new Request.Builder()
|
final Request request = new Request.Builder()
|
||||||
.url(URL + NEW_WEBDAV_PATH + USERNAME)
|
.url(URL + NEW_WEBDAV_PATH + USERNAME)
|
||||||
.addHeader("Authorization", credentials)
|
.addHeader("Authorization", mCredentials)
|
||||||
.method("PROPFIND", null)
|
.method("PROPFIND", null)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
client.newCall(request).enqueue(new Callback() {
|
mOkHttpClient.newCall(request).enqueue(new Callback() {
|
||||||
|
|
||||||
@Override public void onResponse(Call call, final Response response) throws IOException {
|
@Override public void onResponse(Call call, final Response response) throws IOException {
|
||||||
|
|
||||||
if (!response.isSuccessful()) {
|
if (!response.isSuccessful()) {
|
||||||
|
|
||||||
runOnUiThread(new Runnable() {
|
showUnsuccessfulMessage(response.code());
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
|
|
||||||
showToastMessage("Response not successful with code " + response.code());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
throw new IOException("Unexpected code " + response);
|
throw new IOException("Unexpected code " + response);
|
||||||
|
|
||||||
@ -289,12 +281,7 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
|
|||||||
|
|
||||||
final String propFindResult = response.body().string();
|
final String propFindResult = response.body().string();
|
||||||
|
|
||||||
runOnUiThread(new Runnable() {
|
showSuccessfulMessage(propFindResult);
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
showToastMessage(propFindResult);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
Headers responseHeaders = response.headers();
|
Headers responseHeaders = response.headers();
|
||||||
for (int i = 0, size = responseHeaders.size(); i < size; i++) {
|
for (int i = 0, size = responseHeaders.size(); i < size; i++) {
|
||||||
@ -310,21 +297,67 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void startUpload() {
|
private void startUpload() {
|
||||||
|
|
||||||
File upFolder = new File(getCacheDir(), getString(R.string.upload_folder_path));
|
File upFolder = new File(getCacheDir(), getString(R.string.upload_folder_path));
|
||||||
File fileToUpload = upFolder.listFiles()[0];
|
File fileToUpload = upFolder.listFiles()[0];
|
||||||
String remotePath = FileUtils.PATH_SEPARATOR + fileToUpload.getName();
|
String remotePath = FileUtils.PATH_SEPARATOR + fileToUpload.getName();
|
||||||
String mimeType = getString(R.string.sample_file_mimetype);
|
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
|
// Get the last modification date of the file from the file system
|
||||||
Long timeStampLong = fileToUpload.lastModified()/1000;
|
Long timeStampLong = fileToUpload.lastModified()/1000;
|
||||||
String timeStamp = timeStampLong.toString();
|
String timeStamp = timeStampLong.toString();
|
||||||
|
|
||||||
UploadRemoteFileOperation uploadOperation = new UploadRemoteFileOperation(fileToUpload.getAbsolutePath(), remotePath, mimeType, timeStamp);
|
RequestEntity entity = new FileRequestEntity(fileToUpload, mimeType);
|
||||||
uploadOperation.addDatatransferProgressListener(this);
|
|
||||||
uploadOperation.execute(mClient, this, mHandler);
|
// 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 = new MultipartBody.Builder().setType(MultipartBody.FORM)
|
||||||
|
.addFormDataPart("testImage", fileToUpload.getName(), RequestBody.create(mediaType, fileToUpload))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
final Request request = new Request.Builder()
|
||||||
|
.url(URL + NEW_WEBDAV_PATH + USERNAME + remotePath)
|
||||||
|
.addHeader("Authorization", mCredentials)
|
||||||
|
.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()) {
|
||||||
|
|
||||||
|
showUnsuccessfulMessage(response.code());
|
||||||
|
|
||||||
|
throw new IOException("Unexpected code " + response);
|
||||||
|
|
||||||
|
} else { // Successful response
|
||||||
|
|
||||||
|
final String putResult = response.body().string();
|
||||||
|
|
||||||
|
showSuccessfulMessage(putResult);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public void onFailure(Call call, IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startRemoteDeletion() {
|
private void startRemoteDeletion() {
|
||||||
File upFolder = new File(getCacheDir(), getString(R.string.upload_folder_path));
|
File upFolder = new File(getCacheDir(), getString(R.string.upload_folder_path));
|
||||||
File fileToUpload = upFolder.listFiles()[0];
|
File fileToUpload = upFolder.listFiles()[0];
|
||||||
String remotePath = FileUtils.PATH_SEPARATOR + fileToUpload.getName();
|
String remotePath = FileUtils.PATH_SEPARATOR + fileToUpload.getName();
|
||||||
@ -435,7 +468,10 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean validServerAddress(String serverAddress) {
|
private boolean validServerAddress() {
|
||||||
|
|
||||||
|
String serverAddress = ((TextView) findViewById(R.id.server_address)).getText().toString();
|
||||||
|
|
||||||
if (serverAddress.equals("") || (!serverAddress.contains("http://") && !serverAddress.contains("https://"))) {
|
if (serverAddress.equals("") || (!serverAddress.contains("http://") && !serverAddress.contains("https://"))) {
|
||||||
showToastMessage("Introduce a proper server address with http/https");
|
showToastMessage("Introduce a proper server address with http/https");
|
||||||
return false;
|
return false;
|
||||||
@ -443,6 +479,26 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void showUnsuccessfulMessage (final int errorCode) {
|
||||||
|
runOnUiThread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
|
||||||
|
showToastMessage("Response not successful with code " + String.valueOf(errorCode));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void showSuccessfulMessage (final String message) {
|
||||||
|
runOnUiThread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
|
||||||
|
showToastMessage(message);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private void showToastMessage(String message) {
|
private void showToastMessage(String message) {
|
||||||
Toast toast = Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG);
|
Toast toast = Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user