From 71c32db6d379740fd634f5a1aa528fa7c679eb06 Mon Sep 17 00:00:00 2001 From: Darcy Wong Date: Tue, 11 Feb 2020 15:24:57 +0700 Subject: [PATCH] fix(android): Add abaility to reinitialize `CloudDownloadMgr` Fixes crashlytics [crash](https://console.firebase.google.com/u/0/project/kmapro-ee779/crashlytics/app/android:com.tavultesoft.kmapro/issues/ae59e122f0fac1eb6e082ddb21bb6fae?time=last-seven-days&sessionId=5E41A650017400013B04CE17115A1E88_DNE_0_v2) There's an intermittent condition between the system notification bar showing a resource update available, the user closing the Keyman app, and the user later choosing to update the resource. By then, the CloudDownloadMgr may not be available to handle the download. This makes the following change: * When the Keyman app is closed, shutdown the CloudDownloadMgr (without destroying the InApp and System Keyboard). * When preparing to download a resource update, re-initialize the CloudDownloadMgr as needed. --- .../kmea/cloud/CloudDownloadMgr.java | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/android/KMEA/app/src/main/java/com/tavultesoft/kmea/cloud/CloudDownloadMgr.java b/android/KMEA/app/src/main/java/com/tavultesoft/kmea/cloud/CloudDownloadMgr.java index 4fe92dc33c..fc548bd69f 100644 --- a/android/KMEA/app/src/main/java/com/tavultesoft/kmea/cloud/CloudDownloadMgr.java +++ b/android/KMEA/app/src/main/java/com/tavultesoft/kmea/cloud/CloudDownloadMgr.java @@ -7,6 +7,7 @@ import android.content.Intent; import android.content.IntentFilter; import android.net.Uri; import android.util.Log; +import android.widget.Toast; import java.io.File; import java.util.HashMap; @@ -56,7 +57,13 @@ public class CloudDownloadMgr{ { if(isInitialized) return; - aContext.registerReceiver(completeListener,new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); + try { + aContext.registerReceiver(completeListener, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); + } catch (Exception e) { + String message = "CloudDownloadMgr re-initializing"; + Toast.makeText(aContext, message, + Toast.LENGTH_SHORT).show(); + } isInitialized = true; } @@ -68,7 +75,13 @@ public class CloudDownloadMgr{ { if(!isInitialized) return; - aContext.unregisterReceiver(completeListener); + try { + aContext.unregisterReceiver(completeListener); + } catch (Exception e) { + String message = "CloudDownloadMgr shutting down."; + Toast.makeText(aContext, message, + Toast.LENGTH_SHORT).show(); + } isInitialized = false; } @@ -184,8 +197,10 @@ public class CloudDownloadMgr{ ICloudDownloadCallback aCallback, CloudApiTypes.CloudApiParam... params) { - if(!isInitialized) - throw new IllegalStateException("Downloadmanager is not initialize. Call KMManger.initialize before"); + if(!isInitialized) { + Log.w(TAG, "Downloadmanager not initialized. Initializing CloudDownloadMgr."); + initialize(aContext); + } synchronized (downloadSetByDownloadIdentifier) {