Skip to content

Commit

Permalink
refactor: cleanup code (#15)
Browse files Browse the repository at this point in the history
* refactor: Formatting code, suppress warnings, remove typos (#10)

* refactor: remove unused code (#10)

* refactor: Remove duplicated implementation of close method in PrivmxEndpoint.java (#10)

* refactor: replace Map entry to compatible with Java 8 implementation (#10)

* refactor: Make predefined event types as final (#10)

* refactor: make InboxEntryStream.FileInfo fields as final (#10)

* refactor: remove unused code from parser and PrivmxEndpoint.java

* refactor: remove unchecked suppressWarning from EventDispatcher.java
  • Loading branch information
djenczewski authored Dec 18, 2024
1 parent 25b9c2d commit d2f3158
Show file tree
Hide file tree
Showing 29 changed files with 289 additions and 289 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,7 +63,8 @@ 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) {
Expand All @@ -73,8 +74,7 @@ public <T> void emit(Event<T> event) {
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 Expand Up @@ -103,7 +103,7 @@ public void unbind(Object context) {
map.entrySet()
.stream()
.map(entry ->
Map.entry(entry.getKey().split("_")[0], entry.getValue())
new AbstractMap.SimpleImmutableEntry<>(entry.getKey().split("_")[0], entry.getValue())
)
.filter(entry -> !entry.getValue().isEmpty())
.forEach(entry -> {
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 All @@ -59,7 +58,7 @@ private EventType(String channel, String eventType, Class<T> eventClass) {
/**
* Predefined event type that captures successful platform connection events.
*/
public static EventType<Void> ConnectedEvent = new EventType<>(
public static final EventType<Void> ConnectedEvent = new EventType<>(
"",
"libConnected",
Void.class
Expand All @@ -69,7 +68,7 @@ private EventType(String channel, String eventType, Class<T> eventClass) {
* Predefined event type to catch special events.
* This type could be used to emit/handle events with custom implementations (e.g. to break event loops).
*/
public static EventType<Void> LibBreakEvent = new EventType<>(
public static final EventType<Void> LibBreakEvent = new EventType<>(
"",
"libBreak",
Void.class
Expand All @@ -78,7 +77,7 @@ private EventType(String channel, String eventType, Class<T> eventClass) {
/**
* Predefined event type to catch disconnection events.
*/
public static EventType<Void> DisconnectedEvent = new EventType<>(
public static final EventType<Void> DisconnectedEvent = new EventType<>(
"",
"libDisconnected",
Void.class
Expand All @@ -87,7 +86,7 @@ private EventType(String channel, String eventType, Class<T> eventClass) {
/**
* Predefined event type to catch created Thread events.
*/
public static EventType<Thread> ThreadCreatedEvent = new EventType<>(
public static final EventType<Thread> ThreadCreatedEvent = new EventType<>(
"thread",
"threadCreated",
Thread.class
Expand All @@ -96,7 +95,7 @@ private EventType(String channel, String eventType, Class<T> eventClass) {
/**
* Predefined event type to catch updated Thread events.
*/
public static EventType<Thread> ThreadUpdatedEvent = new EventType<>(
public static final EventType<Thread> ThreadUpdatedEvent = new EventType<>(
"thread",
"threadUpdated",
Thread.class
Expand All @@ -105,7 +104,7 @@ private EventType(String channel, String eventType, Class<T> eventClass) {
/**
* Predefined event type to catch updated Thread stats events.
*/
public static EventType<ThreadStatsEventData> ThreadStatsChangedEvent = new EventType<>(
public static final EventType<ThreadStatsEventData> ThreadStatsChangedEvent = new EventType<>(
"thread",
"threadStats",
ThreadStatsEventData.class
Expand All @@ -114,46 +113,47 @@ private EventType(String channel, String eventType, Class<T> eventClass) {
/**
* Predefined event type to catch deleted Thread events.
*/
public static EventType<ThreadDeletedEventData> ThreadDeletedEvent = new EventType<>(
public static final EventType<ThreadDeletedEventData> ThreadDeletedEvent = new EventType<>(
"thread",
"threadDeleted",
ThreadDeletedEventData.class
);
/**
* Predefined event type to catch created Store events.
*/
public static EventType<Store> StoreCreatedEvent = new EventType<>(
public static final EventType<Store> StoreCreatedEvent = new EventType<>(
"store",
"storeCreated",
Store.class
);
/**
* Predefined event type to catch updated Store events.
*/
public static EventType<Store> StoreUpdatedEvent = new EventType<>(
public static final EventType<Store> StoreUpdatedEvent = new EventType<>(
"store",
"storeUpdated",
Store.class
);
/**
* Predefined event type to catch updated Store stats events.
*/
public static EventType<StoreStatsChangedEventData> StoreStatsChangedEvent = new EventType<>(
public static final EventType<StoreStatsChangedEventData> StoreStatsChangedEvent = new EventType<>(
"store",
"storeStatsChanged",
StoreStatsChangedEventData.class
);
/**
* Predefined event type to catch deleted Store stats events.
*/
public static EventType<StoreDeletedEventData> StoreDeletedEvent = new EventType<>(
public static final EventType<StoreDeletedEventData> StoreDeletedEvent = new EventType<>(
"store",
"storeDeleted",
StoreDeletedEventData.class
);

/**
* 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
Loading

0 comments on commit d2f3158

Please sign in to comment.