Skip to content

Commit

Permalink
Merge commit '994392f34324317a221bf2e87494fd76d69ddd60'
Browse files Browse the repository at this point in the history
* commit '994392f34324317a221bf2e87494fd76d69ddd60':
  Fix getResources() null crash in mapview

Conflicts:
	lib/android/googlemap/src/main/java/com/airbnb/android/react/maps/googlemap/AirGoogleMapView.java
  • Loading branch information
jiaminglu committed May 24, 2017
2 parents 79a7dda + 994392f commit c66f664
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected AirAMapView createViewInstance(ThemedReactContext context) {
emitMapError(context, "Map initialize error", "map_init_error");
}

return new AirAMapView(context, this.appContext.getCurrentActivity(), this, this.mapOptions);
return new AirAMapView(context, this, this.mapOptions);
}

@ReactProp(name = "region")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,37 @@ public class AirAMapView extends MapView implements AMap.InfoWindowAdapter, AMap
private final ThemedReactContext context;
private final EventDispatcher eventDispatcher;

public AirAMapView(ThemedReactContext reactContext, Context appContext, AirAMapManager manager, AMapOptions options) {
super(appContext, options);
private static boolean contextHasBug(Context context) {
return context == null ||
context.getResources() == null ||
context.getResources().getConfiguration() == null;
}

// We do this to fix this bug:
// https://github.com/airbnb/react-native-maps/issues/271
//
// which conflicts with another bug regarding the passed in context:
// https://github.com/airbnb/react-native-maps/issues/1147
//
// Doing this allows us to avoid both bugs.
private static Context getNonBuggyContext(ThemedReactContext reactContext) {
Context superContext = reactContext;

if (contextHasBug(superContext)) {
// we have the bug! let's try to find a better context to use
if (!contextHasBug(reactContext.getCurrentActivity())) {
superContext = reactContext.getCurrentActivity();
} else if (!contextHasBug(reactContext.getApplicationContext())) {
superContext = reactContext.getApplicationContext();
} else {
// ¯\_(ツ)_/¯
}
}
return superContext;
}

public AirAMapView(ThemedReactContext reactContext, AirAMapManager manager, AMapOptions options) {
super(getNonBuggyContext(reactContext), options);

this.manager = manager;
this.context = reactContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected AirGoogleMapView createViewInstance(ThemedReactContext context) {
emitMapError(context, "Map initialize error", "map_init_error");
}

return new AirGoogleMapView(context, this.appContext.getCurrentActivity(), this, this.googleMapOptions);
return new AirGoogleMapView(context, this, this.googleMapOptions);
}

@ReactProp(name = "region")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.airbnb.android.react.maps.googlemap;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.ColorStateList;
Expand Down Expand Up @@ -90,9 +91,38 @@ public class AirGoogleMapView extends MapView implements GoogleMap.InfoWindowAda
private final ThemedReactContext context;
private final EventDispatcher eventDispatcher;

public AirGoogleMapView(ThemedReactContext reactContext, Context appContext, AirGoogleMapManager manager,
GoogleMapOptions googleMapOptions) {
super(appContext, googleMapOptions);
private static boolean contextHasBug(Context context) {
return context == null ||
context.getResources() == null ||
context.getResources().getConfiguration() == null;
}

// We do this to fix this bug:
// https://github.com/airbnb/react-native-maps/issues/271
//
// which conflicts with another bug regarding the passed in context:
// https://github.com/airbnb/react-native-maps/issues/1147
//
// Doing this allows us to avoid both bugs.
private static Context getNonBuggyContext(ThemedReactContext reactContext) {
Context superContext = reactContext;

if (contextHasBug(superContext)) {
// we have the bug! let's try to find a better context to use
if (!contextHasBug(reactContext.getCurrentActivity())) {
superContext = reactContext.getCurrentActivity();
} else if (!contextHasBug(reactContext.getApplicationContext())) {
superContext = reactContext.getApplicationContext();
} else {
// ¯\_(ツ)_/¯
}
}
return superContext;
}

public AirGoogleMapView(ThemedReactContext reactContext, AirGoogleMapManager manager,
GoogleMapOptions googleMapOptions) {
super(getNonBuggyContext(reactContext), googleMapOptions);

this.manager = manager;
this.context = reactContext;
Expand Down

0 comments on commit c66f664

Please sign in to comment.