/* * Copyright (C) 2016 Glucosio Foundation * * This file is part of Glucosio. * * Glucosio is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, version 3. * * Glucosio is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Glucosio. If not, see <http://www.gnu.org/licenses/>. * * */ package org.glucosio.android.tools.network; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import java.lang.ref.WeakReference; public class BasicNetworkConnectivity implements NetworkConnectivity { private WeakReference<Context> context; public BasicNetworkConnectivity(Context context) { this.context = new WeakReference<>(context); } public boolean isConnected() { ConnectivityManager cm = provideConnectivityService(context.get()); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); return activeNetwork != null && activeNetwork.isConnectedOrConnecting(); } private static ConnectivityManager provideConnectivityService(Context context) { return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); } }