Skip to content

Commit

Permalink
Add android sample 'BasicMediaRouter'
Browse files Browse the repository at this point in the history
  • Loading branch information
pjcollins committed Aug 15, 2013
1 parent 9733d7d commit 21155dc
Show file tree
Hide file tree
Showing 19 changed files with 618 additions and 0 deletions.
77 changes: 77 additions & 0 deletions BasicMediaRouter/BasicMediaRouter.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>10.0.0</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{ABE6CAA3-B6A9-4907-9049-05CE79D8630C}</ProjectGuid>
<ProjectTypeGuids>{EFBA0AD7-5A72-4C68-AF49-83D382785DCF};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<OutputType>Library</OutputType>
<RootNamespace>BasicMediaRouter</RootNamespace>
<AndroidApplication>True</AndroidApplication>
<AndroidResgenFile>Resources\Resource.designer.cs</AndroidResgenFile>
<AndroidResgenClass>Resource</AndroidResgenClass>
<MonoAndroidResourcePrefix>Resources</MonoAndroidResourcePrefix>
<MonoAndroidAssetsPrefix>Assets</MonoAndroidAssetsPrefix>
<AndroidUseLatestPlatformSdk>False</AndroidUseLatestPlatformSdk>
<AssemblyName>BasicMediaRouter</AssemblyName>
<TargetFrameworkVersion>v4.3</TargetFrameworkVersion>
<AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AndroidLinkMode>None</AndroidLinkMode>
<ConsolePause>false</ConsolePause>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>full</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AndroidUseSharedRuntime>false</AndroidUseSharedRuntime>
<ConsolePause>false</ConsolePause>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Xml" />
<Reference Include="System.Core" />
<Reference Include="Mono.Android" />
</ItemGroup>
<ItemGroup>
<Compile Include="MainActivity.cs" />
<Compile Include="Resources\Resource.designer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SamplePresentation.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Properties\AndroidManifest.xml" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\layout\Main.axml" />
<AndroidResource Include="Resources\values\Strings.xml" />
<AndroidResource Include="Resources\menu\main.xml" />
<AndroidResource Include="Resources\values\colors.xml" />
<AndroidResource Include="Resources\values\styles.xml" />
<AndroidResource Include="Resources\layout\display.axml" />
<AndroidResource Include="Resources\drawable-hdpi\icon.png" />
<AndroidResource Include="Resources\drawable-mdpi\icon.png" />
<AndroidResource Include="Resources\drawable-xhdpi\icon.png" />
<AndroidResource Include="Resources\drawable-xxhdpi\icon.png" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Novell\Novell.MonoDroid.CSharp.targets" />
<ItemGroup>
<Folder Include="Resources\menu\" />
<Folder Include="Resources\drawable-hdpi\" />
<Folder Include="Resources\drawable-xhdpi\" />
<Folder Include="Resources\drawable-xxhdpi\" />
<Folder Include="Resources\drawable-mdpi\" />
</ItemGroup>
</Project>
20 changes: 20 additions & 0 deletions BasicMediaRouter/BasicMediaRouter.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BasicMediaRouter", "BasicMediaRouter.csproj", "{ABE6CAA3-B6A9-4907-9049-05CE79D8630C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{ABE6CAA3-B6A9-4907-9049-05CE79D8630C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ABE6CAA3-B6A9-4907-9049-05CE79D8630C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ABE6CAA3-B6A9-4907-9049-05CE79D8630C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ABE6CAA3-B6A9-4907-9049-05CE79D8630C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
StartupItem = BasicMediaRouter.csproj
EndGlobalSection
EndGlobal
256 changes: 256 additions & 0 deletions BasicMediaRouter/MainActivity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
/*
* Copyright 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

/**
* <p>
* This sample demonstrates the use of the MediaRouter API to show content on a
* secondary display using a {@link Presentation}.
* </p>
* <p>
* The activity uses the {@link MediaRouter} API to automatically detect when a
* presentation display is available and to allow the user to control the media
* routes using a menu item provided by the {@link MediaRouteActionProvider}.
* When a presentation display is available a {@link Presentation} (implemented
* as a {@link SamplePresentation}) is shown on the preferred display. A button
* toggles the background color of the secondary screen to show the interaction
* between the primary and secondary screens.
* </p>
* <p>
* This sample requires an HDMI or Wifi display. Alternatively, the
* "Simulate secondary displays" feature in Development Settings can be enabled
* to simulate secondary displays.
* </p>
*
* @see Presentation
* @see MediaRouter
*/
using Android.Media;

namespace BasicMediaRouter
{
[Activity (Label = "BasicMediaRouter", MainLauncher = true, Theme="@style/AppTheme")]
public class MainActivity : Activity
{
MediaRouter mMediaRouter;
MediaRouterCallback mMediaRouterCallback;

// Active Presentation, set to null if no secondary screen is enabled
SamplePresentation mPresentation;

// Views used to display status information on the primary screen
TextView mTextStatus;
Button mButton;

// selected color index
int mColor = 0;

// background colors
int[] mColors;

protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView (Resource.Layout.Main);

mMediaRouterCallback = new MediaRouterCallback (this);

mTextStatus = FindViewById <TextView> (Resource.Id.textStatus);

// get the list of background colors
mColors = Resources.GetIntArray (Resource.Array.androidcolors);

// Enable clicks on the 'change color' button
mButton = FindViewById <Button> (Resource.Id.button1);
mButton.Click += delegate {
ShowNextColor();
};

// Get the MediaRouter service
mMediaRouter = (MediaRouter) GetSystemService (Context.MediaRouterService);
}

protected override void OnResume ()
{
base.OnResume ();

// Register a callback for all events related to live video devices
mMediaRouter.AddCallback (MediaRouteType.LiveVideo, mMediaRouterCallback);

// Show the 'Not connected' status message
mButton.Enabled = false;
mTextStatus.SetText (Resource.String.secondary_notconnected);

// Update the displays based on the currently active routes
UpdatePresentation ();
}

protected override void OnPause ()
{
base.OnPause ();

// Stop listening for changes to media routes.
mMediaRouter.RemoveCallback (mMediaRouterCallback);
}

protected override void OnStop ()
{
base.OnStop ();

// Dismiss the presentation when the activity is not visible.
if (mPresentation != null) {
mPresentation.Dismiss ();
mPresentation = null;
}
}

/**
* Inflates the ActionBar or options menu. The menu file defines an item for
* the {@link MediaRouteActionProvider}, which is registered here for all
* live video devices using {@link MediaRouter#ROUTE_TYPE_LIVE_VIDEO}.
*/
public override bool OnCreateOptionsMenu (IMenu menu)
{
base.OnCreateOptionsMenu (menu);

MenuInflater.Inflate (Resource.Menu.main, menu);

// Configure the media router action provider
var mediaRouteMenuItem = menu.FindItem (Resource.Id.menu_media_route);
MediaRouteActionProvider mediaRouteActionProvider = (MediaRouteActionProvider) mediaRouteMenuItem.ActionProvider;
mediaRouteActionProvider.SetRouteTypes (MediaRouteType.LiveVideo);

return true;
}

void ShowNextColor()
{
if (mPresentation != null) {
// a second screen is active and initialized, show the next color
mPresentation.SetColor (mColors [mColor]);
mColor = (mColor + 1) % mColors.Length;
}
}

/**
* Updates the displayed presentation to enable a secondary screen if it has
* been selected in the {@link MediaRouter} for the
* {@link MediaRouter#ROUTE_TYPE_LIVE_VIDEO} type. If no screen has been
* selected by the {@link MediaRouter}, the current screen is disabled.
* Otherwise a new {@link SamplePresentation} is initialized and shown on
* the secondary screen.
*/
public void UpdatePresentation ()
{
// Get the selected route for live video
var selectedRoute = mMediaRouter.GetSelectedRoute (MediaRouteType.LiveVideo);

// Get its Display if a valid route has been selected
Display selectedDisplay = null;
if (selectedRoute != null)
selectedDisplay = selectedRoute.PresentationDisplay;

/*
* Dismiss the current presentation if the display has changed or no new
* route has been selected
*/
if (mPresentation != null && mPresentation.Display != selectedDisplay) {
mPresentation.Dismiss ();
mPresentation = null;
mButton.Enabled = false;
mTextStatus.SetText (Resource.String.secondary_notconnected);
}

/*
* Show a new presentation if the previous one has been dismissed and a
* route has been selected.
*/
if (mPresentation == null && selectedDisplay != null) {

// Initialise a new Presentation for the Display
mPresentation = new SamplePresentation (this, selectedDisplay);
mPresentation.DismissEvent += delegate (object sender, EventArgs e) {
if (sender == mPresentation) {
mPresentation = null;
}
};

// Try to show the presentation, this might fail if the display has
// gone away in the mean time
try {
mPresentation.Show ();
mTextStatus.Text = Resources.GetString (Resource.String.secondary_connected, selectedRoute.Name);
mButton.Enabled = true;
ShowNextColor ();
} catch (WindowManagerInvalidDisplayException ex) {
// Couldn't show presentation - display was already removed
mPresentation = null;
Console.WriteLine (ex);
}
}
}
}

/**
* Implementing a {@link MediaRouter.Callback} to update the displayed
* {@link Presentation} when a route is selected, unselected or the
* presentation display has changed. The provided stub implementation
* {@link MediaRouter.SimpleCallback} is extended and only
* {@link MediaRouter.SimpleCallback#onRouteSelected(MediaRouter, int, RouteInfo)}
* ,
* {@link MediaRouter.SimpleCallback#onRouteUnselected(MediaRouter, int, RouteInfo)}
* and
* {@link MediaRouter.SimpleCallback#onRoutePresentationDisplayChanged(MediaRouter, RouteInfo)}
* are overridden to update the displayed {@link Presentation} in
* {@link #updatePresentation()}. These callbacks enable or disable the
* second screen presentation based on the routing provided by the
* {@link MediaRouter} for {@link MediaRouter#ROUTE_TYPE_LIVE_VIDEO}
* streams. @
*/
class MediaRouterCallback : MediaRouter.SimpleCallback
{
MainActivity main;

public MediaRouterCallback (MainActivity m)
{
main = m;
}

public override void OnRouteSelected (MediaRouter router, MediaRouteType type, MediaRouter.RouteInfo info)
{
main.UpdatePresentation ();
}

public override void OnRouteUnselected (MediaRouter router, MediaRouteType type, MediaRouter.RouteInfo info)
{
main.UpdatePresentation ();
}

public override void OnRoutePresentationDisplayChanged (MediaRouter router, MediaRouter.RouteInfo info)
{
main.UpdatePresentation ();
}
}
}


6 changes: 6 additions & 0 deletions BasicMediaRouter/Properties/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.xamarin.basicmediarouter">
<uses-sdk android:minSdkVersion="16" />
<application android:label="BasicMediaRouter" android:icon="@drawable/icon">
</application>
</manifest>
23 changes: 23 additions & 0 deletions BasicMediaRouter/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using Android.App;

// Information about this assembly is defined by the following attributes.
// Change them to the values specific to your project.
[assembly: AssemblyTitle ("BasicMediaRouter")]
[assembly: AssemblyDescription ("")]
[assembly: AssemblyConfiguration ("")]
[assembly: AssemblyCompany ("")]
[assembly: AssemblyProduct ("")]
[assembly: AssemblyCopyright ("pjcollins")]
[assembly: AssemblyTrademark ("")]
[assembly: AssemblyCulture ("")]
// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
[assembly: AssemblyVersion ("1.0.0")]
// The following attributes are used to specify the signing key for the assembly,
// if desired. See the Mono documentation for more information about signing.
//[assembly: AssemblyDelaySign(false)]
//[assembly: AssemblyKeyFile("")]

Loading

0 comments on commit 21155dc

Please sign in to comment.