Skip to content

Commit

Permalink
fix(android): use ConnectivityManager directly, drop androidx depende…
Browse files Browse the repository at this point in the history
…ncy (#509)

* Use ConnectivityManager instead of ConnectivityManagerCompat
* Update build.gradle
* Update README.md
* Update BroadcastReceiverConnectivityReceiver.java

Check for null connectivity manager before dereferencing

Co-authored-by: Mike Hardy <github@mikehardy.net>
  • Loading branch information
Willham12 and mikehardy committed Oct 22, 2021
1 parent dc9ffac commit 2569f56
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 40 deletions.
22 changes: 0 additions & 22 deletions README.md
Expand Up @@ -45,22 +45,6 @@ Linking the package manually is not required anymore with [Autolinking](https://
}
```

- **Android Platform with AndroidX:**

Modify your **android/build.gradle** configuration:
```
buildscript {
ext {
buildToolsVersion = "28.0.3"
minSdkVersion = 16
compileSdkVersion = 28
targetSdkVersion = 28
# Remove 'supportLibVersion' property and put specific versions for AndroidX libraries
androidXCore = "1.0.2"
// Put here other AndroidX dependencies
}
```

- **macOS Platform:**

Autolinking is not yet available on macOS. See the [Manual linking steps for macOS](#manual-linking-macos) below.
Expand Down Expand Up @@ -426,12 +410,6 @@ NetInfo.fetch("wifi").then(state => {
### Errors when building on Android
This library was migrated from using the support library to AndroidX in version `4.0.0`. All of your depenencies must be using either the support library *or* AndroidX. Using a mixture of the two is not possible.
From React Native 0.60 AndroidX is used by default.
If you need to either convert this library back to the support library (to use an older React Native version) or convert other libraries forward to use AndroidX (if they have not been updated yet), you can use the [Jetifier](https://github.com/mikehardy/jetifier) tool.
### Errors while running Jest tests
If you do not have a Jest Setup file configured, you should add the following to your Jest settings and create the `jest.setup.js` file in project root:
Expand Down
14 changes: 1 addition & 13 deletions android/build.gradle
Expand Up @@ -9,7 +9,7 @@ buildscript {
}

dependencies {
classpath("com.android.tools.build:gradle:3.6.3")
classpath("com.android.tools.build:gradle:4.2.2")
}
}
}
Expand Down Expand Up @@ -54,16 +54,4 @@ dependencies {
//noinspection GradleDynamicVersion
implementation 'com.facebook.react:react-native:+'

def supportLibVersion = getExtOrInitialValue('supportLibVersion', getExtOrInitialValue('supportVersion', null))
def androidXVersion = getExtOrInitialValue('androidXVersion', null)
def androidXCore = getExtOrInitialValue('androidXCore', null)
if (supportLibVersion && androidXVersion == null && androidXCore == null) {
implementation "com.android.support:appcompat-v7:$supportLibVersion"
} else {
def defaultAndroidXVersion = "1.3.2"
if (androidXCore == null) {
androidXCore = androidXVersion == null ? defaultAndroidXVersion : androidXVersion
}
implementation "androidx.core:core:$androidXCore"
}
}
Expand Up @@ -28,6 +28,7 @@
@SuppressWarnings("deprecation")
public class BroadcastReceiverConnectivityReceiver extends ConnectivityReceiver {
private final ConnectivityBroadcastReceiver mConnectivityBroadcastReceiver;
public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";

public BroadcastReceiverConnectivityReceiver(ReactApplicationContext reactContext) {
super(reactContext);
Expand All @@ -37,7 +38,7 @@ public BroadcastReceiverConnectivityReceiver(ReactApplicationContext reactContex
@Override
public void register() {
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
filter.addAction(CONNECTIVITY_ACTION);
getReactContext().registerReceiver(mConnectivityBroadcastReceiver, filter);
mConnectivityBroadcastReceiver.setRegistered(true);
updateAndSendConnectionType();
Expand Down Expand Up @@ -117,7 +118,7 @@ public boolean isRegistered() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action != null && action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
if (action != null && action.equals(CONNECTIVITY_ACTION)) {
updateAndSendConnectionType();
}
}
Expand Down
Expand Up @@ -12,8 +12,6 @@
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;

import androidx.core.net.ConnectivityManagerCompat;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
Expand Down Expand Up @@ -136,7 +134,7 @@ private WritableMap createConnectivityEventMap(@Nullable final String requestedI
WritableMap details = createDetailsMap(detailsInterface);
if (isConnected) {
boolean isConnectionExpensive =
ConnectivityManagerCompat.isActiveNetworkMetered(getConnectivityManager());
getConnectivityManager() == null ? true : getConnectivityManager().isActiveNetworkMetered();
details.putBoolean("isConnectionExpensive", isConnectionExpensive);
}
event.putMap("details", details);
Expand Down

0 comments on commit 2569f56

Please sign in to comment.