123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- package de.tudarmstadt.informatik.hostage;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.preference.PreferenceManager;
- /**
- * Saves the amount of active connections and limits them to a specific number.
- *
- * @author Wulf Pfeiffer
- */
- public class ConnectionRegister {
- /** Active connections . **/
- private static int openConnections = 0;
- /** Context in which ConnectionRegister is created. **/
- private Context context;
- /**
- * Constructor sets context.
- *
- * @param context
- * Context in which ConnectionRegister is created.
- */
- public ConnectionRegister(Context context) {
- this.context = context;
- }
- /**
- * Deregisters a active connection if at least one active connection is
- * registered.
- *
- * @return true if the connection has been successfully unregistered, else
- * false.
- */
- public boolean closeConnection() {
- if (openConnections > 0) {
- openConnections--;
- return true;
- } else {
- return false;
- }
- }
- /**
- * Returns the maximum number of active connections.
- *
- * @return maximum number of active connections.
- */
- public int getMaxConnections() {
- SharedPreferences defaultPref = PreferenceManager
- .getDefaultSharedPreferences(context);
- return defaultPref.getInt("max_connections", 5);
- }
- /**
- * Returns the number of active connections.
- *
- * @return number of active connections.
- */
- public int getOpenConnections() {
- return openConnections;
- }
- /**
- * Returns if there are new connections allowed or not.
- *
- * @return true if a new connection is allowed, else false.
- */
- public boolean isConnectionFree() {
- return getMaxConnections() == 0
- || openConnections < getMaxConnections();
- }
- /**
- * Registers a new active connection if there are connections allowed.
- *
- * @return true if a new connection has been successfully registered, else
- * false.
- */
- public boolean newOpenConnection() {
- if (isConnectionFree()) {
- openConnections++;
- return true;
- } else {
- return false;
- }
- }
- }
|