Android Open Source - mha-android Source Fragment






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.ui.fragment;
//  w  w w.j av a2  s.c o m
import java.util.ArrayList;

import com.teamacra.myhomeaudio.R;
import com.teamacra.myhomeaudio.media.MediaDescriptor;
import com.teamacra.myhomeaudio.source.Source;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class SourceFragment extends Fragment {

  private ArrayAdapter<Source> mListAdapter;
  private ListView mSourceListView;
  private ArrayList<Source> mSourceList;
  private OnSourceSelectedListener mListener;

  private boolean hasInitialSelect;

  public static SourceFragment newInstance() {
    SourceFragment fragment = new SourceFragment();
    return fragment;
  }
  
  public void onCreate(Bundle savedInstanceState) {
    Log.i("SOURCEFRAG", "Creating...");
    super.onCreate(savedInstanceState);
    
    mSourceList = new ArrayList<Source>();
    // mSourceList.add(new MediaDescriptor(0, "I like music", "", "", ""));
    mListAdapter = new ArrayAdapter<Source>(this.getActivity(),
        android.R.layout.simple_list_item_single_choice, mSourceList);
  }
  
  public void onResume() {
    super.onResume();
    Log.i("SOURCEFRAG", "Resuming...");
  }
  

  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    Log.i("SOURCEFRAG", "Creating View...");
    hasInitialSelect = false;
    View view = inflater.inflate(R.layout.song_fragment, container, false);
    
    mSourceListView = (ListView) view.findViewById(R.id.songListView);
    mSourceListView.setAdapter(mListAdapter);
    mSourceListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    mSourceListView.setOnItemClickListener(new OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {
        Toast.makeText(getActivity(), mSourceList.get(position).name(),
            Toast.LENGTH_SHORT).show();
        mListener.onSourceSelected(mSourceList.get(position));

        selectSource(position);
      }
    });
    
    return view;
  }

  public void updateSourceList(ArrayList<Source> newSources) {
    Log.i("SOURCEFRAG", "Updating sources...");
    if (mSourceList == null) {
      mSourceList = new ArrayList<Source>();
    }
    if (newSources != null) {
      
      mSourceList.clear();
      mSourceList.addAll(newSources);
      mListAdapter.notifyDataSetChanged();

      /*
       * Check if any source has been set yet. Used so that when the first
       * source update happens, we choose a default source to list songs
       * for.
       */
      if (!hasInitialSelect && mSourceList.size() > 0) {
        selectSource(0);
      }
    }
  }

  /**
   * Performs selection of a source by calling the listener and indicating
   * that the source is actually selected in the list view.
   * 
   * @param position
   *            The position in the source list to set as selected.
   */
  private void selectSource(int position) {
    mListener.onSourceSelected(mSourceList.get(position));
    mSourceListView.setSelection(position);
    View selectedView = mSourceListView.getSelectedView();
    if (selectedView != null) {
      selectedView.setSelected(true);
    }
  }

  @Override
  public void onAttach(Activity activity) {
    Log.i("SOURCEFRAG", "Attaching...");
    super.onAttach(activity);
    try {
      mListener = (OnSourceSelectedListener) activity;
    } catch (ClassCastException e) {
      throw new ClassCastException(activity.toString()
          + " must implement OnSourceSelectedListener");
    }
    
  }
  
  public void onPause() {
    super.onPause();
    
    Log.i("SOURCEFRAG", "Pausing...");
  }
  
  public void onStop() {
    Log.i("SOURCEFRAG", "Stopping...");
    super.onStop();
  }
  
  public void onDestroy() {
    Log.i("SOURCEFRAG", "Destroying...");
    super.onDestroy();
  }
  
  public interface OnSourceSelectedListener {
    public void onSourceSelected(Source source);
  }
}




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