Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Merge branch 'Develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Norne9 committed Dec 30, 2022
2 parents bafeeb5 + 9ecdcf7 commit 08e1b9f
Show file tree
Hide file tree
Showing 37 changed files with 3,763 additions and 7 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
**/.DS_Store
*.meta

5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog

## v1.0.0

- The first version of the package
7 changes: 7 additions & 0 deletions CHANGELOG.md.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 59 additions & 0 deletions Documentation~/com.norne.anrwatchdog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# ANR Watchdog

## Watchdog

This package will allow you to reduce the number of ANRs of your application.
This is achieved by using a watchdog timer which will restart the application in case something goes wrong.

### Watchdog.Timeout

You can change the timeout after which your app will restart. The ANR detection timeout in Android is 10 seconds.
Therefore, I recommend keeping it below this value. The default value is 9 seconds.

##### Example:

```csharp
using Norne;
using UnityEngine;

public class ChangeTimeout: MonoBehaviour
{
private void Start()
{
// Set timeout to 8 seconds
Watchdog.Timeout = 8f;
}
}
```

### Watchdog.TryGetStacktrace(out string stacktrace)

Allows you to get the stack trace of an application if it has terminated on a watchdog timer.

| Name | Description |
| ---- | ----------- |
| stacktrace | *System.String*<br>Stack trace of the main java thread of an application. In JSON format. |

#### Returns

True in case an application has terminated on a watchdog timer. False otherwise.

##### Example:

```csharp
using Norne;
using UnityEngine;

public class SendStacktrace: MonoBehaviour
{
private void Start()
{
// Check if we got an ANR the last time we ran the application
if (Watchdog.TryGetStacktrace(out var stacktrace))
{
// If we did, send it to our analytics service
Analytics.Send("ANR", stacktrace);
}
}
}
```
7 changes: 7 additions & 0 deletions LICENSE.md.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# ANR Watchdog

This package will allow you to reduce the number of ANRs of your application. This is achieved by using a watchdog timer
which will restart the application in case something goes wrong.

## How to use

Just add the package to your project and you are ready to go!

## Installing package

Open package manager and select "Add package from git URL..."

![image](https://user-images.githubusercontent.com/4660466/210089474-7c064036-38b1-4908-948a-8c22d0cc8f23.png)

Enter package url:

```
https://github.com/Norne9/Unity-ANR-Watchdog.git
```

And click "Add"

## Configuration

You can change the timeout after which your app will restart. The ANR detection timeout in Android is 10 seconds.
Therefore, I recommend keeping it below this value. The default value is 9 seconds.

Example:

```csharp
using Norne;
using UnityEngine;

public class ChangeTimeout: MonoBehaviour
{
private void Start()
{
// Set timeout to 8 seconds
Watchdog.Timeout = 8f;
}
}
```

## Analytics

Using this package prevents ANR data from being automatically sent to Google Play. If you are interested in this data
you can still receive it and send it to any analytics service.

Example code:

```csharp
using Norne;
using UnityEngine;

public class SendStacktrace: MonoBehaviour
{
private void Start()
{
// Check if we got an ANR the last time we ran the application
if (Watchdog.TryGetStacktrace(out var stacktrace))
{
// If we did, send it to our analytics service
Analytics.Send("ANR", stacktrace);
}
}
}
```
7 changes: 7 additions & 0 deletions README.md.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Runtime.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Runtime/IWatchdog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Norne
{
internal interface IWatchdog
{
public float Timeout { get; set; }
public void StartWatchdog(float timeout);
public string GetStacktrace();
}
}
3 changes: 3 additions & 0 deletions Runtime/IWatchdog.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Runtime/Implementations.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions Runtime/Implementations/AndroidWatchdog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#if UNITY_ANDROID
using UnityEngine;

namespace Norne.Implementations
{
internal class AndroidWatchdog : IWatchdog
{
private float _timeout;
private AndroidJavaObject _watchdog;

public float Timeout
{
get => _timeout;
set
{
if (_watchdog == null) return;

_watchdog.Call("setTimeout", (long) (value * 1000f));
_timeout = value;
}
}

public void StartWatchdog(float timeout)
{
_timeout = timeout;

using var activityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
using var activityContext = activityClass.GetStatic<AndroidJavaObject>("currentActivity");

_watchdog = new AndroidJavaObject("com.norne.anrwatchdog.Watchdog");
_watchdog.Call("setContext", activityContext);
_watchdog.Call("startWatchdog", (long) (timeout * 1000f));
}

public string GetStacktrace()
{
return _watchdog.Call<string>("getStacktrace");
}
}
}
#endif
3 changes: 3 additions & 0 deletions Runtime/Implementations/AndroidWatchdog.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions Runtime/Implementations/DefaultWatchdog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using UnityEngine;

namespace Norne.Implementations
{
internal class DefaultWatchdog : IWatchdog
{
private float _timeout;

public float Timeout
{
get => _timeout;
set
{
Debug.Log($"[Watchdog] Timeout set to {_timeout:F2}s");
_timeout = value;
}
}

public void StartWatchdog(float timeout)
{
Timeout = timeout;

Debug.Log("[Watchdog] ANR Watchdog started.");
}

public string GetStacktrace()
{
return string.Empty;
}
}
}
3 changes: 3 additions & 0 deletions Runtime/Implementations/DefaultWatchdog.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions Runtime/Norne.ANRWatchdog.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "Norne.ANRWatchdog",
"rootNamespace": "",
"references": [],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
7 changes: 7 additions & 0 deletions Runtime/Norne.ANRWatchdog.asmdef.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Runtime/Plugins.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Runtime/Plugins/Android.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Runtime/Plugins/Android/ANRWatchdog-release.aar
Binary file not shown.
Loading

0 comments on commit 08e1b9f

Please sign in to comment.