Skip to content

Commit

Permalink
added android auto media player demo
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Ruiz committed Feb 14, 2015
1 parent 5299fc1 commit 515063d
Show file tree
Hide file tree
Showing 30 changed files with 875 additions and 3 deletions.
6 changes: 6 additions & 0 deletions AndroidAutoMedia/.gitignore
@@ -0,0 +1,6 @@
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
1 change: 1 addition & 0 deletions AndroidAutoMedia/app/.gitignore
@@ -0,0 +1 @@
/build
20 changes: 20 additions & 0 deletions AndroidAutoMedia/app/build.gradle
@@ -0,0 +1,20 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 'Google Inc.:Google APIs:21'
buildToolsVersion "21.1.1"

defaultConfig {
applicationId "com.ptrprograms.androidautomedia"
minSdkVersion 21
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.squareup.picasso:picasso:2.4.0'

}
17 changes: 17 additions & 0 deletions AndroidAutoMedia/app/proguard-rules.pro
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Applications/Android Studio.app/sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
@@ -0,0 +1,13 @@
package com.ptrprograms.androidautomedia;

import android.app.Application;
import android.test.ApplicationTestCase;

/**
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
*/
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
}
}
36 changes: 36 additions & 0 deletions AndroidAutoMedia/app/src/main/AndroidManifest.xml
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ptrprograms.androidautomedia" >

<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"/>

<meta-data android:name="com.google.android.gms.car.notification.SmallIcon"
android:resource="@drawable/ic_launcher" />


<activity
android:name=".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>

<service android:name=".AutoMediaBrowserService">
<intent-filter>
<action android:name="android.media.browse.MediaBrowserService" />
</intent-filter>
</service>
</application>

