Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(964)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/media/router/cast/MediaSource.java

Issue 1277883003: [PresentationAPI,Android] Implement the ChromeMediaRouterDialogController. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@media-router-dialog-controller-jni
Patch Set: Corrected Cast URN domain and close the dialog fragment upon dialog dismissal. Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/android/java/src/org/chromium/chrome/browser/media/router/cast/MediaSource.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/media/router/cast/MediaSource.java b/chrome/android/java/src/org/chromium/chrome/browser/media/router/cast/MediaSource.java
new file mode 100644
index 0000000000000000000000000000000000000000..0e1576fb3e5c7d92bffd2d1442aa958de2260e80
--- /dev/null
+++ b/chrome/android/java/src/org/chromium/chrome/browser/media/router/cast/MediaSource.java
@@ -0,0 +1,76 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.chrome.browser.media.router.cast;
+
+import android.support.v7.media.MediaRouteSelector;
+
+import com.google.android.gms.cast.CastMediaControlIntent;
+
+import java.util.Locale;
+
+import javax.annotation.Nullable;
+
+/**
+ * Abstracts parsing the Cast application id and other parameters from the source URN.
+ */
+public class MediaSource {
+ private static final String CAST_SOURCE_URN_PREFIX = "https://google.com/cast#";
+ private static final String CAST_SOURCE_URN_PARAMETER_SEPARATOR = "/";
+ private static final String CAST_SOURCE_URN_APPLICATION_ID_PREFIX = "__castappid__=";
+
+ private String mApplicationId;
+
+ /**
+ * Initializes the media source from its source URN.
+ * @param sourceUrn the source urn for the Cast media source
+ * @return an initialized media source if the URN is valid or null.
+ */
+ @Nullable
+ public static MediaSource from(String sourceUrn) {
+ String applicationId = getCastApplicationId(sourceUrn);
+ if (applicationId == null) return null;
+ return new MediaSource(applicationId);
+ }
+
+ /**
+ * Returns a new {@link MediaRouteSelector} to use for Cast device filtering for this
+ * particular media source.
+ * @return an initialized route selector.
+ */
+ public MediaRouteSelector buildRouteSelector() {
+ return new MediaRouteSelector.Builder()
+ .addControlCategory(CastMediaControlIntent.categoryForCast(mApplicationId))
+ .build();
+ }
+
+ private MediaSource(String applicationId) {
+ mApplicationId = applicationId;
+ }
+
+ /**
+ * @param sourceUrn the URN identifying the media source.
+ * @return the corresponding Cast application id or null.
+ */
+ @Nullable
+ private static String getCastApplicationId(String sourceUrn) {
+ String canonicalSourceUrn = null;
+ if (sourceUrn != null) canonicalSourceUrn = sourceUrn.trim().toLowerCase(Locale.US);
+ if (canonicalSourceUrn == null
+ || !canonicalSourceUrn.startsWith(CAST_SOURCE_URN_PREFIX)) {
+ return null;
+ }
+ String[] parameters = canonicalSourceUrn
+ .substring(CAST_SOURCE_URN_PREFIX.length())
+ .split(CAST_SOURCE_URN_PARAMETER_SEPARATOR);
+ for (String parameter : parameters) {
+ if (parameter.startsWith(CAST_SOURCE_URN_APPLICATION_ID_PREFIX)) {
+ String applicationId = parameter.substring(
+ CAST_SOURCE_URN_APPLICATION_ID_PREFIX.length()).toUpperCase(Locale.US);
+ if (!applicationId.isEmpty()) return applicationId;
+ }
+ }
+ return null;
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698