Android Open Source - PlayTunes Merge Adapter






From Project

Back to project page PlayTunes.

License

The source code is released under:

Copyright (c) 2014, Impeccable Labs, LLC. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions ar...

If you think the Android project PlayTunes 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

/***
  Copyright (c) 2008-2009 CommonsWare, LLC
  Portions (c) 2009 Google, Inc./*from w w w .  j a va 2 s.  c  o  m*/
  
  Licensed under the Apache License, Version 2.0 (the "License"); you may
  not use this file except in compliance with the License. You may obtain
  a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
 */

package com.ideabag.playtunes.util;

import android.database.DataSetObserver;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.SectionIndexer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Adapter that merges multiple child adapters and views
 * into a single contiguous whole.
 * 
 * Adapters used as pieces within MergeAdapter must have
 * view type IDs monotonically increasing from 0. Ideally,
 * adapters also have distinct ranges for their row ids, as
 * returned by getItemId().
 * 
 */
public class MergeAdapter extends BaseAdapter implements SectionIndexer {
  protected PieceStateRoster pieces=new PieceStateRoster();

  /**
   * Stock constructor, simply chaining to the superclass.
   */
  public MergeAdapter() {
    super();
  }

  /**
   * Adds a new adapter to the roster of things to appear in
   * the aggregate list.
   * 
   * @param adapter
   *          Source for row views for this section
   */
  public void addAdapter(ListAdapter adapter) {
    pieces.add(adapter);
    adapter.registerDataSetObserver(new CascadeDataSetObserver());
  }

  /**
   * Adds a new View to the roster of things to appear in
   * the aggregate list.
   * 
   * @param view
   *          Single view to add
   */
  public void addView(View view) {
    addView(view, false);
  }

  /**
   * Adds a new View to the roster of things to appear in
   * the aggregate list.
   * 
   * @param view
   *          Single view to add
   * @param enabled
   *          false if views are disabled, true if enabled
   */
  public void addView(View view, boolean enabled) {
    ArrayList<View> list=new ArrayList<View>(1);

    list.add(view);

    addViews(list, enabled);
  }

  /**
   * Adds a list of views to the roster of things to appear
   * in the aggregate list.
   * 
   * @param views
   *          List of views to add
   */
  public void addViews(List<View> views) {
    addViews(views, false);
  }

  /**
   * Adds a list of views to the roster of things to appear
   * in the aggregate list.
   * 
   * @param views
   *          List of views to add
   * @param enabled
   *          false if views are disabled, true if enabled
   */
  public void addViews(List<View> views, boolean enabled) {
    if (enabled) {
      addAdapter(new EnabledSackAdapter(views));
    }
    else {
      addAdapter(new SackOfViewsAdapter(views));
    }
  }

  /**
   * Get the data item associated with the specified
   * position in the data set.
   * 
   * @param position
   *          Position of the item whose data we want
   */
  @Override
  public Object getItem(int position) {
    for (ListAdapter piece : getPieces()) {
      int size=piece.getCount();

      if (position < size) {
        return(piece.getItem(position));
      }

      position-=size;
    }

    return(null);
  }

  /**
   * Get the adapter associated with the specified position
   * in the data set.
   * 
   * @param position
   *          Position of the item whose adapter we want
   */
  public ListAdapter getAdapter(int position) {
    for (ListAdapter piece : getPieces()) {
      int size=piece.getCount();

      if (position < size) {
        return(piece);
      }

      position-=size;
    }

    return(null);
  }

  /**
   * How many items are in the data set represented by this
   * Adapter.
   */
  @Override
  public int getCount() {
    int total=0;

    for (ListAdapter piece : getPieces()) {
      total+=piece.getCount();
    }

    return(total);
  }

  /**
   * Returns the number of types of Views that will be
   * created by getView().
   */
  @Override
  public int getViewTypeCount() {
    int total=0;

    for (PieceState piece : pieces.getRawPieces()) {
      total+=piece.adapter.getViewTypeCount();
    }

    return(Math.max(total, 1)); // needed for
                                // setListAdapter() before
                                // content add'
  }

