Skip to content

Commit a873d59

Browse files
author
Paul Ruiz
committed
saving place with android auto
1 parent d0966d2 commit a873d59

File tree

2 files changed

+63
-76
lines changed

2 files changed

+63
-76
lines changed

AndroidAutoMedia/app/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@ android {
2121

2222
dependencies {
2323
compile fileTree(dir: 'libs', include: ['*.jar'])
24+
compile 'com.squareup.picasso:picasso:2.4.0'
25+
2426
}

AndroidAutoMedia/app/src/main/java/com/ptrprograms/androidautomedia/AutoMediaBrowserService.java

Lines changed: 61 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,23 @@
44
import android.content.Intent;
55
import android.content.pm.PackageInfo;
66
import android.content.pm.PackageManager;
7-
import android.media.AudioManager;
8-
import android.media.MediaDescription;
7+
import android.graphics.Bitmap;
8+
import android.graphics.drawable.Drawable;
99
import android.media.MediaMetadata;
10-
import android.media.MediaPlayer;
1110
import android.media.Rating;
1211
import android.media.browse.MediaBrowser;
1312
import android.media.session.MediaSession;
14-
import android.net.Uri;
15-
import android.os.*;
13+
import android.media.session.PlaybackState;
14+
import android.os.Build;
15+
import android.os.Bundle;
16+
import android.os.ResultReceiver;
1617
import android.service.media.MediaBrowserService;
17-
import android.util.Base64;
1818
import android.util.Log;
1919

20+
import com.squareup.picasso.Picasso;
21+
import com.squareup.picasso.Target;
22+
23+
import java.io.IOException;
2024
import java.io.UnsupportedEncodingException;
2125
import java.util.ArrayList;
2226
import java.util.Arrays;
@@ -25,12 +29,34 @@
2529
/**
2630
* Created by paulruiz on 11/18/14.
2731
*/
28-
public class AutoMediaBrowserService extends MediaBrowserService implements MediaPlayer.OnPreparedListener,
29-
MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener, AudioManager.OnAudioFocusChangeListener {
32+
public class AutoMediaBrowserService extends MediaBrowserService {
3033

3134
public static final String MEDIA_ID_ROOT = "__ROOT__";
3235
public static final String MEDIA_ID_MUSICS_BY_GENRE = "__BY_GENRE__";
3336

37+
private int isPlaying;
38+
39+
Target target = new Target() {
40+
@Override
41+
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
42+
try {
43+
setWallpaper(bitmap);
44+
} catch( IOException e ) {
45+
46+
}
47+
}
48+
49+
@Override
50+
public void onBitmapFailed(Drawable errorDrawable) {
51+
52+
}
53+
54+
@Override
55+
public void onPrepareLoad(Drawable placeHolderDrawable) {
56+
57+
}
58+
};
59+
3460
static final byte[][] VALID_PUBLIC_SIGNATURES = new byte[][]{
3561
// Android Auto release public key
3662
extractKey(
@@ -139,7 +165,6 @@ public class AutoMediaBrowserService extends MediaBrowserService implements Medi
139165

140166
private MediaSession mSession;
141167
private MediaSession.Callback mMediaSessionCallbacks;
142-
private AudioManager mAudioManager;
143168

144169
private static final String TAG = "AutoMediaBrowserService";
145170

@@ -148,27 +173,9 @@ public void onCreate() {
148173
super.onCreate();
149174
Log.e( "AutoMediaBrowserService", "onCreate" );
150175

151-
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
152-
153176
initCallbacks();
154177
initMediaSessions();
155178

156-
mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC,
157-
AudioManager.AUDIOFOCUS_GAIN);
158-
159-
/*
160-
Bundle extras = new Bundle();
161-
162-
extras.putBoolean(
163-
"com.google.android.gms.car.media.ALWAYS_RESERVE_SPACE_FOR.ACTION_SKIP_TO_NEXT",
164-
true);
165-
extras.putBoolean(
166-
"com.google.android.gms.car.media.ALWAYS_RESERVE_SPACE_FOR.ACTION_SKIP_TO_PREVIOUS",
167-
true);
168-
mSession.setExtras(extras);
169-
*/
170-
171-
172179
}
173180

174181
private void initMediaSessions() {
@@ -177,19 +184,24 @@ private void initMediaSessions() {
177184
mSession.setCallback( mMediaSessionCallbacks );
178185
mSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS | MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
179186

180-
List<MediaSession.QueueItem> queue = new ArrayList<MediaSession.QueueItem>();
181-
MediaMetadata data = new MediaMetadata.Builder()
182-
.putString( MediaMetadata.METADATA_KEY_TITLE, "title" )
183-
.putString(MediaMetadata.METADATA_KEY_DISPLAY_DESCRIPTION, "description")
184-
.build();
185-
queue.add( new MediaSession.QueueItem( data.getDescription(), 0 ) );
186-
queue.add( new MediaSession.QueueItem( data.getDescription(), 1 ) );
187-
queue.add( new MediaSession.QueueItem( data.getDescription(), 2 ) );
188-
queue.add( new MediaSession.QueueItem( data.getDescription(), 3 ) );
189-
190-
mSession.setQueue( queue );
187+
PlaybackState.Builder stateBuilder = new PlaybackState.Builder()
188+
.setActions(getAvailableActions());
189+
190+
mSession.setPlaybackState(stateBuilder.build());
191191
}
192192

193+
private long getAvailableActions() {
194+
long actions = PlaybackState.ACTION_PLAY | PlaybackState.ACTION_PLAY_FROM_MEDIA_ID |
195+
PlaybackState.ACTION_PLAY_FROM_SEARCH;
196+
197+
if (isPlaying == PlaybackState.STATE_PLAYING) {
198+
actions |= PlaybackState.ACTION_PAUSE;
199+
}
200+
201+
return actions;
202+
}
203+
204+
193205
private void initCallbacks() {
194206
mMediaSessionCallbacks = new MediaSession.Callback() {
195207

@@ -288,11 +300,6 @@ public BrowserRoot onGetRoot(String clientPackageName, int clientUid, Bundle roo
288300
// be made to other media browsing methods.
289301
return null;
290302
}
291-
if ("com.ptrprograms.androidautomedia".equals(clientPackageName)) {
292-
// Optional: if your app needs to adapt ads, music library or anything else that
293-
// needs to run differently when connected to the car, this is where you should handle
294-
// it.
295-
}
296303
return new BrowserRoot("__ROOT__", null);
297304
}
298305

@@ -307,21 +314,16 @@ public static boolean isCallerAllowed(Context context, String callingPackage, in
307314
packageInfo = packageManager.getPackageInfo(
308315
callingPackage, PackageManager.GET_SIGNATURES);
309316
} catch (PackageManager.NameNotFoundException ignored) {
310-
if (Log.isLoggable(TAG, Log.DEBUG)) {
311-
Log.d(TAG, "Package manager can't find package " + callingPackage
312-
+ ", defaulting to false");
313-
}
314317
return false;
315318
}
316319
if (packageInfo == null) {
317-
Log.w(TAG, "Package manager can't find package: " + callingPackage);
318320
return false;
319321
}
320322

321323
if (packageInfo.signatures.length != 1) {
322-
Log.w(TAG, "Package has more than one signature.");
323324
return false;
324325
}
326+
325327
final byte[] signature = packageInfo.signatures[0].toByteArray();
326328

327329
for (int i = 0; i < VALID_PUBLIC_SIGNATURES.length; i++) {
@@ -331,10 +333,6 @@ public static boolean isCallerAllowed(Context context, String callingPackage, in
331333
}
332334
}
333335

334-
if (Log.isLoggable(TAG, Log.VERBOSE)) {
335-
Log.v(TAG, "Signature not valid. Found: \n" +
336-
Base64.encodeToString(signature, 0));
337-
}
338336
return false;
339337
}
340338

@@ -351,8 +349,16 @@ public void onLoadChildren(final String parentMediaId,
351349
List<MediaBrowser.MediaItem> mediaItems = new ArrayList<MediaBrowser.MediaItem>();
352350

353351
MediaMetadata data = new MediaMetadata.Builder()
354-
.putString( MediaMetadata.METADATA_KEY_TITLE, "Media Title" )
355-
.putString( MediaMetadata.METADATA_KEY_MEDIA_ID, "ID12345").build();
352+
.putString(MediaMetadata.METADATA_KEY_TITLE, "Gallows Pole")
353+
.putString( MediaMetadata.METADATA_KEY_ART_URI, "http://upload.wikimedia.org/wikipedia/en/archive/2/26/20141106002529!Led_Zeppelin_-_Led_Zeppelin_IV.jpg" )
354+
.putString( MediaMetadata.METADATA_KEY_ALBUM_ART_URI, "http://upload.wikimedia.org/wikipedia/en/archive/2/26/20141106002529!Led_Zeppelin_-_Led_Zeppelin_IV.jpg" )
355+
.putString(MediaMetadata.METADATA_KEY_ALBUM, "Led Zeppelin IV")
356+
.putString(MediaMetadata.METADATA_KEY_ARTIST, "Led Zeppelin")
357+
.putLong(MediaMetadata.METADATA_KEY_DURATION, 430000)
358+
.putString(MediaMetadata.METADATA_KEY_GENRE, "Rock")
359+
.putLong(MediaMetadata.METADATA_KEY_TRACK_NUMBER, 1)
360+
.putLong(MediaMetadata.METADATA_KEY_NUM_TRACKS, 8)
361+
.putString(MediaMetadata.METADATA_KEY_MEDIA_ID, "ID12345").build();
356362

357363

358364
mediaItems.add( new MediaBrowser.MediaItem(
@@ -366,27 +372,6 @@ public void onDestroy() {
366372
mSession.release();
367373
}
368374

369-
@Override
370-
public void onAudioFocusChange(int focusChange) {
371-
Log.e( "AutoMediaBrowserService", "onAudioFocusChange" );
372-
}
373-
374-
@Override
375-
public void onCompletion(MediaPlayer mp) {
376-
Log.e( "AutoMediaBrowserService", "onCompletion" );
377-
}
378-
379-
@Override
380-
public boolean onError(MediaPlayer mp, int what, int extra) {
381-
Log.e( "AutoMediaBrowserService", "onError" );
382-
return false;
383-
}
384-
385-
@Override
386-
public void onPrepared(MediaPlayer mp) {
387-
Log.e( "AutoMediaBrowserService", "onPrepared" );
388-
}
389-
390375
private static byte[] extractKey(String keyString) {
391376
try {
392377
return keyString.getBytes("ISO-8859-1");

0 commit comments

Comments
 (0)