|
| 1 | +package com.mapbox.services.android.telemetry.connectivity; |
| 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.net.NetworkInfo; |
| 9 | + |
| 10 | +import java.util.concurrent.CopyOnWriteArrayList; |
| 11 | + |
| 12 | +/** |
| 13 | + * ConnectivityReceiver is a BroadcastReceiver that helps you keep track of the connectivity |
| 14 | + * status. When used statically (getSystemConnectivity) the ConnectivityReceiver will always |
| 15 | + * return the connectivity status as reported by the Android system. |
| 16 | + * |
| 17 | + * When instantiating ConnectivityReceiver, you have the option to set a connectedFlag. You can |
| 18 | + * override the connectivity value reported by the system by setting this flag to true or false. If |
| 19 | + * left in its default value (null), ConnectivityReceiver will report the system value. |
| 20 | + * |
| 21 | + * ConnectivityReceiver also lets you subscribe to connecitivity changes using a |
| 22 | + * ConnectivityListener. |
| 23 | + */ |
| 24 | +public class ConnectivityReceiver extends BroadcastReceiver { |
| 25 | + |
| 26 | + private Context context; |
| 27 | + private CopyOnWriteArrayList<ConnectivityListener> connectivityListeners; |
| 28 | + private Boolean connectedFlag; |
| 29 | + |
| 30 | + /** |
| 31 | + * ConnectivityReceiver constructor |
| 32 | + * |
| 33 | + * @param context Android context. To avoid memory leaks, you might want to pass the application |
| 34 | + * context and make sure you call removeConnectivityUpdates() when you don't need |
| 35 | + * further updates (https://github.com/mapbox/mapbox-gl-native/issues/7176) |
| 36 | + */ |
| 37 | + public ConnectivityReceiver(Context context) { |
| 38 | + this.context = context; |
| 39 | + connectivityListeners = new CopyOnWriteArrayList<>(); |
| 40 | + connectedFlag = null; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Get the connectivity state as reported by the Android system |
| 45 | + * |
| 46 | + * @param context Android context |
| 47 | + * @return the connectivity state as reported by the Android system |
| 48 | + */ |
| 49 | + private static boolean getSystemConnectivity(Context context) { |
| 50 | + ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 51 | + NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); |
| 52 | + return activeNetwork != null && activeNetwork.isConnectedOrConnecting(); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Get the connectivity state. This can be overriden using the connectedFlag. |
| 57 | + * |
| 58 | + * @return the connectivity state |
| 59 | + */ |
| 60 | + private boolean getManagedConnectivity() { |
| 61 | + if (connectedFlag == null) { |
| 62 | + return getSystemConnectivity(context); |
| 63 | + } |
| 64 | + |
| 65 | + return connectedFlag; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Get the connectivity state as reported by the Android system |
| 70 | + * |
| 71 | + * @param context Android context |
| 72 | + * @return the connectivity state as reported by the Android system |
| 73 | + */ |
| 74 | + public static boolean isConnected(Context context) { |
| 75 | + return getSystemConnectivity(context); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Get the connectivity state. This can be overriden using the connectedFlag. |
| 80 | + * |
| 81 | + * @return the connectivity state |
| 82 | + */ |
| 83 | + public boolean isConnected() { |
| 84 | + return getManagedConnectivity(); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Get the connectedFlag value |
| 89 | + * |
| 90 | + * @return the connectedFlag value, true/false if the connectivity state has ben overriden, |
| 91 | + * null otherwise. |
| 92 | + */ |
| 93 | + public Boolean getConnectedFlag() { |
| 94 | + return connectedFlag; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Set the connectedFlag value |
| 99 | + * |
| 100 | + * @param connectedFlag Set it to true/false to override the connectivity state |
| 101 | + */ |
| 102 | + public void setConnectedFlag(Boolean connectedFlag) { |
| 103 | + this.connectedFlag = connectedFlag; |
| 104 | + } |
| 105 | + |
| 106 | + public void addConnectivityListener(ConnectivityListener listener) { |
| 107 | + if (!connectivityListeners.contains(listener)) { |
| 108 | + connectivityListeners.add(listener); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + public boolean removeConnectivityListener(ConnectivityListener listener) { |
| 113 | + return connectivityListeners.remove(listener); |
| 114 | + } |
| 115 | + |
| 116 | + public void requestConnectivityUpdates() { |
| 117 | + context.registerReceiver(this, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE")); |
| 118 | + } |
| 119 | + |
| 120 | + public void removeConnectivityUpdates() { |
| 121 | + context.unregisterReceiver(this); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public void onReceive(Context context, Intent intent) { |
| 126 | + boolean connected = getManagedConnectivity(); |
| 127 | + for (ConnectivityListener listener : connectivityListeners) { |
| 128 | + listener.onConnectivityChanged(connected); |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments