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) 2013 - 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 w w . jav a2 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; import android.content.Context; import android.content.SharedPreferences; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import com.wit.android.device.receiver.BroadcastProcessor; /** * <h3>Interface Overview</h3> * todo: description * * @author Martin Albedinsky */ public interface Connection extends BroadcastProcessor { /** * Constants =================================================================================== */ /** * Enums ======================================================================================= */ /** * <h3>Enum Overview</h3> * Represents type of an Android device's connection. * <p> * See {@link android.net.ConnectivityManager} for types supported from the * {@link android.os.Build.VERSION_CODES#HONEYCOMB_MR2 API level 13} and higher. * * @author Martin Albedinsky */ public enum ConnectionType { /** * Connection type indicating that there is no connection currently available. * <p> * <i>Constant value:</i> <b>-1</b> * <ul> * <li><i>Original name:</i> <b>Unavailable</b></li> * <li><i>Preferences key:</i> <b>NETWORK_TYPE.UNAVAILABLE</b></li> * </ul> */ UNAVAILABLE(-1, "Unavailable", "NETWORK_TYPE.UNAVAILABLE"), /** * Connection type indicating that the current connection is established trough <b>bluetooth</b>. * <p> * From {@link android.net.ConnectivityManager#TYPE_BLUETOOTH} * <ul> * <li><i>Original name:</i> <b>Bluetooth</b></li> * <li><i>Preferences key:</i> <b>NETWORK_TYPE.BLUETOOTH</b></li> * </ul> * * @since {@link android.os.Build.VERSION_CODES#HONEYCOMB_MR2 API level 13} */ BLUETOOTH(ConnectivityManager.TYPE_BLUETOOTH, "Bluetooth", "NETWORK_TYPE.BLUETOOTH"), /** * Connection type indicating that the current connection is <b>dummy</b>. * <p> * From {@link android.net.ConnectivityManager#TYPE_DUMMY} * <ul> * <li><i>Original name:</i> <b>Dummy</b></li> * <li><i>Preferences key:</i> <b>NETWORK_TYPE.DUMMY</b></li> * </ul> * * @since {@link android.os.Build.VERSION_CODES#HONEYCOMB_MR2 API level 13} */ DUMMY(ConnectivityManager.TYPE_DUMMY, "Dummy", "NETWORK_TYPE.DUMMY"), /** * Connection type indicating that the current connection is established trough <b>ethernet</b>. * <p> * From {@link android.net.ConnectivityManager#TYPE_ETHERNET} * <ul> * <li><i>Original name:</i> <b>Ethernet</b></li> * <li><i>Preferences key:</i> <b>NETWORK_TYPE.ETHERNET</b></li> * </ul> * * @since {@link android.os.Build.VERSION_CODES#HONEYCOMB_MR2 API level 13} */ ETHERNET(ConnectivityManager.TYPE_ETHERNET, "Ethernet", "NETWORK_TYPE.ETHERNET"), /** * Connection type indicating that the current connection is established trough common * <b>mobile</b> network (<i>3G, ...</i>). * <p> * From {@link android.net.ConnectivityManager#TYPE_MOBILE} * <ul> * <li><i>Original name:</i> <b>Mobile</b></li> * <li><i>Preferences key:</i> <b>NETWORK_TYPE.MOBILE</b></li> * </ul> */ MOBILE(ConnectivityManager.TYPE_MOBILE, "Mobile", "NETWORK_TYPE.MOBILE"), /** * Connection type indicating that the current connection is established trough <b>mobile</b> * network (<i>DUN traffic</i>). * <p> * From {@link android.net.ConnectivityManager#TYPE_MOBILE_DUN} * <ul> * <li><i>Original name:</i> <b>Mobile (DUN)</b></li> * <li><i>Preferences key:</i> <b>NETWORK_TYPE.MOBILE_DUN</b></li> * </ul> */ MOBILE_DUN(ConnectivityManager.TYPE_MOBILE_DUN, "Mobile (DUN)", "NETWORK_TYPE.MOBILE_DUN"), /** * Connection type indicating that the current connection is established trough <b>mobile</b> * network. * <p> * From {@link android.net.ConnectivityManager#TYPE_MOBILE_HIPRI} * <ul> * <li><i>Original name:</i> <b>Mobile (HIPRI)</b></li> * <li><i>Preferences key:</i> <b>NETWORK_TYPE.MOBILE_HIPRI</b></li> * </ul> */ MOBILE_HIPRI(ConnectivityManager.TYPE_MOBILE_HIPRI, "Mobile (HIPRI)", "NETWORK_TYPE.MOBILE_HIPRI"), /** * Connection type indicating that the current connection is established trough <b>mobile</b> * network specific for Multimedia Messaging Services. * <p> * From {@link android.net.ConnectivityManager#TYPE_MOBILE_MMS} * <ul> * <li><i>Original name:</i> <b>Mobile (MMS)</b></li> * <li><i>Preferences key:</i> <b>NETWORK_TYPE.MOBILE_MMS</b></li> * </ul> */ MOBILE_MMS(ConnectivityManager.TYPE_MOBILE_MMS, "Mobile (MMS)", "NETWORK_TYPE.MOBILE_MMS"), /** * Connection type indicating that the current connection is established trough <b>mobile</b> * network which can be used to use Secure User Plane Location servers. * <p> * From {@link android.net.ConnectivityManager#TYPE_MOBILE_SUPL} * <ul> * <li><i>Original name:</i> <b>Mobile (SUPL)</b></li> * <li><i>Preferences key:</i> <b>NETWORK_TYPE.MOBILE_SUPL</b></li> * </ul> */ MOBILE_SUPL(ConnectivityManager.TYPE_MOBILE_SUPL, "Mobile (SUPL)", "NETWORK_TYPE.MOBILE_SUPL"), /** * Connection type indicating that the current connection is established trough <b>Wi-Fi</b> * network access point. * <p> * From {@link android.net.ConnectivityManager#TYPE_WIFI} * <ul> * <li><i>Original name:</i> <b>Wi-Fi</b></li> * <li><i>Preferences key:</i> <b>NETWORK_TYPE.WIFI</b></li> * </ul> */ WIFI(ConnectivityManager.TYPE_WIFI, "Wi-Fi", "NETWORK_TYPE.WIFI"), /** * Connection type indicating that the current connection is established trough <b>WiMAX</b> * specific for data connection. * <p> * From {@link android.net.ConnectivityManager#TYPE_WIMAX} * <ul> * <li><i>Original name:</i> <b>WiMAX</b></li> * <li><i>Preferences key:</i> <b>NETWORK_TYPE.WIMAX</b></li> * </ul> */ WIMAX(ConnectivityManager.TYPE_WIMAX, "WiMAX", "NETWORK_TYPE.WIMAX"); /** * The flag provided by {@link android.net.ConnectivityManager} for this connectivity type. */ public final int systemConstant; /** * Original name of this connection type. Can be used to present this connection type by * its name in an Android application's UI. */ public final String originalName; /** * The key under which will be this connection type saved within shared preferences when * {@link #saveUserPreferredConnections(Connection.ConnectionType[], android.content.SharedPreferences)} * is called. */ public final String preferencesKey; /** * Creates a new instance of ConnectionType with the given ConnectivityManager flag, original * name and preferences key. * name. * * @param id Id of connectivity type as flag provided by {@link android.net.ConnectivityManager}. * @param origName Original name of this connectivity type. * @param prefsKey The key under which will be this connectivity type saved into shared preferences when * {@link #saveUserPreferredConnections(Connection.ConnectionType[], android.content.SharedPreferences)} * is called. */ private ConnectionType(int id, String origName, String prefsKey) { this.systemConstant = id; this.preferencesKey = prefsKey; this.originalName = origName; } /** * Resolves an instance of ConnectionType according to the given <var>typeId</var> from the * current set of ConnectionType values. * * @param systemConstant An id ({@link #systemConstant}) of the the desired connection type * to resolve. * @return Resolved connection type instance or {@link ConnectionType#UNAVAILABLE} if there * is no connection type with the requested constant. */ @NonNull public static ConnectionType resolve(int systemConstant) { for (ConnectionType type : ConnectionType.values()) { if (type.systemConstant == systemConstant) { return type; } } return UNAVAILABLE; } } /** * Methods ===================================================================================== */ /** * Checks whether this Android device has some connection or is currently in the process * to establish some connection. * <p> * See {@link android.net.NetworkInfo#isConnectedOrConnecting()} for additional info. * * @return {@code True} if some connection is established or will be established in a while, * {@code false} otherwise. * @see #isConnected() * @see #getConnectionType() */ public boolean isConnectedOrConnecting(); /** * Checks whether this Android device has some connection currently established. * <p> * See {@link android.net.NetworkInfo#isConnected()} for additional info. * * @return {@code True} if some connection is established, {@code false} otherwise. * @see #isConnectedOrConnecting() * @see #getConnectionType() */ public boolean isConnected(); /** * Checks whether this Android device has established the connection or is currently in * the process to establish the connection for the requested <var>connectionType</var>. * <p> * See {@link android.net.NetworkInfo#isConnectedOrConnecting()} for additional info. * * @param connectionType Type of the connection of which the current state should be checked. * @return {@code True} if connection is established or is in the process of being established, * {@code false} otherwise. */ public boolean isConnectedOrConnecting(@NonNull ConnectionType connectionType); /** * Checks whether this Android device has established the connection for the requested <var>connectionType</var>. * <p> * See {@link android.net.NetworkInfo#isConnected()} for additional info. * * @param connectionType Type of the connection of which the current state should be checked. * @return {@code True} if connection is established, {@code false} otherwise. * @see #isConnectedOrConnecting(Connection.ConnectionType) */ public boolean isConnected(@NonNull ConnectionType connectionType); /** * Checks whether this Android device can establish connection for the requested <var>connectionType</var> * <p> * See {@link android.net.NetworkInfo#isAvailable()} for additional info. * * @param connectionType Type of the connection of which availability should be checked. * @return {@code True} if the requested connection can be established, {@code false} * if establishing of the requested connection is not possible due to current network conditions. */ public boolean isAvailable(@NonNull ConnectionType connectionType); /** * Returns type of the current established connection. * * @return One of {@link Connection.ConnectionType} values or * {@link Connection.ConnectionType#UNAVAILABLE} if there is no connection currently available. */ @NonNull public ConnectionType getConnectionType(); /** * Returns the current info about the requested <var>connectionType</var>. * * @param connectionType Type of the connection of which current info should be obtained. * @return Info of the requested connection type. * @see #getConnectionType() */ @Nullable public NetworkInfo getConnectionInfo(@NonNull ConnectionType connectionType); /** * Registers a callback to be invoked when some connection change occur. * * @param listener Callback to register. * @see #registerConnectionReceiver(android.content.Context) */ public void registerOnConnectionListener(@NonNull OnConnectionListener listener); /** * Un-registers the given connection callback. * * @param listener Callback to un-register. */ public void unregisterOnConnectionListener(@NonNull OnConnectionListener listener); /** * Registers {@link com.wit.android.device.receiver.ConnectionStateReceiver} to receive messages * about the current connection state. * * @param context The main activity of application. * @see #registerOnConnectionListener(Connection.OnConnectionListener) * @see #unregisterConnectionReceiver(android.content.Context) */ public void registerConnectionReceiver(@NonNull Context context); /** * Un-registers registered {@link com.wit.android.device.receiver.ConnectionStateReceiver}. * * @param context Context in which was connection receiver registered before. * @see #registerConnectionReceiver(android.content.Context) */ public void unregisterConnectionReceiver(@NonNull Context context); /** * Called to dispatch message that registered {@link com.wit.android.device.receiver.ConnectionStateReceiver ConnectionStateReceiver} * was currently unregistered. This should be called immediately after successful un-registration * of connection receiver. */ public void dispatchConnectionReceiverUnregistered(); /** * Returns flag indicating whether {@link com.wit.android.device.receiver.ConnectionStateReceiver ConnectionStateReceiver} * is currently registered or not. * * @return {@code True} if receiver is registered, {@code false} otherwise. */ public boolean isConnectionReceiverRegistered(); /** * Saves the given set of user preferred connection types. Data can be latter obtained using * {@link #getUserPreferredConnections(android.content.SharedPreferences)}. * * @param types The set of connection types to save. * @param preferences The instance of shared preferences into which should be the given set of * connection types saved. */ public void saveUserPreferredConnections(@NonNull ConnectionType[] types, @NonNull SharedPreferences preferences); /** * Returns the set of user preferred connection types. * * @param preferences The instance of shared preferences within which was requested set of * connection types saved before by * {@link #saveUserPreferredConnections(Connection.ConnectionType[], android.content.SharedPreferences)}. * @return Set of connection types if the given instance of shared preferences contains these data, * {@code null} otherwise. */ @NonNull public ConnectionType[] getUserPreferredConnections(@NonNull SharedPreferences preferences); /** * Inner classes =============================================================================== */ /** * Listeners =================================================================================== */ /** * <h3>Interface Overview</h3> * Base callback to receive connection state changes. * * @author Martin Albedinsky */ public static interface OnConnectionListener { /** * Invoked whenever registered {@link com.wit.android.device.receiver.ConnectionStateReceiver ConnectionStateReceiver} * revive an Intent for the {@link ConnectivityManager#CONNECTIVITY_ACTION} and there is some * connection established. * <p> * <b>Note:</b> this is also fired while connection receiver is being registered ({@link #registerConnectionReceiver(android.content.Context)}) * and there is currently established some connection. * * @param type Type of the connection which was just now established or was already established * while this callback was being registered. * @param info Info about the current established connection. * @param context Current application context. */ public void onConnectionEstablished(@NonNull ConnectionType type, @Nullable NetworkInfo info, @NonNull Context context); /** * Invoked whenever registered {@link com.wit.android.device.receiver.ConnectionStateReceiver ConnectionStateReceiver} * receive an Intent for the {@link ConnectivityManager#CONNECTIVITY_ACTION} and there is no * connection available. * <p> * <b>Note:</b> this is also fired while connection receiver is being registered ({@link #registerConnectionReceiver(android.content.Context)}) * and there isn't currently any connection available. * * @param type Type of the connection which this Android device just lost or {@link ConnectionType#UNAVAILABLE UNAVAILABLE} * if there was no connection available while this callback was being registered. * @param context Current application context. */ public void onConnectionLost(@NonNull ConnectionType type, @NonNull Context context); } }