Android Open Source - mha-android Stream Manager






From Project

Back to project page mha-android.

License

The source code is released under:

Copyright (c) 2011-2012 Cameron Porter, Ryan Brown http://github.com/camporter/mha-android Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated...

If you think the Android project mha-android 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.teamacra.myhomeaudio.manager;
//from w  ww .ja va 2  s .c  o  m
import java.util.ArrayList;
import java.util.Iterator;

import com.teamacra.myhomeaudio.MHAApplication;
import com.teamacra.myhomeaudio.http.HttpSource;
import com.teamacra.myhomeaudio.http.HttpStream;
import com.teamacra.myhomeaudio.media.MediaDescriptor;
import com.teamacra.myhomeaudio.node.Node;
import com.teamacra.myhomeaudio.source.Source;
import com.teamacra.myhomeaudio.stream.Stream;

public class StreamManager {
  private ArrayList<Stream> streamList;
  private ArrayList<Source> sourceList;
  private HttpStream httpStream;
  private HttpSource httpSource;
  
  private static StreamManager instance;

  private StreamManager(MHAApplication app) {
    httpStream = new HttpStream(app);
    httpSource = new HttpSource(app);
    streamList = new ArrayList<Stream>();
    sourceList = new ArrayList<Source>();
  }
  
  public synchronized static StreamManager getInstance(MHAApplication app) {
    if (instance == null) {
      instance = new StreamManager(app);
    }
    
    return instance;
  }
  
  /**
   * Updates the list of stream objects.
   * 
   * @return Whether the stream list update succeeded.
   */
  public synchronized boolean updateStreams() {
    
    ArrayList<Stream> newStreamList = httpStream.getStreamList();
    
    if (newStreamList != null) {
      streamList.clear();
      streamList.addAll(newStreamList);
      return true;
    }
    return false;
  }
  
  public synchronized boolean updateSources() {
    ArrayList<Source> newSourceList = httpSource.getSourceList();
    
    if (newSourceList != null) {
      sourceList.clear();
      sourceList.addAll(newSourceList);
      return true;
    }
    return false;
  }
  
  public synchronized Source updateSourceMedia(int sourceId) {
    ArrayList<MediaDescriptor> newMediaList = httpSource.getSourceMedia(sourceId);
    Source source = getSource(sourceId);
    
    if (newMediaList != null && source != null) {
      source.setMediaList(newMediaList);
      return source;
    }
    return null;
  }

  /**
   * Gets a list of Stream objects.
   * <p>
   * Note, this doesn't automatically update from the server. Make sure to
   * call updateStreams() at your discretion.
   * 
   * @return
   */
  public ArrayList<Stream> getStreamList() {
    return new ArrayList<Stream>(streamList);
  }
  
  public ArrayList<Source> getSourceList() {
    return new ArrayList<Source>(sourceList);
  }
  
  

  /**
   * Get a Stream by its name.
   * 
   * @param name
   *            The name of the Stream.
   * @return The Stream object, or null if a matching stream object wasn't
   *         found.
   */
  public Stream getStream(String name) {
    for (Iterator<Stream> i = streamList.iterator(); i.hasNext();) {
      Stream nextStream = i.next();
      if (nextStream.name().equals(name)) {
        return new Stream(nextStream);
      }
    }
    return null;
  }

  /**
   * Get a Stream by its id.
   * 
   * @param id
   *            The id of the Stream.
   * @return The Stream object, or null if a matching stream object wasn't
   *         found.
   */
  public Stream getStream(int id) {
    for (Iterator<Stream> i = streamList.iterator(); i.hasNext();) {
      Stream nextStream = i.next();
      if (nextStream.id() == id) {
        return nextStream;
      }
    }
    return null;
  }
  
  public Source getSource(int id) {
    for (Iterator<Source> i = sourceList.iterator(); i.hasNext();) {
      Source nextSource = i.next();
      if (nextSource.id() == id) {
        return nextSource;
      }
    }
    return null;
  }
  
  public boolean addStream(String name) {
    if (httpStream.addStream(name) && updateStreams()) {
      return true;
    }
    return false;
  }
  
  /**
   * Assigns new nodes to the stream on the server.
   * @param streamId
   * @param nodes
   * @return
   */
  public boolean assignNodes(int streamId, ArrayList<Node> nodes) {
    Stream stream = getStream(streamId);
    
    if (stream != null && httpStream.assignNodes(stream, nodes)) {
      return true;
    }
    return false;
  }
}




Java Source Code List

com.teamacra.myhomeaudio.MHAApplication.java
com.teamacra.myhomeaudio.bluetooth.BluetoothService.java
com.teamacra.myhomeaudio.discovery.DiscoveryConstants.java
com.teamacra.myhomeaudio.discovery.DiscoveryDescription.java
com.teamacra.myhomeaudio.discovery.DiscoverySearchListener.java
com.teamacra.myhomeaudio.discovery.DiscoverySearch.java
com.teamacra.myhomeaudio.discovery.MDNSDiscovery.java
com.teamacra.myhomeaudio.http.HttpBase.java
com.teamacra.myhomeaudio.http.HttpClient.java
com.teamacra.myhomeaudio.http.HttpNode.java
com.teamacra.myhomeaudio.http.HttpSource.java
com.teamacra.myhomeaudio.http.HttpStream.java
com.teamacra.myhomeaudio.http.StatusCode.java
com.teamacra.myhomeaudio.locations.NodeSignalRange.java
com.teamacra.myhomeaudio.locations.NodeSignature.java
com.teamacra.myhomeaudio.manager.ConfigurationManager.java
com.teamacra.myhomeaudio.manager.LocationManager.java
com.teamacra.myhomeaudio.manager.NodeManager.java
com.teamacra.myhomeaudio.manager.StreamManager.java
com.teamacra.myhomeaudio.media.MediaDescriptor.java
com.teamacra.myhomeaudio.node.Node.java
com.teamacra.myhomeaudio.source.Source.java
com.teamacra.myhomeaudio.stream.StreamAction.java
com.teamacra.myhomeaudio.stream.Stream.java
com.teamacra.myhomeaudio.ui.InitialConfigActivity.java
com.teamacra.myhomeaudio.ui.LoginActivity.java
com.teamacra.myhomeaudio.ui.MyHomeAudioActivity.java
com.teamacra.myhomeaudio.ui.RegisterActivity.java
com.teamacra.myhomeaudio.ui.fragment.SongFragment.java
com.teamacra.myhomeaudio.ui.fragment.SourceFragment.java
com.teamacra.myhomeaudio.ui.fragment.TestFragment.java