-
Notifications
You must be signed in to change notification settings - Fork 473
Expand file tree
/
Copy pathOnOperationFinishedHandler.cs
More file actions
164 lines (139 loc) · 4.81 KB
/
Copy pathOnOperationFinishedHandler.cs
File metadata and controls
164 lines (139 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
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 System;
using Android;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Widget;
using Google.Android.Material.Dialog;
using KeePassLib.Interfaces;
namespace keepass2android
{
public interface IActiveContextProvider
{
Context ActiveContext { get; }
}
public abstract class OnOperationFinishedHandler
{
protected bool Success;
protected String Message;
protected Exception Exception;
protected bool ImportantMessage
{
get;
set;
}
protected Context ActiveContext
{
get
{
return _activeContextProvider?.ActiveContext;
}
}
protected OnOperationFinishedHandler NextOnOperationFinishedHandler;
protected Handler Handler;
private IKp2aStatusLogger _statusLogger = new Kp2aNullStatusLogger(); //default: no logging but not null -> can be used whenever desired
private readonly IActiveContextProvider _activeContextProvider;
public IKp2aStatusLogger StatusLogger
{
get { return _statusLogger; }
set { _statusLogger = value; }
}
protected OnOperationFinishedHandler(IActiveContextProvider activeContextProvider, Handler handler)
{
_activeContextProvider = activeContextProvider;
NextOnOperationFinishedHandler = null;
Handler = handler;
}
protected OnOperationFinishedHandler(IActiveContextProvider activeContextProvider, OnOperationFinishedHandler operationFinishedHandler, Handler handler)
{
_activeContextProvider = activeContextProvider;
NextOnOperationFinishedHandler = operationFinishedHandler;
Handler = handler;
}
protected OnOperationFinishedHandler(IActiveContextProvider activeContextProvider, OnOperationFinishedHandler operationFinishedHandler)
{
_activeContextProvider = activeContextProvider;
NextOnOperationFinishedHandler = operationFinishedHandler;
Handler = null;
}
public void SetResult(bool success, string message, bool importantMessage, Exception exception)
{
Success = success;
Message = message;
ImportantMessage = importantMessage;
Exception = exception;
}
public void SetResult(bool success)
{
Success = success;
}
public virtual void Run()
{
if (NextOnOperationFinishedHandler == null) return;
// Pass on result on call finish
NextOnOperationFinishedHandler.SetResult(Success, Message, ImportantMessage, Exception);
var handler = Handler ?? NextOnOperationFinishedHandler.Handler ?? null;
if (handler != null)
{
handler.Post(() =>
{
NextOnOperationFinishedHandler.Run();
});
}
else
{
NextOnOperationFinishedHandler.Run();
}
}
protected void DisplayMessage(Context ctx)
{
DisplayMessage(ctx, Message, ImportantMessage);
}
public static void DisplayMessage(Context ctx, string message, bool makeDialog)
{
if (String.IsNullOrEmpty(message))
return;
Kp2aLog.Log("OnOperationFinishedHandler message: " + message);
// Showing a dialog or toast must happen on the UI thread, but finish handlers
// can run on a background (save/load) worker thread. Touching Toast/Dialog there
// throws ("Can't toast on a thread that has not called Looper.prepare()"), so
// marshal the actual display to the main looper.
void ShowUi()
{
if (makeDialog && ctx != null)
{
try
{
new MaterialAlertDialogBuilder(ctx)
.SetMessage(message)
.SetPositiveButton(Android.Resource.String.Ok, (sender, args) => ((Dialog)sender).Dismiss())
.Show();
}
catch (Exception)
{
Toast.MakeText(ctx, message, ToastLength.Long).Show();
}
}
else
Toast.MakeText(ctx ?? Application.Context, message, ToastLength.Long).Show();
}
if (Looper.MyLooper() == Looper.MainLooper)
ShowUi();
else
new Handler(Looper.MainLooper).Post(ShowUi);
}
}
}