Skip to content

Commit

Permalink
WIP: basic interfaces for using websocket provided by notify_push(#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xf104a committed Nov 19, 2023
1 parent 01c4348 commit c13a877
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 46 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ dependencies {
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
implementation 'com.github.invissvenska:NumberPickerPreference:1.0.3'
implementation "com.github.nextcloud:Android-SingleSignOn:0.6.1"
implementation 'org.java-websocket:Java-WebSocket:1.5.4'

// Image Loading Library:Glide
implementation 'com.github.bumptech.glide:glide:4.15.1'
Expand Down
22 changes: 14 additions & 8 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.INTERNET" />
<!-- Permissions for old android versions -->
<uses-permission android:name="android.permission.INTERNET" /> <!-- Permissions for old android versions -->
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />

Expand All @@ -21,18 +20,24 @@
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:usesCleartextTraffic="true"
android:theme="@style/Theme.NextcloudServices">
android:theme="@style/Theme.NextcloudServices"
android:usesCleartextTraffic="true">
<service
android:name=".Services.NotificationWebsocketService"
android:enabled="true"
android:exported="true"></service>

<activity android:name=".CreditsActivity"
<activity
android:name=".CreditsActivity"
android:parentActivityName=".SettingsActivity" />

<service
android:name=".Services.NotificationService"
android:name=".Services.NotificationPollService"
android:enabled="true"
android:exported="false" />

<receiver android:name=".BootReceiver"
<receiver
android:name=".BootReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
Expand All @@ -44,9 +49,10 @@
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

import android.graphics.Bitmap;

import com.polar.nextcloudservices.Services.PollUpdateListener;
import com.polar.nextcloudservices.Services.NotificationListener;
import com.polar.nextcloudservices.Services.Status.StatusCheckable;

import org.java_websocket.client.WebSocketClient;
import org.json.JSONObject;

import java.io.IOException;

/*
* Nextcloud abstract API crates possibility to use different libraries for
* Nextcloud abstract API creates possibility to use different libraries for
* polling for notifications. This is needed to use Nextcloud SSO library
* since it does not give per-app key.
* The inheritors of this interface should be passed to NotificationService.
Expand All @@ -21,7 +22,7 @@ public interface NextcloudAbstractAPI extends StatusCheckable {
* @param service PollUpdateListener which handles notifications
* @return notifications response as a JSONObject
*/
JSONObject getNotifications(PollUpdateListener service);
JSONObject getNotifications(NotificationListener service);

/**
* Removes notification from server
Expand Down Expand Up @@ -67,4 +68,12 @@ public interface NextcloudAbstractAPI extends StatusCheckable {
* @throws Exception in case of any error
*/
boolean checkNewNotifications() throws Exception;

/**
* @doc Gets websocket client which is authorized and receives notification updates
* @return WebsocketClient instance which holds pre-authorized connection
* @throws Exception in case of any unhandlable error
*/
WebSocketClient getNotificationsWebsocket() throws Exception;

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
import androidx.annotation.NonNull;

import com.polar.nextcloudservices.BuildConfig;
import com.polar.nextcloudservices.Services.PollUpdateListener;
import com.polar.nextcloudservices.Services.NotificationListener;
import com.polar.nextcloudservices.Services.Settings.ServiceSettings;
import com.polar.nextcloudservices.Services.Status.Status;

import org.java_websocket.client.WebSocketClient;
import org.json.JSONException;
import org.json.JSONObject;

Expand Down Expand Up @@ -196,7 +197,12 @@ public boolean checkNewNotifications() throws Exception {
}

@Override
public JSONObject getNotifications(PollUpdateListener service) {
public WebSocketClient getNotificationsWebsocket() throws Exception {
return null;
}

@Override
public JSONObject getNotifications(NotificationListener service) {
try {
HttpURLConnection conn = request("/ocs/v2.php/apps/notifications/api/v2/notifications",
"GET", true);
Expand All @@ -216,7 +222,7 @@ public JSONObject getNotifications(PollUpdateListener service) {
JSONObject response = new JSONObject(buffer.toString());
lastPollSuccessful = true;

service.onPollFinished(response);
service.onNewNotifications(response);
return response;
} catch (JSONException e) {
Log.e(TAG, "Error parsing JSON");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
import com.nextcloud.android.sso.QueryParam;
import com.nextcloud.android.sso.aidl.NextcloudRequest;
import com.nextcloud.android.sso.api.NextcloudAPI;
import com.nextcloud.android.sso.api.Response;
import com.nextcloud.android.sso.model.SingleSignOnAccount;
import com.polar.nextcloudservices.Services.PollUpdateListener;
import com.polar.nextcloudservices.Services.NotificationListener;
import com.polar.nextcloudservices.Services.Status.Status;

import org.java_websocket.client.WebSocketClient;
import org.json.JSONException;
import org.json.JSONObject;

Expand All @@ -30,7 +30,6 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class NextcloudSSOAPI implements NextcloudAbstractAPI {
final private NextcloudAPI API;
Expand All @@ -56,7 +55,7 @@ public void onError(Exception ex) {
}

@Override
public JSONObject getNotifications(PollUpdateListener service) {
public JSONObject getNotifications(NotificationListener service) {
Log.d(TAG, "getNotifications");
Map<String, List<String>> header = new HashMap<>();
LinkedList<String> values = new LinkedList<>();
Expand Down Expand Up @@ -85,7 +84,7 @@ public JSONObject getNotifications(PollUpdateListener service) {

try {
JSONObject response = new JSONObject(buffer.toString());
service.onPollFinished(response);
service.onNewNotifications(response);
Log.d(TAG, "Setting lastPollSuccessful as true");
lastPollSuccessful = true;
return response;
Expand Down Expand Up @@ -175,6 +174,11 @@ public boolean checkNewNotifications() throws Exception {
return true;
}

@Override
public WebSocketClient getNotificationsWebsocket() throws Exception {
return null;
}

@Override
public Status getStatus(Context context) {
if(lastPollSuccessful){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
import android.util.Log;
import android.os.Build;

import com.polar.nextcloudservices.Services.NotificationService;
import com.polar.nextcloudservices.Services.NotificationPollService;


public class BootReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent _intent = new Intent(context, NotificationService.class);
Intent _intent = new Intent(context, NotificationPollService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(_intent);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package com.polar.nextcloudservices.Notification;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import androidx.core.app.NotificationCompat;

import com.polar.nextcloudservices.Services.NotificationService;

import org.json.JSONObject;

import java.util.Vector;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.polar.nextcloudservices.Services;

import org.json.JSONObject;

public interface NotificationListener {
void onNewNotifications(JSONObject response);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
import com.polar.nextcloudservices.Services.Settings.ServiceSettings;
import com.polar.nextcloudservices.Services.Status.StatusController;

class PollTask extends AsyncTask<NotificationService, Void, JSONObject> {
private static final String TAG = "NotificationService.PollTask";
class PollTask extends AsyncTask<NotificationPollService, Void, JSONObject> {
private static final String TAG = "Services.NotificationPollService.PollTask";
@Override
protected JSONObject doInBackground(NotificationService... services) {
protected JSONObject doInBackground(NotificationPollService... services) {
Log.d(TAG, "Checking notifications");
NextcloudAbstractAPI api = services[0].getAPI();
try {
Expand All @@ -47,10 +47,10 @@ protected JSONObject doInBackground(NotificationService... services) {
}
}

public class NotificationService extends Service implements PollUpdateListener {
public class NotificationPollService extends Service implements NotificationListener {
// constant
public Integer pollingInterval = null;
public static final String TAG = "Services.NotificationService";
public static final String TAG = "Services.NotificationPollService";
private Binder mBinder;
private PollTimerTask task;
public NextcloudAbstractAPI mAPI;
Expand All @@ -67,7 +67,7 @@ public String getStatus() {
return mStatusController.getStatusString();
}

public void onPollFinished(JSONObject response) {
public void onNewNotifications(JSONObject response) {
if(response != null) {
mNotificationController.onNotificationsUpdated(response);
}
Expand Down Expand Up @@ -203,7 +203,7 @@ public void run() {
// run on another thread
mHandler.post(() -> {
if (mConnectionController.checkConnection(getApplicationContext())) {
new PollTask().execute(NotificationService.this);
new PollTask().execute(NotificationPollService.this);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.polar.nextcloudservices.Services;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import org.json.JSONObject;

public class NotificationWebsocketService extends Service implements NotificationListener {
public NotificationWebsocketService() {
}

@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public void onNewNotifications(JSONObject response) {

}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
import com.nextcloud.android.sso.exceptions.NextcloudFilesAppNotInstalledException;
import com.nextcloud.android.sso.model.SingleSignOnAccount;
import com.nextcloud.android.sso.ui.UiExceptionManager;
import com.polar.nextcloudservices.Services.NotificationService;
import com.polar.nextcloudservices.Services.NotificationPollService;

import nl.invissvenska.numberpickerpreference.NumberDialogPreference;
import nl.invissvenska.numberpickerpreference.NumberPickerPreferenceDialogFragment;
Expand All @@ -54,7 +54,7 @@
class NotificationServiceConnection implements ServiceConnection {
private final String TAG = "SettingsActivity.NotificationServiceConnection";
private final SettingsActivity.SettingsFragment settings;
private NotificationService.Binder mService;
private NotificationPollService.Binder mService;
public boolean isConnected = false;

public NotificationServiceConnection(SettingsActivity.SettingsFragment _settings) {
Expand All @@ -63,9 +63,9 @@ public NotificationServiceConnection(SettingsActivity.SettingsFragment _settings

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
if (service instanceof NotificationService.Binder) {
mService = (NotificationService.Binder) service;
settings.setStatus(((NotificationService.Binder) service).getServiceStatus());
if (service instanceof NotificationPollService.Binder) {
mService = (NotificationPollService.Binder) service;
settings.setStatus(((NotificationPollService.Binder) service).getServiceStatus());
isConnected = true;
} else {
Log.wtf(TAG, "Bad Binder type passed!");
Expand Down Expand Up @@ -151,15 +151,15 @@ public void stopNotificationService() {
//mServiceConnection = null;
unbindService(mServiceConnection);
mServiceConnection = null;
context.stopService(new Intent(context, NotificationService.class));
context.stopService(new Intent(context, NotificationPollService.class));
}
}
public void startNotificationService() {
///--------
//Log.d(TAG, "startService: ENTERING");
if (!isNotificationServiceRunning() && getBoolPreference("enable_polling",true)) {
Log.d(TAG, "Service is not running: creating intent to start it");
startService(new Intent(getApplicationContext(), NotificationService.class));
startService(new Intent(getApplicationContext(), NotificationPollService.class));
}
}

Expand All @@ -176,7 +176,7 @@ private void updateNotificationServiceStatus(SettingsFragment settings) {
} else if(mServiceConnection == null && isNotificationServiceRunning()) {
Log.d(TAG, "Service is running but disconnected");
mServiceConnection = new NotificationServiceConnection(settings);
bindService(new Intent(getApplicationContext(), NotificationService.class),
bindService(new Intent(getApplicationContext(), NotificationPollService.class),
mServiceConnection, 0);
} else {
mServiceConnection.updateStatus();
Expand Down

0 comments on commit c13a877

Please sign in to comment.