Skip to content

Instantly share code, notes, and snippets.

@DevJohnC
Created February 1, 2014 04:25
Show Gist options
  • Select an option

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

Select an option

Save DevJohnC/8747928 to your computer and use it in GitHub Desktop.
Basic AdjutantOS android add-on that browses and plays videos on a selected computer.
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
{
private List<string> _items = new List<string>();
private ArrayAdapter<string> _adapter;
private Dtos.BrowseItem[] _browseItems;
private List<string> _browsePath = new List<string>();
protected async override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
_adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, _items);
ListAdapter = _adapter;
AddonClient.Server.Connected += async (sender, e) => await DirectoryListing();
AddonClient.Server.Disconnected += async (sender, e) => await DirectoryListing();
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()
{
if (!AddonClient.Server.IsConnected)
{
_items.Clear();
_items.Add("Not connected");
SyncUI();
return;
}
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);
}
SyncUI();
}
catch(Exception ex)
{
ShowToast("An error occured while listing directory");
Logger.Write(LogLevel.Warning, "Error while listing directory: {0}", ex);
}
}
private void SyncUI()
{
RunOnUiThread(delegate {
_adapter.Clear();
foreach(var item in _items)
_adapter.Add(item);
});
}
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