From 9d0b76d611df063d9e48b2a637968aecadf82df4 Mon Sep 17 00:00:00 2001
From: Akinwale Ariwodola <akinwale@gmail.com>
Date: Mon, 22 Oct 2018 09:53:20 +0100
Subject: [PATCH 1/2] added react native error handler package

---
 app/package-lock.json                                     | 5 +++++
 app/package.json                                          | 1 +
 .../lbry/build/gradle/wrapper/gradle-wrapper.properties   | 2 +-
 .../bootstraps/lbry/build/templates/build.tmpl.gradle     | 8 ++++++++
 .../bootstraps/lbry/build/templates/settings.tmpl.gradle  | 2 ++
 5 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/app/package-lock.json b/app/package-lock.json
index dac133e4..d15ffda6 100644
--- a/app/package-lock.json
+++ b/app/package-lock.json
@@ -5272,6 +5272,11 @@
                 "react-native-drawer-layout": "1.3.2"
             }
         },
+        "react-native-exception-handler": {
+            "version": "2.10.0",
+            "resolved": "https://registry.npmjs.org/react-native-exception-handler/-/react-native-exception-handler-2.10.0.tgz",
+            "integrity": "sha512-MJLE1NxGFlsdoOk3jBXUMzceZyVuLLXGWjK406ABH5+mfFjbIjmxAAEm/pkvzD8E5QXNo1chUSzpwtq2wCjygA=="
+        },
         "react-native-fast-image": {
             "version": "5.0.3",
             "resolved": "https://registry.npmjs.org/react-native-fast-image/-/react-native-fast-image-5.0.3.tgz",
diff --git a/app/package.json b/app/package.json
index 3c61ce67..0584bc0a 100644
--- a/app/package.json
+++ b/app/package.json
@@ -13,6 +13,7 @@
         "react": "16.2.0",
         "react-native": "0.55.3",
         "react-native-country-picker-modal": "^0.6.2",
+        "react-native-exception-handler": "2.10.0",
         "react-native-fast-image": "^5.0.3",
         "react-native-fetch-blob": "^0.10.8",
         "react-native-image-zoom-viewer": "^2.2.5",
diff --git a/p4a/pythonforandroid/bootstraps/lbry/build/gradle/wrapper/gradle-wrapper.properties b/p4a/pythonforandroid/bootstraps/lbry/build/gradle/wrapper/gradle-wrapper.properties
index ac1799fa..8733ff37 100644
--- a/p4a/pythonforandroid/bootstraps/lbry/build/gradle/wrapper/gradle-wrapper.properties
+++ b/p4a/pythonforandroid/bootstraps/lbry/build/gradle/wrapper/gradle-wrapper.properties
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
 distributionPath=wrapper/dists
 zipStoreBase=GRADLE_USER_HOME
 zipStorePath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
diff --git a/p4a/pythonforandroid/bootstraps/lbry/build/templates/build.tmpl.gradle b/p4a/pythonforandroid/bootstraps/lbry/build/templates/build.tmpl.gradle
index 5d8b782c..008bfcf3 100644
--- a/p4a/pythonforandroid/bootstraps/lbry/build/templates/build.tmpl.gradle
+++ b/p4a/pythonforandroid/bootstraps/lbry/build/templates/build.tmpl.gradle
@@ -72,6 +72,13 @@ android {
     }
 }
 
