Android Open Source - umpire_buddy Event Manager






From Project

Back to project page umpire_buddy.

License

The source code is released under:

Apache License

If you think the Android project umpire_buddy listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.bluefairyapps.java.android.umpirebuddy.systems;
//  www  .java2s  . c  o  m
import java.util.ArrayList;
import java.util.HashMap;

import android.util.Log;

import com.bluefairyapps.java.android.umpirebuddy.enums.EventsEnum;
import com.bluefairyapps.java.android.umpirebuddy.event.Event;
import com.bluefairyapps.java.android.umpirebuddy.event.EventListener;

public class EventManager {
  private static final String TAG = EventManager.class.getSimpleName();
  
  private static EventManager mInstance = null;
  
  private ArrayList<EventsEnum> mEventList = new ArrayList<EventsEnum>();
  
  private ArrayList<EventListener> mListeners = new ArrayList<EventListener>();
  private HashMap<EventListener, ArrayList<EventsEnum>> mListenerEventMap = 
      new HashMap<EventListener, ArrayList<EventsEnum>>();
  
  public EventManager() {
    
    for(EventsEnum event : EventsEnum.values()) {
      
      mEventList.add(event);
    }
  }
  
  public static EventManager get() {
    
    if(mInstance == null) {
      
      Log.d(TAG, "Creating static instance.");
      
      mInstance = new EventManager();
    }
    
    return mInstance;
  }
  
  public void registerForEvent(EventListener listener, EventsEnum eventType) {
    
    Log.d(TAG, "Registering " + listener.getClass().getSimpleName() + " for "
        + eventType.name());

    ArrayList<EventsEnum> listenerEventList;
    
    if(mListeners.contains(listener)) {
      
      listenerEventList = mListenerEventMap.get(listener);
    } else {
      
      mListeners.add(listener);
      listenerEventList = new ArrayList<EventsEnum>();
    }
    
    listenerEventList.add(eventType);
    mListenerEventMap.put(listener, listenerEventList);
  }
  
  public void registerForAllEvents(EventListener listener) {
    
    Log.d(TAG, "Registering " + listener.getClass().getSimpleName() + " for all events.");
    
    mListenerEventMap.put(listener, mEventList);
  }
  
  public void unregister(EventListener listener) {
    
    Log.d(TAG, "Unregistering " + listener.getClass().getSimpleName() + " from all events.");
    
    mListenerEventMap.remove(listener);
    mListeners.remove(listener);
  }
  
  public void unregisterFromEvent(EventListener listener, EventsEnum eventType) {

    Log.d(TAG, "Unregistering " + listener.getClass().getSimpleName() + " from "
        + eventType.name());

    ArrayList<EventsEnum> listenerEventList =
        mListenerEventMap.get(listener);
    
    listenerEventList.remove(eventType);
    mListenerEventMap.put(listener, listenerEventList);
  }
  
  public void dispatch(Event event) {
    
    Log.d(TAG, "Checking listeners for those interested in " + event.getType().name());
    
    for(EventListener listener : mListeners) {
      
      ArrayList<EventsEnum> listenerEventList =
          mListenerEventMap.get(listener);
      
      if(listenerEventList.contains(event.getType())) {
        
        Log.d(TAG, "Dispatching " + event.getType().name() + " to " 
            + listener.getClass().getSimpleName());
        
        listener.onEvent(event);
      }
    }
  }
  
}




Java Source Code List

com.bluefairyapps.java.android.umpirebuddy.AboutActivity.java
com.bluefairyapps.java.android.umpirebuddy.MainActivity.java
com.bluefairyapps.java.android.umpirebuddy.enums.EventsEnum.java
com.bluefairyapps.java.android.umpirebuddy.enums.GameStatsEnum.java
com.bluefairyapps.java.android.umpirebuddy.event.EventListener.java
com.bluefairyapps.java.android.umpirebuddy.event.Event.java
com.bluefairyapps.java.android.umpirebuddy.event.game.GameStatUpdateEvent.java
com.bluefairyapps.java.android.umpirebuddy.systems.AudioSystem.java
com.bluefairyapps.java.android.umpirebuddy.systems.BaseballGameSystem.java
com.bluefairyapps.java.android.umpirebuddy.systems.EventManager.java
com.bluefairyapps.java.android.umpirebuddy.utils.BaseballRules.java
com.bluefairyapps.java.android.umpirebuddy.utils.GameStats.java