  /**
   * Get the type of View that will be created by getView()
   * for the specified item.
   * 
   * @param position
   *          Position of the item whose data we want
   */
  @Override
  public int getItemViewType(int position) {
    int typeOffset=0;
    int result=-1;

    for (PieceState piece : pieces.getRawPieces()) {
      if (piece.isActive) {
        int size=piece.adapter.getCount();
  
        if (position < size) {
          result=typeOffset + piece.adapter.getItemViewType(position);
          break;
        }
  
        position-=size;
      }
      
      typeOffset+=piece.adapter.getViewTypeCount();
    }

    return(result);
  }

  /**
   * Are all items in this ListAdapter enabled? If yes it
   * means all items are selectable and clickable.
   */
  @Override
  public boolean areAllItemsEnabled() {
    return(false);
  }

  /**
   * Returns true if the item at the specified position is
   * not a separator.
   * 
   * @param position
   *          Position of the item whose data we want
   */
  @Override
  public boolean isEnabled(int position) {
    for (ListAdapter piece : getPieces()) {
      int size=piece.getCount();

      if (position < size) {
        return(piece.isEnabled(position));
      }

      position-=size;
    }

    return(false);
  }

  /**
   * Get a View that displays the data at the specified
   * position in the data set.
   * 
   * @param position
   *          Position of the item whose data we want
   * @param convertView
   *          View to recycle, if not null
   * @param parent
   *          ViewGroup containing the returned View
   */
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    for (ListAdapter piece : getPieces()) {
      int size=piece.getCount();

      if (position < size) {

        return(piece.getView(position, convertView, parent));
      }

      position-=size;
    }

    return(null);
  }

  /**
   * Get the row id associated with the specified position
   * in the list.
   * 
   * @param position
   *          Position of the item whose data we want
   */
  @Override
  public long getItemId(int position) {
    for (ListAdapter piece : getPieces()) {
      int size=piece.getCount();

      if (position < size) {
        return(piece.getItemId(position));
      }

      position-=size;
    }

    return(-1);
  }

  @Override
  public int getPositionForSection(int section) {
    int position=0;

    for (ListAdapter piece : getPieces()) {
      if (piece instanceof SectionIndexer) {
        Object[] sections=((SectionIndexer)piece).getSections();
        int numSections=0;

        if (sections != null) {
          numSections=sections.length;
        }

        if (section < numSections) {
          return(position + ((SectionIndexer)piece).getPositionForSection(section));
        }
        else if (sections != null) {
          section-=numSections;
        }
      }

      position+=piece.getCount();
    }

    return(0);
  }

  @Override
  public int getSectionForPosition(int position) {
    int section=0;

    for (ListAdapter piece : getPieces()) {
      int size=piece.getCount();

      if (position < size) {
        if (piece instanceof SectionIndexer) {
          return(section + ((SectionIndexer)piece).getSectionForPosition(position));
        }

        return(0);
      }
      else {
        if (piece instanceof SectionIndexer) {
          Object[] sections=((SectionIndexer)piece).getSections();

          if (sections != null) {
            section+=sections.length;
          }
        }
      }

      position-=size;
    }

    return(0);
  }

  @Override
  public Object[] getSections() {
    ArrayList<Object> sections=new ArrayList<Object>();

    for (ListAdapter piece : getPieces()) {
      if (piece instanceof SectionIndexer) {
        Object[] curSections=((SectionIndexer)piece).getSections();

        if (curSections != null) {
          Collections.addAll(sections, curSections);
        }
      }
    }

    if (sections.size() == 0) {
      return(new String[0]);
    }

    return(sections.toArray(new Object[0]));
  }
  
  public void setActive(ListAdapter adapter, boolean isActive) {
    pieces.setActive(adapter, isActive);
    notifyDataSetChanged();
  }

  public void setActive(View v, boolean isActive) {
    pieces.setActive(v, isActive);
    notifyDataSetChanged();
  }

  protected List<ListAdapter> getPieces() {
    return(pieces.getPieces());
  }

  private static class PieceState {
    ListAdapter adapter;
    boolean isActive=true;

    PieceState(ListAdapter adapter, boolean isActive) {
      this.adapter=adapter;
      this.isActive=isActive;
    }
  }

  private static class PieceStateRoster {
    protected ArrayList<PieceState> pieces=new ArrayList<PieceState>();
    protected ArrayList<ListAdapter> active=null;

    void add(ListAdapter adapter) {
      pieces.add(new PieceState(adapter, true));
    }
    
    void setActive(ListAdapter adapter, boolean isActive) {
      for (PieceState state : pieces) {
        if (state.adapter==adapter) {
          state.isActive=isActive;
          active=null;
          break;
        }
      }
    }
    
    void setActive(View v, boolean isActive) {
      for (PieceState state : pieces) {
        if (state.adapter instanceof SackOfViewsAdapter &&
            ((SackOfViewsAdapter)state.adapter).hasView(v)) {
          state.isActive=isActive;
          active=null;
          break;
        }
      }
    }
    
    List<PieceState> getRawPieces() {
      return(pieces);
    }

    List<ListAdapter> getPieces() {
      if (active == null) {
        active=new ArrayList<ListAdapter>();

        for (PieceState state : pieces) {
          if (state.isActive) {
            active.add(state.adapter);
          }
        }
      }

      return(active);
    }
  }

  private static class EnabledSackAdapter extends SackOfViewsAdapter {
    public EnabledSackAdapter(List<View> views) {
      super(views);
    }

    @Override
    public boolean areAllItemsEnabled() {
      return(true);
    }

    @Override
    public boolean isEnabled(int position) {
      return(true);
    }
  }

  private class CascadeDataSetObserver extends DataSetObserver {
    @Override
    public void onChanged() {
      notifyDataSetChanged();
    }

    @Override
    public void onInvalidated() {
      notifyDataSetInvalidated();
    }
  }
}




