Skip to content

Commit

Permalink
Add activity previews
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
  • Loading branch information
nickvergessen authored and tobiasKaminsky committed Oct 12, 2018
1 parent 8fd0dd4 commit 313cac0
Show file tree
Hide file tree
Showing 5 changed files with 233 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.lib.resources.activities.models.Activity;
import com.owncloud.android.lib.resources.activities.models.PreviewListAdapter;
import com.owncloud.android.lib.resources.activities.models.PreviewObject;
import com.owncloud.android.lib.resources.activities.models.RichElement;
import com.owncloud.android.lib.resources.activities.models.RichElementTypeAdapter;
import com.owncloud.android.lib.resources.status.OwnCloudVersion;

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

import java.lang.reflect.Type;
import java.util.ArrayList;
Expand All @@ -63,7 +65,6 @@ public class GetRemoteActivitiesOperation extends RemoteOperation {
// OCS Routes
private static final String OCS_ROUTE_V12_AND_UP = "/ocs/v2.php/apps/activity/api/v2/activity";
private static final String OCS_ROUTE_PRE_V12 = "/ocs/v1.php/cloud/activity";
private static final String FORMAT_JSON = "?format=json";

// JSON Node names
private static final String NODE_OCS = "ocs";
Expand Down Expand Up @@ -104,9 +105,7 @@ protected RemoteOperationResult run(OwnCloudClient client) {

// add filter for fileId, if available
if (!fileId.isEmpty()) {
url = url + "/filter" + FORMAT_JSON + "&sort=desc&object_type=files&object_id=" + fileId;
} else if (nextUrl.isEmpty()){
url = url + FORMAT_JSON;
url = url + "/filter";
}

Log_OC.d(TAG, "URL: " + url);
Expand All @@ -115,6 +114,21 @@ protected RemoteOperationResult run(OwnCloudClient client) {
get = new GetMethod(url);
get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);

ArrayList<NameValuePair> parameters = new ArrayList<>();
parameters.add(new NameValuePair("format", "json"));

if (client.getOwnCloudVersion().compareTo(OwnCloudVersion.nextcloud_12) >= 0) {
parameters.add(new NameValuePair("previews", "true"));
}

if (!fileId.isEmpty()) {
parameters.add(new NameValuePair("sort", "desc"));
parameters.add(new NameValuePair("object_type", "files"));
parameters.add(new NameValuePair("object_id", fileId));
}

get.setQueryString(parameters.toArray(new NameValuePair[]{}));

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

Expand Down Expand Up @@ -169,13 +183,14 @@ public boolean hasMoreActivities() {
return !nextUrl.isEmpty();
}

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

Gson gson = new GsonBuilder()
.registerTypeAdapter(RichElement.class,new RichElementTypeAdapter())//Add TypeAdapter to parse RichElement
.registerTypeAdapter(RichElement.class, new RichElementTypeAdapter())
.registerTypeAdapter(PreviewObject.class, new PreviewListAdapter())
.create();
Type listType = new TypeToken<List<Activity>>(){}.getType();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@

import com.google.gson.annotations.SerializedName;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* Activity Data Model
Expand Down Expand Up @@ -57,6 +59,8 @@ public class Activity {
@SerializedName("object_name")
public String objectName;

public List<PreviewObject> previews;

@SerializedName("subject_rich")
public RichElement richSubjectElement;

Expand Down Expand Up @@ -180,4 +184,12 @@ public RichElement getRichSubjectElement() {
public void setRichSubjectElement(RichElement richSubjectElement) {
this.richSubjectElement = richSubjectElement;
}

public List<PreviewObject> getPreviews() {
return previews;
}

public void setPreviews(ArrayList<PreviewObject> previews) {
this.previews = previews;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/* Nextcloud Android Library is available under MIT license
* Copyright (C) 2017 Joas Schilling
*
* @author Joas Schilling
*
* 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.activities.models;

import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;

/**
* PreviewList Parser
*/

public class PreviewListAdapter extends TypeAdapter<PreviewObject> {

@Override
public void write(JsonWriter out, PreviewObject value) {

}

@Override
public PreviewObject read(JsonReader in) throws IOException {
PreviewObject preview = new PreviewObject();
in.beginObject();

while (in.hasNext()) {
String name = in.nextName();
if (name != null && !name.isEmpty()) {
preview = readObject(name, in);
}
}


in.endObject();

return preview;
}

private PreviewObject readObject(String tag, JsonReader in) throws IOException {
PreviewObject preview = new PreviewObject();

do {
if (in.peek() == JsonToken.NAME) {
tag = in.nextName();
}

switch (tag) {
case "source":
preview.setSource(in.nextString());
break;

case "link":
preview.setLink(in.nextString());
break;

case "isMimeTypeIcon":
preview.setMimeTypeIcon(in.nextBoolean());
break;

case "mimeType":
preview.setMimeType(in.nextString());
break;

case "fileId":
preview.setFileId(in.nextInt());
break;

case "view":
preview.setView(in.nextString());
break;

default:
// do nothing
break;
}
} while (in.hasNext());

return preview;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/* Nextcloud Android Library is available under MIT license
* Copyright (C) 2017 Joas Schilling
*
* @author Joas Schilling
*
* 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.activities.models;

/**
* PreviewObject Data Model
*/

public class PreviewObject {

private int fileId;
private String source;
private String link;
private Boolean isMimeTypeIcon;
private String mimeType;
private String view;

public PreviewObject() {
}

public String getLink() {
return link;
}

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

public String getSource() {
return source;
}

public void setSource(String source) {
this.source = source;
}

public Boolean isMimeTypeIcon() {
return isMimeTypeIcon;
}

public void setMimeTypeIcon(Boolean mimeTypeIcon) {
isMimeTypeIcon = mimeTypeIcon;
}

public String getMimeType() {
return mimeType;
}

public void setMimeType(String mimeType) {
this.mimeType = mimeType;
}

public int getFileId() {
return fileId;
}

public void setFileId(int fileId) {
this.fileId = fileId;
}

public String getView() {
return view;
}

public void setView(String view) {
this.view = view;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@

public class RichElement {

public String richSubject;
ArrayList<RichObject> richObjectList = new ArrayList<>();
private String richSubject;
private ArrayList<RichObject> richObjectList = new ArrayList<>();


public String getRichSubject() {
Expand Down

0 comments on commit 313cac0

Please sign in to comment.