-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from nextcloud/notifications
Notifications
- Loading branch information
Showing
8 changed files
with
639 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
src/com/owncloud/android/lib/resources/notifications/GetRemoteNotificationsOperation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/com/owncloud/android/lib/resources/notifications/models/Action.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.