Back to project page android_device.
The source code is released under:
[Apache License](http://www.apache.org/licenses/): Version 2.0, January 2004 =============== ## TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION ## ### 1. Definitions. ### "License" sha...
If you think the Android project android_device listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * ================================================================================================= * Copyright (C) 2014 Martin Albedinsky [Wolf-ITechnologies] * ================================================================================================= * Licensed under the Apache License, Version 2.0 or later (further "License" only). * ------------------------------------------------------------------------------------------------- * You may use this file only in compliance with the License. More details and copy of this License * you may obtain at/* w ww . ja va 2 s . c o m*/ * * http://www.apache.org/licenses/LICENSE-2.0 * * You can redistribute, modify or publish any part of the code written within this file but as it * is described in the License, the software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES or CONDITIONS OF ANY KIND. * * See the License for the specific language governing permissions and limitations under the License. * ================================================================================================= */ package com.wit.android.device.util; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.support.annotation.NonNull; import android.support.annotation.Nullable; /** * <h3>Class Overview</h3> * todo: description * * @author Martin Albedinsky */ public class ConnectionUtils { /** * Interface =================================================================================== */ /** * Constants =================================================================================== */ /** * Log TAG. */ // private static final String TAG = "ConnectionUtils"; /** * Flag indicating whether the output trough log-cat is enabled or not. */ // private static final boolean LOG_ENABLED = true; /** * Flag indicating whether the debug output trough log-cat is enabled or not. */ // private static final boolean DEBUG_ENABLED = true; /** * Methods ===================================================================================== */ /** * Checks whether there is some connection currently established or not. * <p> * See {@link android.net.ConnectivityManager#getActiveNetworkInfo()} for additional info. * * @param context Context used to access {@link android.net.ConnectivityManager}. * @return {@code True} if there is some connection available and it is currently established, * {@code false} otherwise. * @see #isConnectionEstablished(android.content.Context, int) */ public static boolean isConnectionEstablished(@NonNull Context context) { final NetworkInfo info = obtainEstablishedConnectionInfo(context); return info != null && info.isConnected(); } /** * Checks whether there is connection of the requested <var>connectionType</var> currently established * or not. * <p> * See {@link android.net.NetworkInfo#isConnected()} for additional info. * * @param context Context used to access {@link android.net.ConnectivityManager}. * @param connectionType Type of the desired connection of which current connectivity to check. * @return {@code True} if there is connection of the requested type available and it is * currently established, {@code false} otherwise. * @see #isConnectionEstablished(android.content.Context) * @see #isConnectionAvailable(android.content.Context, int) */ public static boolean isConnectionEstablished(@NonNull Context context, int connectionType) { final NetworkInfo info = accessManager(context).getNetworkInfo(connectionType); return info != null && info.isConnected(); } /** * Checks whether there is connection of the requested <var>connectionType</var> available or not. * <p> * See {@link android.net.NetworkInfo#isAvailable()} for additional info. * * @param context Context used to access {@link android.net.ConnectivityManager}. * @param connectionType Type of the desired connection of which availability to check. * @return {@code True} if connection of the requested type available, {@code false} * otherwise. * @see #isConnectionEstablished(android.content.Context, int) */ public static boolean isConnectionAvailable(@NonNull Context context, int connectionType) { final NetworkInfo info = accessManager(context).getNetworkInfo(connectionType); return info != null && info.isAvailable(); } /** * Obtains type of the currently established connection. * <p> * See {@link android.net.ConnectivityManager#getActiveNetworkInfo()} for additional info. * * @param context Context used to access {@link android.net.ConnectivityManager}. * @return Type provided by {@link android.net.ConnectivityManager} or {@code -1} if * there is no connection currently established. * @see #obtainEstablishedConnectionInfo(android.content.Context) * @see #isConnectionEstablished(android.content.Context) */ public static int obtainEstablishedConnectionType(@NonNull Context context) { final NetworkInfo info = obtainEstablishedConnectionInfo(context); return info != null ? info.getType() : -1; } /** * Obtains info of the currently established connection. * * @param context Context used to access {@link android.net.ConnectivityManager}. * @return Info provided by {@link android.net.ConnectivityManager} or {@code null} if there * is on connection currently established. * @see #obtainEstablishedConnectionType(android.content.Context) * @see #isConnectionEstablished(android.content.Context) */ @Nullable public static NetworkInfo obtainEstablishedConnectionInfo(@NonNull Context context) { return accessManager(context).getActiveNetworkInfo(); } /** * Accesses the ConnectivityManager service using the given <var>context</var>. * * @param context Context from which ConnectivityManager should be accessed. * @return An instance of ConnectivityManager. */ private static ConnectivityManager accessManager(Context context) { return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); } }