diff options
Diffstat (limited to 'tests/MusicBrowserDemo')
-rw-r--r-- | tests/MusicBrowserDemo/Android.mk | 35 | ||||
-rw-r--r-- | tests/MusicBrowserDemo/AndroidManifest.xml | 45 | ||||
-rw-r--r-- | tests/MusicBrowserDemo/res/drawable-hdpi/ic_launcher.png | bin | 0 -> 4805 bytes | |||
-rw-r--r-- | tests/MusicBrowserDemo/res/drawable-mdpi/ic_launcher.png | bin | 0 -> 2592 bytes | |||
-rw-r--r-- | tests/MusicBrowserDemo/res/drawable-xhdpi/ic_launcher.png | bin | 0 -> 5246 bytes | |||
-rw-r--r-- | tests/MusicBrowserDemo/res/drawable-xxhdpi/ic_launcher.png | bin | 0 -> 14755 bytes | |||
-rw-r--r-- | tests/MusicBrowserDemo/res/values/strings.xml | 21 | ||||
-rw-r--r-- | tests/MusicBrowserDemo/res/values/styles.xml | 36 | ||||
-rw-r--r-- | tests/MusicBrowserDemo/src/com/example/android/musicbrowserdemo/AppListFragment.java | 149 | ||||
-rw-r--r-- | tests/MusicBrowserDemo/src/com/example/android/musicbrowserdemo/BrowserListFragment.java | 212 | ||||
-rw-r--r-- | tests/MusicBrowserDemo/src/com/example/android/musicbrowserdemo/MainActivity.java | 48 |
11 files changed, 546 insertions, 0 deletions
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 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + 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. + --> +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.example.android.musicbrowserdemo" + android:versionCode="1" + android:versionName="1.0" > + + <uses-sdk + android:minSdkVersion="9" + android:targetSdkVersion="19" /> + + <application + android:allowBackup="true" + android:icon="@drawable/ic_launcher" + android:label="@string/app_name" + android:theme="@style/AppTheme" + > + + <activity + android:name="com.example.android.musicbrowserdemo.MainActivity" + android:label="@string/app_name" + > + <intent-filter> + <action android:name="android.intent.action.MAIN" /> + <category android:name="android.intent.category.LAUNCHER" /> + </intent-filter> + </activity> + + </application> + +</manifest> diff --git a/tests/MusicBrowserDemo/res/drawable-hdpi/ic_launcher.png b/tests/MusicBrowserDemo/res/drawable-hdpi/ic_launcher.png Binary files differnew file mode 100644 index 0000000..47d6854 --- /dev/null +++ b/tests/MusicBrowserDemo/res/drawable-hdpi/ic_launcher.png diff --git a/tests/MusicBrowserDemo/res/drawable-mdpi/ic_launcher.png b/tests/MusicBrowserDemo/res/drawable-mdpi/ic_launcher.png Binary files differnew file mode 100644 index 0000000..01b53fd --- /dev/null +++ b/tests/MusicBrowserDemo/res/drawable-mdpi/ic_launcher.png diff --git a/tests/MusicBrowserDemo/res/drawable-xhdpi/ic_launcher.png b/tests/MusicBrowserDemo/res/drawable-xhdpi/ic_launcher.png Binary files differnew file mode 100644 index 0000000..af762f2 --- /dev/null +++ b/tests/MusicBrowserDemo/res/drawable-xhdpi/ic_launcher.png diff --git a/tests/MusicBrowserDemo/res/drawable-xxhdpi/ic_launcher.png b/tests/MusicBrowserDemo/res/drawable-xxhdpi/ic_launcher.png Binary files differnew file mode 100644 index 0000000..eef47aa --- /dev/null +++ b/tests/MusicBrowserDemo/res/drawable-xxhdpi/ic_launcher.png 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 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + 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. + --> +<resources> + + <string name="app_name">Music Browser</string> + +</resources> 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 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + 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. + --> +<resources> + + <!-- + Base application theme, dependent on API level. This theme is replaced + by AppBaseTheme from res/values-vXX/styles.xml on newer devices. + --> + <style name="AppBaseTheme" parent="Theme.AppCompat.Light"> + <!-- + Theme customizations available in newer API levels can go in + res/values-vXX/styles.xml, while customizations related to + backward-compatibility can go here. + --> + </style> + + <!-- Application theme. --> + <style name="AppTheme" parent="AppBaseTheme"> + <!-- All customizations that are NOT specific to a particular API-level can go here. --> + </style> + +</resources> 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<Item> 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<ResolveInfo> list = pm.queryIntentServices(intent, 0); + final int N = list.size(); + mItems = new ArrayList(N); + for (int i=0; i<N; i++) { + final ResolveInfo ri = list.get(i); + mItems.add(new Item(ri.loadLabel(pm).toString(), new ComponentName( + ri.serviceInfo.applicationInfo.packageName, + ri.serviceInfo.name))); + } + } + + @Override + public int getCount() { + return mItems.size(); + } + + @Override + public Item getItem(int position) { + return mItems.get(position); + } + + @Override + public long getItemId(int position) { + return position; + } + + @Override + public int getItemViewType(int position) { + return 1; + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + if (convertView == null) { + convertView = mInflater.inflate(android.R.layout.simple_list_item_1, parent, false); + } + + final TextView tv = (TextView)convertView; + final Item item = mItems.get(position); + tv.setText(item.label); + + return convertView; + } + + @Override + public int getViewTypeCount() { + return 1; + } + } +} + + diff --git a/tests/MusicBrowserDemo/src/com/example/android/musicbrowserdemo/BrowserListFragment.java b/tests/MusicBrowserDemo/src/com/example/android/musicbrowserdemo/BrowserListFragment.java new file mode 100644 index 0000000..3fc468d --- /dev/null +++ b/tests/MusicBrowserDemo/src/com/example/android/musicbrowserdemo/BrowserListFragment.java @@ -0,0 +1,212 @@ +/* + * 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.MediaBrowser; +import android.media.browse.MediaBrowserItem; +import android.media.browse.MediaBrowserService; +import android.os.Bundle; +import android.net.Uri; +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; + +public class BrowserListFragment extends ListFragment { + private static final String TAG = "BrowserListFragment"; + + // Hints + public static final String HINT_DISPLAY = "com.example.android.musicbrowserdemo.DISPLAY"; + + // For args + public static final String ARG_COMPONENT = "component"; + public static final String ARG_URI = "uri"; + + private Adapter mAdapter; + private List<Item> 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<MediaBrowserItem> children) { + Log.d(TAG, "onChildrenLoaded parentUri=" + parentUri + + " children= " + children); + mItems.clear(); + final int N = children.size(); + for (int i=0; i<N; i++) { + mItems.add(new Item(children.get(i))); + } + mAdapter.notifyDataSetChanged(); + } + + @Override + public void onError(Uri parentUri) { + Log.d(TAG, "onError parentUri=" + parentUri); + } + }); + } + + @Override + public void onConnectionSuspended() { + Log.d(TAG, "mConnectionCallbacks.onConnectionSuspended"); + } + + @Override + public void onConnectionFailed() { + Log.d(TAG, "mConnectionCallbacks.onConnectionFailed"); + } + }; + + private class Adapter extends BaseAdapter { + private final LayoutInflater mInflater; + + Adapter() { + super(); + + final Context context = getActivity(); + mInflater = LayoutInflater.from(context); + } + + @Override + public int getCount() { + return mItems.size(); + } + + @Override + public Item getItem(int position) { + return mItems.get(position); + } + + @Override + public long getItemId(int position) { + return position; + } + + @Override + public int getItemViewType(int position) { + return 1; + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + if (convertView == null) { + convertView = mInflater.inflate(android.R.layout.simple_list_item_1, parent, false); + } + + final TextView tv = (TextView)convertView; + final Item item = mItems.get(position); + tv.setText(item.media.getTitle()); + + return convertView; + } + + @Override + public int getViewTypeCount() { + return 1; + } + } +} + + diff --git a/tests/MusicBrowserDemo/src/com/example/android/musicbrowserdemo/MainActivity.java b/tests/MusicBrowserDemo/src/com/example/android/musicbrowserdemo/MainActivity.java new file mode 100644 index 0000000..ed91aad --- /dev/null +++ b/tests/MusicBrowserDemo/src/com/example/android/musicbrowserdemo/MainActivity.java @@ -0,0 +1,48 @@ +/* + * 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.support.v4.app.FragmentActivity; +import android.support.v4.app.FragmentManager; + +import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.widget.ArrayAdapter; +import android.widget.ListView; + +/** + * Main activity class. + */ +public class MainActivity extends FragmentActivity { + + private static final String BROWSER_FRAGMENT_TAG = "browser"; + + @Override + protected void onCreate(Bundle icicle) { + super.onCreate(icicle); + + Log.d("MainActivity", "-------------------------------------------------------"); + + // If we are starting afresh, start at the app list. + final FragmentManager fm = getSupportFragmentManager(); + if (fm.findFragmentById(android.R.id.content) == null) { + fm.beginTransaction().add(android.R.id.content, new AppListFragment()).commit(); + } + } +} + |