</manifest>
@@ -0,0 +1,212 @@
package com.ptrprograms.androidautomedia;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.MediaDescription;
import android.media.MediaMetadata;
import android.media.Rating;
import android.media.browse.MediaBrowser;
import android.media.session.MediaSession;
import android.media.session.PlaybackState;
import android.net.Uri;
import android.os.Bundle;
import android.os.ResultReceiver;
import android.service.media.MediaBrowserService;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
* Created by paulruiz on 11/18/14.
*/
public class AutoMediaBrowserService extends MediaBrowserService {

private static final String BROWSEABLE_ROOT = "root";
private static final String BROWSEABLE_ROCK = "Rock";
private static final String BROWSEABLE_JAZZ = "Jazz";
private static final String BROWSEABLE_CAJUN = "Cajun";

private MediaSession mMediaSession;
private MediaSession.Token mMediaSessionToken;

private List<Song> mSongs;

private MediaSession.Callback mMediaSessionCallback = new MediaSession.Callback() {
@Override
public void onPlay() {
super.onPlay();

toggleMediaPlaybackState( true );
}

//This is called when the pause button is pressed, or when onPlayFromMediaId is called in
//order to pause any currently playing media
@Override
public void onPause() {
super.onPause();

Toast.makeText( getApplicationContext(), "MediaSession onPause", Toast.LENGTH_SHORT ).show();
toggleMediaPlaybackState( false );
}

@Override
public void onPlayFromMediaId(String mediaId, Bundle extras) {
super.onPlayFromMediaId(mediaId, extras);
Log.e( "Android Auto", "mediaId: " + mediaId );

initMediaMetaData( mediaId );
toggleMediaPlaybackState( true );
}

@Override
public void onCustomAction(String action, Bundle extras) {
super.onCustomAction(action, extras);
}
};

@Override
public void onCreate() {
super.onCreate();

mSongs = SongGenerator.generateSongs();

initMediaSession();
}

private void initMediaSession() {
mMediaSession = new MediaSession( this, "MediaSessionId" );
mMediaSession.setActive( true );
mMediaSession.setCallback( mMediaSessionCallback );

mMediaSessionToken = mMediaSession.getSessionToken();
setSessionToken( mMediaSessionToken );
}

private void initMediaMetaData( String id ) {

for( Song song : mSongs ) {
if( !TextUtils.isEmpty( song.getuId() ) && song.getuId().equalsIgnoreCase( id ) ) {
MediaMetadata.Builder builder = new MediaMetadata.Builder();

if( !TextUtils.isEmpty( song.getTitle() ) )
builder.putText( MediaMetadata.METADATA_KEY_TITLE, song.getTitle() );

if( !TextUtils.isEmpty( song.getArtist() ) )
builder.putText( MediaMetadata.METADATA_KEY_ARTIST, song.getArtist() );

if( !TextUtils.isEmpty( song.getGenre() ) )
builder.putText( MediaMetadata.METADATA_KEY_GENRE, song.getGenre() );

if( !TextUtils.isEmpty( song.getAlbum() ) )
builder.putText( MediaMetadata.METADATA_KEY_ALBUM, song.getAlbum() );

if( !TextUtils.isEmpty( song.getAlbumUrl() ) )
builder.putText( MediaMetadata.METADATA_KEY_ALBUM_ART_URI, song.getAlbumUrl() );

mMediaSession.setMetadata( builder.build() );
}
}
}

private void toggleMediaPlaybackState( boolean playing ) {
PlaybackState playbackState;
if( playing ) {
playbackState = new PlaybackState.Builder()
.setActions(PlaybackState.ACTION_PLAY_PAUSE)
.setState(PlaybackState.STATE_PLAYING, 0, 1)
.build();
} else {
playbackState = new PlaybackState.Builder()
.setActions(PlaybackState.ACTION_PLAY_PAUSE)
.setState(PlaybackState.STATE_PAUSED, 0, 1)
.build();
}

mMediaSession.setPlaybackState( playbackState );
}

@Override
public BrowserRoot onGetRoot(String clientPackageName, int clientUid, Bundle rootHints) {
return new BrowserRoot(BROWSEABLE_ROOT, null);
}

@Override
public void onLoadChildren(String parentId, Result<List<MediaBrowser.MediaItem>> result) {
Log.e( "Android Auto", parentId);

List<MediaBrowser.MediaItem> items = getMediaItemsById(parentId);
if( items == null ) {
Toast.makeText( getApplicationContext(), "MediaItems null, should play", Toast.LENGTH_SHORT ).show();
} else {
result.sendResult( items );
}
}

private List<MediaBrowser.MediaItem> getMediaItemsById( String id ) {
List<MediaBrowser.MediaItem> mediaItems = new ArrayList();
if( BROWSEABLE_ROOT.equalsIgnoreCase( id ) ) {
mediaItems.add( generateBrowseableMediaItemByGenre(BROWSEABLE_CAJUN) );
mediaItems.add( generateBrowseableMediaItemByGenre(BROWSEABLE_JAZZ) );
mediaItems.add( generateBrowseableMediaItemByGenre(BROWSEABLE_ROCK) );
} else if( !TextUtils.isEmpty( id ) ) {
return getPlayableMediaItemsByGenre( id );
}

return mediaItems;
}

private List<MediaBrowser.MediaItem> getPlayableMediaItemsByGenre( String genre ) {
if( TextUtils.isEmpty( genre ) )
return null;

List<MediaBrowser.MediaItem> mediaItems = new ArrayList();

for( Song song : mSongs ) {
if( !TextUtils.isEmpty( song.getGenre() ) && genre.equalsIgnoreCase( song.getGenre() ) ) {
mediaItems.add( generatePlayableMediaItem( song ) );
}
}
return mediaItems;
}


private MediaBrowser.MediaItem generateBrowseableMediaItemByGenre( String genre ) {
MediaDescription.Builder mediaDescriptionBuilder = new MediaDescription.Builder();
mediaDescriptionBuilder.setMediaId( genre );
mediaDescriptionBuilder.setTitle( genre );
mediaDescriptionBuilder.setIconBitmap( BitmapFactory.decodeResource( getResources(), R.drawable.ic_launcher ) );

return new MediaBrowser.MediaItem( mediaDescriptionBuilder.build(), MediaBrowser.MediaItem.FLAG_BROWSABLE );
}

private MediaBrowser.MediaItem generatePlayableMediaItem( Song song ) {
if( song == null )
return null;

MediaDescription.Builder mediaDescriptionBuilder = new MediaDescription.Builder();
mediaDescriptionBuilder.setMediaId( song.getuId() );

if( !TextUtils.isEmpty( song.getTitle() ) )
mediaDescriptionBuilder.setTitle( song.getTitle() );

if( !TextUtils.isEmpty( song.getArtist() ) )
mediaDescriptionBuilder.setSubtitle( song.getArtist() );

if( !TextUtils.isEmpty( song.getThumbnailUrl() ) )
mediaDescriptionBuilder.setIconUri( Uri.parse( song.getThumbnailUrl() ) );

return new MediaBrowser.MediaItem( mediaDescriptionBuilder.build(), MediaBrowser.MediaItem.FLAG_PLAYABLE );
}

}
@@ -0,0 +1,36 @@
package com.ptrprograms.androidautomedia;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

0 comments on commit 515063d

Please sign in to comment.