keepass2android/src/Kp2aBusinessLogic/ProgressTask.cs

82 lines
2.3 KiB
C#
Raw Normal View History

2013-02-23 11:43:42 -05:00
/*
This file is part of Keepass2Android, Copyright 2013 Philipp Crocoll. This file is based on Keepassdroid, Copyright Brian Pellin.
Keepass2Android is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
Keepass2Android is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Keepass2Android. If not, see <http://www.gnu.org/licenses/>.
*/
using Android.App;
using Android.Content;
using Android.OS;
using Java.Lang;
namespace keepass2android
{
/// <summary>
/// Class to run a task while a progress dialog is shown
/// </summary>
2013-02-23 11:43:42 -05:00
public class ProgressTask {
private readonly Handler _handler;
private readonly RunnableOnFinish _task;
private readonly ProgressDialog _progressDialog;
private readonly IKp2aApp _app;
public ProgressTask(IKp2aApp app, Context ctx, RunnableOnFinish task, UiStringKey messageKey) {
_task = task;
_handler = new Handler();
_app = app;
2013-02-23 11:43:42 -05:00
// Show process dialog
_progressDialog = new ProgressDialog(ctx);
_progressDialog.SetTitle(_app.GetResourceString(UiStringKey.progress_title));
_progressDialog.SetMessage(_app.GetResourceString(messageKey));
2013-02-23 11:43:42 -05:00
// Set code to run when this is finished
_task.SetStatus(new UpdateStatus(_app, _handler, _progressDialog));
_task.OnFinishToRun = new AfterTask(task.OnFinishToRun, _handler, _progressDialog);
2013-02-23 11:43:42 -05:00
}
public void Run() {
2013-02-23 11:43:42 -05:00
// Show process dialog
_progressDialog.Show();
2013-02-23 11:43:42 -05:00
// Start Thread to Run task
Thread t = new Thread(_task.Run);
2013-02-23 11:43:42 -05:00
t.Start();
}
private class AfterTask : OnFinish {
readonly ProgressDialog _progressDialog;
2013-02-23 11:43:42 -05:00
public AfterTask (OnFinish finish, Handler handler, ProgressDialog pd): base(finish, handler)
{
_progressDialog = pd;
2013-02-23 11:43:42 -05:00
}
public override void Run() {
base.Run();
2013-02-23 11:43:42 -05:00
// Remove the progress dialog
Handler.Post(delegate {_progressDialog.Dismiss();});
2013-02-23 11:43:42 -05:00
}
}
}
}