Java Source Code List

com.ideabag.playtunes.PlaylistManager.java
com.ideabag.playtunes.DragNDrop.DragListener.java
com.ideabag.playtunes.DragNDrop.DragNDropListView.java
com.ideabag.playtunes.DragNDrop.DropListener.java
com.ideabag.playtunes.DragNDrop.RemoveListener.java
com.ideabag.playtunes.activity.MainActivity.java
com.ideabag.playtunes.activity.NowPlayingActivity.java
com.ideabag.playtunes.activity.SettingsActivity.java
com.ideabag.playtunes.adapter.AlbumListAdapter.java
com.ideabag.playtunes.adapter.AlbumsAllAdapter.java
com.ideabag.playtunes.adapter.AlbumsOneAdapter.java
com.ideabag.playtunes.adapter.ArtistAlbumsAdapter.java
com.ideabag.playtunes.adapter.ArtistAllSongsAdapter.java
com.ideabag.playtunes.adapter.ArtistListAdapter.java
com.ideabag.playtunes.adapter.ArtistSinglesAdapter.java
com.ideabag.playtunes.adapter.ArtistsAllAdapter.java
com.ideabag.playtunes.adapter.AsyncQueryAdapter.java
com.ideabag.playtunes.adapter.GenresAllAdapter.java
com.ideabag.playtunes.adapter.GenresOneAdapter.java
com.ideabag.playtunes.adapter.NavigationListAdapter.java
com.ideabag.playtunes.adapter.PlaylistsAllAdapter.java
com.ideabag.playtunes.adapter.PlaylistsOneAdapter.java
com.ideabag.playtunes.adapter.SongListAdapter.java
com.ideabag.playtunes.adapter.SongsAllAdapter.java
com.ideabag.playtunes.adapter.search.SearchAlbumsAdapter.java
com.ideabag.playtunes.adapter.search.SearchArtistsAdapter.java
com.ideabag.playtunes.adapter.search.SearchSongsAdapter.java
com.ideabag.playtunes.database.MediaQuery.java
com.ideabag.playtunes.dialog.AddToPlaylistDialogFragment.java
com.ideabag.playtunes.dialog.CreatePlaylistDialogFragment.java
com.ideabag.playtunes.dialog.FeedbackDialogFragment.java
com.ideabag.playtunes.dialog.PlaylistDeleteDialogFragment.java
com.ideabag.playtunes.dialog.PlaylistMenuDialogFragment.java
com.ideabag.playtunes.dialog.PlaylistRenameDialogFragment.java
com.ideabag.playtunes.dialog.RateAppDialogFragment.java
com.ideabag.playtunes.dialog.SongMenuDialogFragment.java
com.ideabag.playtunes.fragment.AlbumsAllFragment.java
com.ideabag.playtunes.fragment.AlbumsOneFragment.java
com.ideabag.playtunes.fragment.ArtistSinglesFragment.java
com.ideabag.playtunes.fragment.ArtistsAllFragment.java
com.ideabag.playtunes.fragment.ArtistsOneFragment.java
com.ideabag.playtunes.fragment.FooterControlsFragment.java
com.ideabag.playtunes.fragment.GenresAllFragment.java
com.ideabag.playtunes.fragment.GenresOneFragment.java
com.ideabag.playtunes.fragment.MusicBrowserFragment.java
com.ideabag.playtunes.fragment.NavigationDrawerFragment.java
com.ideabag.playtunes.fragment.NavigationFragment.java
com.ideabag.playtunes.fragment.PlaylistsAllFragment.java
com.ideabag.playtunes.fragment.PlaylistsOneFragment.java
com.ideabag.playtunes.fragment.SaveScrollListFragment.java
com.ideabag.playtunes.fragment.SongsFragment.java
com.ideabag.playtunes.fragment.TrackProgressFragment.java
com.ideabag.playtunes.fragment.search.SearchAlbumsFragment.java
com.ideabag.playtunes.fragment.search.SearchAllFragment.java
com.ideabag.playtunes.fragment.search.SearchArtistsFragment.java
com.ideabag.playtunes.fragment.search.SearchFragment.java
com.ideabag.playtunes.fragment.search.SearchSongsFragment.java
com.ideabag.playtunes.fragment.search.SearchSuggestionsFragment.java
com.ideabag.playtunes.media.AudioFocusHelper.java
com.ideabag.playtunes.media.MusicFocusable.java
com.ideabag.playtunes.media.MusicIntentReceiver.java
com.ideabag.playtunes.media.PlaylistMediaPlayer.java
com.ideabag.playtunes.media.RemoteControlClientCompat.java
com.ideabag.playtunes.media.RemoteControlHelper.java
com.ideabag.playtunes.service.LockscreenManager.java
com.ideabag.playtunes.service.MusicPlayerService.java
com.ideabag.playtunes.service.PlaybackNotification.java
com.ideabag.playtunes.util.AdmobUtil.java
com.ideabag.playtunes.util.AlbumSongsCountTask.java
com.ideabag.playtunes.util.AsyncDrawable.java
com.ideabag.playtunes.util.BitmapWorkerTask.java
com.ideabag.playtunes.util.CheckRemoteVersionFileTask.java
com.ideabag.playtunes.util.DrawerLayout.java
com.ideabag.playtunes.util.GAEvent.java
com.ideabag.playtunes.util.IMusicBrowser.java
com.ideabag.playtunes.util.ISearchableAdapter.java
com.ideabag.playtunes.util.LoadAlbumStackTask.java
com.ideabag.playtunes.util.MergeAdapter.java
com.ideabag.playtunes.util.QueryCountTask.java
com.ideabag.playtunes.util.SackOfViewsAdapter.java
com.ideabag.playtunes.util.SearchHistory.java
com.ideabag.playtunes.util.StarToggleTask.java
com.ideabag.playtunes.util.TrackerSingleton.java