summaryrefslogtreecommitdiff
path: root/src/com/android/car/media/MediaDispatcherActivity.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/com/android/car/media/MediaDispatcherActivity.java')
-rw-r--r--src/com/android/car/media/MediaDispatcherActivity.java64
1 files changed, 42 insertions, 22 deletions
diff --git a/src/com/android/car/media/MediaDispatcherActivity.java b/src/com/android/car/media/MediaDispatcherActivity.java
index 973937b..282cfa5 100644
--- a/src/com/android/car/media/MediaDispatcherActivity.java
+++ b/src/com/android/car/media/MediaDispatcherActivity.java
@@ -1,13 +1,17 @@
package com.android.car.media;
+import static android.car.media.CarMediaIntents.EXTRA_MEDIA_COMPONENT;
import static android.car.media.CarMediaManager.MEDIA_SOURCE_MODE_BROWSE;
import android.car.Car;
+import android.car.media.CarMediaIntents;
import android.content.ComponentName;
import android.content.Intent;
+import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
+import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import com.android.car.media.common.source.MediaSource;
@@ -25,50 +29,60 @@ import java.util.Set;
public class MediaDispatcherActivity extends FragmentActivity {
private static final String TAG = "MediaDispatcherActivity";
+ private static Set<String> sCustomMediaComponents = null;
- private final Set<String> mCustomMediaComponents = new HashSet<>();
+ static boolean isCustomMediaSource(Resources res, @Nullable MediaSource source) {
+ if (sCustomMediaComponents == null) {
+ sCustomMediaComponents = new HashSet<>();
+ sCustomMediaComponents.addAll(
+ Arrays.asList(res.getStringArray(R.array.custom_media_packages)));
+ }
+
+ return (source != null)
+ && sCustomMediaComponents.contains(
+ source.getBrowseServiceComponentName().flattenToString());
+ }
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- mCustomMediaComponents.addAll(
- Arrays.asList(getResources().getStringArray(R.array.custom_media_packages)));
-
Intent intent = getIntent();
- String action = intent != null ? intent.getAction() : null;
+ String action = null;
+ String componentName = null;
+ if (intent != null) {
+ action = intent.getAction();
+ componentName = intent.getStringExtra(EXTRA_MEDIA_COMPONENT);
+ }
- MediaSourceViewModel mediaSrcVM = MediaSourceViewModel.get(getApplication(),
- MEDIA_SOURCE_MODE_BROWSE);
- MediaSource mediaSrc = null;
+ if (Log.isLoggable(TAG, Log.INFO)) {
+ Log.i(TAG, "onCreate action: " + action + " component: " + componentName);
+ }
- if (Car.CAR_INTENT_ACTION_MEDIA_TEMPLATE.equals(action)) {
- String componentName = intent.getStringExtra(Car.CAR_EXTRA_MEDIA_COMPONENT);
+ MediaSource mediaSrc = null;
+ if (CarMediaIntents.ACTION_MEDIA_TEMPLATE.equals(action)) {
if (componentName != null) {
- ComponentName component = ComponentName.unflattenFromString(componentName);
- mediaSrc = MediaSource.create(this, component);
- if (mediaSrc != null) {
- mediaSrcVM.setPrimaryMediaSource(mediaSrc, MEDIA_SOURCE_MODE_BROWSE);
- if (Log.isLoggable(TAG, Log.DEBUG)) {
- Log.d(TAG, "onCreate componentName : " + componentName);
- }
+ ComponentName mediaSrcComp = ComponentName.unflattenFromString(componentName);
+ if (mediaSrcComp != null) {
+ mediaSrc = MediaSource.create(this, mediaSrcComp);
}
}
}
+ // Retrieve the current source if none was set. However, do NOT set it and rely on setting
+ // the EXTRA_MEDIA_COMPONENT on the intent launched below. This avoids source notifications
+ // as well as extra trips back here, all of which would be useless.
if (mediaSrc == null) {
+ MediaSourceViewModel mediaSrcVM = MediaSourceViewModel.get(getApplication(),
+ MEDIA_SOURCE_MODE_BROWSE);
mediaSrc = mediaSrcVM.getPrimaryMediaSource().getValue();
}
Intent newIntent = null;
- if (mediaSrc != null
- && mCustomMediaComponents.contains(
- mediaSrc.getBrowseServiceComponentName().flattenToString())) {
+ if ((mediaSrc != null) && isCustomMediaSource(getResources(), mediaSrc)) {
// Launch custom app (e.g. Radio)
String srcPackage = mediaSrc.getPackageName();
newIntent = getPackageManager().getLaunchIntentForPackage(srcPackage);
- newIntent.putExtra(Car.CAR_EXTRA_MEDIA_COMPONENT,
- mediaSrc.getBrowseServiceComponentName().flattenToString());
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Getting launch intent for package : " + srcPackage + (newIntent != null
? " succeeded" : " failed"));
@@ -79,6 +93,12 @@ public class MediaDispatcherActivity extends FragmentActivity {
newIntent = new Intent(this, MediaActivity.class);
}
+ // Add the selected media source to the intent so the launched activity gets it right away
+ if (mediaSrc != null) {
+ newIntent.putExtra(EXTRA_MEDIA_COMPONENT,
+ mediaSrc.getBrowseServiceComponentName().flattenToString());
+ }
+
newIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(newIntent);
finish();