+ext {
+    compileSdkVersion = {{ android_api }}
+    buildToolsVersion = '{{ build_tools_version }}'
+    minSdkVersion = {{ args.min_sdk_version }}
+	targetSdkVersion = {{ android_api }}
+}
+
 subprojects {
     afterEvaluate {project ->
         if (project.hasProperty("android")) {
@@ -84,6 +91,7 @@ subprojects {
 }
 
 dependencies {
+    compile project(':react-native-exception-handler')
     compile project(':react-native-fast-image')
     compile project(':react-native-fetch-blob')
 	compile project(':react-native-video')
diff --git a/p4a/pythonforandroid/bootstraps/lbry/build/templates/settings.tmpl.gradle b/p4a/pythonforandroid/bootstraps/lbry/build/templates/settings.tmpl.gradle
index aeb014f7..4f1d2181 100644
--- a/p4a/pythonforandroid/bootstraps/lbry/build/templates/settings.tmpl.gradle
+++ b/p4a/pythonforandroid/bootstraps/lbry/build/templates/settings.tmpl.gradle
@@ -1,4 +1,6 @@
 rootProject.name = 'browser'
+include ':react-native-exception-handler'
+project(':react-native-exception-handler').projectDir = new File(rootProject.projectDir, './react/node_modules/react-native-exception-handler/android')
 include ':react-native-fast-image'
 project(':react-native-fast-image').projectDir = new File(rootProject.projectDir, './react/node_modules/react-native-fast-image/android')
 include ':react-native-fetch-blob'
-- 
2.45.3


From 543222c171e429f17b56fb7390634d5ea0ad9256 Mon Sep 17 00:00:00 2001
From: Akinwale Ariwodola <akinwale@gmail.com>
Date: Sun, 28 Oct 2018 22:32:55 +0100
Subject: [PATCH 2/2] implement Mixpanel error tracking

---
 app/package-lock.json                         |  6 +--
 app/package.json                              |  2 +-
 app/src/index.js                              |  9 +++++
 recipes/pyjnius/__init__.py                   |  5 +--
 .../browser/reactmodules/MixpanelModule.java  | 37 +++++++++++++++++--
 5 files changed, 48 insertions(+), 11 deletions(-)

diff --git a/app/package-lock.json b/app/package-lock.json
index d15ffda6..ddf5ec02 100644
--- a/app/package-lock.json
+++ b/app/package-lock.json
@@ -5273,9 +5273,9 @@
             }
         },
         "react-native-exception-handler": {
-            "version": "2.10.0",
-            "resolved": "https://registry.npmjs.org/react-native-exception-handler/-/react-native-exception-handler-2.10.0.tgz",
-            "integrity": "sha512-MJLE1NxGFlsdoOk3jBXUMzceZyVuLLXGWjK406ABH5+mfFjbIjmxAAEm/pkvzD8E5QXNo1chUSzpwtq2wCjygA=="
+            "version": "2.9.0",
+            "resolved": "https://registry.npmjs.org/react-native-exception-handler/-/react-native-exception-handler-2.9.0.tgz",
+            "integrity": "sha512-XRHhGH5aM4lSenX4zZBa07JaszJGXeF8cv1KY314Q4qJWOihKWLpkdvwqwsBieZ2iy8DPhdAVioQzw8JLD/Okw=="
         },
         "react-native-fast-image": {
             "version": "5.0.3",
diff --git a/app/package.json b/app/package.json
index 0584bc0a..3e0a86e2 100644
--- a/app/package.json
+++ b/app/package.json
@@ -13,7 +13,7 @@
         "react": "16.2.0",
         "react-native": "0.55.3",
         "react-native-country-picker-modal": "^0.6.2",
-        "react-native-exception-handler": "2.10.0",
+        "react-native-exception-handler": "2.9.0",
         "react-native-fast-image": "^5.0.3",
         "react-native-fetch-blob": "^0.10.8",
         "react-native-image-zoom-viewer": "^2.2.5",
diff --git a/app/src/index.js b/app/src/index.js
index 94012017..a01895ff 100644
--- a/app/src/index.js
+++ b/app/src/index.js
@@ -1,4 +1,5 @@
 import React from 'react';
+import { setJSExceptionHandler } from 'react-native-exception-handler';
 import { Provider, connect } from 'react-redux';
 import {
   AppRegistry,
@@ -31,6 +32,14 @@ import moment from 'moment';
 import settingsReducer from './redux/reducers/settings';
 import thunk from 'redux-thunk';
 
+const globalExceptionHandler = (error, isFatal) => {
+  if (error && NativeModules.Mixpanel) {
+    NativeModules.Mixpanel.logException(isFatal, error.message ? error.message : "No message", error);
+  }
+};
+setJSExceptionHandler(globalExceptionHandler, true);
+
+
 function isFunction(object) {
   return typeof object === 'function';
 }
diff --git a/recipes/pyjnius/__init__.py b/recipes/pyjnius/__init__.py
index 90d63a86..331f4810 100644
--- a/recipes/pyjnius/__init__.py
+++ b/recipes/pyjnius/__init__.py
@@ -9,11 +9,10 @@ class PyjniusRecipe(CythonRecipe):
     version = '1.1.3'
     url = 'https://github.com/kivy/pyjnius/archive/{version}.zip'
     name = 'pyjnius'
-    depends = [('python2', 'python3crystax'), ('genericndkbuild', 'sdl2', 'sdl'), 'six']
+    depends = [('python2', 'python3crystax'), 'genericndkbuild', 'six']
     site_packages_name = 'jnius'
 
-    patches = [('sdl2_jnienv_getter.patch', will_build('sdl2')),
-               ('genericndkbuild_jnienv_getter.patch', will_build('genericndkbuild'))]
+    patches = [('genericndkbuild_jnienv_getter.patch', will_build('genericndkbuild'))]
 
     def postbuild_arch(self, arch):
         super(PyjniusRecipe, self).postbuild_arch(arch)
diff --git a/src/main/java/io/lbry/browser/reactmodules/MixpanelModule.java b/src/main/java/io/lbry/browser/reactmodules/MixpanelModule.java
index 33d9d8e9..57874d14 100644
--- a/src/main/java/io/lbry/browser/reactmodules/MixpanelModule.java
+++ b/src/main/java/io/lbry/browser/reactmodules/MixpanelModule.java
@@ -1,6 +1,7 @@
 package io.lbry.browser.reactmodules;
 
 import android.content.Context;
+import android.widget.Toast;
 
 import com.facebook.react.bridge.ReactApplicationContext;
 import com.facebook.react.bridge.ReactContextBaseJavaModule;
@@ -23,7 +24,7 @@ public class MixpanelModule extends ReactContextBaseJavaModule {
         "93b81fb957cb0ddcd3198c10853a6a95"; /* Production */
 
     private Context context;
-    
+
     private MixpanelAPI mixpanel;
 
     public MixpanelModule(ReactApplicationContext reactContext) {
@@ -36,7 +37,7 @@ public class MixpanelModule extends ReactContextBaseJavaModule {
     public String getName() {
         return "Mixpanel";
     }
-    
+
     @ReactMethod
     public void track(String name, ReadableMap payload) {
         JSONObject props = new JSONObject();
@@ -48,11 +49,39 @@ public class MixpanelModule extends ReactContextBaseJavaModule {
                 }
             }
         } catch (JSONException e) {
-            // Cannot use props. Stick with empty props.    
+            // Cannot use props. Stick with empty props.
         }
-        
+
         if (mixpanel != null) {
             mixpanel.track(name, props);
         }
     }
+
+    @ReactMethod
+    public void logException(boolean fatal, String message, ReadableMap payload) {
+        JSONObject props = new JSONObject();
+        try {
+            props.put("Message", message);
+            if (payload != null) {
+                HashMap<String, Object> payloadMap = payload.toHashMap();
+                for (Map.Entry<String, Object> entry : payloadMap.entrySet()) {
+                    props.put(entry.getKey(), entry.getValue());
+                }
+            }
+        } catch (JSONException e) {
+            // Cannot use props. Stick with empty props.
+        }
+
+        if (mixpanel != null) {
+            mixpanel.track(fatal ? "Exception" : "Warning", props);
+        }
+
+        if (fatal) {
+            Toast.makeText(context,
+                           "An application error occurred which has been automatically logged. " +
+                           "If you keep seeing this message, please provide feedback to the LBRY " +
+                           "team by emailing hello@lbry.io.",
+                            Toast.LENGTH_LONG).show();
+        }
+    }
 }
-- 
2.45.3