Commit 87461e2a authored by Leonardo Aramaki's avatar Leonardo Aramaki

Refactor indentation

parent 184cfda5
...@@ -13,19 +13,19 @@ import io.reactivex.Single; ...@@ -13,19 +13,19 @@ import io.reactivex.Single;
* interfaces used for Activity/Fragment and other UI-related logic. * interfaces used for Activity/Fragment and other UI-related logic.
*/ */
public interface ConnectivityManagerApi { public interface ConnectivityManagerApi {
void keepAliveServer(); void keepAliveServer();
void addOrUpdateServer(String hostname, @Nullable String name, boolean insecure); void addOrUpdateServer(String hostname, @Nullable String name, boolean insecure);
void removeServer(String hostname); void removeServer(String hostname);
Single<Boolean> connect(String hostname); Single<Boolean> connect(String hostname);
List<ServerInfo> getServerList(); List<ServerInfo> getServerList();
Flowable<ServerConnectivity> getServerConnectivityAsObservable(); Flowable<ServerConnectivity> getServerConnectivityAsObservable();
int getConnectivityState(@NonNull String hostname); int getConnectivityState(@NonNull String hostname);
void resetConnectivityStateList(); void resetConnectivityStateList();
} }
...@@ -23,130 +23,130 @@ import io.reactivex.Single; ...@@ -23,130 +23,130 @@ import io.reactivex.Single;
*/ */
public class RocketChatService extends Service implements ConnectivityServiceInterface { public class RocketChatService extends Service implements ConnectivityServiceInterface {
private ConnectivityManagerInternal connectivityManager; private ConnectivityManagerInternal connectivityManager;
private static volatile Semaphore webSocketThreadLock = new Semaphore(1); private static volatile Semaphore webSocketThreadLock = new Semaphore(1);
private static volatile RocketChatWebSocketThread currentWebSocketThread; private static volatile RocketChatWebSocketThread currentWebSocketThread;
public class LocalBinder extends Binder {
ConnectivityServiceInterface getServiceInterface() {
return RocketChatService.this;
}
}
private final LocalBinder localBinder = new LocalBinder();
/**
* ensure RocketChatService alive.
*/
/*package*/static void keepAlive(Context context) {
context.startService(new Intent(context, RocketChatService.class));
}
public static void bind(Context context, ServiceConnection serviceConnection) {
context.bindService(
new Intent(context, RocketChatService.class), serviceConnection, Context.BIND_AUTO_CREATE);
}
public static void unbind(Context context, ServiceConnection serviceConnection) {
context.unbindService(serviceConnection);
}
@DebugLog
@Override
public void onCreate() {
super.onCreate();
connectivityManager = ConnectivityManager.getInstanceForInternal(getApplicationContext());
connectivityManager.resetConnectivityStateList();
}
public class LocalBinder extends Binder { @DebugLog
ConnectivityServiceInterface getServiceInterface() { @Override
return RocketChatService.this; public int onStartCommand(Intent intent, int flags, int startId) {
connectivityManager.ensureConnections();
return START_NOT_STICKY;
} }
}
@Override
private final LocalBinder localBinder = new LocalBinder(); public Single<Boolean> ensureConnectionToServer(String hostname) { //called via binder.
return getOrCreateWebSocketThread(hostname)
/** .flatMap(RocketChatWebSocketThread::keepAlive);
* ensure RocketChatService alive. }
*/
/*package*/ static void keepAlive(Context context) { @Override
context.startService(new Intent(context, RocketChatService.class)); public Single<Boolean> disconnectFromServer(String hostname) { //called via binder.
} return Single.defer(() -> {
if (!existsThreadForHostname(hostname)) {
public static void bind(Context context, ServiceConnection serviceConnection) { return Single.just(true);
context.bindService( }
new Intent(context, RocketChatService.class), serviceConnection, Context.BIND_AUTO_CREATE);
} if (currentWebSocketThread != null) {
return currentWebSocketThread.terminate()
public static void unbind(Context context, ServiceConnection serviceConnection) { // after disconnection from server
context.unbindService(serviceConnection); .doAfterTerminate(() -> {
}
@DebugLog
@Override
public void onCreate() {
super.onCreate();
connectivityManager = ConnectivityManager.getInstanceForInternal(getApplicationContext());
connectivityManager.resetConnectivityStateList();
}
@DebugLog
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
connectivityManager.ensureConnections();
return START_NOT_STICKY;
}
@Override
public Single<Boolean> ensureConnectionToServer(String hostname) { //called via binder.
return getOrCreateWebSocketThread(hostname)
.flatMap(RocketChatWebSocketThread::keepAlive);
}
@Override
public Single<Boolean> disconnectFromServer(String hostname) { //called via binder.
return Single.defer(() -> {
if (!existsThreadForHostname(hostname)) {
return Single.just(true);
}
if (currentWebSocketThread != null) {
return currentWebSocketThread.terminate()
// after disconnection from server
.doAfterTerminate(() -> {
currentWebSocketThread = null;
// remove RealmConfiguration key from HashMap
RealmStore.sStore.remove(hostname);
});
} else {
return Observable.timer(1, TimeUnit.SECONDS).singleOrError()
.flatMap(_val -> disconnectFromServer(hostname));
}
});
}
@DebugLog
private Single<RocketChatWebSocketThread> getOrCreateWebSocketThread(String hostname) {
return Single.defer(() -> {
webSocketThreadLock.acquire();
int connectivityState = ConnectivityManager.getInstance(getApplicationContext()).getConnectivityState(hostname);
boolean isDisconnected = connectivityState != ServerConnectivity.STATE_CONNECTED;
if (currentWebSocketThread != null && existsThreadForHostname(hostname) && !isDisconnected) {
webSocketThreadLock.release();
return Single.just(currentWebSocketThread);
}
if (currentWebSocketThread != null) {
return currentWebSocketThread.terminate()
.doAfterTerminate(() -> currentWebSocketThread = null)
.flatMap(terminated ->
RocketChatWebSocketThread.getStarted(getApplicationContext(), hostname)
.doOnSuccess(thread -> {
currentWebSocketThread = thread;
webSocketThreadLock.release();
})
.doOnError(throwable -> {
currentWebSocketThread = null; currentWebSocketThread = null;
RCLog.e(throwable); // remove RealmConfiguration key from HashMap
Logger.report(throwable); RealmStore.sStore.remove(hostname);
webSocketThreadLock.release(); });
}) } else {
); return Observable.timer(1, TimeUnit.SECONDS).singleOrError()
} .flatMap(_val -> disconnectFromServer(hostname));
}
return RocketChatWebSocketThread.getStarted(getApplicationContext(), hostname) });
.doOnSuccess(thread -> { }
currentWebSocketThread = thread;
webSocketThreadLock.release(); @DebugLog
}) private Single<RocketChatWebSocketThread> getOrCreateWebSocketThread(String hostname) {
.doOnError(throwable -> { return Single.defer(() -> {
currentWebSocketThread = null; webSocketThreadLock.acquire();
RCLog.e(throwable); int connectivityState = ConnectivityManager.getInstance(getApplicationContext()).getConnectivityState(hostname);
Logger.report(throwable); boolean isDisconnected = connectivityState != ServerConnectivity.STATE_CONNECTED;
webSocketThreadLock.release(); if (currentWebSocketThread != null && existsThreadForHostname(hostname) && !isDisconnected) {
}); webSocketThreadLock.release();
}); return Single.just(currentWebSocketThread);
} }
private boolean existsThreadForHostname(String hostname) { if (currentWebSocketThread != null) {
if (hostname == null || currentWebSocketThread == null) { return currentWebSocketThread.terminate()
return false; .doAfterTerminate(() -> currentWebSocketThread = null)
.flatMap(terminated ->
RocketChatWebSocketThread.getStarted(getApplicationContext(), hostname)
.doOnSuccess(thread -> {
currentWebSocketThread = thread;
webSocketThreadLock.release();
})
.doOnError(throwable -> {
currentWebSocketThread = null;
RCLog.e(throwable);
Logger.report(throwable);
webSocketThreadLock.release();
})
);
}
return RocketChatWebSocketThread.getStarted(getApplicationContext(), hostname)
.doOnSuccess(thread -> {
currentWebSocketThread = thread;
webSocketThreadLock.release();
})
.doOnError(throwable -> {
currentWebSocketThread = null;
RCLog.e(throwable);
Logger.report(throwable);
webSocketThreadLock.release();
});
});
}
private boolean existsThreadForHostname(String hostname) {
if (hostname == null || currentWebSocketThread == null) {
return false;
}
return currentWebSocketThread.getName().equals("RC_thread_" + hostname);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return localBinder;
} }
return currentWebSocketThread.getName().equals("RC_thread_" + hostname);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return localBinder;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment