Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 260c5714 authored by Andy Scherzinger's avatar Andy Scherzinger Committed by GitHub
Browse files

Merge pull request #55 from nextcloud/notifications

Notifications
parents 9342d8ff 189d7e28
Loading
Loading
Loading
Loading
+23 −10
Original line number Diff line number Diff line
@@ -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;
@@ -48,6 +39,18 @@ import org.apache.commons.httpclient.HttpStatus;
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;


@@ -125,6 +128,7 @@ public class RemoteOperationResult implements Serializable {
    private String mLastPermanentLocation = null;

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

    public RemoteOperationResult(ResultCode code) {
        mCode = code;
@@ -280,6 +284,15 @@ public class RemoteOperationResult implements Serializable {
        return mData;
    }

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

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


    public boolean isSuccess() {
        return mSuccess;
    }
+2 −1
Original line number Diff line number Diff line
@@ -60,7 +60,8 @@ public class SearchOperation extends RemoteOperation {
        FAVORITE_SEARCH,
        CONTENT_TYPE_SEARCH,
        RECENTLY_MODIFIED_SEARCH,
        RECENTLY_ADDED_SEARCH
        RECENTLY_ADDED_SEARCH,
        SHARED_SEARCH
    }

    private String searchQuery;
+143 −0
Original line number Diff line number Diff line
/*  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 −0
Original line number Diff line number Diff line
/*  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;
    }
}
+273 −0
Original line number Diff line number Diff line
/*  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;

import com.google.gson.annotations.SerializedName;

import java.util.Collection;
import java.util.Date;
import java.util.Map;

/**
 * Notification data model.
 */
public class Notification {
    /**
     * Unique identifier of the notification, can be used to dismiss a notification.
     */
    @SerializedName("notification_id")
    public int notificationId;

    /**
     * Name of the app that triggered the notification.
     */
    public String app;

    /**
     * User id of the user that receives the notification.
     */
    public String user;

    /**
     * Date and time when the notification was published.
     */
    public Date datetime;

    /**
     * Type of the object the notification is about, that can be used
     * to mark a notification as resolved.
     */
    @SerializedName("object_type")
    public String objectType;

    /**
     * ID of the object the notification is about, that can be used
     * to mark a notification as resolved.
     */
    @SerializedName("object_id")
    public String objectId;

    /**
     * Translated short subject that should be presented to the user.
     */
    public String subject;

    /**
     * (Optional) Translated subject string with placeholders (see Rich Object String).
     */
    public String subjectRich;

    /**
     * (Optional) Subject parameters for {@code subjectRich} (see Rich Object String).
     */
    public Map<String, RichObject> subjectRichParameters;

    /**
     * (Optional) Translated potentially longer message that should be presented to the user.
     */
    public String message;

    /**
     * (Optional) Translated message string with placeholders (see Rich Object String).
     */
    public String messageRich;

    /**
     * (Optional) Message parameters for messageRich (see Rich Object String).
     */
    public Map<String, RichObject> messageRichParameters;

    /**
     * (Optional) A link that should be followed when the subject/message is clicked.
     */
    public String link;

    /**
     * (Optional) A link to an icon that should be shown next to the notification..
     */
    public String icon;

    /**
     * (Optional) An array of action elements.
     */
    public Collection<Action> actions;

    public Notification() {
    }

    public Notification(int notificationId,
                        String app,
                        String user,
                        Date datetime,
                        String objectType,
                        String objectId,
                        String subject,
                        String subjectRich,
                        Map<String, RichObject> subjectRichParameters,
                        String message, String messageRich,
                        Map<String, RichObject> messageRichParameters,
                        String link,
                        String icon,
                        Collection<Action> actions) {
        this.notificationId = notificationId;
        this.app = app;
        this.user = user;
        this.datetime = datetime;
        this.objectType = objectType;
        this.objectId = objectId;
        this.subject = subject;
        this.subjectRich = subjectRich;
        this.subjectRichParameters = subjectRichParameters;
        this.message = message;
        this.messageRich = messageRich;
        this.messageRichParameters = messageRichParameters;
        this.link = link;
        this.icon = icon;
        this.actions = actions;
    }

    public int getNotificationId() {
        return notificationId;
    }

    public void setNotificationId(int notificationId) {
        this.notificationId = notificationId;
    }

    public String getApp() {
        return app;
    }

    public void setApp(String app) {
        this.app = app;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public Date getDatetime() {
        return datetime;
    }

    public void setDatetime(Date datetime) {
        this.datetime = datetime;
    }

    public String getObjectType() {
        return objectType;
    }

    public void setObjectType(String objectType) {
        this.objectType = objectType;
    }

    public String getObjectId() {
        return objectId;
    }

    public void setObjectId(String objectId) {
        this.objectId = objectId;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getSubjectRich() {
        return subjectRich;
    }

    public void setSubjectRich(String subjectRich) {
        this.subjectRich = subjectRich;
    }

    public Map<String, RichObject> getSubjectRichParameters() {
        return subjectRichParameters;
    }

    public void setSubjectRichParameters(Map<String, RichObject> subjectRichParameters) {
        this.subjectRichParameters = subjectRichParameters;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getMessageRich() {
        return messageRich;
    }

    public void setMessageRich(String messageRich) {
        this.messageRich = messageRich;
    }

    public Map<String, RichObject> getMessageRichParameters() {
        return messageRichParameters;
    }

    public void setMessageRichParameters(Map<String, RichObject> messageRichParameters) {
        this.messageRichParameters = messageRichParameters;
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public Collection<Action> getActions() {
        return actions;
    }

    public void setActions(Collection<Action> actions) {
        this.actions = actions;
    }
}
Loading