All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.apptik.comm.jus.ConnectivityManager Maven / Gradle / Ivy

The newest version!
package io.apptik.comm.jus;


import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;


/**
 * Implementers should call {@link #connected()} or {@link #disconnected()} whener the state changes
 */
public abstract class ConnectivityManager {

    List connectivityListeners = new ArrayList<>();

    public abstract NetworkInfo getActiveNetwork();

    public  T addListener(ConnectivityListener listener) {
        connectivityListeners.add(listener);
        return (T) this;
    }

    public  T removeListener(ConnectivityListener listener) {
        connectivityListeners.remove(listener);
        return (T) this;
    }

    /**
     * Call this whenever connectivity changes to disconnected
     */
    protected final void disconnected() {
        for (ConnectivityListener listener : connectivityListeners) {
            listener.onDisconnected();
        }
    }

    /**
     * Call this whenever connectivity changes to connected
     */
    protected final void connected() {
        for (ConnectivityListener listener : connectivityListeners) {
            listener.onConnected();
        }
    }

    public interface ConnectivityListener {
        void onConnected();

        void onDisconnected();
    }


    abstract class Factory {
        public abstract ConnectivityManager get(Request request);
    }

    /** Copyright (C) 2008 The Android Open Source Project
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *      http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     *
     *
     * Describes the status of a network interface.
     * 

Use {@link ConnectivityManager#getActiveNetwork()} to get an instance that represents * the current network connection. */ public static class NetworkInfo { /** * The absence of a connection type. * */ public static final String TYPE_NONE = "TYPE_NONE"; /** * The Mobile data connection. When active, all data traffic * will use this network type's interface by default * (it has a default route) */ public static final String TYPE_MOBILE = "TYPE_MOBILE"; /** * The WIFI data connection. When active, all data traffic * will use this network type's interface by default * (it has a default route). */ public static final String TYPE_WIFI = "TYPE_WIFI"; /** * An MMS-specific Mobile data connection. This network type may use the * same network interface as {@link #TYPE_MOBILE} or it may use a different * one. This is used by applications needing to talk to the carrier's * Multimedia Messaging Service servers. * */ public static final String TYPE_MOBILE_MMS = "TYPE_MOBILE_MMS"; /** * A SUPL-specific Mobile data connection. This network type may use the * same network interface as {@link #TYPE_MOBILE} or it may use a different * one. This is used by applications needing to talk to the carrier's * Secure User Plane Location servers for help locating the device. * */ public static final String TYPE_MOBILE_SUPL = "TYPE_MOBILE_SUPL"; /** * A DUN-specific Mobile data connection. This network type may use the * same network interface as {@link #TYPE_MOBILE} or it may use a different * one. This is sometimes by the system when setting up an upstream connection * for tethering so that the carrier is aware of DUN traffic. */ public static final String TYPE_MOBILE_DUN = "TYPE_MOBILE_DUN"; /** * A High Priority Mobile data connection. This network type uses the * same network interface as {@link #TYPE_MOBILE} but the routing setup * is different. * */ public static final String TYPE_MOBILE_HIPRI = "TYPE_MOBILE_HIPRI"; /** * The WiMAX data connection. When active, all data traffic * will use this network type's interface by default * (it has a default route). */ public static final String TYPE_WIMAX = "TYPE_WIMAX"; /** * The Bluetooth data connection. When active, all data traffic * will use this network type's interface by default * (it has a default route). */ public static final String TYPE_BLUETOOTH = "TYPE_BLUETOOTH"; /** * The Ethernet data connection. When active, all data traffic * will use this network type's interface by default * (it has a default route). */ public static final String TYPE_ETHERNET = "TYPE_ETHERNET"; /** * Over the air Administration. * {} */ public static final String TYPE_MOBILE_FOTA = "TYPE_MOBILE_FOTA"; /** * IP Multimedia Subsystem. * {} */ public static final String TYPE_MOBILE_IMS = "TYPE_MOBILE_IMS"; /** * Carrier Branded Services. * {} */ public static final String TYPE_MOBILE_CBS = "TYPE_MOBILE_CBS"; /** * A Wi-Fi p2p connection. Only requesting processes will have access to * the peers connected. * {} */ public static final String TYPE_WIFI_P2P = "TYPE_WIFI_P2P"; /** * The network to use for initially attaching to the network * {} */ public static final String TYPE_MOBILE_IA = "TYPE_MOBILE_IA"; /** * Emergency PDN connection for emergency services. This * may include IMS and MMS in emergency situations. * {} */ public static final String TYPE_MOBILE_EMERGENCY = "TYPE_MOBILE_EMERGENCY"; /** * The network that uses proxy to achieve connectivity. * {} */ public static final String TYPE_PROXY = "TYPE_PROXY"; /** * A virtual network using one or more native bearers. * It may or may not be providing security services. */ public static final String TYPE_VPN = "TYPE_VPN"; /** * Coarse-grained network state. This is probably what most applications should * use, rather than {@link NetworkInfo.DetailedState DetailedState}. * The mapping between the two is as follows: *

*

* * * * * * * * * * *
Detailed stateCoarse-grained state
IDLEDISCONNECTED
SCANNINGCONNECTING
CONNECTINGCONNECTING
AUTHENTICATINGCONNECTING
CONNECTEDCONNECTED
DISCONNECTINGDISCONNECTING
DISCONNECTEDDISCONNECTED
UNAVAILABLEDISCONNECTED
FAILEDDISCONNECTED
*/ public enum State { CONNECTING, CONNECTED, SUSPENDED, DISCONNECTING, DISCONNECTED, UNKNOWN } /** * The fine-grained state of a network connection. This level of detail * is probably of interest to few applications. Most should use * {@link NetworkInfo.State State} instead. */ public enum DetailedState { /** Ready to start data connection setup. */ IDLE, /** Searching for an available access point. */ SCANNING, /** Currently setting up data connection. */ CONNECTING, /** Network link established, performing authentication. */ AUTHENTICATING, /** Awaiting response from DHCP server in order to assign IP address information. */ OBTAINING_IPADDR, /** IP traffic should be available. */ CONNECTED, /** IP traffic is suspended */ SUSPENDED, /** Currently tearing down data connection. */ DISCONNECTING, /** IP traffic not available. */ DISCONNECTED, /** Attempt to connect failed. */ FAILED, /** Access to this network is blocked. */ BLOCKED, /** Link has poor connectivity. */ VERIFYING_POOR_LINK, /** Checking if network is a captive portal */ CAPTIVE_PORTAL_CHECK } /** * This is the map described in the Javadoc comment above. The positions * of the elements of the array must correspond to the ordinal values * of DetailedState. */ private static final EnumMap stateMap = new EnumMap(DetailedState.class); static { stateMap.put(DetailedState.IDLE, State.DISCONNECTED); stateMap.put(DetailedState.SCANNING, State.DISCONNECTED); stateMap.put(DetailedState.CONNECTING, State.CONNECTING); stateMap.put(DetailedState.AUTHENTICATING, State.CONNECTING); stateMap.put(DetailedState.OBTAINING_IPADDR, State.CONNECTING); stateMap.put(DetailedState.VERIFYING_POOR_LINK, State.CONNECTING); stateMap.put(DetailedState.CAPTIVE_PORTAL_CHECK, State.CONNECTING); stateMap.put(DetailedState.CONNECTED, State.CONNECTED); stateMap.put(DetailedState.SUSPENDED, State.SUSPENDED); stateMap.put(DetailedState.DISCONNECTING, State.DISCONNECTING); stateMap.put(DetailedState.DISCONNECTED, State.DISCONNECTED); stateMap.put(DetailedState.FAILED, State.DISCONNECTED); stateMap.put(DetailedState.BLOCKED, State.DISCONNECTED); } private String mNetworkType; private String mSubtype; private State mState; private DetailedState mDetailedState; private String mReason; private String mExtraInfo; private boolean mIsFailover; private boolean mIsRoaming; /** * Indicates whether network connectivity is possible: */ private boolean mIsAvailable; /** * */ public NetworkInfo(String type, String subtype) { mNetworkType = type; mSubtype = subtype; setDetailedState(DetailedState.IDLE, null, null); mState = State.UNKNOWN; mIsAvailable = false; // until we're told otherwise, assume unavailable mIsRoaming = false; } /** {} */ public NetworkInfo(NetworkInfo source) { if (source != null) { synchronized (source) { mNetworkType = source.mNetworkType; mSubtype = source.mSubtype; mState = source.mState; mDetailedState = source.mDetailedState; mReason = source.mReason; mExtraInfo = source.mExtraInfo; mIsFailover = source.mIsFailover; mIsRoaming = source.mIsRoaming; mIsAvailable = source.mIsAvailable; } } } /** * Reports the type of network to which the * info in this {@code NetworkInfo} pertains. * @return one of {@link #TYPE_MOBILE}, {@link * #TYPE_WIFI}, {@link #TYPE_WIMAX}, {@link * #TYPE_ETHERNET}, {@link #TYPE_BLUETOOTH}, or other * types defined by {@link ConnectivityManager} */ public String getType() { synchronized (this) { return mNetworkType; } } public void setType(String type) { synchronized (this) { mNetworkType = type; } } /** * Return a network-type-specific integer describing the subtype * of the network. * @return the network subtype */ public String getSubtype() { synchronized (this) { return mSubtype; } } /** * */ public void setSubtype(String subtype) { synchronized (this) { mSubtype = subtype; } } /** * Indicates whether network connectivity exists or is in the process * of being established. This is good for applications that need to * do anything related to the network other than read or write data. * For the latter, call {@link #isConnected()} instead, which guarantees * that the network is fully usable. * @return {@code true} if network connectivity exists or is in the process * of being established, {@code false} otherwise. */ public boolean isConnectedOrConnecting() { synchronized (this) { return mState == State.CONNECTED || mState == State.CONNECTING; } } /** * Indicates whether network connectivity exists and it is possible to establish * connections and pass data. *

Always call this before attempting to perform data transactions. * @return {@code true} if network connectivity exists, {@code false} otherwise. */ public boolean isConnected() { synchronized (this) { return mState == State.CONNECTED; } } /** * Indicates whether network connectivity is possible. A network is unavailable * when a persistent or semi-persistent condition prevents the possibility * of connecting to that network. Examples include *

    *
  • The device is out of the coverage area for any network of this type.
  • *
  • The device is on a network other than the home network (i.e., roaming), and * data roaming has been disabled.
  • *
  • The device's radio is turned off, e.g., because airplane mode is enabled.
  • *
* @return {@code true} if the network is available, {@code false} otherwise */ public boolean isAvailable() { synchronized (this) { return mIsAvailable; } } /** * Sets if the network is available, ie, if the connectivity is possible. * @param isAvailable the new availability value. * * */ public void setIsAvailable(boolean isAvailable) { synchronized (this) { mIsAvailable = isAvailable; } } /** * Indicates whether the current attempt to connect to the network * resulted from the ConnectivityManager trying to fail over to this * network following a disconnect from another network. * @return {@code true} if this is a failover attempt, {@code false} * otherwise. */ public boolean isFailover() { synchronized (this) { return mIsFailover; } } /** * Set the failover boolean. * @param isFailover {@code true} to mark the current connection attempt * as a failover. * */ public void setFailover(boolean isFailover) { synchronized (this) { mIsFailover = isFailover; } } /** * Indicates whether the device is currently roaming on this network. * When {@code true}, it suggests that use of data on this network * may incur extra costs. * @return {@code true} if roaming is in effect, {@code false} otherwise. */ public boolean isRoaming() { synchronized (this) { return mIsRoaming; } } public void setRoaming(boolean isRoaming) { synchronized (this) { mIsRoaming = isRoaming; } } /** * Reports the current coarse-grained state of the network. * @return the coarse-grained state */ public State getState() { synchronized (this) { return mState; } } /** * Reports the current fine-grained state of the network. * @return the fine-grained state */ public DetailedState getDetailedState() { synchronized (this) { return mDetailedState; } } /** * Sets the fine-grained state of the network. * @param detailedState the {@link DetailedState}. * @param reason a {@code String} indicating the reason for the state change, * if one was supplied. May be {@code null}. * @param extraInfo an optional {@code String} providing addditional network state * information passed up from the lower networking layers. * */ public void setDetailedState(DetailedState detailedState, String reason, String extraInfo) { synchronized (this) { this.mDetailedState = detailedState; this.mState = stateMap.get(detailedState); this.mReason = reason; this.mExtraInfo = extraInfo; } } /** * Set the extraInfo field. * @param extraInfo an optional {@code String} providing addditional network state * information passed up from the lower networking layers. * */ public void setExtraInfo(String extraInfo) { synchronized (this) { this.mExtraInfo = extraInfo; } } /** * Report the reason an attempt to establish connectivity failed, * if one is available. * @return the reason for failure, or null if not available */ public String getReason() { synchronized (this) { return mReason; } } /** * Report the extra information about the network state, if any was * provided by the lower networking layers. * @return the extra information, or null if not available */ public String getExtraInfo() { synchronized (this) { return mExtraInfo; } } @Override public String toString() { synchronized (this) { String builder = "[" + "type: " + getType() + "[" + getSubtype() + "], state: " + mState + "/" + mDetailedState + ", reason: " + (mReason == null ? "(unspecified)" : mReason) + ", extra: " + (mExtraInfo == null ? "(none)" : mExtraInfo) + ", roaming: " + mIsRoaming + ", failover: " + mIsFailover + ", isAvailable: " + mIsAvailable + "]"; return builder; } } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy