Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
VLC-Android
Manage
Activity
Members
Labels
Plan
Issues
531
Issue boards
Milestones
Wiki
Code
Merge requests
16
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
VideoLAN
VLC-Android
Commits
d4d297ab
Commit
d4d297ab
authored
5 years ago
by
Geoffrey Métais
Committed by
Geoffrey Métais
5 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Create NetworkMonitor in tools
Make it VLC independant to be used un modules
parent
253e9a43
No related branches found
No related tags found
1 merge request
!379
Modules
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
tools/src/main/java/org/videolan/tools/NetworkMonitor.kt
+99
-0
99 additions, 0 deletions
tools/src/main/java/org/videolan/tools/NetworkMonitor.kt
with
99 additions
and
0 deletions
tools/src/main/java/org/videolan/tools/NetworkMonitor.kt
0 → 100644
+
99
−
0
View file @
d4d297ab
package
org.videolan.tools
import
android.annotation.SuppressLint
import
android.annotation.TargetApi
import
android.content.BroadcastReceiver
import
android.content.Context
import
android.content.Intent
import
android.content.IntentFilter
import
android.net.ConnectivityManager
import
android.net.NetworkCapabilities
import
android.os.Build
import
androidx.lifecycle.*
import
kotlinx.coroutines.CoroutineStart
import
kotlinx.coroutines.Dispatchers
import
kotlinx.coroutines.GlobalScope
import
kotlinx.coroutines.launch
import
java.lang.ref.WeakReference
import
java.net.NetworkInterface
import
java.net.SocketException
interface
NetworkObserver
{
fun
onNetworkChanged
()
}
class
NetworkMonitor
(
private
val
context
:
Context
)
:
LifecycleObserver
{
private
var
registered
=
false
private
val
cm
=
context
.
getSystemService
(
Context
.
CONNECTIVITY_SERVICE
)
as
ConnectivityManager
val
connected
=
MutableLiveData
<
Boolean
>()
@Volatile
var
isMobile
=
true
private
set
@Volatile
var
isVPN
=
false
private
set
private
var
networkObservers
:
MutableList
<
WeakReference
<
NetworkObserver
>>
=
mutableListOf
()
init
{
ProcessLifecycleOwner
.
get
().
lifecycle
.
addObserver
(
this
@NetworkMonitor
)
}
@OnLifecycleEvent
(
Lifecycle
.
Event
.
ON_START
)
fun
start
()
{
if
(
registered
)
return
registered
=
true
val
networkFilter
=
IntentFilter
(
ConnectivityManager
.
CONNECTIVITY_ACTION
)
context
.
registerReceiver
(
receiver
,
networkFilter
)
}
@OnLifecycleEvent
(
Lifecycle
.
Event
.
ON_STOP
)
fun
stop
()
{
registered
=
false
context
.
unregisterReceiver
(
receiver
)
}
@SuppressLint
(
"MissingPermission"
)
@TargetApi
(
Build
.
VERSION_CODES
.
LOLLIPOP
)
private
fun
updateVPNStatus
():
Boolean
{
if
(
Build
.
VERSION
.
SDK_INT
>=
Build
.
VERSION_CODES
.
LOLLIPOP
)
{
for
(
network
in
cm
.
allNetworks
)
{
val
nc
=
cm
.
getNetworkCapabilities
(
network
)
?:
return
false
if
(
nc
.
hasTransport
(
NetworkCapabilities
.
TRANSPORT_VPN
))
return
true
}
return
false
}
else
{
try
{
val
networkInterfaces
=
NetworkInterface
.
getNetworkInterfaces
()
while
(
networkInterfaces
.
hasMoreElements
())
{
val
networkInterface
=
networkInterfaces
.
nextElement
()
val
name
=
networkInterface
.
displayName
if
(
name
.
startsWith
(
"ppp"
)
||
name
.
startsWith
(
"tun"
)
||
name
.
startsWith
(
"tap"
))
return
true
}
}
catch
(
ignored
:
SocketException
)
{}
return
false
}
}
val
receiver
=
object
:
BroadcastReceiver
()
{
@SuppressLint
(
"MissingPermission"
)
override
fun
onReceive
(
context
:
Context
?,
intent
:
Intent
?)
{
when
(
intent
?.
action
)
{
ConnectivityManager
.
CONNECTIVITY_ACTION
->
{
val
networkInfo
=
cm
.
activeNetworkInfo
val
isConnected
=
networkInfo
!=
null
&&
networkInfo
.
isConnected
isMobile
=
isConnected
&&
networkInfo
!!
.
type
==
ConnectivityManager
.
TYPE_MOBILE
isVPN
=
isConnected
&&
updateVPNStatus
()
if
(
connected
.
value
==
null
||
isConnected
!=
connected
.
value
)
{
connected
.
value
=
isConnected
}
networkObservers
.
forEach
{
it
.
get
()
?.
onNetworkChanged
()
}
}
}
}
}
companion
object
:
SingletonHolder
<
NetworkMonitor
,
Context
>({
NetworkMonitor
(
it
)
})
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment