summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/net/ConnectivityReceiver.java
blob: bf7341ac96655fce7f8ac1a300cd06442e436284 (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
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 android.support.annotation.NonNull;
import android.support.annotation.UiThread;
import com.mapbox.mapboxsdk.Mapbox;
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";

  @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;

  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(INSTANCE, 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) {
    boolean connected = isConnected(context);
    Logger.v(TAG, String.format("Connected: %s", connected));

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

  /**
   * 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
   *
   * @param context current Context
   * @return true if connected
   */
  public boolean isConnected(@NonNull Context context) {
    Boolean connected = Mapbox.isConnected();
    if (connected != null) {
      // Connectivity state overridden by app
      return connected;
    }

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