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. j av a 2 s .c om*/ * * 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.Intent; import android.content.SharedPreferences; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.util.Log; import com.wit.android.device.receiver.ConnectionStateReceiver; import java.util.ArrayList; import java.util.List; /** * <h3>Class Overview</h3> * Implementation of {@link com.wit.android.device.Connection} wrapper for {@link com.wit.android.device.AndroidDevice AndroidDevice}. * * @author Martin Albedinsky */ final class ConnectionImpl implements Connection { /** * Interface =================================================================================== */ /** * Constants =================================================================================== */ /** * Log TAG. */ private static final String TAG = "ConnectionImpl"; /** * Flag indicating whether the debug output trough log-cat is enabled or not. */ // private static final boolean DEBUG_ENABLED = true; /** * Flag indicating whether the output trough log-cat is enabled or not. */ private static final boolean LOG_ENABLED = DeviceConfig.LIBRARY_LOG_ENABLED; /** * The key value for the user preferred connections saved in the shared preferences. */ private static final String PREFS_USER_NETWORKS = "com.wit.android.device.ConnectionImpl.PREFERENCE.UserNetworks"; /** * Separator used preferred connections saving. */ private static final String NET_SEPARATOR = ":"; /** * Static members ============================================================================== */ /** * Members ===================================================================================== */ /** * Application context obtained from the context passed during initialization of this wrapper. */ private final Context mContext; /** * Connectivity manager which provides current connection info. */ private ConnectivityManager mConnectivityManager; /** * Default connection receiver (broadcast receiver) to receive connection changes. */ private ConnectionStateReceiver mNetworkReceiver; /** * Current connection status. */ private CurrentConnection mCurrentConnection = new CurrentConnection(ConnectionType.UNAVAILABLE, false); /** * Set of connection listeners. */ private List<OnConnectionListener> mListeners; /** * Flag indicating whether the {@link com.wit.android.device.receiver.ConnectionStateReceiver} is already registered or not. */ private boolean mReceiverRegistered; /** * Constructors ================================================================================ */ /** * Creates a new instance of ConnectionImpl wrapper with also initialized {@link ConnectivityManager}. * * @param context Application context or activity context. */ ConnectionImpl(Context context) { this.mContext = context.getApplicationContext(); this.mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); } /** * Methods ===================================================================================== */ /** * Public -------------------------------------------------------------------------------------- */ /** */ @Override public boolean isConnectedOrConnecting() { final NetworkInfo info = mConnectivityManager.getActiveNetworkInfo(); return info != null && info.isConnectedOrConnecting(); } /** */ @Override public boolean isConnected() { final NetworkInfo info = mConnectivityManager.getActiveNetworkInfo(); return info != null && info.isConnected(); } /** */ @Override public boolean isConnectedOrConnecting(@NonNull ConnectionType connectionType) { final NetworkInfo info = mConnectivityManager.getNetworkInfo(connectionType.systemConstant); return info != null && info.isConnectedOrConnecting(); } /** */ @Override public boolean isConnected(@NonNull ConnectionType connectionType) { final NetworkInfo info = mConnectivityManager.getNetworkInfo(connectionType.systemConstant); return info != null && info.isConnected(); } /** */ @Override public boolean isAvailable(@NonNull ConnectionType connectionType) { final NetworkInfo info = mConnectivityManager.getNetworkInfo(connectionType.systemConstant); return info != null && info.isAvailable(); } /** */ @NonNull @Override public ConnectionType getConnectionType() { final NetworkInfo info = mConnectivityManager.getActiveNetworkInfo(); if (info != null && info.isConnected()) { return ConnectionType.resolve(info.getType()); } return ConnectionType.UNAVAILABLE; } /** */ @Nullable @Override public NetworkInfo getConnectionInfo(@NonNull ConnectionType type) { return mConnectivityManager.getNetworkInfo(type.systemConstant); } /** */ @Override public void registerOnConnectionListener(@NonNull OnConnectionListener listener) { this.ensureListeners(); if (!mListeners.contains(listener) && listener != null) { mListeners.add(listener); } } /** */ @Override public void unregisterOnConnectionListener(@NonNull OnConnectionListener listener) { if (hasListeners()) { mListeners.remove(listener); } } /** */ @Override public void registerConnectionReceiver(@NonNull Context context) { synchronized (this) { // Check for already registered receiver. if (mReceiverRegistered) { if (LOG_ENABLED) { Log.v(TAG, "Connection receiver already registered."); } } else { context.registerReceiver( mNetworkReceiver = new ConnectionStateReceiver(), mNetworkReceiver.newIntentFilter()); this.mReceiverRegistered = true; if (LOG_ENABLED) { Log.v(TAG, "Connection receiver successfully registered."); } } } } /** */ @Override public void unregisterConnectionReceiver(@NonNull Context context) { synchronized (this) { // Check if is receiver registered. if (!mReceiverRegistered) { if (LOG_ENABLED) { Log.v(TAG, "Can not un-register not registered connection receiver."); } } else { context.unregisterReceiver(mNetworkReceiver); dispatchConnectionReceiverUnregistered(); if (LOG_ENABLED) { Log.v(TAG, "Connection receiver successfully unregistered."); } } } } /** */ @Override public void dispatchConnectionReceiverUnregistered() { this.mReceiverRegistered = false; this.mNetworkReceiver = null; } /** */ @Override public boolean isConnectionReceiverRegistered() { return mReceiverRegistered; } /** */ @Override public void saveUserPreferredConnections(@NonNull ConnectionType[] types, @NonNull SharedPreferences preferences) { final StringBuilder builder = new StringBuilder(); for (int i = 0; i < types.length; i++) { builder.append(types[i].systemConstant); if (i != (types.length - 1)) builder.append(NET_SEPARATOR); } preferences.edit().putString(PREFS_USER_NETWORKS, builder.toString()).apply(); } /** */ @NonNull @Override public ConnectionType[] getUserPreferredConnections(@NonNull SharedPreferences preferences) { ConnectionType[] types = new ConnectionType[0]; final String connections = preferences.getString(PREFS_USER_NETWORKS, null); if (connections != null) { String[] data = connections.split(NET_SEPARATOR); types = new ConnectionType[data.length]; for (int i = 0; i < data.length; i++) { types[i] = ConnectionType.resolve(Integer.parseInt(data[i])); } } return types; } /** */ @Override public void processReceivedBroadcast(@NonNull Context context, @NonNull Intent intent, int receiverId) { // Check if we have connection. if (isConnected()) { final ConnectionType type = getConnectionType(); // Save connection status. this.mCurrentConnection = new CurrentConnection(type, true); notifyConnectionEstablished(context, type, getConnectionInfo(type)); } else { final ConnectionType lostType = (mCurrentConnection == null) ? null : ConnectionType.values()[mCurrentConnection.type.ordinal()]; // Save connection status. this.mCurrentConnection = new CurrentConnection(ConnectionType.UNAVAILABLE, false); notifyConnectionLost(context, lostType); } } /** * Getters + Setters --------------------------------------------------------------------------- */ /** * Returns an application context obtained from the context passed during initialization of this * wrapper. * * @return Application context. */ public Context getApplicationContext() { return mContext; } /** * Protected ----------------------------------------------------------------------------------- */ /** * Private ------------------------------------------------------------------------------------- */ /** * Notifies all connection listeners, that new connection is available. * * @param context Current context. * @param type Type of the connection. * @param info current info of the passed connection type. */ private void notifyConnectionEstablished(Context context, ConnectionType type, NetworkInfo info) { if (hasListeners()) { for (OnConnectionListener listener : mListeners) { if (listener != null) { listener.onConnectionEstablished(type, info, context); } } } } /** * Notifies all connection listeners, that connection was lost. * * @param context Current context. * @param type Type of the connection. */ private void notifyConnectionLost(Context context, ConnectionType type) { if (hasListeners()) { for (OnConnectionListener listener : mListeners) { if (listener != null) { listener.onConnectionLost(type, context); } } } } /** * Ensures that the array with listeners is initialized. */ private void ensureListeners() { if (mListeners == null) { this.mListeners = new ArrayList<>(); } } /** * Checks whether there are some connection listener presented or not. * * @return {@code True} if this connection implementation has some listeners set, {@code false} * otherwise. */ private boolean hasListeners() { return mListeners != null && mListeners.size() > 0; } /** * Inner classes =============================================================================== */ /** * Simple wrapper for current connection data. */ private static final class CurrentConnection { /** * Members ================================================================================= */ /** * Connection type. */ ConnectionType type = ConnectionType.UNAVAILABLE; /** * Flag indicating whether the connection is established or not. */ boolean connected = false; /** * Constructors ============================================================================ */ /** * Creates a new instance of CurrentConnection with the given parameters. * * @param type Type current connection type. * @param connected {@code True} if connection is established, {@code false} otherwise. */ CurrentConnection(ConnectionType type, boolean connected) { this.type = type; this.connected = connected; } } }