You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
202 lines
7.5 KiB
C#
202 lines
7.5 KiB
C#
using System;
|
|
using Android.App;
|
|
using Android.OS;
|
|
using Android.Views;
|
|
using Android.Widget;
|
|
using Android.Content;
|
|
using Android.Support.V7.App;
|
|
using Toolbar = Android.Support.V7.Widget.Toolbar;
|
|
using GridLayoutManager = Android.Support.V7.Widget.GridLayoutManager;
|
|
using Android.Runtime;
|
|
|
|
namespace QuickPlay
|
|
{
|
|
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
|
|
public class MainActivity : AppCompatActivity
|
|
{
|
|
private AppConfiguration appConfig;
|
|
private Android.Support.V7.Widget.RecyclerView recyclerView;
|
|
private IPlayer currentPlayer;
|
|
|
|
protected override async void OnCreate(Bundle savedInstanceState)
|
|
{
|
|
base.OnCreate(savedInstanceState);
|
|
|
|
|
|
// App initialization
|
|
appConfig = AppConfiguration.loadConfiguration();
|
|
var playerConfig = await appConfig.GetPlayerConfig();
|
|
currentPlayer = playerConfig.GetPlayer();
|
|
|
|
// UI initialization
|
|
SetContentView(Resource.Layout.activity_main);
|
|
|
|
// UI Toolbar initialization
|
|
var toolbar = FindViewById<Toolbar>(Resource.Id.toolbar);
|
|
SetSupportActionBar(toolbar);
|
|
SupportActionBar.Title = "My Toolbar";
|
|
|
|
// Hide the play bar by default
|
|
var bar = FindViewById(Resource.Id.currentSongBar);
|
|
bar.Visibility = ViewStates.Invisible;
|
|
|
|
// Initialize the RecyclerView
|
|
// Since this is rather complicated, it is in a separate method
|
|
InitializeRecyclerView();
|
|
|
|
// I don't know why, but this needs to be here in order to show correct colors and player name.
|
|
OnPlayerUpdate();
|
|
}
|
|
|
|
private void InitializeRecyclerView()
|
|
{
|
|
recyclerView = FindViewById<Android.Support.V7.Widget.RecyclerView>(Resource.Id.recyclerView1);
|
|
var layoutStrategy = new LexicographicLayoutStrategy();
|
|
var adapter = new SongRecyclerAdapter(currentPlayer, layoutStrategy);
|
|
adapter.SongClick += OnSongClick;
|
|
recyclerView.SetAdapter(adapter);
|
|
var layoutManager = new GridLayoutManager(this, 2, GridLayoutManager.Vertical, false);
|
|
recyclerView.SetLayoutManager(layoutManager);
|
|
}
|
|
|
|
public override bool OnCreateOptionsMenu(IMenu menu)
|
|
{
|
|
MenuInflater.Inflate(Resource.Menu.menu_main, menu);
|
|
return base.OnCreateOptionsMenu(menu);
|
|
}
|
|
|
|
public override bool OnOptionsItemSelected(IMenuItem item)
|
|
{
|
|
if (item.ItemId == Resource.Id.action_settings)
|
|
{
|
|
// Set player config URL
|
|
var b = new Android.Support.V7.App.AlertDialog.Builder(this);
|
|
b.SetTitle("Player config URL");
|
|
var input = new EditText(this);
|
|
input.Text = appConfig.playerConfigUrl;
|
|
b.SetView(input);
|
|
b.SetPositiveButton("Set", delegate
|
|
{
|
|
string text = input.Text;
|
|
appConfig.playerConfigUrl = text;
|
|
appConfig.saveConfiguration();
|
|
Toast.MakeText(this, "Configuration saved, reloading", ToastLength.Short).Show();
|
|
var i = new Intent(this, typeof(MainActivity));
|
|
StartActivity(i);
|
|
});
|
|
b.SetNegativeButton("Scan QR", delegate {
|
|
try
|
|
{
|
|
var i = new Intent("com.google.zxing.client.android.SCAN");
|
|
StartActivityForResult(i, 0);
|
|
} catch (ActivityNotFoundException) {
|
|
Toast.MakeText(this, "You need ZXing Barcode scanner for this", ToastLength.Long).Show();
|
|
}
|
|
});
|
|
b.SetCancelable(true);
|
|
|
|
b.Show();
|
|
}
|
|
|
|
if (item.ItemId == Resource.Id.action_edit)
|
|
{
|
|
// Hide the play bar
|
|
var bar = FindViewById(Resource.Id.currentSongBar);
|
|
bar.Visibility = ViewStates.Invisible;
|
|
}
|
|
return base.OnOptionsItemSelected(item);
|
|
}
|
|
|
|
/// <summary>
|
|
/// A callback for all player updates.
|
|
///
|
|
/// The update should be easy on resources, so no need to have separate
|
|
/// partial updates. This simply performs full update of the activity
|
|
/// state and views.
|
|
/// </summary>
|
|
public void OnPlayerUpdate()
|
|
{
|
|
TextView playerName = FindViewById<TextView>(Resource.Id.playerNameText);
|
|
playerName.Text = currentPlayer.PlayerName;
|
|
var bar = FindViewById(Resource.Id.currentSongBar);
|
|
switch (currentPlayer.Status)
|
|
{
|
|
case IPlayer.PlayerStatus.Disconnected:
|
|
Toolbar tb = FindViewById<Toolbar>(Resource.Id.toolbar);
|
|
tb.SetBackgroundColor(Android.Graphics.Color.Red);
|
|
bar.Visibility = ViewStates.Invisible;
|
|
break;
|
|
case IPlayer.PlayerStatus.Playing:
|
|
ResetToolbarColor();
|
|
// Show progress bar
|
|
bar.Visibility = ViewStates.Visible;
|
|
break;
|
|
case IPlayer.PlayerStatus.Stopped:
|
|
ResetToolbarColor();
|
|
bar.Visibility = ViewStates.Invisible;
|
|
break;
|
|
}
|
|
}
|
|
private void ResetToolbarColor()
|
|
{
|
|
Toolbar tb = FindViewById<Toolbar>(Resource.Id.toolbar);
|
|
tb.SetBackgroundColor(
|
|
new Android.Graphics.Color(
|
|
Android.Support.V4.Content.ContextCompat.GetColor(this, Resource.Color.colorPrimary)
|
|
)
|
|
);
|
|
}
|
|
|
|
public async void OnSongClick(object sender, IPlayable song)
|
|
{
|
|
await currentPlayer.Play(song);
|
|
}
|
|
|
|
protected new async void OnResume()
|
|
{
|
|
base.OnResume();
|
|
|
|
// Reconnect the player
|
|
try
|
|
{
|
|
await currentPlayer.ConnectAsync();
|
|
} catch (CannotConnectException e)
|
|
{
|
|
//TODO: View a toast with details and change some colors?
|
|
var t = Toast.MakeText(this, e.Message + ": " + e.InnerException.Message ?? "", ToastLength.Long);
|
|
t.Show();
|
|
}
|
|
|
|
// Refresh player info
|
|
OnPlayerUpdate();
|
|
}
|
|
protected new void OnDestroy()
|
|
{
|
|
base.OnDestroy();
|
|
|
|
this.appConfig.saveConfiguration();
|
|
}
|
|
|
|
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
|
|
{
|
|
base.OnActivityResult(requestCode, resultCode, data);
|
|
if (requestCode == 0)
|
|
{
|
|
if (resultCode == Result.Ok)
|
|
{
|
|
string url = data.GetStringExtra("SCAN_RESULT");
|
|
var cfg = new AppConfiguration { playerConfigUrl = url };
|
|
cfg.saveConfiguration();
|
|
Toast.MakeText(this, "Configuration saved, reloading", ToastLength.Short).Show();
|
|
var i = new Intent(this, typeof(MainActivity));
|
|
StartActivity(i);
|
|
}
|
|
else if (resultCode == Result.Canceled)
|
|
{
|
|
Toast.MakeText(this, "Could not load QR code", ToastLength.Short).Show();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|