Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
AloqaIM-Android
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
AloqaIM-Android
Commits
87461e2a
Commit
87461e2a
authored
Nov 27, 2017
by
Leonardo Aramaki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor indentation
parent
184cfda5
Changes
4
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
583 additions
and
585 deletions
+583
-585
MainPresenter.java
...main/java/chat/rocket/android/activity/MainPresenter.java
+219
-219
ConnectivityManagerApi.java
...a/chat/rocket/android/service/ConnectivityManagerApi.java
+8
-8
RealmBasedConnectivityManager.java
...rocket/android/service/RealmBasedConnectivityManager.java
+234
-236
RocketChatService.java
...n/java/chat/rocket/android/service/RocketChatService.java
+122
-122
No files found.
app/src/main/java/chat/rocket/android/activity/MainPresenter.java
View file @
87461e2a
This diff is collapsed.
Click to expand it.
app/src/main/java/chat/rocket/android/service/ConnectivityManagerApi.java
View file @
87461e2a
...
...
@@ -13,19 +13,19 @@ import io.reactivex.Single;
* interfaces used for Activity/Fragment and other UI-related logic.
*/
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
();
}
app/src/main/java/chat/rocket/android/service/RealmBasedConnectivityManager.java
View file @
87461e2a
This diff is collapsed.
Click to expand it.
app/src/main/java/chat/rocket/android/service/RocketChatService.java
View file @
87461e2a
...
...
@@ -23,130 +23,130 @@ import io.reactivex.Single;
*/
public
class
RocketChatService
extends
Service
implements
ConnectivityServiceInterface
{
private
ConnectivityManagerInternal
connectivityManager
;
private
static
volatile
Semaphore
webSocketThreadLock
=
new
Semaphore
(
1
);
private
static
volatile
RocketChatWebSocketThread
currentWebSocketThread
;
private
ConnectivityManagerInternal
connectivityManager
;
private
static
volatile
Semaphore
webSocketThreadLock
=
new
Semaphore
(
1
);
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
{
ConnectivityServiceInterface
getServiceInterface
()
{
return
RocketChatService
.
this
;
@DebugLog
@Override
public
int
onStartCommand
(
Intent
intent
,
int
flags
,
int
startId
)
{
connectivityManager
.
ensureConnections
();
return
START_NOT_STICKY
;
}
}
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
();
}
@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
->
{
@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
;
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
;
// 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
;
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
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment