Play-/Source/ui_android/java/com/virtualapplications/play/VirtualMachineManager.java

111 lines
2.4 KiB
Java
Raw Permalink Normal View History

package com.virtualapplications.play;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
2015-11-22 19:00:17 -05:00
public class VirtualMachineManager
{
public interface OnGameLaunchCompleteListener
{
void onComplete();
}
private static final class GameLauncher extends AsyncTask<Void, Void, Void>
2015-11-22 19:00:17 -05:00
{
private ProgressDialog progDialog;
final Context _ctx;
final String _bootablePath;
final OnGameLaunchCompleteListener _completeListener;
Exception _exception;
public GameLauncher(Context ctx, String bootablePath, OnGameLaunchCompleteListener completeListener)
{
_ctx = ctx;
_bootablePath = bootablePath;
_completeListener = completeListener;
}
protected void onPreExecute()
{
progDialog = ProgressDialog.show(_ctx,
_ctx.getString(R.string.launch_game),
_ctx.getString(R.string.launch_game_msg), true);
}
@Override
protected Void doInBackground(Void... params)
2015-11-22 19:00:17 -05:00
{
try
{
if(BootablesInterop.IsBootableExecutablePath(_bootablePath))
{
NativeInterop.loadElf(_bootablePath);
}
else
{
NativeInterop.bootDiskImage(_bootablePath);
}
Intent intent = new Intent(_ctx, EmulatorActivity.class);
_ctx.startActivity(intent);
}
catch(Exception e)
{
_exception = e;
}
return null;
2015-11-22 19:00:17 -05:00
}
@Override
protected void onPostExecute(Void result)
{
if(progDialog != null && progDialog.isShowing())
{
try
{
progDialog.dismiss();
}
catch(final Exception e)
{
//We don't really care if we get an exception while dismissing
}
}
if(_exception != null)
{
displaySimpleMessage("Error", _exception.getMessage());
}
else
{
if(_completeListener != null)
{
_completeListener.onComplete();
}
}
}
private void displaySimpleMessage(String title, String message)
2015-11-22 19:00:17 -05:00
{
new AlertDialog.Builder(_ctx)
.setTitle(title)
.setMessage(message)
.setPositiveButton(android.R.string.ok, null)
.setOnDismissListener(dialogInterface -> {
if(_completeListener != null)
{
_completeListener.onComplete();
}
})
.create()
.show();
2015-11-22 19:00:17 -05:00
}
}
public static void launchGame(Context ctx, String bootablePath, OnGameLaunchCompleteListener completeListener)
{
GameLauncher launcher = new GameLauncher(ctx, bootablePath, completeListener);
launcher.execute();
2015-11-22 19:00:17 -05:00
}
}