/
MainApp.cs
141 lines (123 loc) · 4.77 KB
/
MainApp.cs
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
///
/// WF.Player.Android - A Wherigo Player for Android, which use the Wherigo Foundation Core.
/// Copyright (C) 2012-2014 Dirk Weltz <mail@wfplayer.com>
///
/// This program is free software: you can redistribute it and/or modify
/// it under the terms of the GNU Lesser General Public License as
/// published by the Free Software Foundation, either version 3 of the
/// License, or (at your option) any later version.
///
/// This program 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 Lesser General Public License for more details.
///
/// You should have received a copy of the GNU Lesser General Public License
/// along with this program. If not, see <http://www.gnu.org/licenses/>.
///
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using Android.Preferences;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Locations;
using Vernacular;
//using Com.TestFlightApp.Lib;
using WF.Player.Core;
using WF.Player.Core.Live;
using WF.Player.Location;
using WF.Player.Preferences;
using Android.Hardware;
namespace WF.Player
{
[Application(Debuggable=true)]
public class MainApp : Application
{
static Cartridges cartridges;
public MainApp(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
// Catch unhandled exceptions
// Found at http://xandroid4net.blogspot.de/2013/11/how-to-capture-unhandled-exceptions.html
// Add an exception handler for all uncaught exceptions.
AndroidEnvironment.UnhandledExceptionRaiser += AndroidUnhandledExceptionHandler;
AppDomain.CurrentDomain.UnhandledException += ApplicationUnhandledExceptionHandler;
// Save prefernces instance
Main.Prefs = new PreferenceValues(PreferenceManager.GetDefaultSharedPreferences(this));
// Get path from preferences or default path
string path = Main.Prefs.GetString("filepath", Path.Combine(global::Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, "WF.Player"));
try {
if (!Directory.Exists (path))
Directory.CreateDirectory (path);
}
catch {
}
if (!Directory.Exists (path))
{
AlertDialog.Builder builder = new AlertDialog.Builder (this);
builder.SetTitle (GetString (Resource.String.main_error));
builder.SetMessage(String.Format(GetString(Resource.String.main_error_directory_not_found), path));
builder.SetCancelable (true);
builder.SetNeutralButton(Resource.String.ok,(obj,arg) => { });
builder.Show ();
} else {
Main.Path = path;
Main.Prefs.SetString("filepath", path);
}
}
public static Cartridges Cartridges {
get {
return cartridges;
}
set {
cartridges = value;
}
}
#region Events
protected override void Dispose(bool disposing)
{
// Remove the exception handler.
AndroidEnvironment.UnhandledExceptionRaiser -= AndroidUnhandledExceptionHandler;
AppDomain.CurrentDomain.UnhandledException -= ApplicationUnhandledExceptionHandler;
base.Dispose(disposing);
}
void AndroidUnhandledExceptionHandler(object sender, RaiseThrowableEventArgs e)
{
// When the UI Thread crashes this is the code that will be executed. There is no context at this point
// and no way to recover from the exception. This is where you would capture the error and log it to a
// file for example. You might be able to post to a web handler, I have not tried that.
//
// You can access the information about the exception in the args.Exception object.
// Send the Exception to HockeyApp
HockeyApp.ManagedExceptionHandler.SaveException (e.Exception);
}
void ApplicationUnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs e)
{
// When a background thread crashes this is the code that will be executed. You can
// recover from this.
// You might for example:
// _CurrentActivity.RunOnUiThread(() => Toast.MakeText(_CurrentActivity, "Unhadled Exception was thrown", ToastLength.Short).Show());
//
// or
//
// _CurrentActivity.StartActivity(typeof(SomeClass));
// _CurrentActivity.Finish();
//
// It is up to the developer as to what he/she wants to do here.
//
// If you are requiring a minimum version less than API 14, you would have to set _CurrentActivity in each time
// a different activity is brought to the foreground.
// Send the Exception to TestFlight.
// TestFlight.SendCrash(e.ToString());
throw new System.Exception(e.ToString());
}
#endregion
}
}