Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
briar
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Julian Dehm
briar
Commits
8e9fc3b3
Verified
Commit
8e9fc3b3
authored
7 years ago
by
akwizgran
Browse files
Options
Downloads
Patches
Plain Diff
Enable LAN plugin to use wifi AP interface.
parent
c3a70fe5
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
bramble-android/src/main/java/org/briarproject/bramble/plugin/tcp/AndroidLanTcpPlugin.java
+38
-7
38 additions, 7 deletions
.../briarproject/bramble/plugin/tcp/AndroidLanTcpPlugin.java
with
38 additions
and
7 deletions
bramble-android/src/main/java/org/briarproject/bramble/plugin/tcp/AndroidLanTcpPlugin.java
+
38
−
7
View file @
8e9fc3b3
...
...
@@ -36,9 +36,23 @@ import static java.util.Collections.singletonList;
@NotNullByDefault
class
AndroidLanTcpPlugin
extends
LanTcpPlugin
{
private
static
final
String
WIFI_AP_STATE_ACTION
=
"android.net.wifi.WIFI_AP_STATE_CHANGED"
;
private
static
final
byte
[]
WIFI_AP_ADDRESS_BYTES
=
{(
byte
)
192
,
(
byte
)
168
,
43
,
1
};
private
static
final
InetAddress
WIFI_AP_ADDRESS
;
private
static
final
Logger
LOG
=
Logger
.
getLogger
(
AndroidLanTcpPlugin
.
class
.
getName
());
static
{
try
{
WIFI_AP_ADDRESS
=
InetAddress
.
getByAddress
(
WIFI_AP_ADDRESS_BYTES
);
}
catch
(
UnknownHostException
e
)
{
// Should only be thrown if the address has an illegal length
throw
new
AssertionError
(
e
);
}
}
private
final
Context
appContext
;
private
final
ConnectivityManager
connectivityManager
;
@Nullable
...
...
@@ -59,7 +73,7 @@ class AndroidLanTcpPlugin extends LanTcpPlugin {
this
.
connectivityManager
=
connectivityManager
;
wifiManager
=
(
WifiManager
)
appContext
.
getApplicationContext
()
.
getSystemService
(
WIFI_SERVICE
);
socketFactory
=
get
SocketFactory
();
socketFactory
=
SocketFactory
.
getDefault
();
}
@Override
...
...
@@ -68,7 +82,9 @@ class AndroidLanTcpPlugin extends LanTcpPlugin {
running
=
true
;
// Register to receive network status events
networkStateReceiver
=
new
NetworkStateReceiver
();
IntentFilter
filter
=
new
IntentFilter
(
CONNECTIVITY_ACTION
);
IntentFilter
filter
=
new
IntentFilter
();
filter
.
addAction
(
CONNECTIVITY_ACTION
);
filter
.
addAction
(
WIFI_AP_STATE_ACTION
);
appContext
.
registerReceiver
(
networkStateReceiver
,
filter
);
}
...
...
@@ -87,10 +103,17 @@ class AndroidLanTcpPlugin extends LanTcpPlugin {
@Override
protected
Collection
<
InetAddress
>
getLocalIpAddresses
()
{
// If the device doesn't have wifi, don't open any sockets
if
(
wifiManager
==
null
)
return
emptyList
();
// If we're connected to a wifi network, use that network
WifiInfo
info
=
wifiManager
.
getConnectionInfo
();
if
(
info
==
null
||
info
.
getIpAddress
()
==
0
)
return
emptyList
();
return
singletonList
(
intToInetAddress
(
info
.
getIpAddress
()));
if
(
info
!=
null
&&
info
.
getIpAddress
()
!=
0
)
return
singletonList
(
intToInetAddress
(
info
.
getIpAddress
()));
// If we're running an access point, return its address
if
(
super
.
getLocalIpAddresses
().
contains
(
WIFI_AP_ADDRESS
))
return
singletonList
(
WIFI_AP_ADDRESS
);
// No suitable addresses
return
emptyList
();
}
private
InetAddress
intToInetAddress
(
int
ip
)
{
...
...
@@ -124,12 +147,20 @@ class AndroidLanTcpPlugin extends LanTcpPlugin {
@Override
public
void
onReceive
(
Context
ctx
,
Intent
i
)
{
if
(!
running
||
wifiManager
==
null
)
return
;
WifiInfo
info
=
wifiManager
.
getConnectionInfo
();
if
(
info
==
null
||
info
.
getIpAddress
()
==
0
)
{
if
(!
running
)
return
;
Collection
<
InetAddress
>
addrs
=
getLocalIpAddresses
();
if
(
addrs
.
isEmpty
()
)
{
LOG
.
info
(
"Not connected to wifi"
);
socketFactory
=
SocketFactory
.
getDefault
();
tryToClose
(
socket
);
}
else
if
(
addrs
.
contains
(
WIFI_AP_ADDRESS
))
{
LOG
.
info
(
"Providing wifi hotspot"
);
// There's no corresponding Network object and thus no way
// to get a suitable socket factory, so we won't be able to
// make outgoing connections on API 21+ if another network
// has internet access
socketFactory
=
SocketFactory
.
getDefault
();
if
(
socket
==
null
||
socket
.
isClosed
())
bind
();
}
else
{
LOG
.
info
(
"Connected to wifi"
);
socketFactory
=
getSocketFactory
();
...
...
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