From 17d47989ee53c9e54f250d29a343ba949edf0ff9 Mon Sep 17 00:00:00 2001 From: Yao Chen Date: Thu, 17 Jul 2014 14:39:34 -0700 Subject: Updated media browsing API & tests. Change-Id: I3a1e3eead99a98b1890e05843dd90203cee517b7 --- tests/MusicBrowserDemo/Android.mk | 35 ++++ tests/MusicBrowserDemo/AndroidManifest.xml | 45 +++++ .../res/drawable-hdpi/ic_launcher.png | Bin 0 -> 4805 bytes .../res/drawable-mdpi/ic_launcher.png | Bin 0 -> 2592 bytes .../res/drawable-xhdpi/ic_launcher.png | Bin 0 -> 5246 bytes .../res/drawable-xxhdpi/ic_launcher.png | Bin 0 -> 14755 bytes tests/MusicBrowserDemo/res/values/strings.xml | 21 ++ tests/MusicBrowserDemo/res/values/styles.xml | 36 ++++ .../android/musicbrowserdemo/AppListFragment.java | 149 +++++++++++++++ .../musicbrowserdemo/BrowserListFragment.java | 212 +++++++++++++++++++++ .../android/musicbrowserdemo/MainActivity.java | 48 +++++ 11 files changed, 546 insertions(+) create mode 100644 tests/MusicBrowserDemo/Android.mk create mode 100644 tests/MusicBrowserDemo/AndroidManifest.xml create mode 100644 tests/MusicBrowserDemo/res/drawable-hdpi/ic_launcher.png create mode 100644 tests/MusicBrowserDemo/res/drawable-mdpi/ic_launcher.png create mode 100644 tests/MusicBrowserDemo/res/drawable-xhdpi/ic_launcher.png create mode 100644 tests/MusicBrowserDemo/res/drawable-xxhdpi/ic_launcher.png create mode 100644 tests/MusicBrowserDemo/res/values/strings.xml create mode 100644 tests/MusicBrowserDemo/res/values/styles.xml create mode 100644 tests/MusicBrowserDemo/src/com/example/android/musicbrowserdemo/AppListFragment.java create mode 100644 tests/MusicBrowserDemo/src/com/example/android/musicbrowserdemo/BrowserListFragment.java create mode 100644 tests/MusicBrowserDemo/src/com/example/android/musicbrowserdemo/MainActivity.java (limited to 'tests/MusicBrowserDemo') diff --git a/tests/MusicBrowserDemo/Android.mk b/tests/MusicBrowserDemo/Android.mk new file mode 100644 index 0000000..207774b --- /dev/null +++ b/tests/MusicBrowserDemo/Android.mk @@ -0,0 +1,35 @@ +# Copyright (C) 2014 The Android Open Source Project +# +# 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. + +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +LOCAL_PACKAGE_NAME := MusicBrowserDemo +#LOCAL_SDK_VERSION := current +LOCAL_SRC_FILES := $(call all-java-files-under, src) +LOCAL_STATIC_JAVA_LIBRARIES := \ + android-support-v4 \ + android-support-v7-appcompat + +LOCAL_RESOURCE_DIR := \ + $(LOCAL_PATH)/res \ + frameworks/support/v7/appcompat/res +LOCAL_PROGUARD_ENABLED := disabled +#LOCAL_PROGUARD_FLAG_FILES := proguard.flags + +LOCAL_AAPT_FLAGS := \ + --auto-add-overlay \ + --extra-packages android.support.v7.appcompat +include $(BUILD_PACKAGE) diff --git a/tests/MusicBrowserDemo/AndroidManifest.xml b/tests/MusicBrowserDemo/AndroidManifest.xml new file mode 100644 index 0000000..d2acfe2 --- /dev/null +++ b/tests/MusicBrowserDemo/AndroidManifest.xml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + diff --git a/tests/MusicBrowserDemo/res/drawable-hdpi/ic_launcher.png b/tests/MusicBrowserDemo/res/drawable-hdpi/ic_launcher.png new file mode 100644 index 0000000..47d6854 Binary files /dev/null and b/tests/MusicBrowserDemo/res/drawable-hdpi/ic_launcher.png differ diff --git a/tests/MusicBrowserDemo/res/drawable-mdpi/ic_launcher.png b/tests/MusicBrowserDemo/res/drawable-mdpi/ic_launcher.png new file mode 100644 index 0000000..01b53fd Binary files /dev/null and b/tests/MusicBrowserDemo/res/drawable-mdpi/ic_launcher.png differ diff --git a/tests/MusicBrowserDemo/res/drawable-xhdpi/ic_launcher.png b/tests/MusicBrowserDemo/res/drawable-xhdpi/ic_launcher.png new file mode 100644 index 0000000..af762f2 Binary files /dev/null and b/tests/MusicBrowserDemo/res/drawable-xhdpi/ic_launcher.png differ diff --git a/tests/MusicBrowserDemo/res/drawable-xxhdpi/ic_launcher.png b/tests/MusicBrowserDemo/res/drawable-xxhdpi/ic_launcher.png new file mode 100644 index 0000000..eef47aa Binary files /dev/null and b/tests/MusicBrowserDemo/res/drawable-xxhdpi/ic_launcher.png differ diff --git a/tests/MusicBrowserDemo/res/values/strings.xml b/tests/MusicBrowserDemo/res/values/strings.xml new file mode 100644 index 0000000..858f278 --- /dev/null +++ b/tests/MusicBrowserDemo/res/values/strings.xml @@ -0,0 +1,21 @@ + + + + + Music Browser + + diff --git a/tests/MusicBrowserDemo/res/values/styles.xml b/tests/MusicBrowserDemo/res/values/styles.xml new file mode 100644 index 0000000..b83662d --- /dev/null +++ b/tests/MusicBrowserDemo/res/values/styles.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + diff --git a/tests/MusicBrowserDemo/src/com/example/android/musicbrowserdemo/AppListFragment.java b/tests/MusicBrowserDemo/src/com/example/android/musicbrowserdemo/AppListFragment.java new file mode 100644 index 0000000..c0f3a7f --- /dev/null +++ b/tests/MusicBrowserDemo/src/com/example/android/musicbrowserdemo/AppListFragment.java @@ -0,0 +1,149 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * 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.example.android.musicbrowserdemo; + +import android.content.Context; +import android.content.ComponentName; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; +import android.media.browse.MediaBrowserService; +import android.os.Bundle; +import android.support.v4.app.FragmentActivity; +import android.support.v4.app.FragmentTransaction; +import android.support.v4.app.ListFragment; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.BaseAdapter; +import android.widget.ListView; +import android.widget.TextView; + +import java.util.ArrayList; +import java.util.List; + +// TODO: Include an icon. + +public class AppListFragment extends ListFragment { + + private Adapter mAdapter; + private List mItems; + + public AppListFragment() { + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + mAdapter = new Adapter(); + setListAdapter(mAdapter); + } + + @Override + public void onListItemClick(ListView l, View v, int position, long id) { + final Item item = mItems.get(position); + + Log.i("AppListFragment", "Item clicked: " + position + " -- " + item.component); + + final BrowserListFragment fragment = new BrowserListFragment(); + + final Bundle args = new Bundle(); + args.putParcelable(BrowserListFragment.ARG_COMPONENT, item.component); + fragment.setArguments(args); + + getFragmentManager().beginTransaction() + .replace(android.R.id.content, fragment) + .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN) + .addToBackStack(null) + .commit(); + } + + private static class Item { + final String label; + final ComponentName component; + + Item(String l, ComponentName c) { + this.label = l; + this.component = c; + } + } + + private class Adapter extends BaseAdapter { + private final LayoutInflater mInflater; + + Adapter() { + super(); + + final Context context = getActivity(); + mInflater = LayoutInflater.from(context); + + // Load the data + final PackageManager pm = context.getPackageManager(); + final Intent intent = new Intent(MediaBrowserService.SERVICE_ACTION); + final List list = pm.queryIntentServices(intent, 0); + final int N = list.size(); + mItems = new ArrayList(N); + for (int i=0; i mItems = new ArrayList(); + private ComponentName mComponent; + private Uri mUri; + private MediaBrowser mBrowser; + + private static class Item { + final MediaBrowserItem media; + + Item(MediaBrowserItem m) { + this.media = m; + } + } + + public BrowserListFragment() { + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + Log.d(TAG, "onActivityCreated -- " + hashCode()); + mAdapter = new Adapter(); + setListAdapter(mAdapter); + + // Get our arguments + final Bundle args = getArguments(); + mComponent = args.getParcelable(ARG_COMPONENT); + mUri = args.getParcelable(ARG_URI); + + // A hint about who we are, so the service can customize the results if it wants to. + final Bundle rootHints = new Bundle(); + rootHints.putBoolean(HINT_DISPLAY, true); + + mBrowser = new MediaBrowser(getActivity(), mComponent, mConnectionCallbacks, rootHints); + } + + @Override + public void onStart() { + super.onStart(); + mBrowser.connect(); + } + + @Override + public void onStop() { + super.onStop(); + mBrowser.disconnect(); + } + + @Override + public void onListItemClick(ListView l, View v, int position, long id) { + final Item item = mItems.get(position); + + Log.i("BrowserListFragment", "Item clicked: " + position + " -- " + + mAdapter.getItem(position).media.getUri()); + + final BrowserListFragment fragment = new BrowserListFragment(); + + final Bundle args = new Bundle(); + args.putParcelable(BrowserListFragment.ARG_COMPONENT, mComponent); + args.putParcelable(BrowserListFragment.ARG_URI, item.media.getUri()); + fragment.setArguments(args); + + getFragmentManager().beginTransaction() + .replace(android.R.id.content, fragment) + .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN) + .addToBackStack(null) + .commit(); + + } + + final MediaBrowser.ConnectionCallback mConnectionCallbacks + = new MediaBrowser.ConnectionCallback() { + @Override + public void onConnected() { + Log.d(TAG, "mConnectionCallbacks.onConnected"); + if (mUri == null) { + mUri = mBrowser.getRoot(); + } + mBrowser.subscribe(mUri, new MediaBrowser.SubscriptionCallback() { + @Override + public void onChildrenLoaded(Uri parentUri, List children) { + Log.d(TAG, "onChildrenLoaded parentUri=" + parentUri + + " children= " + children); + mItems.clear(); + final int N = children.size(); + for (int i=0; i