monitors;
23 |
24 | private Tovuti(Context context) {
25 | monitors = new HashSet<>();
26 | this.contextRef = new WeakReference<>(context);
27 | }
28 |
29 | public static Tovuti from(Context context) {
30 | if (tovuti == null) {
31 | synchronized (lock) {
32 | if (tovuti == null) {
33 | tovuti = new Tovuti(context);
34 | }
35 | }
36 | }
37 | return tovuti;
38 | }
39 |
40 | public Tovuti monitor(int connectionType, Monitor.ConnectivityListener listener) {
41 | Context context = contextRef.get();
42 | if (context != null)
43 | monitors.add(new DefaultMonitorFactory().create(context, connectionType, listener));
44 |
45 | start();
46 | return tovuti;
47 | }
48 |
49 | public Tovuti monitor(Monitor.ConnectivityListener listener) {
50 | return monitor(-1, listener);
51 | }
52 |
53 | public void start() {
54 | for (Monitor monitor : monitors) {
55 | monitor.onStart();
56 | }
57 |
58 | if (monitors.size() > 0)
59 | Log.i(TAG, "started tovuti");
60 | }
61 |
62 | public void stop() {
63 | for (Monitor monitor : monitors) {
64 | monitor.onStop();
65 | }
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/networkmanager/src/main/java/com/androidstudy/networkmanager/internal/DefaultMonitor.java:
--------------------------------------------------------------------------------
1 | package com.androidstudy.networkmanager.internal;
2 |
3 | import android.content.BroadcastReceiver;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.content.IntentFilter;
7 | import android.net.ConnectivityManager;
8 | import android.os.Handler;
9 | import android.os.Looper;
10 | import android.util.Log;
11 |
12 | import com.androidstudy.networkmanager.Monitor;
13 |
14 | /**
15 | * Created by chweya on 29/08/17.
16 | */
17 |
18 | public class DefaultMonitor implements Monitor {
19 |
20 | private final Handler mainHandler = new Handler(Looper.getMainLooper());
21 | private final Context context;
22 | private final ConnectivityListener listener;
23 | private final int connectionType;
24 |
25 | private boolean isConnected;
26 | private boolean isRegistered;
27 |
28 | private final BroadcastReceiver connectivityReceiver = new BroadcastReceiver() {
29 | @Override
30 | public void onReceive(final Context context, Intent intent) {
31 | boolean wasConnected = isConnected;
32 | isConnected = NetworkUtil.isConnected(context, connectionType);
33 | if (wasConnected != isConnected) {
34 | emitEvent();
35 | }
36 | }
37 | };
38 |
39 | public DefaultMonitor(Context context, ConnectivityListener listener, int connectionType) {
40 | this.context = context;
41 | this.listener = listener;
42 | this.connectionType = connectionType;
43 | }
44 |
45 | public DefaultMonitor(Context context, ConnectivityListener listener) {
46 | this(context, listener, -1);
47 | }
48 |
49 | private void emitEvent() {
50 | Log.i("Monitor", "Network change");
51 | mainHandler.post(new Runnable() {
52 | @Override
53 | public void run() {
54 | listener.onConnectivityChanged(connectionType, isConnected, isConnected && NetworkUtil.isConnectionFast(context));
55 | }
56 | });
57 | }
58 |
59 | private void register() {
60 | if (isRegistered) {
61 | return;
62 | }
63 |
64 | Log.i("Monitor", "Registering");
65 | isConnected = NetworkUtil.isConnected(context, connectionType);
66 | //emit once
67 | emitEvent();
68 | context.registerReceiver(
69 | connectivityReceiver,
70 | new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
71 |
72 | isRegistered = true;
73 | }
74 |
75 | private void unregister() {
76 | if (!isRegistered) {
77 | return;
78 | }
79 |
80 | Log.i("Monitor", "Unregistering");
81 | context.unregisterReceiver(connectivityReceiver);
82 | isRegistered = false;
83 | }
84 |
85 | @Override
86 | public void onStart() {
87 | register();
88 | }
89 |
90 | @Override
91 | public void onStop() {
92 | unregister();
93 | }
94 | }
95 |
96 |
--------------------------------------------------------------------------------
/networkmanager/src/main/java/com/androidstudy/networkmanager/internal/DefaultMonitorFactory.java:
--------------------------------------------------------------------------------
1 | package com.androidstudy.networkmanager.internal;
2 |
3 | import android.Manifest;
4 | import android.content.Context;
5 | import android.content.pm.PackageManager;
6 | import android.support.annotation.NonNull;
7 | import android.support.v4.content.ContextCompat;
8 |
9 | import com.androidstudy.networkmanager.Monitor;
10 | import com.androidstudy.networkmanager.MonitorFactory;
11 |
12 | /**
13 | * Created by chweya on 29/08/17.
14 | */
15 |
16 | public class DefaultMonitorFactory implements MonitorFactory {
17 | public static final String ACCESS_NETWORK_PERMISSION = Manifest.permission.ACCESS_NETWORK_STATE;
18 |
19 | @NonNull
20 | @Override
21 | public Monitor create(
22 | @NonNull Context context,
23 | int connectionType,
24 | @NonNull Monitor.ConnectivityListener listener) {
25 |
26 | int permissionResult = ContextCompat.checkSelfPermission(context, ACCESS_NETWORK_PERMISSION);
27 | boolean hasPermission = permissionResult == PackageManager.PERMISSION_GRANTED;
28 |
29 | return hasPermission ? new DefaultMonitor(context, listener, connectionType)
30 | : new NoopMonitor();
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/networkmanager/src/main/java/com/androidstudy/networkmanager/internal/NetworkUtil.java:
--------------------------------------------------------------------------------
1 | package com.androidstudy.networkmanager.internal;
2 |
3 | import android.content.Context;
4 | import android.net.ConnectivityManager;
5 | import android.net.NetworkInfo;
6 | import android.telephony.TelephonyManager;
7 | import android.util.Log;
8 |
9 | /**
10 | * Created by anonymous on 8/27/17.
11 | */
12 |
13 | public class NetworkUtil {
14 |
15 | /**
16 | * A utility class to determine if the application has been connected to either WIFI Or Mobile
17 | * Network, before we make any network request to the server.
18 | *
19 | * The class uses two permission - INTERNET and ACCESS NETWORK STATE, to determine the user's
20 | * connection stats
21 | */
22 |
23 | private NetworkUtil() {
24 | }
25 |
26 | /**
27 | * Get the network info
28 | *
29 | * @param context to get NetworkInfo
30 | * @return {@link NetworkInfo}
31 | */
32 | public static NetworkInfo getNetworkInfo(Context context) {
33 | try {
34 | ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
35 | return cm.getActiveNetworkInfo();
36 | } catch (Exception e) {
37 | System.out.println("CheckConnectivity Exception: " + e.getMessage());
38 | Log.v("connectivity", e.toString());
39 | return null;
40 | }
41 | }
42 |
43 | /**
44 | * Check if there is any connection
45 | *
46 | * @param context a {@link Context}
47 | * @return boolean
48 | */
49 | public static boolean isConnected(Context context) {
50 | NetworkInfo info = getNetworkInfo(context);
51 | return (info != null && info.isConnectedOrConnecting());
52 | }
53 |
54 | public static boolean isConnected(Context context, int connectionType) {
55 | switch (connectionType) {
56 | case ConnectivityManager.TYPE_WIFI:
57 | return isConnectedToWifi(context);
58 | case ConnectivityManager.TYPE_MOBILE:
59 | return isConnectedToMobile(context);
60 | default:
61 | return isConnected(context);
62 | }
63 | }
64 |
65 | /**
66 | * @param context
67 | * @return boolean
68 | *
69 | * Instead use {@link NetworkUtil#isConnected(Context)}
70 | */
71 | @Deprecated
72 | public static boolean isOnline(Context context) {
73 | return isConnected(context);
74 | }
75 |
76 | /**
77 | * Check if there is any connection to a Wifi network
78 | *
79 | * @param context
80 | * @return boolean
81 | */
82 | public static boolean isConnectedToWifi(Context context) {
83 | NetworkInfo info = getNetworkInfo(context);
84 | return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI);
85 | }
86 |
87 | /**
88 | * Check if there is any connection to a mobile network
89 | *
90 | * @param context
91 | * @return
92 | */
93 | public static boolean isConnectedToMobile(Context context) {
94 | NetworkInfo info = getNetworkInfo(context);
95 | return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_MOBILE);
96 | }
97 |
98 | /**
99 | * Check if the connection is fast
100 | *
101 | * @param context
102 | * @return
103 | */
104 | public static boolean isConnectionFast(Context context) {
105 | NetworkInfo info = getNetworkInfo(context);
106 | return (info != null && info.isConnected())
107 | && isConnectionFast(info.getType(), info.getSubtype());
108 | }
109 |
110 | /**
111 | * Check if the connection is fast
112 | *
113 | * @param type
114 | * @param subType
115 | * @return boolean
116 | *
117 | * inspired by https://gist.github.com/emil2k/5130324
118 | */
119 | public static boolean isConnectionFast(int type, int subType) {
120 | if (type == ConnectivityManager.TYPE_WIFI) {
121 | return true;
122 | } else if (type == ConnectivityManager.TYPE_MOBILE) {
123 | switch (subType) {
124 | case TelephonyManager.NETWORK_TYPE_1xRTT:
125 | return false; // ~ 50-100 kbps
126 | case TelephonyManager.NETWORK_TYPE_CDMA:
127 | return false; // ~ 14-64 kbps
128 | case TelephonyManager.NETWORK_TYPE_EDGE:
129 | return false; // ~ 50-100 kbps
130 | case TelephonyManager.NETWORK_TYPE_EVDO_0:
131 | return true; // ~ 400-1000 kbps
132 | case TelephonyManager.NETWORK_TYPE_EVDO_A:
133 | return true; // ~ 600-1400 kbps
134 | case TelephonyManager.NETWORK_TYPE_GPRS:
135 | return false; // ~ 100 kbps
136 | case TelephonyManager.NETWORK_TYPE_HSDPA:
137 | return true; // ~ 2-14 Mbps
138 | case TelephonyManager.NETWORK_TYPE_HSPA:
139 | return true; // ~ 700-1700 kbps
140 | case TelephonyManager.NETWORK_TYPE_HSUPA:
141 | return true; // ~ 1-23 Mbps
142 | case TelephonyManager.NETWORK_TYPE_UMTS:
143 | return true; // ~ 400-7000 kbps
144 | case TelephonyManager.NETWORK_TYPE_EHRPD: // API level 11
145 | return true; // ~ 1-2 Mbps
146 | case TelephonyManager.NETWORK_TYPE_EVDO_B: // API level 9
147 | return true; // ~ 5 Mbps
148 | case TelephonyManager.NETWORK_TYPE_HSPAP: // API level 13
149 | return true; // ~ 10-20 Mbps
150 | case TelephonyManager.NETWORK_TYPE_IDEN: // API level 8
151 | return false; // ~25 kbps
152 | case TelephonyManager.NETWORK_TYPE_LTE: // API level 11
153 | return true; // ~ 10+ Mbps
154 | // Unknown
155 | case TelephonyManager.NETWORK_TYPE_UNKNOWN:
156 | default:
157 | return false;
158 | }
159 | } else {
160 | return false;
161 | }
162 | }
163 | }
--------------------------------------------------------------------------------
/networkmanager/src/main/java/com/androidstudy/networkmanager/internal/NoopMonitor.java:
--------------------------------------------------------------------------------
1 | package com.androidstudy.networkmanager.internal;
2 |
3 | import com.androidstudy.networkmanager.Monitor;
4 |
5 | /**
6 | * Created by chweya on 29/08/17.
7 | */
8 |
9 | public class NoopMonitor implements Monitor {
10 | @Override
11 | public void onStart() {
12 | //no-op
13 | }
14 |
15 | @Override
16 | public void onStop() {
17 | //no-op
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/networkmanager/src/main/java/com/androidstudy/networkmanager/internal/Util.java:
--------------------------------------------------------------------------------
1 | package com.androidstudy.networkmanager.internal;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Collection;
5 | import java.util.List;
6 |
7 | /**
8 | * Created by chweya on 29/08/17.
9 | */
10 |
11 | public class Util {
12 | private Util() {
13 | }
14 |
15 | public static List getSnapshot(Collection other) {
16 | // toArray creates a new ArrayList internally and this way we can guarantee entries will not
17 | // be null. See #322.
18 | List result = new ArrayList<>(other.size());
19 | for (T item : other) {
20 | result.add(item);
21 | }
22 | return result;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/networkmanager/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Network Manager
3 |
4 |
--------------------------------------------------------------------------------
/networkmanager/src/test/java/com/androidstudy/networkmanager/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.androidstudy.networkmanager;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':networkmanager'
--------------------------------------------------------------------------------