This repository has been archived by the owner on Oct 4, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 57b683f
Showing
60 changed files
with
3,698 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
buildscript { | ||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
classpath 'com.android.tools.build:gradle:0.12.+' | ||
} | ||
} | ||
|
||
apply plugin: 'com.android.application' | ||
|
||
|
||
dependencies { | ||
|
||
compile "com.android.support:support-v4:21.+" | ||
compile "com.android.support:support-v13:21.+" | ||
compile "com.android.support:cardview-v7:21.+" | ||
|
||
} | ||
|
||
// The sample build uses multiple directories to | ||
// keep boilerplate and common code separate from | ||
// the main sample code. | ||
List<String> dirs = [ | ||
'main', // main sample code; look here for the interesting stuff. | ||
'common', // components that are reused by multiple samples | ||
'template'] // boilerplate code that is generated by the sample template process | ||
|
||
android { | ||
compileSdkVersion 21 | ||
buildToolsVersion "21.0.0" | ||
|
||
defaultConfig { | ||
minSdkVersion 21 | ||
targetSdkVersion 21 | ||
} | ||
|
||
compileOptions { | ||
sourceCompatibility JavaVersion.VERSION_1_7 | ||
targetCompatibility JavaVersion.VERSION_1_7 | ||
} | ||
|
||
sourceSets { | ||
main { | ||
dirs.each { dir -> | ||
java.srcDirs "src/${dir}/java" | ||
res.srcDirs "src/${dir}/res" | ||
} | ||
} | ||
androidTest.setRoot('tests') | ||
androidTest.java.srcDirs = ['tests/src'] | ||
|
||
} | ||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?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.mediabrowserservice" | ||
android:versionCode="1" | ||
android:versionName="1.0" > | ||
|
||
<uses-permission android:name="android.permission.INTERNET" /> | ||
<uses-permission android:name="android.permission.WAKE_LOCK" /> | ||
|
||
<uses-sdk | ||
android:minSdkVersion="21" | ||
android:targetSdkVersion="21" /> | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:icon="@drawable/ic_launcher" | ||
android:label="@string/app_name" | ||
android:theme="@style/AppTheme"> | ||
|
||
<meta-data android:name="com.google.android.gms.car.application" | ||
android:resource="@xml/automotive_app_desc"/> | ||
|
||
|
||
<activity android:name="com.example.android.mediabrowserservice.MusicPlayerActivity" | ||
android:label="@string/app_name"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
|
||
<!-- (OPTIONAL) use this meta data to indicate which icon should be used in media | ||
notifications (for example, when the music changes and the user is | ||
looking at another app) --> | ||
<meta-data | ||
android:name="com.google.android.gms.car.notification.SmallIcon" | ||
android:resource="@drawable/ic_notification" /> | ||
|
||
<service | ||
android:name="com.example.android.mediabrowserservice.MusicService" | ||
android:exported="true" | ||
> | ||
<intent-filter> | ||
<action android:name="android.media.browse.MediaBrowserService" /> | ||
</intent-filter> | ||
</service> | ||
|
||
</application> | ||
|
||
</manifest> |
210 changes: 210 additions & 0 deletions
210
Application/src/main/java/com/example/android/mediabrowserservice/BrowseFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
/* | ||
* 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. | ||
*/ | ||
package com.example.android.mediabrowserservice; | ||
|
||
import android.app.Fragment; | ||
import android.content.ComponentName; | ||
import android.content.Context; | ||
import android.media.browse.MediaBrowser; | ||
import android.media.session.MediaController; | ||
import android.os.Bundle; | ||
import android.util.Log; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.AdapterView; | ||
import android.widget.ArrayAdapter; | ||
import android.widget.ImageView; | ||
import android.widget.ListView; | ||
import android.widget.TextView; | ||
import android.widget.Toast; | ||
|
||
import com.example.android.mediabrowserservice.utils.LogHelper; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* A Fragment that lists all the various browsable queues available | ||
* from a {@link android.service.media.MediaBrowserService}. | ||
* <p/> | ||
* It uses a {@link MediaBrowser} to connect to the {@link MusicService}. Once connected, | ||
* the fragment subscribes to get all the children. All {@link MediaBrowser.MediaItem}'s | ||
* that can be browsed are shown in a ListView. | ||
*/ | ||
public class BrowseFragment extends Fragment { | ||
|
||
private static final String TAG = BrowseFragment.class.getSimpleName(); | ||
|
||
public static final String ARG_MEDIA_ID = "media_id"; | ||
|
||
public static interface FragmentDataHelper { | ||
void onMediaItemSelected(MediaBrowser.MediaItem item); | ||
} | ||
|
||
// The mediaId to be used for subscribing for children using the MediaBrowser. | ||
private String mMediaId; | ||
|
||
private MediaBrowser mMediaBrowser; | ||
private BrowseAdapter mBrowserAdapter; | ||
|
||
private MediaBrowser.SubscriptionCallback mSubscriptionCallback = new MediaBrowser.SubscriptionCallback() { | ||
|
||
@Override | ||
public void onChildrenLoaded(String parentId, List<MediaBrowser.MediaItem> children) { | ||
mBrowserAdapter.clear(); | ||
mBrowserAdapter.notifyDataSetInvalidated(); | ||
for (MediaBrowser.MediaItem item : children) { | ||
mBrowserAdapter.add(item); | ||
} | ||
mBrowserAdapter.notifyDataSetChanged(); | ||
} | ||
|
||
@Override | ||
public void onError(String id) { | ||
Toast.makeText(getActivity(), R.string.error_loading_media, | ||
Toast.LENGTH_LONG).show(); | ||
} | ||
}; | ||
|
||
private MediaBrowser.ConnectionCallback mConnectionCallback = | ||
new MediaBrowser.ConnectionCallback() { | ||
@Override | ||
public void onConnected() { | ||
LogHelper.d(TAG, "onConnected: session token " + mMediaBrowser.getSessionToken()); | ||
|
||
if (mMediaId == null) { | ||
mMediaId = mMediaBrowser.getRoot(); | ||
} | ||
mMediaBrowser.subscribe(mMediaId, mSubscriptionCallback); | ||
if (mMediaBrowser.getSessionToken() == null) { | ||
throw new IllegalArgumentException("No Session token"); | ||
} | ||
MediaController mediaController = new MediaController(getActivity(), | ||
mMediaBrowser.getSessionToken()); | ||
getActivity().setMediaController(mediaController); | ||
} | ||
|
||
@Override | ||
public void onConnectionFailed() { | ||
LogHelper.d(TAG, "onConnectionFailed"); | ||
} | ||
|
||
@Override | ||
public void onConnectionSuspended() { | ||
LogHelper.d(TAG, "onConnectionSuspended"); | ||
getActivity().setMediaController(null); | ||
} | ||
}; | ||
|
||
public static BrowseFragment newInstance(String mediaId) { | ||
Bundle args = new Bundle(); | ||
args.putString(ARG_MEDIA_ID, mediaId); | ||
BrowseFragment fragment = new BrowseFragment(); | ||
fragment.setArguments(args); | ||
return fragment; | ||
} | ||
|
||
@Override | ||
public View onCreateView(LayoutInflater inflater, ViewGroup container, | ||
Bundle savedInstanceState) { | ||
View rootView = inflater.inflate(R.layout.fragment_list, container, false); | ||
|
||
mBrowserAdapter = new BrowseAdapter(getActivity()); | ||
|
||
View controls = rootView.findViewById(R.id.controls); | ||
controls.setVisibility(View.GONE); | ||
|
||
ListView listView = (ListView) rootView.findViewById(R.id.list_view); | ||
listView.setAdapter(mBrowserAdapter); | ||
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { | ||
@Override | ||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | ||
MediaBrowser.MediaItem item = mBrowserAdapter.getItem(position); | ||
try { | ||
FragmentDataHelper listener = (FragmentDataHelper) getActivity(); | ||
listener.onMediaItemSelected(item); | ||
} catch (ClassCastException ex) { | ||
Log.e(TAG, "Exception trying to cast to FragmentDataHelper", ex); | ||
} | ||
} | ||
}); | ||
|
||
Bundle args = getArguments(); | ||
mMediaId = args.getString(ARG_MEDIA_ID, null); | ||
|
||
mMediaBrowser = new MediaBrowser(getActivity(), | ||
new ComponentName(getActivity(), MusicService.class), | ||
mConnectionCallback, null); | ||
|
||
return rootView; | ||
} | ||
|
||
@Override | ||
public void onStart() { | ||
super.onStart(); | ||
mMediaBrowser.connect(); | ||
} | ||
|
||
@Override | ||
public void onStop() { | ||
super.onStop(); | ||
mMediaBrowser.disconnect(); | ||
} | ||
|
||
// An adapter for showing the list of browsed MediaItem's | ||
private static class BrowseAdapter extends ArrayAdapter<MediaBrowser.MediaItem> { | ||
|
||
public BrowseAdapter(Context context) { | ||
super(context, R.layout.media_list_item, new ArrayList<MediaBrowser.MediaItem>()); | ||
} | ||
|
||
static class ViewHolder { | ||
ImageView mImageView; | ||
TextView mTitleView; | ||
TextView mDescriptionView; | ||
} | ||
|
||
@Override | ||
public View getView(int position, View convertView, ViewGroup parent) { | ||
|
||
ViewHolder holder; | ||
|
||
if (convertView == null) { | ||
convertView = LayoutInflater.from(getContext()) | ||
.inflate(R.layout.media_list_item, parent, false); | ||
holder = new ViewHolder(); | ||
holder.mImageView = (ImageView) convertView.findViewById(R.id.play_eq); | ||
holder.mImageView.setVisibility(View.GONE); | ||
holder.mTitleView = (TextView) convertView.findViewById(R.id.title); | ||
holder.mDescriptionView = (TextView) convertView.findViewById(R.id.description); | ||
convertView.setTag(holder); | ||
} else { | ||
holder = (ViewHolder) convertView.getTag(); | ||
} | ||
|
||
MediaBrowser.MediaItem item = getItem(position); | ||
holder.mTitleView.setText(item.getDescription().getTitle()); | ||
holder.mDescriptionView.setText(item.getDescription().getDescription()); | ||
if (item.isPlayable()) { | ||
holder.mImageView.setImageDrawable( | ||
getContext().getDrawable(R.drawable.ic_play_arrow_white_24dp)); | ||
holder.mImageView.setVisibility(View.VISIBLE); | ||
} | ||
return convertView; | ||
} | ||
} | ||
} |
Oops, something went wrong.