Bug 1148149 - Support Android Presentation API. r=snorp, r=jgilbert
This commit is contained in:
@@ -5,6 +5,9 @@
|
||||
|
||||
package org.mozilla.gecko;
|
||||
|
||||
import android.app.Presentation;
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v7.media.MediaControlIntent;
|
||||
@@ -12,6 +15,13 @@ import android.support.v7.media.MediaRouteSelector;
|
||||
import android.support.v7.media.MediaRouter;
|
||||
import android.support.v7.media.MediaRouter.RouteInfo;
|
||||
import android.util.Log;
|
||||
import android.view.Display;
|
||||
import android.view.Surface;
|
||||
import android.view.SurfaceHolder;
|
||||
import android.view.SurfaceView;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.ViewGroup.LayoutParams;
|
||||
import android.view.WindowManager;
|
||||
|
||||
import com.google.android.gms.cast.CastMediaControlIntent;
|
||||
|
||||
@@ -61,6 +71,7 @@ public class MediaPlayerManager extends Fragment implements NativeEventListener
|
||||
|
||||
private MediaRouter mediaRouter = null;
|
||||
private final Map<String, GeckoMediaPlayer> displays = new HashMap<String, GeckoMediaPlayer>();
|
||||
private GeckoPresentation presentation = null;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
@@ -135,19 +146,23 @@ public class MediaPlayerManager extends Fragment implements NativeEventListener
|
||||
displays.remove(route.getId());
|
||||
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent(
|
||||
"MediaPlayer:Removed", route.getId()));
|
||||
updatePresentation();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public void onRouteSelected(MediaRouter router, int type, MediaRouter.RouteInfo route) {
|
||||
updatePresentation();
|
||||
}
|
||||
|
||||
// These methods aren't used by the support version Media Router
|
||||
@SuppressWarnings("unused")
|
||||
public void onRouteUnselected(MediaRouter router, int type, RouteInfo route) {
|
||||
updatePresentation();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRoutePresentationDisplayChanged(MediaRouter router, RouteInfo route) {
|
||||
updatePresentation();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -159,6 +174,7 @@ public class MediaPlayerManager extends Fragment implements NativeEventListener
|
||||
debug("onRouteAdded: route=" + route);
|
||||
final GeckoMediaPlayer display = getMediaPlayerForRoute(route);
|
||||
saveAndNotifyOfDisplay("MediaPlayer:Added", route, display);
|
||||
updatePresentation();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -166,6 +182,7 @@ public class MediaPlayerManager extends Fragment implements NativeEventListener
|
||||
debug("onRouteChanged: route=" + route);
|
||||
final GeckoMediaPlayer display = displays.get(route.getId());
|
||||
saveAndNotifyOfDisplay("MediaPlayer:Changed", route, display);
|
||||
updatePresentation();
|
||||
}
|
||||
|
||||
private void saveAndNotifyOfDisplay(final String eventName,
|
||||
@@ -221,4 +238,86 @@ public class MediaPlayerManager extends Fragment implements NativeEventListener
|
||||
.build();
|
||||
mediaRouter.addCallback(selectorBuilder, callback, MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
super.onStop();
|
||||
if (presentation != null) {
|
||||
presentation.dismiss();
|
||||
presentation = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void updatePresentation() {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (mediaRouter == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
MediaRouter.RouteInfo route = mediaRouter.getSelectedRoute();
|
||||
Display display = route != null ? route.getPresentationDisplay() : null;
|
||||
|
||||
if (display != null) {
|
||||
if ((presentation != null) && (presentation.getDisplay() != display)) {
|
||||
presentation.dismiss();
|
||||
presentation = null;
|
||||
}
|
||||
|
||||
if (presentation == null) {
|
||||
presentation = new GeckoPresentation(getActivity(), display);
|
||||
|
||||
try {
|
||||
presentation.show();
|
||||
} catch (WindowManager.InvalidDisplayException ex) {
|
||||
Log.w(LOGTAG, "Couldn't show presentation! Display was removed in "
|
||||
+ "the meantime.", ex);
|
||||
presentation = null;
|
||||
}
|
||||
}
|
||||
} else if (presentation != null) {
|
||||
presentation.dismiss();
|
||||
presentation = null;
|
||||
}
|
||||
}
|
||||
|
||||
private static class SurfaceListener implements SurfaceHolder.Callback {
|
||||
@Override
|
||||
public void surfaceChanged(SurfaceHolder holder, int format, int width,
|
||||
int height) {
|
||||
// Surface changed so force a composite
|
||||
GeckoAppShell.scheduleComposite();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void surfaceCreated(SurfaceHolder holder) {
|
||||
GeckoAppShell.addPresentationSurface(holder.getSurface());
|
||||
GeckoAppShell.scheduleComposite();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||
GeckoAppShell.removePresentationSurface(holder.getSurface());
|
||||
}
|
||||
}
|
||||
|
||||
private final static class GeckoPresentation extends Presentation {
|
||||
private SurfaceView mView;
|
||||
public GeckoPresentation(Context context, Display display) {
|
||||
super(context, display);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
mView = new SurfaceView(getContext());
|
||||
setContentView(mView, new ViewGroup.LayoutParams(
|
||||
ViewGroup.LayoutParams.MATCH_PARENT,
|
||||
ViewGroup.LayoutParams.MATCH_PARENT));
|
||||
mView.getHolder().addCallback(new SurfaceListener());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user