Skip to content

Instantly share code, notes, and snippets.

@DevJohnC
Created January 31, 2014 07:41
Show Gist options
  • Select an option

  • Save DevJohnC/8727978 to your computer and use it in GitHub Desktop.

Select an option

Save DevJohnC/8727978 to your computer and use it in GitHub Desktop.
Video browsing and playback android app
using System;
using Android.App;
using Android.Content;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using FragLabs.AdjutantOS.API;
using FragLabs.AdjutantOS.API.Addons;
using FragLabs.AdjutantOS.API.Devices;
using FragLabs.AdjutantOS.API.Log;
namespace FragLabs.AdjutantOS.Apps.Video
{
[Activity (Label = "AdjutantOS Video", MainLauncher = true)]
public class MainActivity : ListActivity
{
public static AddonClient AddonClient { get; private set; }
private List<string> _items = new List<string>();
private ArrayAdapter<string> _adapter;
private Dtos.BrowseItem[] _browseItems;
private List<string> _browsePath = new List<string>();
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
_adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, _items);
ListAdapter = _adapter;
// todo: move service auto-start into library
var serviceRunning = false;
var activityManager = GetSystemService(Context.ActivityService) as ActivityManager;
foreach(var service in activityManager.GetRunningServices(Int32.MaxValue))
{
if (service.Service.ClassName.ToLower() == "fraglabs.adjutantos.client.adjutantservice")
{
serviceRunning = true;
}
}
if (!serviceRunning)
{
var intent = new Intent("com.adjutantos.AdjutantService");
intent.SetPackage("AdjutantTouch.Android");
intent.SetClassName("AdjutantTouch.Android", "fraglabs.adjutantos.client.AdjutantService");
var component = StartService(intent);
}
if (AddonClient == null)
{
Logger.Init(Console.Out);
Logger.Write(LogLevel.Debug, "Starting Addon");
AddonClient.Bootstrap();
AddonClient = new AddonClient();
AddonClient.Connect();
AddonClient.ConnectComplete += async (s,a) =>
{
await DirectoryListing();
};
}
}
protected override async void OnListItemClick (ListView l, View v, int position, long id)
{
base.OnListItemClick (l, v, position, id);
var item = _browseItems[position];
if (item.Type == Dtos.BrowseItemType.Directory || item.Type == Dtos.BrowseItemType.Mount)
{
_browsePath.Add(item.Path);
await DirectoryListing();
}
else if (item.Type == Dtos.BrowseItemType.File)
{
var intent = new Intent(this, typeof(ChooseDevice));
intent.PutExtra("filename", item.Path);
StartActivityForResult(intent, 1);
}
}
protected async override void OnActivityResult (int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult (requestCode, resultCode, data);
if (requestCode == 1)
{
if (resultCode == Result.Ok)
{
var filename = data.GetStringExtra("filename") ?? "";
var deviceIdStr = data.GetStringExtra("computerId") ?? "";
var deviceId = Guid.Parse(deviceIdStr);
var computer = DeviceRepository.GetSingle<Computer>(deviceId);
Logger.Write(LogLevel.Debug, "Starting playback of {0} on {1}", filename, deviceId);
try
{
var response = await ServiceInvoker.Call<Dtos.PlayResponse>(computer, new Dtos.Play {
Mount = _browsePath[0],
Directory = String.Join("/",_browsePath.Skip(1)),
Filename = filename
});
if (!response.IsPlaying)
{
ShowToast("Failed to play video");
}
} catch(Exception ex) {
Logger.Write(LogLevel.Warning, "Failed to play video: {0}", ex);
ShowToast("An error occured while trying to play video");
}
}
}
}
public override async void OnBackPressed ()
{
if (_browsePath.Count == 0)
{
base.OnBackPressed();
return;
}
_browsePath.RemoveAt(_browsePath.Count - 1);
await DirectoryListing();
}
private async Task DirectoryListing()
{
try
{
var request = new Dtos.Browse{Mount = null, Directory = ""};
if (_browsePath.Count > 0)
{
request.Mount = _browsePath[0];
if (_browsePath.Count > 1)
{
request.Directory = String.Join("/", _browsePath.Skip(1));
}
}
var browseResp = await ServiceInvoker.Call<Dtos.BrowseResponse>(DeviceRepository.Server,
request);
_browseItems = browseResp.Items.OrderBy(t => t.Type).ThenBy(str => str.Name, new NaturalSortComparer<string>()).ToArray();
_items.Clear();
foreach(var item in _browseItems)
{
_items.Add(item.Name);
}
RunOnUiThread(delegate {
_adapter.Clear();
foreach(var item in _items)
_adapter.Add(item);
});
}
catch(Exception ex)
{
}
}
private void ShowToast(string text)
{
RunOnUiThread(delegate {
Toast.MakeText(this, text, ToastLength.Short).Show();
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment