Skip to content

Commit

Permalink
Merge pull request #55 from nextcloud/notifications
Browse files Browse the repository at this point in the history
Notifications
  • Loading branch information
AndyScherzinger authored Mar 30, 2017
2 parents 9342d8f + 189d7e2 commit 260c571
Show file tree
Hide file tree
Showing 8 changed files with 639 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,13 @@

package com.owncloud.android.lib.common.operations;

import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import android.accounts.Account;
import android.accounts.AccountsException;

import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;
import com.owncloud.android.lib.common.network.CertificateCombinedException;
import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.lib.resources.notifications.models.Notification;

import org.apache.commons.httpclient.ConnectTimeoutException;
import org.apache.commons.httpclient.Header;
Expand All @@ -48,6 +39,18 @@
import org.apache.jackrabbit.webdav.DavException;
import org.json.JSONException;

import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import javax.net.ssl.SSLException;


Expand Down Expand Up @@ -125,6 +128,7 @@ public enum ResultCode {
private String mLastPermanentLocation = null;

private ArrayList<Object> mData;
private List<Notification> mNotificationData;

public RemoteOperationResult(ResultCode code) {
mCode = code;
Expand Down Expand Up @@ -280,6 +284,15 @@ public ArrayList<Object> getData() {
return mData;
}

public void setNotificationData(List<Notification> notifications) {
mNotificationData = notifications;
}

public List<Notification> getNotificationData() {
return mNotificationData;
}


public boolean isSuccess() {
return mSuccess;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ public enum SearchType {
FAVORITE_SEARCH,
CONTENT_TYPE_SEARCH,
RECENTLY_MODIFIED_SEARCH,
RECENTLY_ADDED_SEARCH
RECENTLY_ADDED_SEARCH,
SHARED_SEARCH
}

private String searchQuery;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/* Nextcloud Android Library is available under MIT license
* Copyright (C) 2017 Andy Scherzinger
* Copyright (C) 2017 Mario Danic
* Copyright (C) 2017 Nextcloud GmbH
*
* @author Andy Scherzinger
* @author Mario Danic
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

package com.owncloud.android.lib.resources.notifications;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
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.models.Notification;
import com.owncloud.android.lib.resources.status.OwnCloudVersion;

import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.json.JSONException;

import java.lang.reflect.Type;
import java.util.List;

/**
* Provides the remote notifications from the server handling the following data structure
* accessible via the notifications endpoint at {@value OCS_ROUTE_LIST_V12_AND_UP}, specified at
* {@link "https://github.com/nextcloud/notifications/blob/master/docs/ocs-endpoint-v2.md"}.
*/
public class GetRemoteNotificationsOperation extends RemoteOperation {

// OCS Route
private static final String OCS_ROUTE_LIST_V12_AND_UP =
"/ocs/v2.php/apps/notifications/api/v2/notifications?format=json";
private static final String OCS_ROUTE_LIST_V9_AND_UP =
"/ocs/v2.php/apps/notifications/api/v1/notifications?format=json";


private static final String TAG = GetRemoteNotificationsOperation.class.getSimpleName();

// JSON Node names
private static final String NODE_OCS = "ocs";

private static final String NODE_DATA = "data";

/**
* This status code means that there is no app that can generate notifications.
* Slow down the polling to once per hour.
*/
public static final String STATUS_NO_CONTENT = "204";

@Override
protected RemoteOperationResult run(OwnCloudClient client) {
RemoteOperationResult result = null;
int status = -1;
GetMethod get = null;
List<Notification> notifications;
String url;
if (client.getOwnCloudVersion().compareTo(OwnCloudVersion.nextcloud_12) >= 0) {
url = client.getBaseUri() + OCS_ROUTE_LIST_V12_AND_UP;
} else {
url = client.getBaseUri() + OCS_ROUTE_LIST_V9_AND_UP;
}

// get the notifications
try {
get = new GetMethod(url);
get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);

status = client.executeMethod(get);
String response = get.getResponseBodyAsString();

if (isSuccess(status)) {
result = new RemoteOperationResult(true, status, get.getResponseHeaders());
Log_OC.d(TAG, "Successful response: " + response);

// Parse the response
notifications = parseResult(response);
result.setNotificationData(notifications);
} else {
result = new RemoteOperationResult(false, status, get.getResponseHeaders());
Log_OC.e(TAG, "Failed response while getting user notifications ");
if (response != null) {
Log_OC.e(TAG, "*** status code: " + status + " ; response message: " + response);
} else {
Log_OC.e(TAG, "*** status code: " + status);
}
}
} catch (Exception e) {
result = new RemoteOperationResult(e);
Log_OC.e(TAG, "Exception while getting remote notifications", e);
} finally {
if (get != null) {
get.releaseConnection();
}
}

return result;
}

private List<Notification> parseResult(String response) throws JSONException {
JsonParser jsonParser = new JsonParser();
JsonObject jo = (JsonObject)jsonParser.parse(response);
JsonArray jsonDataArray = jo.getAsJsonObject(NODE_OCS).getAsJsonArray(NODE_DATA);

Gson gson = new Gson();
Type listType = new TypeToken<List<Notification>>(){}.getType();
List<Notification> notifications = gson.fromJson(jsonDataArray, listType);

return notifications;
}

private boolean isSuccess(int status) {
return (status == HttpStatus.SC_OK);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* Nextcloud Android Library is available under MIT license
* Copyright (C) 2017 Andy Scherzinger
* Copyright (C) 2017 Nextcloud GmbH
*
* @author Andy Scherzinger
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

package com.owncloud.android.lib.resources.notifications.models;

/**
* Action data model.
*/
public class Action {

/**
* Translated short label of the action/button that should be presented to the user.
*/
public String label;
/**
* A link that should be followed when the action is performed/clicked.
*/
public String link;
/**
* HTTP method that should be used for the request against the link: GET, POST, DELETE.
*/
public String type;

/**
* If the action is the primary action for the notification or not.
*/
public boolean primary;

public Action() {

}

public Action(String label, String link, String type, boolean primary) {
this.label = label;
this.link = link;
this.type = type;
this.primary = primary;
}
}
Loading

0 comments on commit 260c571

Please sign in to comment.