summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/net/ConnectivityReceiver.java
blob: 632b519f83b63be438fe14fa83f81a29f50cb2ab (plain)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package com.mapbox.mapboxsdk.net;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.UiThread;

import com.mapbox.mapboxsdk.log.Logger;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Interface definition for a callback to be invoked when connectivity changes.
 * Not public api.
 */
public class ConnectivityReceiver extends BroadcastReceiver {

  private static final String TAG = "Mbgl-ConnectivityReceiver";
  private static final String LOG_CONNECTED = "connected - true";
  private static final String LOG_NOT_CONNECTED = "connected - false";

  @SuppressLint("StaticFieldLeak")
  private static ConnectivityReceiver INSTANCE;

  /**
   * Get a single instance of ConnectivityReceiver.
   *
   * @param context the context to extract the application context from
   * @return single instance of ConnectivityReceiver
   */
  public static synchronized ConnectivityReceiver instance(@NonNull Context context) {
    if (INSTANCE == null) {
      // Register new instance
      INSTANCE = new ConnectivityReceiver(context.getApplicationContext());
      // Add default listeners
      INSTANCE.addListener(new NativeConnectivityListener());
    }

    return INSTANCE;
  }

  @NonNull
  private List<ConnectivityListener> listeners = new CopyOnWriteArrayList<>();
  private Context context;
  private int activationCounter;
  @Nullable
  private Boolean connected;

  private ConnectivityReceiver(@NonNull Context context) {
    this.context = context;
  }

  /**
   * Activates the connectivity receiver.
   * <p>
   * if the underlying connectivity receiver isn't active, register the connectivity receiver.
   * </p>
   */
  @UiThread
  public void activate() {
    if (activationCounter == 0) {
      context.registerReceiver(this, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
    }
    activationCounter++;
  }

  /**
   * Deactivates the connectivity receiver.
   * <p>
   * if no other components are listening, unregister the underlying connectivity receiver.
   * </p>
   */
  @UiThread
  public void deactivate() {
    activationCounter--;
    if (activationCounter == 0) {
      context.unregisterReceiver(INSTANCE);
    }
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void onReceive(@NonNull Context context, Intent intent) {
    if (connected != null) {
      // Connectivity state overridden by app
      return;
    }

    notifyListeners(isNetworkActive());
  }

  /**
   * Overwrites system connectivity state. To set, use {@link com.mapbox.mapboxsdk.Mapbox#setConnected(Boolean)}.
   *
   * @param connected flag to determine the connectivity state, true for connected, false for
   *                  disconnected, and null for ConnectivityManager to determine.
   */
  public void setConnected(Boolean connected) {
    this.connected = connected;

    boolean state;
    if (connected != null) {
      state = connected;
    } else {
      state = isNetworkActive();
    }
    notifyListeners(state);
  }

  private void notifyListeners(boolean isConnected) {
    Logger.v(TAG, isConnected ? LOG_CONNECTED : LOG_NOT_CONNECTED);

    // Loop over listeners
    for (ConnectivityListener listener : listeners) {
      listener.onNetworkStateChanged(isConnected);
    }
  }

  /**
   * Add a listener to be notified
   *
   * @param listener the listener to add
   */
  public void addListener(@NonNull ConnectivityListener listener) {
    listeners.add(listener);
  }

  /**
   * Remove a listener
   *
   * @param listener the listener to remove
   */
  public void removeListener(@NonNull ConnectivityListener listener) {
    listeners.remove(listener);
  }

  /**
   * Get current connectivity state
   *
   * @return true if connected
   */
  public boolean isConnected() {
    return connected != null ? connected : isNetworkActive();
  }

  private boolean isNetworkActive() {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    return (activeNetwork != null && activeNetwork.isConnected());
  }
}