execute(User user, Context context) {
- return execute(user.toPlatformAccount(), context);
- }
-
- /**
- * Synchronously executes the remote operation on the received ownCloud account.
- *
- * Do not call this method from the main thread.
- *
- * This method should be used whenever an ownCloud account is available, instead of
- * {@link #execute(NextcloudClient)}.
- *
- * @param account ownCloud account in remote ownCloud server to reach during the
- * execution of the operation.
- * @param context Android context for the component calling the method.
- * @return Result of the operation.
- */
- public RemoteOperationResult executeNextcloudClient(@NonNull Account account, @NonNull Context context) {
- mAccount = account;
- mContext = context.getApplicationContext();
- try {
- OwnCloudAccount ocAccount = new OwnCloudAccount(mAccount, mContext);
- clientNew = OwnCloudClientManagerFactory.getDefaultSingleton().getNextcloudClientFor(ocAccount, mContext);
- } catch (Exception e) {
- Log_OC.e(TAG, "Error while trying to access to " + mAccount.name, e);
- return new RemoteOperationResult(e);
- }
- return run(clientNew);
- }
+ protected Activity mCallerActivity;
- /**
- * This is a transitional wrapper around {@link #executeNextcloudClient(Account, Context)}
- * using modern {@link User} interface instead of platform {@link Account}
- */
- public RemoteOperationResult executeNextcloudClient(@NonNull User user, @NonNull Context context) {
- return executeNextcloudClient(user.toPlatformAccount(), context);
- }
-
- /**
- * Synchronously executes the remote operation
- *
- * Do not call this method from the main thread.
- *
- * @param client Client object to reach an ownCloud server during the execution of the operation.
- * @return Result of the operation.
- */
- @Deprecated
- public RemoteOperationResult execute(OwnCloudClient client) {
- if (client == null) {
- throw new IllegalArgumentException("Trying to execute a remote operation with a NULL OwnCloudClient");
- }
- mClient = client;
- return run(client);
- }
+
- /**
- * Synchronously executes the remote operation
- *
- * Do not call this method from the main thread.
- *
- * @param client Client object to reach an ownCloud server during the execution of
- * the operation.
- * @return Result of the operation.
- */
- public RemoteOperationResult execute(@NonNull NextcloudClient client) {
- clientNew = client;
+
- return run(client);
- }
+
+
/**
* Asynchronously executes the remote operation
*
* This method should be used whenever an ownCloud account is available, instead of
- * {@link #execute(OwnCloudClient)}.
+ * {@link LegacyRemoteOperation#execute(OwnCloudClient)}.
*
* @deprecated This method will be removed in version 1.0.
* Use {@link #execute(Account, Context, OnRemoteOperationListener,
@@ -351,99 +222,7 @@ public Thread execute(OwnCloudClient client, OnRemoteOperationListener listener,
return runnerThread;
}
- /**
- * Asynchronous execution of the operation
- * started by {@link RemoteOperation#execute(OwnCloudClient,
- * OnRemoteOperationListener, Handler)},
- * and result posting.
- *
- * TODO refactor && clean the code; now it's a mess
- */
- @Override
- public final void run() {
- RemoteOperationResult result = null;
- boolean repeat;
- do {
- try{
- if (mClient == null) {
- if (mAccount != null && mContext != null) {
- /** DEPRECATED BLOCK - will be removed at version 1.0 */
- if (mCallerActivity != null) {
- mClient = OwnCloudClientFactory.createOwnCloudClient(
- mAccount, mContext, mCallerActivity);
- } else {
- /** EOF DEPRECATED */
- OwnCloudAccount ocAccount = new OwnCloudAccount(mAccount, mContext);
- mClient = OwnCloudClientManagerFactory.getDefaultSingleton().
- getClientFor(ocAccount, mContext);
- }
-
- } else {
- throw new IllegalStateException("Trying to run a remote operation " +
- "asynchronously with no client instance or account");
- }
- }
-
- } catch (IOException e) {
- Log_OC.e(TAG, "Error while trying to access to " + mAccount.name,
- new AccountsException("I/O exception while trying to authorize the account",
- e));
- result = new RemoteOperationResult(e);
- } catch (AccountsException e) {
- Log_OC.e(TAG, "Error while trying to access to " + mAccount.name, e);
- result = new RemoteOperationResult(e);
- }
-
- if (result == null)
- result = run(mClient);
-
- repeat = false;
- /** DEPRECATED BLOCK - will be removed at version 1.0 ; don't trust in this code
- * to trigger authentication update */
- if (mCallerActivity != null && mAccount != null && mContext != null &&
- !result.isSuccess() &&
- ResultCode.UNAUTHORIZED == result.getCode()
- ) {
- /// possible fail due to lack of authorization
- // in an operation performed in foreground
- OwnCloudCredentials cred = mClient.getCredentials();
- if (cred != null) {
- /// confirmed : unauthorized operation
- AccountManager am = AccountManager.get(mContext);
- if (cred.authTokenExpires()) {
- am.invalidateAuthToken(
- mAccount.type,
- cred.getAuthToken()
- );
- } else {
- am.clearPassword(mAccount);
- }
- mClient = null;
- // when repeated, the creation of a new OwnCloudClient after erasing the saved
- // credentials will trigger the login activity
- repeat = true;
- result = null;
- }
- }
- /** EOF DEPRECATED BLOCK **/
- } while (repeat);
-
- if (mAccount != null && mContext != null) {
- // Save Client Cookies
- AccountUtils.saveClient(mClient, mAccount, mContext);
- }
-
- final RemoteOperationResult resultToSend = result;
- if (mListenerHandler != null && mListener != null) {
- mListenerHandler.post(new Runnable() {
- @Override
- public void run() {
- mListener.onRemoteOperationFinish(RemoteOperation.this, resultToSend);
- }
- });
- }
- }
/**
@@ -455,4 +234,8 @@ public final OwnCloudClient getClient() {
return mClient;
}
+ @Override
+ public void run() {
+ throw new NotImplementedError("this should never be used!");
+ }
}
diff --git a/library/src/main/java/com/owncloud/android/lib/resources/OCSNextcloudRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/OCSNextcloudRemoteOperation.java
new file mode 100644
index 000000000..5e709fb91
--- /dev/null
+++ b/library/src/main/java/com/owncloud/android/lib/resources/OCSNextcloudRemoteOperation.java
@@ -0,0 +1,63 @@
+/*
+ * Nextcloud Android client application
+ *
+ * @author Tobias Kaminsky
+ * Copyright (C) 2023 Tobias Kaminsky
+ * Copyright (C) 2023 Nextcloud GmbH
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see .
+ *
+ */
+
+package com.owncloud.android.lib.resources;
+
+import com.google.gson.Gson;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonParser;
+import com.google.gson.reflect.TypeToken;
+import com.nextcloud.common.OkHttpMethodBase;
+import com.owncloud.android.lib.common.operations.NextcloudRemoteOperation;
+
+import org.apache.commons.httpclient.HttpMethodBase;
+
+import java.io.IOException;
+
+/**
+ * Base class for OCS remote operations with convenient methods
+ *
+ * @author Bartosz Przybylski
+ */
+public abstract class OCSNextcloudRemoteOperation extends NextcloudRemoteOperation {
+
+ @Deprecated
+ public T getServerResponse(HttpMethodBase method, TypeToken type) throws IOException {
+ String response = method.getResponseBodyAsString();
+ JsonParser parser = new JsonParser();
+ JsonElement element = parser.parse(response);
+
+ Gson gson = new Gson();
+
+ return gson.fromJson(element, type.getType());
+ }
+
+ public T getServerResponse(OkHttpMethodBase method, TypeToken type) {
+ String response = method.getResponseBodyAsString();
+ JsonParser parser = new JsonParser();
+ JsonElement element = parser.parse(response);
+
+ Gson gson = new Gson();
+
+ return gson.fromJson(element, type.getType());
+ }
+}
diff --git a/library/src/main/java/com/owncloud/android/lib/resources/OCSRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/OCSRemoteOperation.java
index 0c496ea0b..371e8a617 100644
--- a/library/src/main/java/com/owncloud/android/lib/resources/OCSRemoteOperation.java
+++ b/library/src/main/java/com/owncloud/android/lib/resources/OCSRemoteOperation.java
@@ -32,7 +32,7 @@
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.nextcloud.common.OkHttpMethodBase;
-import com.owncloud.android.lib.common.operations.RemoteOperation;
+import com.owncloud.android.lib.common.operations.LegacyRemoteOperation;
import org.apache.commons.httpclient.HttpMethodBase;
@@ -43,7 +43,7 @@
*
* @author Bartosz Przybylski
*/
-public abstract class OCSRemoteOperation extends RemoteOperation {
+public abstract class OCSRemoteOperation extends LegacyRemoteOperation {
@Deprecated
public T getServerResponse(HttpMethodBase method, TypeToken type) throws IOException {
diff --git a/library/src/main/java/com/owncloud/android/lib/resources/activities/GetActivitiesRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/activities/GetActivitiesRemoteOperation.java
index 01c0e9f9d..000f8d4b0 100644
--- a/library/src/main/java/com/owncloud/android/lib/resources/activities/GetActivitiesRemoteOperation.java
+++ b/library/src/main/java/com/owncloud/android/lib/resources/activities/GetActivitiesRemoteOperation.java
@@ -36,8 +36,7 @@
import com.google.gson.reflect.TypeToken;
import com.nextcloud.common.NextcloudClient;
import com.nextcloud.operations.GetMethod;
-import com.owncloud.android.lib.common.OwnCloudClient;
-import com.owncloud.android.lib.common.operations.RemoteOperation;
+import com.owncloud.android.lib.common.operations.NextcloudRemoteOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.lib.resources.activities.model.Activity;
@@ -46,9 +45,7 @@
import com.owncloud.android.lib.resources.activities.models.PreviewObject;
import com.owncloud.android.lib.resources.activities.models.PreviewObjectAdapter;
-import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpStatus;
-import org.apache.commons.httpclient.NameValuePair;
import java.io.IOException;
import java.lang.reflect.Type;
@@ -61,7 +58,7 @@
* accessible via the activities endpoint at {@value OCS_ROUTE_V12_AND_UP}, specified at
* {@link "https://github.com/nextcloud/activity/blob/master/docs/endpoint-v2.md"}.
*/
-public class GetActivitiesRemoteOperation extends RemoteOperation {
+public class GetActivitiesRemoteOperation extends NextcloudRemoteOperation {
private static final String TAG = GetActivitiesRemoteOperation.class.getSimpleName();
@@ -163,86 +160,6 @@ public RemoteOperationResult run(NextcloudClient client) {
return result;
}
-
- @Override
- protected RemoteOperationResult run(OwnCloudClient client) {
- RemoteOperationResult result;
- int status;
- org.apache.commons.httpclient.methods.GetMethod get = null;
- ArrayList activities;
- String url = client.getBaseUri() + OCS_ROUTE_V12_AND_UP;
-
- // add filter for fileId, if available
- if (fileId > 0) {
- url = url + "/filter";
- }
-
- Log_OC.d(TAG, "URL: " + url);
-
- try {
- get = new org.apache.commons.httpclient.methods.GetMethod(url);
- get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
-
- ArrayList parameters = new ArrayList<>();
- parameters.add(new NameValuePair("format", "json"));
- parameters.add(new NameValuePair("previews", "true"));
-
- if (lastGiven != -1) {
- parameters.add(new NameValuePair("since", String.valueOf(lastGiven)));
- }
-
- if (fileId > 0) {
- parameters.add(new NameValuePair("sort", "desc"));
- parameters.add(new NameValuePair("object_type", "files"));
- parameters.add(new NameValuePair("object_id", String.valueOf(fileId)));
- }
-
- get.setQueryString(parameters.toArray(new NameValuePair[]{}));
-
- status = client.executeMethod(get);
- String response = get.getResponseBodyAsString();
-
- Header nextPageHeader = get.getResponseHeader("X-Activity-Last-Given");
- if (nextPageHeader != null) {
- lastGiven = Integer.parseInt(nextPageHeader.getValue());
- } else {
- lastGiven = -1;
- }
-
- if (isSuccess(status)) {
- Log_OC.d(TAG, "Successful response: " + response);
- result = new RemoteOperationResult(true, status, get.getResponseHeaders());
- // Parse the response
- if (response == null) {
- activities = new ArrayList<>();
- } else {
- activities = parseResult(response);
- }
-
- ArrayList