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.service.media.MediaBrowserService;
     25 import android.os.Bundle;
     26 import android.support.v4.app.FragmentActivity;
     27 import android.support.v4.app.FragmentTransaction;
     28 import android.support.v4.app.ListFragment;
     29 import android.util.Log;
     30 import android.view.LayoutInflater;
     31 import android.view.View;
     32 import android.view.ViewGroup;
     33 import android.widget.BaseAdapter;
     34 import android.widget.ListView;
     35 import android.widget.TextView;
     36 
     37 import java.util.ArrayList;
     38 import java.util.List;
     39 
     40 // TODO: Include an icon.
     41 
     42 public class AppListFragment extends ListFragment {
     43 
     44     private Adapter mAdapter;
     45     private List<Item> mItems;
     46 
     47     public AppListFragment() {
     48     }
     49 
     50     @Override
     51     public void onActivityCreated(Bundle savedInstanceState) {
     52         super.onActivityCreated(savedInstanceState);
     53         mAdapter = new Adapter();
     54         setListAdapter(mAdapter);
     55     }
     56 
     57     @Override
     58     public void onListItemClick(ListView l, View v, int position, long id) {
     59         final Item item = mItems.get(position);
     60 
     61         Log.i("AppListFragment", "Item clicked: " + position + " -- " + item.component);
     62 
     63         final BrowserListFragment fragment = new BrowserListFragment();
     64 
     65         final Bundle args = new Bundle();
     66         args.putParcelable(BrowserListFragment.ARG_COMPONENT, item.component);
     67         fragment.setArguments(args);
     68 
     69         getFragmentManager().beginTransaction()
     70                 .replace(android.R.id.content, fragment)
     71                 .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
     72                 .addToBackStack(null)
     73                 .commit();
     74     }
     75 
     76     private static class Item {
     77         final String label;
     78         final ComponentName component;
     79 
     80         Item(String l, ComponentName c) {
     81             this.label = l;
     82             this.component = c;
     83         }
     84     }
     85 
     86     private class Adapter extends BaseAdapter {
     87         private final LayoutInflater mInflater;
     88 
     89         Adapter() {
     90             super();
     91 
     92             final Context context = getActivity();
     93             mInflater = LayoutInflater.from(context);
     94 
     95             // Load the data
     96             final PackageManager pm = context.getPackageManager();
     97             final Intent intent = new Intent(MediaBrowserService.SERVICE_INTERFACE);
     98             final List<ResolveInfo> list = pm.queryIntentServices(intent, 0);
     99             final int N = list.size();
    100             mItems = new ArrayList(N);
    101             for (int i=0; i<N; i++) {
    102                 final ResolveInfo ri = list.get(i);
    103                 mItems.add(new Item(ri.loadLabel(pm).toString(), new ComponentName(
    104                             ri.serviceInfo.applicationInfo.packageName,
    105                             ri.serviceInfo.name)));
    106             }
    107         }
    108 
    109         @Override
    110         public int getCount() {
    111             return mItems.size();
    112         }
    113 
    114         @Override
    115         public Item getItem(int position) {
    116             return mItems.get(position);
    117         }
    118 
    119         @Override
    120         public long getItemId(int position) {
    121             return position;
    122         }
    123 
    124         @Override
    125         public int getItemViewType(int position) {
    126             return 1;
    127         }
    128 
    129         @Override
    130         public View getView(int position, View convertView, ViewGroup parent) {
    131             if (convertView == null) {
    132                 convertView = mInflater.inflate(android.R.layout.simple_list_item_1, parent, false);
    133             }
    134 
    135             final TextView tv = (TextView)convertView;
    136             final Item item = mItems.get(position);
    137             tv.setText(item.label);
    138 
    139             return convertView;
    140         }
    141 
    142         @Override
    143         public int getViewTypeCount() {
    144             return 1;
    145         }
    146     }
    147 }
    148 
    149 
    150