Home | History | Annotate | Download | only in musicbrowserdemo
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.musicbrowserdemo;
     18 
     19 import android.content.Context;
     20 import android.content.ComponentName;
     21 import android.content.Intent;
     22 import android.content.pm.PackageManager;
     23 import android.content.pm.ResolveInfo;
     24 import android.media.browse.MediaBrowser;
     25 import android.service.media.MediaBrowserService;
     26 import android.os.Bundle;
     27 import android.net.Uri;
     28 import android.support.v4.app.FragmentActivity;
     29 import android.support.v4.app.FragmentTransaction;
     30 import android.support.v4.app.ListFragment;
     31 import android.util.Log;
     32 import android.view.LayoutInflater;
     33 import android.view.View;
     34 import android.view.ViewGroup;
     35 import android.widget.BaseAdapter;
     36 import android.widget.ListView;
     37 import android.widget.TextView;
     38 
     39 import java.util.ArrayList;
     40 import java.util.List;
     41 
     42 public class BrowserListFragment extends ListFragment {
     43     private static final String TAG = "BrowserListFragment";
     44 
     45     // Hints
     46     public static final String HINT_DISPLAY = "com.example.android.musicbrowserdemo.DISPLAY";
     47 
     48     // For args
     49     public static final String ARG_COMPONENT = "component";
     50     public static final String ARG_ID = "uri";
     51 
     52     private Adapter mAdapter;
     53     private List<Item> mItems = new ArrayList();
     54     private ComponentName mComponent;
     55     private String mNodeId;
     56     private MediaBrowser mBrowser;
     57 
     58     private static class Item {
     59         final MediaBrowser.MediaItem media;
     60 
     61         Item(MediaBrowser.MediaItem m) {
     62             this.media = m;
     63         }
     64     }
     65 
     66     public BrowserListFragment() {
     67     }
     68 
     69     @Override
     70     public void onActivityCreated(Bundle savedInstanceState) {
     71         super.onActivityCreated(savedInstanceState);
     72         Log.d(TAG, "onActivityCreated -- " + hashCode());
     73         mAdapter = new Adapter();
     74         setListAdapter(mAdapter);
     75 
     76         // Get our arguments
     77         final Bundle args = getArguments();
     78         mComponent = args.getParcelable(ARG_COMPONENT);
     79         mNodeId = args.getString(ARG_ID);
     80 
     81         // A hint about who we are, so the service can customize the results if it wants to.
     82         final Bundle rootHints = new Bundle();
     83         rootHints.putBoolean(HINT_DISPLAY, true);
     84 
     85         mBrowser = new MediaBrowser(getActivity(), mComponent, mConnectionCallbacks, rootHints);
     86     }
     87 
     88     @Override
     89     public void onStart() {
     90         super.onStart();
     91         mBrowser.connect();
     92     }
     93 
     94     @Override
     95     public void onStop() {
     96         super.onStop();
     97         mBrowser.disconnect();
     98     }
     99 
    100     @Override
    101     public void onListItemClick(ListView l, View v, int position, long id) {
    102         final Item item = mItems.get(position);
    103 
    104         Log.i("BrowserListFragment", "Item clicked: " + position + " -- "
    105                 + mAdapter.getItem(position).media.getDescription().getIconUri());
    106 
    107         final BrowserListFragment fragment = new BrowserListFragment();
    108 
    109         final Bundle args = new Bundle();
    110         args.putParcelable(BrowserListFragment.ARG_COMPONENT, mComponent);
    111         args.putParcelable(BrowserListFragment.ARG_ID, item.media.getDescription().getIconUri());
    112         fragment.setArguments(args);
    113 
    114         getFragmentManager().beginTransaction()
    115                 .replace(android.R.id.content, fragment)
    116                 .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
    117                 .addToBackStack(null)
    118                 .commit();
    119 
    120     }
    121 
    122     final MediaBrowser.ConnectionCallback mConnectionCallbacks
    123             = new MediaBrowser.ConnectionCallback() {
    124         @Override
    125         public void onConnected() {
    126             Log.d(TAG, "mConnectionCallbacks.onConnected");
    127             if (mNodeId == null) {
    128                 mNodeId = mBrowser.getRoot();
    129             }
    130             mBrowser.subscribe(mNodeId, new MediaBrowser.SubscriptionCallback() {
    131                     @Override
    132                 public void onChildrenLoaded(String parentId,
    133                             List<MediaBrowser.MediaItem> children) {
    134                     Log.d(TAG, "onChildrenLoaded parentId=" + parentId
    135                                 + " children= " + children);
    136                         mItems.clear();
    137                         final int N = children.size();
    138                         for (int i=0; i<N; i++) {
    139                             mItems.add(new Item(children.get(i)));
    140                         }
    141                         mAdapter.notifyDataSetChanged();
    142                     }
    143 
    144                     @Override
    145                 public void onError(String parentId) {
    146                     Log.d(TAG, "onError parentId=" + parentId);
    147                     }
    148                 });
    149         }
    150 
    151         @Override
    152         public void onConnectionSuspended() {
    153             Log.d(TAG, "mConnectionCallbacks.onConnectionSuspended");
    154         }
    155 
    156         @Override
    157         public void onConnectionFailed() {
    158             Log.d(TAG, "mConnectionCallbacks.onConnectionFailed");
    159         }
    160     };
    161 
    162     private class Adapter extends BaseAdapter {
    163         private final LayoutInflater mInflater;
    164 
    165         Adapter() {
    166             super();
    167 
    168             final Context context = getActivity();
    169             mInflater = LayoutInflater.from(context);
    170         }
    171 
    172         @Override
    173         public int getCount() {
    174             return mItems.size();
    175         }
    176 
    177         @Override
    178         public Item getItem(int position) {
    179             return mItems.get(position);
    180         }
    181 
    182         @Override
    183         public long getItemId(int position) {
    184             return position;
    185         }
    186 
    187         @Override
    188         public int getItemViewType(int position) {
    189             return 1;
    190         }
    191 
    192         @Override
    193         public View getView(int position, View convertView, ViewGroup parent) {
    194             if (convertView == null) {
    195                 convertView = mInflater.inflate(android.R.layout.simple_list_item_1, parent, false);
    196             }
    197 
    198             final TextView tv = (TextView)convertView;
    199             final Item item = mItems.get(position);
    200             tv.setText(item.media.getDescription().getTitle());
    201 
    202             return convertView;
    203         }
    204 
    205         @Override
    206         public int getViewTypeCount() {
    207             return 1;
    208         }
    209     }
    210 }
    211 
    212 
    213