Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: ConnectivityManager to ConnectivityManager #1801

Merged
merged 1 commit into from
Sep 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 24 additions & 22 deletions app/src/main/java/com/money/manager/ex/utils/NetworkUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2012-2018 The Android Money Manager Ex Project Team
* Copyright (C) 2012-2024 The Android Money Manager Ex Project Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
Expand All @@ -18,22 +18,23 @@

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Network;
import android.net.NetworkCapabilities;

/**
* Network utility functions
*/
public class NetworkUtils {

public static boolean isOnline(Context context) {
return new NetworkUtils(context).isOnline();
}
private final Context mContext;

public NetworkUtils(Context context) {
mContext = context;
}

private final Context mContext;
public static boolean isOnline(Context context) {
return new NetworkUtils(context).isOnline();
}

public Context getContext() {
return mContext;
Expand All @@ -46,24 +47,25 @@ public Context getContext() {
*/
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();

return netInfo != null && netInfo.isConnected();
// isConnectedOrConnecting
if (cm != null) {
Network network = cm.getActiveNetwork();
if (network != null) {
NetworkCapabilities capabilities = cm.getNetworkCapabilities(network);
return capabilities != null && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
}
}
return false;
}

public boolean isOnWiFi() {
ConnectivityManager connManager = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
// check connManager.getAllNetworks()

// deprecated as of API 23.
// NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
if (networkInfo == null) return false; // no network
if (!networkInfo.isConnected()) return false;

return networkInfo.getType() == ConnectivityManager.TYPE_WIFI;

ConnectivityManager cm = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm != null) {
Network network = cm.getActiveNetwork();
if (network != null) {
NetworkCapabilities capabilities = cm.getNetworkCapabilities(network);
return capabilities != null && capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI);
}
}
return false;
}
}
Loading