1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-08 00:16:09 +00:00

Test PROPFIND with new endpoind and new network library

This commit is contained in:
davigonz 2018-04-17 18:37:00 +02:00
parent ede34920c2
commit d79bdb7893
4 changed files with 241 additions and 203 deletions

View File

@ -15,7 +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.9.1' compile 'com.squareup.okhttp3:okhttp:3.10.0'
} }
android { android {

View File

@ -27,19 +27,24 @@
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:theme="@android:style/Theme.Light.NoTitleBar">
<activity
android:name="MainActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name" android:label="@string/app_name"
android:screenOrientation="portrait" android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden"
> >
<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>

View File

@ -28,20 +28,21 @@
android:layout_height="match_parent" android:layout_height="match_parent"
> >
<Button <EditText
android:id="@+id/button_refresh" android:id="@+id/server_address"
style="@style/ButtonStyle" android:layout_width="wrap_content"
android:layout_alignParentEnd="true" android:layout_height="wrap_content"
android:layout_alignParentRight="true" android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" android:layout_alignParentTop="true"
android:enabled="false" android:layout_toLeftOf="@+id/button_check_server"
android:onClick="onClickHandler" android:layout_toStartOf="@+id/button_check_server"
android:text="@string/refresh" android:ems="10"
android:visibility="invisible"/> android:hint="Server address (with http or https)"
android:inputType="textPersonName"/>
<Button <Button
android:id="@+id/button_check_server" android:id="@+id/button_check_server"
style="@style/ButtonStyle"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/server_address" android:layout_alignBaseline="@+id/server_address"
@ -51,105 +52,103 @@
android:onClick="onClickHandler" android:onClick="onClickHandler"
android:text="@string/check_server"/> android:text="@string/check_server"/>
<Button
android:id="@+id/button_refresh"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="@id/server_address"
android:layout_marginTop="10dp"
android:onClick="onClickHandler"
android:text="@string/refresh"
android:visibility="visible"/>
<ListView <ListView
android:id="@+id/list_view" 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_above="@+id/button_upload"
android:layout_alignParentLeft="true" android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" android:layout_alignParentRight="true"
android:layout_below="@+id/button_refresh" android:layout_below="@+id/button_refresh"
android:visibility="invisible"> android:visibility="invisible">
</ListView> </ListView>
<Button <!--<Button-->
android:id="@+id/button_upload" <!--android:id="@+id/button_upload"-->
style="@style/ButtonStyle" <!--style="@style/ButtonStyle"-->
android:layout_above="@+id/frame" <!--android:layout_above="@+id/frame"-->
android:layout_alignParentLeft="true" <!--android:layout_alignParentLeft="true"-->
android:enabled="false" <!--android:enabled="false"-->
android:onClick="onClickHandler" <!--android:onClick="onClickHandler"-->
android:text="@string/upload" <!--android:text="@string/upload"-->
android:visibility="invisible"/> <!--android:visibility="invisible"/>-->
<TextView <!--<TextView-->
android:id="@+id/upload_progress" <!--android:id="@+id/upload_progress"-->
style="@style/ProgressStyle" <!--style="@style/ProgressStyle"-->
android:layout_above="@id/frame" <!--android:layout_above="@id/frame"-->
android:layout_below="@id/list_view" <!--android:layout_below="@id/list_view"-->
android:layout_toLeftOf="@+id/button_delete_remote" <!--android:layout_toLeftOf="@+id/button_delete_remote"-->
android:layout_toRightOf="@id/button_upload" <!--android:layout_toRightOf="@id/button_upload"-->
android:enabled="false" <!--android:enabled="false"-->
android:gravity="center" <!--android:gravity="center"-->
android:text="0%" <!--android:text="0%"-->
android:textSize="14sp" <!--android:textSize="14sp"-->
android:visibility="invisible"/> <!--android:visibility="invisible"/>-->
<Button <!--<Button-->
android:id="@id/button_delete_remote" <!--android:id="@id/button_delete_remote"-->
style="@style/ButtonStyle" <!--style="@style/ButtonStyle"-->
android:layout_above="@id/frame" <!--android:layout_above="@id/frame"-->
android:layout_alignParentRight="true" <!--android:layout_alignParentRight="true"-->
android:enabled="false" <!--android:enabled="false"-->
android:onClick="onClickHandler" <!--android:onClick="onClickHandler"-->
android:text="@string/delete_remote_file" <!--android:text="@string/delete_remote_file"-->
android:visibility="invisible"/> <!--android:visibility="invisible"/>-->
<FrameLayout <!--<FrameLayout-->
android:id="@id/frame" <!--android:id="@id/frame"-->
android:layout_width="match_parent" <!--android:layout_width="match_parent"-->
android:layout_height="@dimen/frame_height" <!--android:layout_height="@dimen/frame_height"-->
android:layout_alignParentLeft="true" <!--android:layout_alignParentLeft="true"-->
android:layout_alignParentRight="true" <!--android:layout_alignParentRight="true"-->
android:layout_above="@+id/button_download" <!--android:layout_above="@+id/button_download"-->
> <!--&gt;-->
</FrameLayout> <!--</FrameLayout>-->
<Button <!--<Button-->
android:id="@id/button_download" <!--android:id="@id/button_download"-->
style="@style/ButtonStyle" <!--style="@style/ButtonStyle"-->
android:layout_alignParentBottom="true" <!--android:layout_alignParentBottom="true"-->
android:layout_alignParentLeft="true" <!--android:layout_alignParentLeft="true"-->
android:enabled="false" <!--android:enabled="false"-->
android:onClick="onClickHandler" <!--android:onClick="onClickHandler"-->
android:text="@string/download" <!--android:text="@string/download"-->
android:visibility="invisible"/> <!--android:visibility="invisible"/>-->
<TextView <!--<TextView-->
android:id="@+id/download_progress" <!--android:id="@+id/download_progress"-->
style="@style/ProgressStyle" <!--style="@style/ProgressStyle"-->
android:layout_alignParentBottom="true" <!--android:layout_alignParentBottom="true"-->
android:layout_below="@id/frame" <!--android:layout_below="@id/frame"-->
android:layout_toLeftOf="@+id/button_delete_local" <!--android:layout_toLeftOf="@+id/button_delete_local"-->
android:layout_toRightOf="@id/button_download" <!--android:layout_toRightOf="@id/button_download"-->
android:enabled="false" <!--android:enabled="false"-->
android:gravity="center" <!--android:gravity="center"-->
android:text="0%" <!--android:text="0%"-->
android:textSize="14sp" <!--android:textSize="14sp"-->
android:visibility="invisible"/> <!--android:visibility="invisible"/>-->
<Button <!--<Button-->
android:id="@id/button_delete_local" <!--android:id="@id/button_delete_local"-->
style="@style/ButtonStyle" <!--style="@style/ButtonStyle"-->
android:layout_alignParentBottom="true" <!--android:layout_alignParentBottom="true"-->
android:layout_alignParentRight="true" <!--android:layout_alignParentRight="true"-->
android:enabled="false" <!--android:enabled="false"-->
android:onClick="onClickHandler" <!--android:onClick="onClickHandler"-->
android:text="@string/delete_local_file" <!--android:text="@string/delete_local_file"-->
android:visibility="invisible"/> <!--android:visibility="invisible"/>-->
<EditText
android:id="@+id/server_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/list_view"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="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"/>
</RelativeLayout> </RelativeLayout>

View File

@ -1,5 +1,5 @@
/* 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.
* *
* 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
@ -84,6 +84,16 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
private static final String NODE_VERSION = "version"; private static final String NODE_VERSION = "version";
private static final String URL = "TO COMPLETE";
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 USERNAME = "TO COMPLETE";
private static final String PASSWORD = "TO COMPLETE";
/** 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) {
@ -125,7 +135,7 @@ 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); // mFrame = findViewById(R.id.frame);
} }
@ -147,18 +157,18 @@ 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;
case R.id.button_download: // case R.id.button_download:
startDownload(); // startDownload();
break; // break;
case R.id.button_delete_local: // case R.id.button_delete_local:
startLocalDeletion(); // startLocalDeletion();
break; // 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();
} }
@ -168,16 +178,7 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
String serverAddress = ((TextView) findViewById(R.id.server_address)).getText().toString(); String serverAddress = ((TextView) findViewById(R.id.server_address)).getText().toString();
if (serverAddress.equals("")) { if (!validServerAddress(serverAddress)) return;
Toast toast = Toast.makeText(getApplicationContext(),
"Introduce a server address", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER,0,0);
toast.show();
return;
}
OkHttpClient client = new OkHttpClient(); OkHttpClient client = new OkHttpClient();
@ -188,6 +189,47 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
client.newCall(request).enqueue(new Callback() { client.newCall(request).enqueue(new Callback() {
@Override public void onResponse(Call call, final Response response) throws IOException {
if (!response.isSuccessful()) {
runOnUiThread(new Runnable() {
@Override
public void run() {
showToastMessage("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();
runOnUiThread(new Runnable() {
@Override
public void run() {
showToastMessage("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) { @Override public void onFailure(Call call, final IOException e) {
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@Override @Override
@ -203,6 +245,31 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
}); });
e.printStackTrace(); e.printStackTrace();
} }
});
}
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);
String serverAddress = ((TextView) findViewById(R.id.server_address)).getText().toString();
if (!validServerAddress(serverAddress)) return;
OkHttpClient client = new OkHttpClient();
String credentials = Credentials.basic(USERNAME, PASSWORD);
final Request request = new Request.Builder()
.url(URL + NEW_WEBDAV_PATH + USERNAME)
.addHeader("Authorization", credentials)
.method("PROPFIND", null)
.build();
client.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 {
@ -212,81 +279,33 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
@Override @Override
public void run() { public void run() {
Toast toast = Toast.makeText(getApplicationContext(), showToastMessage("Response not successful with code " + response.code());
"Response not successful with code " + response.code(), Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER,0,0);
toast.show();
} }
}); });
throw new IOException("Unexpected code " + response); throw new IOException("Unexpected code " + response);
}
try { // Response successful } else { // Successful response
String jsonData = response.body().string(); final String propFindResult = response.body().string();
JSONObject Jobject = new JSONObject(jsonData);
final String serverVersion = Jobject.get("version").toString();
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
showToastMessage(propFindResult);
Toast toast = Toast.makeText(getApplicationContext(),
"Server with version " + serverVersion + " detected", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER,0,0);
toast.show();
} }
}); });
} catch (JSONException e) {
e.printStackTrace();
}
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++) {
System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i)); System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
} }
} }
});
} }
private void startRefresh() {
// ReadRemoteFolderOperation refreshOperation = new ReadRemoteFolderOperation(FileUtils.PATH_SEPARATOR);
// refreshOperation.execute(mClient, this, mHandler);
OkHttpClient client = new OkHttpClient();
String credentials = Credentials.basic("", "");
Request request = new Request.Builder()
.url("")
.addHeader("Authorization", credentials)
.method("PROPFIND", null)
.build();
client.newCall(request).enqueue(new Callback() {
@Override public void onFailure(Call call, IOException e) { @Override public void onFailure(Call call, IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@Override public void onResponse(Call call, Response response) throws IOException {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
Headers responseHeaders = response.headers();
for (int i = 0, size = responseHeaders.size(); i < size; i++) {
System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
}
System.out.println(response.body().string());
}
}); });
} }
@ -331,8 +350,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);
} }
} }
@ -381,10 +400,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")
@ -404,11 +423,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) + "%");
} }
@ -416,4 +435,19 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
}); });
} }
private boolean validServerAddress(String serverAddress) {
if (serverAddress.equals("") || (!serverAddress.contains("http://") && !serverAddress.contains("https://"))) {
showToastMessage("Introduce a proper server address with http/https");
return false;
}
return true;
}
private void showToastMessage(String message) {
Toast toast = Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
} }