Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch all notification to V2
Browse files Browse the repository at this point in the history
Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
tobiasKaminsky authored and AlvaroBrey committed Dec 15, 2022
1 parent 9f5ca72 commit f9b15ca
Showing 7 changed files with 55 additions and 58 deletions.
40 changes: 19 additions & 21 deletions app/src/main/java/com/nextcloud/client/jobs/NotificationWork.kt
Original file line number Diff line number Diff line change
@@ -41,11 +41,16 @@ import com.google.gson.Gson
import com.nextcloud.client.account.User
import com.nextcloud.client.account.UserAccountManager
import com.nextcloud.client.integrations.deck.DeckApi
import com.nextcloud.common.NextcloudClient
import com.nextcloud.common.OkHttpMethodBase
import com.nextcloud.operations.DeleteMethod
import com.nextcloud.operations.GetMethod
import com.nextcloud.operations.PutMethod
import com.nextcloud.operations.Utf8PostMethod
import com.owncloud.android.R
import com.owncloud.android.datamodel.DecryptedPushMessage
import com.owncloud.android.lib.common.OwnCloudClient
import com.owncloud.android.lib.common.OwnCloudClientManagerFactory
import com.owncloud.android.lib.common.operations.RemoteOperation
import com.owncloud.android.lib.common.operations.RemoteOperationResult
import com.owncloud.android.lib.common.utils.Log_OC
import com.owncloud.android.lib.resources.notifications.DeleteNotificationRemoteOperation
import com.owncloud.android.lib.resources.notifications.GetNotificationRemoteOperation
@@ -56,12 +61,7 @@ import com.owncloud.android.ui.notifications.NotificationUtils
import com.owncloud.android.utils.PushUtils
import com.owncloud.android.utils.theme.ViewThemeUtils
import dagger.android.AndroidInjection
import org.apache.commons.httpclient.HttpMethod
import org.apache.commons.httpclient.HttpStatus
import org.apache.commons.httpclient.methods.DeleteMethod
import org.apache.commons.httpclient.methods.GetMethod
import org.apache.commons.httpclient.methods.PutMethod
import org.apache.commons.httpclient.methods.Utf8PostMethod
import java.io.IOException
import java.security.GeneralSecurityException
import java.security.PrivateKey
@@ -251,12 +251,11 @@ class NotificationWork constructor(
val user = optionalUser.get()
try {
val client = OwnCloudClientManagerFactory.getDefaultSingleton()
.getClientFor(user.toOwnCloudAccount(), context)
val result = GetNotificationRemoteOperation(decryptedPushMessage.nid)
.execute(client)
.getNextcloudClientFor(user.toOwnCloudAccount(), context)
val result: RemoteOperationResult<Notification> =
GetNotificationRemoteOperation(decryptedPushMessage.nid).execute(client)
if (result.isSuccess) {
val notification = result.notificationData[0]
sendNotification(notification, account)
sendNotification(result.resultData, account)
}
} catch (e: Exception) {
Log_OC.e(this, "Error creating account", e)
@@ -300,7 +299,7 @@ class NotificationWork constructor(
if (optionalUser.isPresent) {
val user = optionalUser.get()
val client = OwnCloudClientManagerFactory.getDefaultSingleton()
.getClientFor(user.toOwnCloudAccount(), context)
.getNextcloudClientFor(user.toOwnCloudAccount(), context)
val actionType = intent.getStringExtra(KEY_NOTIFICATION_ACTION_TYPE)
val actionLink = intent.getStringExtra(KEY_NOTIFICATION_ACTION_LINK)
val success: Boolean = if (!actionType.isNullOrEmpty() && !actionLink.isNullOrEmpty()) {
@@ -331,18 +330,17 @@ class NotificationWork constructor(
}

@Suppress("ReturnCount") // legacy code
private fun executeAction(actionType: String, actionLink: String, client: OwnCloudClient): Int {
val method: HttpMethod
private fun executeAction(actionType: String, actionLink: String, client: NextcloudClient): Int {
val method: OkHttpMethodBase
method = when (actionType) {
"GET" -> GetMethod(actionLink)
"POST" -> Utf8PostMethod(actionLink)
"DELETE" -> DeleteMethod(actionLink)
"PUT" -> PutMethod(actionLink)
"GET" -> GetMethod(actionLink, true)
"POST" -> Utf8PostMethod(actionLink, true, null)
"DELETE" -> DeleteMethod(actionLink, true)
"PUT" -> PutMethod(actionLink, true, null)
else -> return 0 // do nothing
}
method.setRequestHeader(RemoteOperation.OCS_API_HEADER, RemoteOperation.OCS_API_HEADER_VALUE)
try {
return client.executeMethod(method)
return client.execute(method)
} catch (e: IOException) {
Log_OC.e(TAG, "Execution of notification action failed: $e")
}
Original file line number Diff line number Diff line change
@@ -34,13 +34,12 @@
import com.nextcloud.client.account.UserAccountManager;
import com.nextcloud.client.jobs.NotificationWork;
import com.nextcloud.client.network.ClientFactory;
import com.nextcloud.common.NextcloudClient;
import com.nextcloud.java.util.Optional;
import com.owncloud.android.R;
import com.owncloud.android.databinding.NotificationsLayoutBinding;
import com.owncloud.android.datamodel.ArbitraryDataProvider;
import com.owncloud.android.datamodel.ArbitraryDataProviderImpl;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.lib.resources.notifications.GetNotificationsRemoteOperation;
@@ -68,7 +67,7 @@ public class NotificationsActivity extends DrawerActivity implements Notificatio
private NotificationsLayoutBinding binding;
private NotificationListAdapter adapter;
private Snackbar snackbar;
private OwnCloudClient client;
private NextcloudClient client;
private Optional<User> optionalUser;

@Inject ClientFactory clientFactory;
@@ -226,13 +225,11 @@ private void fetchAndSetData() {
Thread t = new Thread(() -> {
initializeAdapter();

RemoteOperation getRemoteNotificationOperation = new GetNotificationsRemoteOperation();
final RemoteOperationResult result = getRemoteNotificationOperation.execute(client);
GetNotificationsRemoteOperation getRemoteNotificationOperation = new GetNotificationsRemoteOperation();
final RemoteOperationResult<List<? extends Notification>> result = getRemoteNotificationOperation.execute(client);

if (result.isSuccess() && result.getNotificationData() != null) {
final List<Notification> notifications = result.getNotificationData();

runOnUiThread(() -> populateList(notifications));
if (result.isSuccess() && result.getResultData() != null) {
runOnUiThread(() -> populateList((List<Notification>) result.getResultData()));
} else {
Log_OC.d(TAG, result.getLogMessage());
// show error
@@ -249,7 +246,7 @@ private void initializeClient() {
if (client == null && optionalUser.isPresent()) {
try {
User user = optionalUser.get();
client = clientFactory.create(user);
client = clientFactory.createNextcloudClient(user);
} catch (ClientFactory.CreationException e) {
Log_OC.e(TAG, "Error initializing client", e);
}
Original file line number Diff line number Diff line change
@@ -46,9 +46,9 @@
import com.bumptech.glide.load.resource.file.FileToStreamDecoder;
import com.caverock.androidsvg.SVG;
import com.google.android.material.button.MaterialButton;
import com.nextcloud.common.NextcloudClient;
import com.owncloud.android.R;
import com.owncloud.android.databinding.NotificationListItemBinding;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.resources.notifications.models.Action;
import com.owncloud.android.lib.resources.notifications.models.Notification;
import com.owncloud.android.lib.resources.notifications.models.RichObject;
@@ -80,11 +80,11 @@ public class NotificationListAdapter extends RecyclerView.Adapter<NotificationLi
private final ForegroundColorSpan foregroundColorSpanBlack;

private final List<Notification> notificationsList;
private final OwnCloudClient client;
private final NextcloudClient client;
private final NotificationsActivity notificationsActivity;
private final ViewThemeUtils viewThemeUtils;

public NotificationListAdapter(OwnCloudClient client,
public NotificationListAdapter(NextcloudClient client,
NotificationsActivity notificationsActivity,
ViewThemeUtils viewThemeUtils) {
this.notificationsList = new ArrayList<>();
Original file line number Diff line number Diff line change
@@ -23,18 +23,18 @@

import android.os.AsyncTask;

import com.owncloud.android.lib.common.OwnCloudClient;
import com.nextcloud.common.NextcloudClient;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.resources.notifications.DeleteAllNotificationsRemoteOperation;
import com.owncloud.android.lib.resources.notifications.models.Action;
import com.owncloud.android.ui.activity.NotificationsActivity;
import com.owncloud.android.ui.notifications.NotificationsContract;

public class DeleteAllNotificationsTask extends AsyncTask<Action, Void, Boolean> {
private OwnCloudClient client;
private NextcloudClient client;
private NotificationsContract.View notificationsActivity;

public DeleteAllNotificationsTask(OwnCloudClient client, NotificationsActivity notificationsActivity) {
public DeleteAllNotificationsTask(NextcloudClient client, NotificationsActivity notificationsActivity) {
this.client = client;
this.notificationsActivity = notificationsActivity;
}
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@

import android.os.AsyncTask;

import com.owncloud.android.lib.common.OwnCloudClient;
import com.nextcloud.common.NextcloudClient;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.resources.notifications.DeleteNotificationRemoteOperation;
import com.owncloud.android.lib.resources.notifications.models.Action;
@@ -35,10 +35,10 @@
public class DeleteNotificationTask extends AsyncTask<Action, Void, Boolean> {
private Notification notification;
private NotificationListAdapter.NotificationViewHolder holder;
private OwnCloudClient client;
private NextcloudClient client;
private NotificationsContract.View notificationsActivity;

public DeleteNotificationTask(OwnCloudClient client, Notification notification,
public DeleteNotificationTask(NextcloudClient client, Notification notification,
NotificationListAdapter.NotificationViewHolder holder,
NotificationsActivity notificationsActivity) {
this.client = client;
Original file line number Diff line number Diff line change
@@ -2,31 +2,30 @@

import android.os.AsyncTask;

import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.nextcloud.common.NextcloudClient;
import com.nextcloud.common.OkHttpMethodBase;
import com.nextcloud.operations.DeleteMethod;
import com.nextcloud.operations.GetMethod;
import com.nextcloud.operations.PutMethod;
import com.nextcloud.operations.Utf8PostMethod;
import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.lib.resources.notifications.models.Action;
import com.owncloud.android.lib.resources.notifications.models.Notification;
import com.owncloud.android.ui.activity.NotificationsActivity;
import com.owncloud.android.ui.adapter.NotificationListAdapter;

import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.Utf8PostMethod;

import java.io.IOException;

public class NotificationExecuteActionTask extends AsyncTask<Action, Void, Boolean> {

private NotificationListAdapter.NotificationViewHolder holder;
private OwnCloudClient client;
private NextcloudClient client;
private Notification notification;
private NotificationsActivity notificationsActivity;

public NotificationExecuteActionTask(OwnCloudClient client,
public NotificationExecuteActionTask(NextcloudClient client,
NotificationListAdapter.NotificationViewHolder holder,
Notification notification,
NotificationsActivity notificationsActivity) {
@@ -38,36 +37,39 @@ public NotificationExecuteActionTask(OwnCloudClient client,

@Override
protected Boolean doInBackground(Action... actions) {
HttpMethod method;
OkHttpMethodBase method;
Action action = actions[0];

if (action.link == null) {
Log_OC.e(this, "Link is empty!");
return Boolean.FALSE;
}

switch (action.type) {
case "GET":
method = new GetMethod(action.link);
method = new GetMethod(action.link, true);
break;

case "POST":
method = new Utf8PostMethod(action.link);
method = new Utf8PostMethod(action.link, true, null);
break;

case "DELETE":
method = new DeleteMethod(action.link);
method = new DeleteMethod(action.link, true);
break;

case "PUT":
method = new PutMethod(action.link);
method = new PutMethod(action.link, true, null);
break;

default:
// do nothing
return Boolean.FALSE;
}

method.setRequestHeader(RemoteOperation.OCS_API_HEADER, RemoteOperation.OCS_API_HEADER_VALUE);

int status;
try {
status = client.executeMethod(method);
status = client.execute(method);
} catch (IOException e) {
Log_OC.e(this, "Execution of notification action failed: " + e);
return Boolean.FALSE;
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ buildscript {
daggerVersion = "2.44.2"
markwonVersion = "4.6.2"
prismVersion = "2.0.0"
androidLibraryVersion = "master-SNAPSHOT"
androidLibraryVersion = "notificationV2-SNAPSHOT"
mockitoVersion = "4.9.0"
mockitoKotlinVersion = "4.1.0"
mockkVersion = "1.13.3"

0 comments on commit f9b15ca

Please sign in to comment.