mirror of
https://github.com/owncloud/android-library.git
synced 2025-06-08 08:26:10 +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 {
|
dependencies {
|
||||||
compile 'org.apache.jackrabbit:jackrabbit-webdav:2.12.4'
|
compile 'org.apache.jackrabbit:jackrabbit-webdav:2.12.4'
|
||||||
|
compile 'com.squareup.okhttp3:okhttp:3.10.0'
|
||||||
}
|
}
|
||||||
|
|
||||||
android {
|
android {
|
||||||
|
@ -24,22 +24,27 @@
|
|||||||
-->
|
-->
|
||||||
|
|
||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
package="com.owncloud.android.lib.sampleclient"
|
package="com.owncloud.android.lib.sampleclient"
|
||||||
android:versionCode="1"
|
android:versionCode="1"
|
||||||
android:versionName="1.0">
|
android:versionName="1.0">
|
||||||
<uses-permission android:name="android.permission.INTERNET" />
|
|
||||||
|
<uses-permission android:name="android.permission.INTERNET"/>
|
||||||
<uses-sdk
|
<uses-sdk
|
||||||
android:minSdkVersion="8"
|
android:minSdkVersion="14"
|
||||||
android:targetSdkVersion="26" />
|
android:targetSdkVersion="26"/>
|
||||||
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
|
<application
|
||||||
<activity android:name="MainActivity"
|
android:icon="@drawable/ic_launcher"
|
||||||
android:label="@string/app_name"
|
android:label="@string/app_name"
|
||||||
android:screenOrientation="portrait"
|
android:theme="@android:style/Theme.Light.NoTitleBar">
|
||||||
android:configChanges="orientation|keyboardHidden"
|
<activity
|
||||||
>
|
android:name="MainActivity"
|
||||||
|
android:configChanges="orientation|keyboardHidden"
|
||||||
|
android:label="@string/app_name"
|
||||||
|
android:screenOrientation="portrait"
|
||||||
|
>
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.MAIN" />
|
<action android:name="android.intent.action.MAIN"/>
|
||||||
<category android:name="android.intent.category.LAUNCHER" />
|
<category android:name="android.intent.category.LAUNCHER"/>
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
</activity>
|
</activity>
|
||||||
</application>
|
</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
|
@ -24,99 +24,150 @@
|
|||||||
-->
|
-->
|
||||||
|
|
||||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
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
|
<Button
|
||||||
android:id="@+id/button_refresh"
|
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_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_below="@+id/button_refresh"
|
android:layout_alignParentEnd="true"
|
||||||
android:layout_above="@+id/button_upload"
|
|
||||||
android:layout_alignParentLeft="true"
|
|
||||||
android:layout_alignParentRight="true"
|
android:layout_alignParentRight="true"
|
||||||
>
|
android:layout_below="@id/server_address"
|
||||||
</ListView>
|
android:layout_marginTop="10dp"
|
||||||
|
android:onClick="onClickHandler"
|
||||||
|
android:text="@string/refresh"/>
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
android:id="@+id/button_upload"
|
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_width="match_parent"
|
||||||
android:layout_height="@dimen/frame_height"
|
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_above="@+id/button_download"
|
android:layout_marginTop="10dp"
|
||||||
>
|
android:onClick="onClickHandler"
|
||||||
</FrameLayout>
|
android:text="@string/upload"/>
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
android:id="@id/button_download"
|
android:id="@+id/button_download"
|
||||||
style="@style/ButtonStyle"
|
android:layout_width="match_parent"
|
||||||
android:layout_alignParentBottom="true"
|
android:layout_height="wrap_content"
|
||||||
android:layout_alignParentLeft="true"
|
android:layout_alignParentLeft="true"
|
||||||
android:text="@string/download"
|
android:layout_below="@id/button_upload"
|
||||||
|
android:layout_marginTop="10dp"
|
||||||
android:onClick="onClickHandler"
|
android:onClick="onClickHandler"
|
||||||
/>
|
android:text="@string/download"/>
|
||||||
|
|
||||||
<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%"
|
|
||||||
/>
|
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
android:id="@id/button_delete_local"
|
android:id="@+id/button_delete_remote"
|
||||||
style="@style/ButtonStyle"
|
android:layout_width="match_parent"
|
||||||
android:layout_alignParentBottom="true"
|
android:layout_height="wrap_content"
|
||||||
android:layout_alignParentRight="true"
|
android:layout_alignParentLeft="true"
|
||||||
android:text="@string/delete_local_file"
|
android:layout_below="@id/button_download"
|
||||||
|
android:layout_marginTop="10dp"
|
||||||
android:onClick="onClickHandler"
|
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>
|
</RelativeLayout>
|
||||||
|
|
||||||
|
@ -26,6 +26,7 @@
|
|||||||
<resources>
|
<resources>
|
||||||
<string name="app_name">ownCloud Sample Client</string>
|
<string name="app_name">ownCloud Sample Client</string>
|
||||||
<string name="refresh">Refresh</string>
|
<string name="refresh">Refresh</string>
|
||||||
|
<string name="check_server">Check server</string>
|
||||||
<string name="upload">Upload</string>
|
<string name="upload">Upload</string>
|
||||||
<string name="delete_remote_file">Delete remote file</string>
|
<string name="delete_remote_file">Delete remote file</string>
|
||||||
<string name="download">Download</string>
|
<string name="download">Download</string>
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
/* ownCloud Android Library is available under MIT license
|
/* 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
|
* 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
|
||||||
@ -24,6 +26,35 @@
|
|||||||
|
|
||||||
package com.owncloud.android.lib.sampleclient;
|
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.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -32,36 +63,31 @@ import java.util.ArrayList;
|
|||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
|
import okhttp3.Call;
|
||||||
import com.owncloud.android.lib.common.OwnCloudClientFactory;
|
import okhttp3.Callback;
|
||||||
import com.owncloud.android.lib.common.OwnCloudClient;
|
import okhttp3.Credentials;
|
||||||
import com.owncloud.android.lib.common.authentication.OwnCloudCredentialsFactory;
|
import okhttp3.Headers;
|
||||||
import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
|
import okhttp3.MediaType;
|
||||||
import com.owncloud.android.lib.resources.files.RemoteFile;
|
import okhttp3.OkHttpClient;
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperation;
|
import okhttp3.Request;
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
|
import okhttp3.RequestBody;
|
||||||
import com.owncloud.android.lib.resources.files.DownloadRemoteFileOperation;
|
import okhttp3.Response;
|
||||||
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 {
|
public class MainActivity extends Activity implements OnRemoteOperationListener, OnDatatransferProgressListener {
|
||||||
|
|
||||||
private static String LOG_TAG = MainActivity.class.getCanonicalName();
|
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 Handler mHandler;
|
||||||
|
|
||||||
private OwnCloudClient mClient;
|
private OwnCloudClient mClient;
|
||||||
@ -70,6 +96,10 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
|
|||||||
|
|
||||||
private View mFrame;
|
private View mFrame;
|
||||||
|
|
||||||
|
private OkHttpClient mOkHttpClient;
|
||||||
|
|
||||||
|
private String mCredentials;
|
||||||
|
|
||||||
/** Called when the activity is first created. */
|
/** Called when the activity is first created. */
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
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);
|
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();
|
||||||
@ -111,7 +141,13 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
|
|||||||
Log.e(LOG_TAG, getString(R.string.error_copying_sample_file), e);
|
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) {
|
public void onClickHandler(View button) {
|
||||||
switch (button.getId()) {
|
switch (button.getId()) {
|
||||||
|
case R.id.button_check_server:
|
||||||
|
startCheck();
|
||||||
|
break;
|
||||||
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_download:
|
||||||
|
startDownload();
|
||||||
|
break;
|
||||||
case R.id.button_delete_remote:
|
case R.id.button_delete_remote:
|
||||||
startRemoteDeletion();
|
startRemoteDeletion();
|
||||||
break;
|
break;
|
||||||
case R.id.button_download:
|
// case R.id.button_delete_local:
|
||||||
startDownload();
|
// startLocalDeletion();
|
||||||
break;
|
// break;
|
||||||
case R.id.button_delete_local:
|
|
||||||
startLocalDeletion();
|
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
Toast.makeText(this, R.string.youre_doing_it_wrong, Toast.LENGTH_SHORT).show();
|
Toast.makeText(this, R.string.youre_doing_it_wrong, Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startRefresh() {
|
private void startCheck() {
|
||||||
ReadRemoteFolderOperation refreshOperation = new ReadRemoteFolderOperation(FileUtils.PATH_SEPARATOR);
|
|
||||||
refreshOperation.execute(mClient, this, mHandler);
|
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 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];
|
final 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);
|
// Let's first use OKHttp with the new endpoint without depending on our library operations
|
||||||
uploadOperation.addDatatransferProgressListener(this);
|
|
||||||
uploadOperation.execute(mClient, this, mHandler);
|
// 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());
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startRemoteDeletion() {
|
private void startDownload() {
|
||||||
File upFolder = new File(getCacheDir(), getString(R.string.upload_folder_path));
|
File downFolder = new File(getCacheDir(), getString(R.string.download_folder_path));
|
||||||
File fileToUpload = upFolder.listFiles()[0];
|
downFolder.mkdir();
|
||||||
String remotePath = FileUtils.PATH_SEPARATOR + fileToUpload.getName();
|
File upFolder = new File(getCacheDir(), getString(R.string.upload_folder_path));
|
||||||
RemoveRemoteFileOperation removeOperation = new RemoveRemoteFileOperation(remotePath);
|
final File fileToUpload = upFolder.listFiles()[0];
|
||||||
removeOperation.execute(mClient, this, mHandler);
|
String remotePath = FileUtils.PATH_SEPARATOR + fileToUpload.getName();
|
||||||
}
|
|
||||||
|
|
||||||
private void startDownload() {
|
// Let's first use OKHttp with the new endpoint without depending on our library operations
|
||||||
File downFolder = new File(getCacheDir(), getString(R.string.download_folder_path));
|
|
||||||
downFolder.mkdir();
|
// 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));
|
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 remotePath = FileUtils.PATH_SEPARATOR + fileToUpload.getName();
|
||||||
DownloadRemoteFileOperation downloadOperation = new DownloadRemoteFileOperation(remotePath, downFolder.getAbsolutePath());
|
|
||||||
downloadOperation.addDatatransferProgressListener(this);
|
// Let's first use OKHttp with the new endpoint without depending on our library operations
|
||||||
downloadOperation.execute(mClient, this, mHandler);
|
// 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")
|
@SuppressWarnings("deprecation")
|
||||||
@ -193,8 +430,8 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
|
|||||||
if (!downloadedFile.delete() && downloadedFile.exists()) {
|
if (!downloadedFile.delete() && downloadedFile.exists()) {
|
||||||
Toast.makeText(this, R.string.error_deleting_local_file, Toast.LENGTH_SHORT).show();
|
Toast.makeText(this, R.string.error_deleting_local_file, Toast.LENGTH_SHORT).show();
|
||||||
} else {
|
} else {
|
||||||
((TextView) findViewById(R.id.download_progress)).setText("0%");
|
// ((TextView) findViewById(R.id.download_progress)).setText("0%");
|
||||||
findViewById(R.id.frame).setBackgroundDrawable(null);
|
// findViewById(R.id.frame).setBackgroundDrawable(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -243,10 +480,10 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
|
|||||||
|
|
||||||
private void onSuccessfulRemoteDeletion(RemoveRemoteFileOperation operation, RemoteOperationResult result) {
|
private void onSuccessfulRemoteDeletion(RemoveRemoteFileOperation operation, RemoteOperationResult result) {
|
||||||
startRefresh();
|
startRefresh();
|
||||||
TextView progressView = (TextView) findViewById(R.id.upload_progress);
|
// TextView progressView = (TextView) findViewById(R.id.upload_progress);
|
||||||
if (progressView != null) {
|
// if (progressView != null) {
|
||||||
progressView.setText("0%");
|
// progressView.setText("0%");
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
@SuppressWarnings("deprecation")
|
||||||
@ -266,11 +503,11 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
|
|||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
TextView progressView = null;
|
TextView progressView = null;
|
||||||
if (upload) {
|
// if (upload) {
|
||||||
progressView = (TextView) findViewById(R.id.upload_progress);
|
// progressView = (TextView) findViewById(R.id.upload_progress);
|
||||||
} else {
|
// } else {
|
||||||
progressView = (TextView) findViewById(R.id.download_progress);
|
// progressView = (TextView) findViewById(R.id.download_progress);
|
||||||
}
|
// }
|
||||||
if (progressView != null) {
|
if (progressView != null) {
|
||||||
progressView.setText(Long.toString(percentage) + "%");
|
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