mirror of
https://github.com/owncloud/android-library.git
synced 2025-06-08 00:16:09 +00:00
Merge pull request #187 from owncloud/test_ok_http
Test OkHttp and new endpoint before network library replacement
This commit is contained in:
commit
41fed5d9f0
@ -15,6 +15,7 @@ repositories {
|
||||
|
||||
dependencies {
|
||||
compile 'org.apache.jackrabbit:jackrabbit-webdav:2.12.4'
|
||||
compile 'com.squareup.okhttp3:okhttp:3.10.0'
|
||||
}
|
||||
|
||||
android {
|
||||
|
@ -27,19 +27,24 @@
|
||||
package="com.owncloud.android.lib.sampleclient"
|
||||
android:versionCode="1"
|
||||
android:versionName="1.0">
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET"/>
|
||||
<uses-sdk
|
||||
android:minSdkVersion="8"
|
||||
android:targetSdkVersion="26" />
|
||||
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
|
||||
<activity android:name="MainActivity"
|
||||
android:minSdkVersion="14"
|
||||
android:targetSdkVersion="26"/>
|
||||
<application
|
||||
android:icon="@drawable/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
android:theme="@android:style/Theme.Light.NoTitleBar">
|
||||
<activity
|
||||
android:name="MainActivity"
|
||||
android:configChanges="orientation|keyboardHidden"
|
||||
android:label="@string/app_name"
|
||||
android:screenOrientation="portrait"
|
||||
android:configChanges="orientation|keyboardHidden"
|
||||
>
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
<action android:name="android.intent.action.MAIN"/>
|
||||
<category android:name="android.intent.category.LAUNCHER"/>
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
|
1
sample_client/assets/oc_sample.txt
Normal file
1
sample_client/assets/oc_sample.txt
Normal file
@ -0,0 +1 @@
|
||||
Testing ownCloud uploads
|
@ -28,95 +28,146 @@
|
||||
android:layout_height="match_parent"
|
||||
>
|
||||
|
||||
<EditText
|
||||
android:id="@+id/server_address"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentLeft="true"
|
||||
android:layout_alignParentStart="true"
|
||||
android:layout_alignParentTop="true"
|
||||
android:layout_toLeftOf="@+id/button_check_server"
|
||||
android:layout_toStartOf="@+id/button_check_server"
|
||||
android:ems="10"
|
||||
android:hint="Server address (with http or https)"
|
||||
android:inputType="textPersonName"/>
|
||||
|
||||
<Button
|
||||
android:id="@+id/button_check_server"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignBaseline="@+id/server_address"
|
||||
android:layout_alignBottom="@+id/server_address"
|
||||
android:layout_alignParentEnd="true"
|
||||
android:layout_alignParentRight="true"
|
||||
android:onClick="onClickHandler"
|
||||
android:text="@string/check_server"/>
|
||||
|
||||
<Button
|
||||
android:id="@+id/button_refresh"
|
||||
style="@style/ButtonStyle"
|
||||
android:layout_alignParentLeft="true"
|
||||
android:layout_alignParentTop="true"
|
||||
android:text="@string/refresh"
|
||||
android:onClick="onClickHandler"
|
||||
/>
|
||||
|
||||
<ListView
|
||||
android:id="@+id/list_view"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@+id/button_refresh"
|
||||
android:layout_above="@+id/button_upload"
|
||||
android:layout_alignParentLeft="true"
|
||||
android:layout_alignParentEnd="true"
|
||||
android:layout_alignParentRight="true"
|
||||
>
|
||||
</ListView>
|
||||
android:layout_below="@id/server_address"
|
||||
android:layout_marginTop="10dp"
|
||||
android:onClick="onClickHandler"
|
||||
android:text="@string/refresh"/>
|
||||
|
||||
<Button
|
||||
android:id="@+id/button_upload"
|
||||
style="@style/ButtonStyle"
|
||||
android:layout_alignParentLeft="true"
|
||||
android:layout_above="@+id/frame"
|
||||
android:text="@string/upload"
|
||||
android:onClick="onClickHandler"
|
||||
/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/upload_progress"
|
||||
style="@style/ProgressStyle"
|
||||
android:layout_below="@id/list_view"
|
||||
android:layout_above="@id/frame"
|
||||
android:layout_toRightOf="@id/button_upload"
|
||||
android:layout_toLeftOf="@+id/button_delete_remote"
|
||||
android:gravity="center"
|
||||
android:textSize="14sp"
|
||||
android:text="0%"
|
||||
/>
|
||||
|
||||
<Button
|
||||
android:id="@id/button_delete_remote"
|
||||
style="@style/ButtonStyle"
|
||||
android:layout_alignParentRight="true"
|
||||
android:layout_above="@id/frame"
|
||||
android:text="@string/delete_remote_file"
|
||||
android:onClick="onClickHandler"
|
||||
/>
|
||||
|
||||
<FrameLayout
|
||||
android:id="@id/frame"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="@dimen/frame_height"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentLeft="true"
|
||||
android:layout_alignParentRight="true"
|
||||
android:layout_above="@+id/button_download"
|
||||
>
|
||||
</FrameLayout>
|
||||
android:layout_below="@id/button_refresh"
|
||||
android:layout_marginTop="10dp"
|
||||
android:onClick="onClickHandler"
|
||||
android:text="@string/upload"/>
|
||||
|
||||
<Button
|
||||
android:id="@id/button_download"
|
||||
style="@style/ButtonStyle"
|
||||
android:layout_alignParentBottom="true"
|
||||
android:id="@+id/button_download"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentLeft="true"
|
||||
android:text="@string/download"
|
||||
android:layout_below="@id/button_upload"
|
||||
android:layout_marginTop="10dp"
|
||||
android:onClick="onClickHandler"
|
||||
/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/download_progress"
|
||||
style="@style/ProgressStyle"
|
||||
android:layout_below="@id/frame"
|
||||
android:layout_alignParentBottom="true"
|
||||
android:layout_toRightOf="@id/button_download"
|
||||
android:layout_toLeftOf="@+id/button_delete_local"
|
||||
android:gravity="center"
|
||||
android:textSize="14sp"
|
||||
android:text="0%"
|
||||
/>
|
||||
android:text="@string/download"/>
|
||||
|
||||
<Button
|
||||
android:id="@id/button_delete_local"
|
||||
style="@style/ButtonStyle"
|
||||
android:layout_alignParentBottom="true"
|
||||
android:layout_alignParentRight="true"
|
||||
android:text="@string/delete_local_file"
|
||||
android:id="@+id/button_delete_remote"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentLeft="true"
|
||||
android:layout_below="@id/button_download"
|
||||
android:layout_marginTop="10dp"
|
||||
android:onClick="onClickHandler"
|
||||
/>
|
||||
android:text="@string/delete_remote_file"/>
|
||||
|
||||
<!--<ListView-->
|
||||
<!--android:id="@+id/list_view"-->
|
||||
<!--android:layout_width="match_parent"-->
|
||||
<!--android:layout_height="wrap_content"-->
|
||||
<!--android:layout_alignParentLeft="true"-->
|
||||
<!--android:layout_alignParentRight="true"-->
|
||||
<!--android:layout_below="@+id/button_refresh"-->
|
||||
<!--android:visibility="invisible">-->
|
||||
<!--</ListView>-->
|
||||
|
||||
<!--<TextView-->
|
||||
<!--android:id="@+id/upload_progress"-->
|
||||
<!--style="@style/ProgressStyle"-->
|
||||
<!--android:layout_above="@id/frame"-->
|
||||
<!--android:layout_below="@id/list_view"-->
|
||||
<!--android:layout_toLeftOf="@+id/button_delete_remote"-->
|
||||
<!--android:layout_toRightOf="@id/button_upload"-->
|
||||
<!--android:enabled="false"-->
|
||||
<!--android:gravity="center"-->
|
||||
<!--android:text="0%"-->
|
||||
<!--android:textSize="14sp"-->
|
||||
<!--android:visibility="invisible"/>-->
|
||||
|
||||
<!--<Button-->
|
||||
<!--android:id="@id/button_delete_remote"-->
|
||||
<!--style="@style/ButtonStyle"-->
|
||||
<!--android:layout_above="@id/frame"-->
|
||||
<!--android:layout_alignParentRight="true"-->
|
||||
<!--android:enabled="false"-->
|
||||
<!--android:onClick="onClickHandler"-->
|
||||
<!--android:text="@string/delete_remote_file"-->
|
||||
<!--android:visibility="invisible"/>-->
|
||||
|
||||
<!--<FrameLayout-->
|
||||
<!--android:id="@id/frame"-->
|
||||
<!--android:layout_width="match_parent"-->
|
||||
<!--android:layout_height="@dimen/frame_height"-->
|
||||
<!--android:layout_alignParentLeft="true"-->
|
||||
<!--android:layout_alignParentRight="true"-->
|
||||
<!--android:layout_above="@+id/button_download"-->
|
||||
<!-->-->
|
||||
<!--</FrameLayout>-->
|
||||
|
||||
<!--<Button-->
|
||||
<!--android:id="@id/button_download"-->
|
||||
<!--style="@style/ButtonStyle"-->
|
||||
<!--android:layout_alignParentBottom="true"-->
|
||||
<!--android:layout_alignParentLeft="true"-->
|
||||
<!--android:enabled="false"-->
|
||||
<!--android:onClick="onClickHandler"-->
|
||||
<!--android:text="@string/download"-->
|
||||
<!--android:visibility="invisible"/>-->
|
||||
|
||||
<!--<TextView-->
|
||||
<!--android:id="@+id/download_progress"-->
|
||||
<!--style="@style/ProgressStyle"-->
|
||||
<!--android:layout_alignParentBottom="true"-->
|
||||
<!--android:layout_below="@id/frame"-->
|
||||
<!--android:layout_toLeftOf="@+id/button_delete_local"-->
|
||||
<!--android:layout_toRightOf="@id/button_download"-->
|
||||
<!--android:enabled="false"-->
|
||||
<!--android:gravity="center"-->
|
||||
<!--android:text="0%"-->
|
||||
<!--android:textSize="14sp"-->
|
||||
<!--android:visibility="invisible"/>-->
|
||||
|
||||
<!--<Button-->
|
||||
<!--android:id="@id/button_delete_local"-->
|
||||
<!--style="@style/ButtonStyle"-->
|
||||
<!--android:layout_alignParentBottom="true"-->
|
||||
<!--android:layout_alignParentRight="true"-->
|
||||
<!--android:enabled="false"-->
|
||||
<!--android:onClick="onClickHandler"-->
|
||||
<!--android:text="@string/delete_local_file"-->
|
||||
<!--android:visibility="invisible"/>-->
|
||||
|
||||
</RelativeLayout>
|
||||
|
||||
|
@ -26,6 +26,7 @@
|
||||
<resources>
|
||||
<string name="app_name">ownCloud Sample Client</string>
|
||||
<string name="refresh">Refresh</string>
|
||||
<string name="check_server">Check server</string>
|
||||
<string name="upload">Upload</string>
|
||||
<string name="delete_remote_file">Delete remote file</string>
|
||||
<string name="download">Download</string>
|
||||
|
@ -1,5 +1,7 @@
|
||||
/* ownCloud Android Library is available under MIT license
|
||||
* Copyright (C) 2016 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
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
@ -24,6 +26,35 @@
|
||||
|
||||
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;
|
||||
@ -32,36 +63,31 @@ import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
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;
|
||||
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;
|
||||
|
||||
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;
|
||||
|
||||
private OwnCloudClient mClient;
|
||||
@ -70,6 +96,10 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
|
||||
|
||||
private View mFrame;
|
||||
|
||||
private OkHttpClient mOkHttpClient;
|
||||
|
||||
private String mCredentials;
|
||||
|
||||
/** Called when the activity is first created. */
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
@ -88,7 +118,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();
|
||||
@ -111,7 +141,13 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
|
||||
Log.e(LOG_TAG, getString(R.string.error_copying_sample_file), e);
|
||||
}
|
||||
|
||||
mFrame = findViewById(R.id.frame);
|
||||
((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);
|
||||
}
|
||||
|
||||
|
||||
@ -127,63 +163,264 @@ 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_delete_remote:
|
||||
startRemoteDeletion();
|
||||
break;
|
||||
case R.id.button_download:
|
||||
startDownload();
|
||||
break;
|
||||
case R.id.button_delete_local:
|
||||
startLocalDeletion();
|
||||
case R.id.button_delete_remote:
|
||||
startRemoteDeletion();
|
||||
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() {
|
||||
ReadRemoteFolderOperation refreshOperation = new ReadRemoteFolderOperation(FileUtils.PATH_SEPARATOR);
|
||||
refreshOperation.execute(mClient, this, mHandler);
|
||||
|
||||
// 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 startUpload() {
|
||||
|
||||
File upFolder = new File(getCacheDir(), getString(R.string.upload_folder_path));
|
||||
File fileToUpload = upFolder.listFiles()[0];
|
||||
final 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();
|
||||
|
||||
UploadRemoteFileOperation uploadOperation = new UploadRemoteFileOperation(fileToUpload.getAbsolutePath(), remotePath, mimeType, timeStamp);
|
||||
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 = 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());
|
||||
}
|
||||
}
|
||||
|
||||
private void startRemoteDeletion() {
|
||||
File upFolder = new File(getCacheDir(), getString(R.string.upload_folder_path));
|
||||
File fileToUpload = upFolder.listFiles()[0];
|
||||
String remotePath = FileUtils.PATH_SEPARATOR + fileToUpload.getName();
|
||||
RemoveRemoteFileOperation removeOperation = new RemoveRemoteFileOperation(remotePath);
|
||||
removeOperation.execute(mClient, this, mHandler);
|
||||
@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];
|
||||
final 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);
|
||||
|
||||
// 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() {
|
||||
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
|
||||
// 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());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@ -193,8 +430,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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -243,10 +480,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")
|
||||
@ -266,11 +503,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) + "%");
|
||||
}
|
||||
@ -278,4 +515,32 @@ 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();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user