Only listen to connectivity broadcast events when needed (#8222)
* [android] - only listen to connectivity change events when needed. * [android] - fix compile var args warning, remove redundant test
This commit is contained in:
@@ -3,16 +3,12 @@ package com.mapbox.mapboxsdk.maps;
|
||||
import android.app.Activity;
|
||||
import android.app.Dialog;
|
||||
import android.app.Fragment;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.PointF;
|
||||
import android.graphics.SurfaceTexture;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
@@ -45,6 +41,7 @@ import com.mapbox.mapboxsdk.constants.Style;
|
||||
import com.mapbox.mapboxsdk.maps.widgets.CompassView;
|
||||
import com.mapbox.mapboxsdk.maps.widgets.MyLocationView;
|
||||
import com.mapbox.mapboxsdk.maps.widgets.MyLocationViewSettings;
|
||||
import com.mapbox.mapboxsdk.net.ConnectivityReceiver;
|
||||
import com.mapbox.services.android.telemetry.MapboxEvent;
|
||||
import com.mapbox.services.android.telemetry.MapboxTelemetry;
|
||||
|
||||
@@ -72,7 +69,7 @@ public class MapView extends FrameLayout {
|
||||
|
||||
private NativeMapView nativeMapView;
|
||||
private boolean destroyed;
|
||||
private boolean hasSurface = false;
|
||||
private boolean hasSurface;
|
||||
|
||||
private MapboxMap mapboxMap;
|
||||
private MapCallback mapCallback;
|
||||
@@ -83,8 +80,6 @@ public class MapView extends FrameLayout {
|
||||
private MapKeyListener mapKeyListener;
|
||||
private MapZoomButtonController mapZoomButtonController;
|
||||
|
||||
private ConnectivityReceiver connectivityReceiver;
|
||||
|
||||
@UiThread
|
||||
public MapView(@NonNull Context context) {
|
||||
super(context);
|
||||
@@ -167,7 +162,7 @@ public class MapView extends FrameLayout {
|
||||
setWillNotDraw(false);
|
||||
|
||||
// notify Map object about current connectivity state
|
||||
nativeMapView.setReachability(isConnected());
|
||||
nativeMapView.setReachability(ConnectivityReceiver.instance(context).isConnected(context));
|
||||
|
||||
// initialise MapboxMap
|
||||
mapboxMap.initialise(context, options);
|
||||
@@ -235,7 +230,7 @@ public class MapView extends FrameLayout {
|
||||
public void onStart() {
|
||||
onStartCalled = true;
|
||||
mapboxMap.onStart();
|
||||
registerConnectivityReceiver();
|
||||
ConnectivityReceiver.instance(getContext()).activate();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -265,7 +260,7 @@ public class MapView extends FrameLayout {
|
||||
public void onStop() {
|
||||
onStopCalled = true;
|
||||
mapboxMap.onStop();
|
||||
unregisterConnectivityReceiver();
|
||||
ConnectivityReceiver.instance(getContext()).deactivate();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -287,18 +282,6 @@ public class MapView extends FrameLayout {
|
||||
nativeMapView = null;
|
||||
}
|
||||
|
||||
private void registerConnectivityReceiver() {
|
||||
getContext().registerReceiver(connectivityReceiver = new ConnectivityReceiver(),
|
||||
new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
|
||||
}
|
||||
|
||||
private void unregisterConnectivityReceiver() {
|
||||
if (connectivityReceiver != null) {
|
||||
getContext().unregisterReceiver(connectivityReceiver);
|
||||
connectivityReceiver = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onTouchEvent(MotionEvent event) {
|
||||
if (event.getAction() == MotionEvent.ACTION_DOWN) {
|
||||
@@ -547,30 +530,6 @@ public class MapView extends FrameLayout {
|
||||
mapZoomButtonController.setVisible(visibility == View.VISIBLE);
|
||||
}
|
||||
|
||||
//
|
||||
// Connectivity events
|
||||
//
|
||||
|
||||
// This class handles connectivity changes
|
||||
private class ConnectivityReceiver extends BroadcastReceiver {
|
||||
|
||||
// Called when an action we are listening to in the manifest has been sent
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (!destroyed && intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
|
||||
nativeMapView.setReachability(!intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Called when MapView is being created
|
||||
private boolean isConnected() {
|
||||
ConnectivityManager connectivityManager = (ConnectivityManager)
|
||||
getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
|
||||
return (activeNetwork != null) && activeNetwork.isConnectedOrConnecting();
|
||||
}
|
||||
|
||||
//
|
||||
// Map events
|
||||
//
|
||||
|
||||
@@ -7,6 +7,7 @@ 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;
|
||||
|
||||
@@ -28,9 +29,7 @@ public class ConnectivityReceiver extends BroadcastReceiver {
|
||||
public static synchronized ConnectivityReceiver instance(Context context) {
|
||||
if (INSTANCE == null) {
|
||||
// Register new instance
|
||||
INSTANCE = new ConnectivityReceiver();
|
||||
context.registerReceiver(INSTANCE, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
|
||||
|
||||
INSTANCE = new ConnectivityReceiver(context.getApplicationContext());
|
||||
// Add default listeners
|
||||
INSTANCE.addListener(new NativeConnectivityListener());
|
||||
}
|
||||
@@ -39,8 +38,39 @@ public class ConnectivityReceiver extends BroadcastReceiver {
|
||||
}
|
||||
|
||||
private List<ConnectivityListener> listeners = new CopyOnWriteArrayList<>();
|
||||
private Context context;
|
||||
private int activationCounter;
|
||||
|
||||
private ConnectivityReceiver() {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -92,5 +122,4 @@ public class ConnectivityReceiver extends BroadcastReceiver {
|
||||
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
|
||||
return (activeNetwork != null && activeNetwork.isConnected());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import com.mapbox.mapboxsdk.net.ConnectivityReceiver;
|
||||
import com.mapbox.mapboxsdk.storage.FileSource;
|
||||
|
||||
import java.io.File;
|
||||
@@ -39,6 +40,9 @@ public class OfflineManager {
|
||||
// This object is implemented as a singleton
|
||||
private static OfflineManager instance;
|
||||
|
||||
// The application context
|
||||
private Context context;
|
||||
|
||||
/**
|
||||
* This callback receives an asynchronous response containing a list of all
|
||||
* {@link OfflineRegion} in the database, or an error message otherwise.
|
||||
@@ -83,6 +87,7 @@ public class OfflineManager {
|
||||
* Constructor
|
||||
*/
|
||||
private OfflineManager(Context context) {
|
||||
this.context = context.getApplicationContext();
|
||||
this.fileSource = FileSource.getInstance(context);
|
||||
initialize(fileSource);
|
||||
|
||||
@@ -180,6 +185,7 @@ public class OfflineManager {
|
||||
@NonNull byte[] metadata,
|
||||
@NonNull final CreateOfflineRegionCallback callback) {
|
||||
|
||||
ConnectivityReceiver.instance(context).activate();
|
||||
createOfflineRegion(fileSource, definition, metadata, new CreateOfflineRegionCallback() {
|
||||
|
||||
@Override
|
||||
@@ -187,6 +193,7 @@ public class OfflineManager {
|
||||
getHandler().post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
ConnectivityReceiver.instance(context).deactivate();
|
||||
callback.onCreate(offlineRegion);
|
||||
}
|
||||
});
|
||||
@@ -197,6 +204,7 @@ public class OfflineManager {
|
||||
getHandler().post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
ConnectivityReceiver.instance(context).deactivate();
|
||||
callback.onError(error);
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user