Skip to content

Commit

Permalink
refactor: Formatting code, suppress warnings, remove typos (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
djenczewski committed Dec 13, 2024
1 parent ce2807b commit 7f08b0d
Show file tree
Hide file tree
Showing 27 changed files with 262 additions and 195 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import com.simplito.java.privmx_endpoint_android.services.PrivmxEndpointService;
import com.simplito.java.privmx_endpoint_extra.events.EventCallback;
import com.simplito.java.privmx_endpoint_extra.events.EventType;
import com.simplito.java.privmx_endpoint_extra.lib.PrivmxEndpointContainer;
import com.simplito.java.privmx_endpoint_extra.lib.PrivmxEndpoint;
import com.simplito.java.privmx_endpoint_extra.lib.PrivmxEndpointContainer;

/**
* Manages {@link PrivmxEndpointService} and active connections.
Expand All @@ -46,9 +46,9 @@ public abstract class PrivmxEndpointBaseActivity extends AppCompatActivity {
@Override
protected void onStart() {
super.onStart();
Log.d(TAG,"onStart");
Log.d(TAG, "onStart");
Intent intent = new Intent(this, PrivmxEndpointService.class);
intent.putExtra(PrivmxEndpointService.CERTS_PATH_EXTRA,getCertPath());
intent.putExtra(PrivmxEndpointService.CERTS_PATH_EXTRA, getCertPath());
startService(intent);
bindService(intent, privmxEndpointServiceConnection, Context.BIND_AUTO_CREATE);
}
Expand All @@ -69,12 +69,16 @@ protected void onStop() {
* Method called when {@link PrivmxEndpointService} and {@link PrivmxEndpointContainer}
* have been successfully initialized.
* Override this method to safely work with {@link PrivmxEndpointBaseActivity#privmxEndpointContainer}.
*
* @noinspection EmptyMethod
*/
protected void onPrivmxEndpointStart() {}
protected void onPrivmxEndpointStart() {
}

/**
* Override this method to set the path to your .pem certificate to create secure connection with PrivMX Bridge.
* If the passed path does not contain .pem file, the default PrivMX certificate is installed.
*
* @return Path to .pem certificate used to initialize {@link PrivmxEndpointService}
*/
protected abstract String getCertPath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public PrivmxEndpointService getService() {

/**
* Sets callback executed when service has been successfully prepared to use.
*
* @param onInit callback
*/
public void setOnInit(Runnable onInit) {
Expand All @@ -64,6 +65,7 @@ public void setOnInit(Runnable onInit) {
/**
* Initializes {@link PrivmxEndpointContainer} with certsPath passed in intent extras.
* If intent does not contain the path, the default value is used.
*
* @see Service#onBind(Intent)
*/
@Override
Expand All @@ -75,6 +77,7 @@ public IBinder onBind(Intent intent) {
/**
* Initialize {@link PrivmxEndpointContainer} with certsPath passed in intent extras.
* If intent does not contain the path, the default value is used.
*
* @see Service#onStartCommand(Intent, int, int)
*/
@Override
Expand All @@ -85,22 +88,23 @@ public int onStartCommand(Intent intent, int flags, int startId) {

/**
* Disconnects active connections if any exist.
*
* @see Service#onDestroy()
*/
@Override
public void onDestroy() {
try {
privmxEndpoint.disconnectAll();
privmxEndpoint.close();
}catch (Exception e){
} catch (Exception e) {
System.out.println("Cannot disconnect from server, reason: " + e.getMessage());
}
super.onDestroy();
}

private String getCertsPath(Intent intent) {
String certsPath = getFilesDir() + "/cacert.pem";
if(intent != null) {
if (intent != null) {
Bundle extras = intent.getExtras();
if (extras != null) {
certsPath = extras.getString(CERTS_PATH_EXTRA);
Expand All @@ -111,6 +115,7 @@ private String getCertsPath(Intent intent) {

/**
* Gets {@link PrivmxEndpointContainer}.
*
* @return Initialized container. If the service does not initialize the container successfully, it returns {@code null}
*/
public PrivmxEndpointContainer getContainer() {
Expand All @@ -127,7 +132,6 @@ private synchronized void init(String certsPath) {
binder.onInit.forEach(Runnable::run);
} catch (Exception e) {
Log.e(TAG, "Cannot initialize lib");
e.printStackTrace();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@

import com.simplito.java.privmx_endpoint.model.Event;

import java.util.AbstractMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Vector;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -63,18 +63,18 @@ public boolean register(String channel, String type, Object context, EventCallba

/**
* Emits specified event. It should only be called by event loops.
* @param <T> type of event data
*
* @param <T> type of event data
* @param event event data to emit
*/
public <T> void emit(Event<T> event) {
List<Pair> callbacks = getCallbacks(getFormattedType(event.channel, event.type));
for (Pair p : callbacks) {
try {
EventCallback<T> e = (EventCallback<T>) p.callback;
@SuppressWarnings("unchecked") EventCallback<T> e = (EventCallback<T>) p.callback;
try {
e.call(event.data);
} catch (Exception error) {
error.printStackTrace();
} catch (Exception ignored) {
}
} catch (ClassCastException e) {
System.out.println("Cannot process event: issue with cast event data");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* @param <T> the type of data contained in the Event.
* @category core
*/
public class EventType<T>{
public class EventType<T> {
/**
* Channel of this event type.
*/
Expand All @@ -48,7 +48,6 @@ public class EventType<T>{
* Type of event data.
*/
public final Class<T> eventResultClass;


private EventType(String channel, String eventType, Class<T> eventClass) {
this.channel = channel;
Expand Down Expand Up @@ -154,6 +153,7 @@ private EventType(String channel, String eventType, Class<T> eventClass) {

/**
* Returns instance to register on new message Events.
*
* @param threadId ID of the Thread to observe
* @return Predefined event type to catch new messages in matching Thread events
*/
Expand All @@ -168,6 +168,7 @@ public static EventType<Message> ThreadNewMessageEvent(String threadId) throws N

/**
* Returns instance to register on message update Events.
*
* @param threadId ID of the Thread to observe
* @return predefined event type to catch message updates in matching Thread events
*/
Expand All @@ -182,6 +183,7 @@ public static EventType<Message> ThreadMessageUpdatedEvent(String threadId) thro

/**
* Returns instance to register on deleted message Events.
*
* @param threadId ID of the Thread to observe
* @return Predefined event type to catch deleted messages in matching Thread events
*/
Expand All @@ -196,6 +198,7 @@ public static EventType<ThreadDeletedMessageEventData> ThreadMessageDeletedEvent

/**
* Returns instance to register on created file Events.
*
* @param storeId ID of the store to observe
* @return Predefined event type to catch new files in matching Store events
*/
Expand All @@ -210,6 +213,7 @@ public static EventType<File> StoreFileCreatedEvent(String storeId) throws NullP

/**
* Returns instance to register on file update Events.
*
* @param storeId ID of the Store to observe
* @return Predefined event type to catch updated files in matching Store events
*/
Expand All @@ -224,6 +228,7 @@ public static EventType<File> StoreFileUpdatedEvent(String storeId) throws NullP

/**
* Returns instance to register on deleted file Events.
*
* @param storeId ID of the Store to observe
* @return Predefined event type to catch deleted files in matching Store events
*/
Expand Down Expand Up @@ -265,6 +270,7 @@ public static EventType<StoreFileDeletedEventData> StoreFileDeletedEvent(String

/**
* Returns instance to register on created entry Events.
*
* @param inboxId ID of the Inbox to observe
* @return predefined event type to catch created entries in matching Inbox events
*/
Expand All @@ -279,6 +285,7 @@ public static EventType<InboxEntry> InboxEntryCreatedEvent(String inboxId) throw

/**
* Returns instance to register on deleting entries Events.
*
* @param inboxId ID of the Inbox to observe
* @return predefined event type to catch deleted entries in matching Inbox events
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,18 +184,22 @@ public synchronized void sendFiles(
if (streamState != State.PREPARED) {
throw new IllegalStateException("Stream should be in state PREPARED. Current state is: " + streamState.name());
}
inboxFiles.forEach((fileInfo, fileHandle) -> {
sendingFiles.add(fileStreamExecutor.submit(() -> {
try {
sendFile(fileInfo, fileHandle);
entryStreamListener.onEndFileSending(fileInfo);
} catch (Exception e) {
stopFileStreams();
onError(e);
entryStreamListener.onErrorDuringSending(fileInfo, e);
}
}));
});
inboxFiles.forEach(
(fileInfo, fileHandle) -> sendingFiles.add(
fileStreamExecutor.submit(
() -> {
try {
sendFile(fileInfo, fileHandle);
entryStreamListener.onEndFileSending(fileInfo);
} catch (Exception e) {
stopFileStreams();
onError(e);
entryStreamListener.onErrorDuringSending(fileInfo, e);
}
}
)
)
);
for (Future<?> future : sendingFiles) {
if (Thread.interrupted()) {
cancel();
Expand Down Expand Up @@ -297,9 +301,7 @@ public void cancel() {
private void stopFileStreams() {
if (streamState == State.PREPARED && !sendingFiles.isEmpty()) {
synchronized (sendingFiles) {
sendingFiles.forEach((task) -> {
task.cancel(true);
});
sendingFiles.forEach(task -> task.cancel(true));
}
}
}
Expand Down Expand Up @@ -393,7 +395,7 @@ public static class FileInfo {
* {@link EntryStreamListener#onNextChunkRequest} to request chunks of data
* for sending.
*/
public InputStream fileStream;
public final InputStream fileStream;

/**
* Creates instance of {@link FileInfo}.
Expand Down Expand Up @@ -424,6 +426,7 @@ public FileInfo(
* <p>
* Implement this interface to monitor and interact with the entry stream.
*/
@SuppressWarnings("EmptyMethod")
public abstract static class EntryStreamListener {

/**
Expand Down Expand Up @@ -455,6 +458,7 @@ public void onEndFileSending(FileInfo file) {
* @param file info about the file, which chunk is requested
* @return next chunk of the file
*/
@SuppressWarnings("SameReturnValue")
public byte[] onNextChunkRequest(FileInfo file) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ public PrivmxEndpoint(

/**
* Registers callbacks with the specified type.
* @param context an object that identifies callbacks in the list
*
* @param context an object that identifies callbacks in the list
* @param eventType type of event to listen to
* @param callback a block of code to execute when event was handled
* @param <T> type of data passed to callback
* @param callback a block of code to execute when event was handled
* @param <T> type of data passed to callback
* @throws RuntimeException thrown when method encounters an exception during subscribing on channel.
*/
public final <T> void registerCallback(Object context, EventType<T> eventType, EventCallback<T> callback) throws RuntimeException {
Expand All @@ -82,6 +83,7 @@ public final <T> void registerCallback(Object context, EventType<T> eventType, E

/**
* Unregisters all callbacks registered by {@link #registerCallback(Object, EventType, EventCallback)} and identified with given Context.
*
* @param context an object that identifies callbacks in the list.
*/
public void unregisterCallbacks(Object context) {
Expand All @@ -97,6 +99,7 @@ public void unregisterAll() {

/**
* Handles event and invokes all related callbacks. It should only be called by event loops.
*
* @param event event to handle
*/
public void handleEvent(Event<?> event) {
Expand Down Expand Up @@ -145,7 +148,6 @@ private void subscribeChannel(String channelStr) {
return;
}
inboxApi.subscribeForInboxEvents();
return;
}
}

Expand Down Expand Up @@ -191,7 +193,6 @@ private void unsubscribeChannel(String channelStr) {
return;
}
inboxApi.unsubscribeFromInboxEvents();
return;
}
}

Expand Down
Loading

0 comments on commit 7f08b0d

Please sign in to comment.