Skip to content

Commit

Permalink
WIP: implementing HEAD-requests to check new notifications(#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xf104a committed Oct 23, 2023
1 parent 5d99f8f commit 53da14b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,55 @@
* The inheritors of this interface should be passed to NotificationService.
*/
public interface NextcloudAbstractAPI extends StatusCheckable {
/**
* Gets all notifications from server
* @param service PollUpdateListener which handles notifications
* @return notifications response as a JSONObject
*/
JSONObject getNotifications(PollUpdateListener service);

/**
* Removes notification from server
* @param id id of notification to remove
*/
void removeNotification(int id);

/**
* Sends reply to talk chatroom
* @param chatroom id of a chat
* @param message message to send
* @throws IOException in case of network error
*/
void sendTalkReply(String chatroom, String message) throws IOException;

/**
* Get user avatar
* @param userId username to get avatar of
* @return avatar bitmap
* @throws Exception in case of any errors
*/
Bitmap getUserAvatar(String userId) throws Exception;

/**
* Gets image preview from server
* @param path path to image
* @return bitmap received from server
* @throws Exception in case of any errors
*/
Bitmap getImagePreview(String path) throws Exception;

/**
* Executes action which is inside of notifications
* @param link Link to action
* @param method method which should be used for querying link
* @throws Exception in case of any errors
*/
void sendAction(String link, String method) throws Exception;

/**
* @doc Checks new notifications without querying all of them directly
* @return true if there is new notifications on server
* @throws Exception in case of any error
*/
boolean checkNewNotifications() throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ public void sendAction(String link,
+ link + "--" + responseCode);
}

@Override
public boolean checkNewNotifications() throws Exception {
//TODO: implement checking that new notifications available via HEAD request
return false;
}

@Override
public JSONObject getNotifications(PollUpdateListener service) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ public void sendAction(String link, String method) throws Exception {
API.performNetworkRequest(request);
}

@Override
public boolean checkNewNotifications() throws Exception {
//TODO: implement checking that new notifications available via HEAD request
return false;
}

@Override
public Status getStatus(Context context) {
if(lastPollSuccessful){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,19 @@
import com.polar.nextcloudservices.Services.Status.StatusController;

class PollTask extends AsyncTask<NotificationService, Void, JSONObject> {

private static final String TAG = "NotificationService.PollTask";
@Override
protected JSONObject doInBackground(NotificationService... services) {
return services[0].getAPI().getNotifications(services[0]);
NextcloudAbstractAPI api = services[0].getAPI();
try {
if(api.checkNewNotifications()) {
return api.getNotifications(services[0]);
}
} catch (Exception e) {
Log.e(TAG, "Can not check new notifications");
e.printStackTrace();
}
return null;
}
}

Expand All @@ -57,7 +66,9 @@ public String getStatus() {
}

public void onPollFinished(JSONObject response) {
mNotificationController.onNotificationsUpdated(response);
if(response != null) {
mNotificationController.onNotificationsUpdated(response);
}
}

@Override
Expand Down

0 comments on commit 53da14b

Please sign in to comment.