forked from ob-f/OpenBot
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathConnectionManager.java
More file actions
55 lines (44 loc) · 1.54 KB
/
ConnectionManager.java
File metadata and controls
55 lines (44 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package org.openbot.env;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class ConnectionManager {
private static final String TAG = "ConnectionManager";
private static ConnectionManager _connectionManager;
private static Context _context;
private ILocalConnection connection;
private final ILocalConnection networkConnection = new NetworkServiceConnection();
private final ILocalConnection nearbyConnection = new NearbyConnection();
private ConnectionManager() {
if (_connectionManager != null) {
throw new RuntimeException(
"Use getInstance() method to get the single instance of this class.");
}
}
public static ConnectionManager getInstance(Context context) {
_context = context;
if (_connectionManager == null) {
synchronized (ConnectionManager.class) {
if (_connectionManager == null) _connectionManager = new ConnectionManager();
}
}
return _connectionManager;
}
ILocalConnection getConnection() {
if (connection != null) {
return connection;
}
if (isConnectedViaWifi()) {
connection = networkConnection;
} else {
connection = nearbyConnection;
}
return connection;
}
private boolean isConnectedViaWifi() {
ConnectivityManager connectivityManager =
(ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
return mWifi.isConnected();
}
}