From 641619f7c1d8a0f51b96666d14e8aa56023fc2f3 Mon Sep 17 00:00:00 2001 From: Stuart Kent Date: Thu, 20 Apr 2017 09:34:44 -0500 Subject: [PATCH] Toast rather than crash when sharing giant traces (#31) --- .../com/github/pedrovgs/lynx/LynxView.java | 37 ++++++++++++++++--- .../lynx/presenter/LynxPresenter.java | 10 ++++- 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/lynx/src/main/java/com/github/pedrovgs/lynx/LynxView.java b/lynx/src/main/java/com/github/pedrovgs/lynx/LynxView.java index 2b062bc..41364b6 100644 --- a/lynx/src/main/java/com/github/pedrovgs/lynx/LynxView.java +++ b/lynx/src/main/java/com/github/pedrovgs/lynx/LynxView.java @@ -19,6 +19,7 @@ import android.content.Context; import android.content.Intent; import android.content.res.TypedArray; +import android.support.annotation.CheckResult; import android.text.Editable; import android.text.TextUtils; import android.text.TextWatcher; @@ -35,6 +36,8 @@ import android.widget.RelativeLayout; import android.widget.Spinner; import android.widget.TextView; +import android.widget.Toast; + import com.github.pedrovgs.lynx.model.AndroidMainThread; import com.github.pedrovgs.lynx.model.Logcat; import com.github.pedrovgs.lynx.model.Lynx; @@ -45,6 +48,7 @@ import com.github.pedrovgs.lynx.renderer.TraceRendererBuilder; import com.pedrogomez.renderers.RendererAdapter; import com.pedrogomez.renderers.RendererBuilder; + import java.lang.reflect.Field; import java.util.List; @@ -181,11 +185,27 @@ public LynxConfig getLynxConfig() { * Uses an intent to share content and given one String with all the information related to the * List of traces shares this information with other applications. */ - @Override public void shareTraces(String plainTraces) { - Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); - sharingIntent.setType(SHARE_INTENT_TYPE); - sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, plainTraces); - getContext().startActivity(Intent.createChooser(sharingIntent, SHARE_INTENT_TITLE)); + @CheckResult @Override public boolean shareTraces(String fullTraces) { + try { + shareTracesInternal(fullTraces); + return true; + } catch (RuntimeException exception1) { // Likely cause is a TransactionTooLargeException on API levels 15+. + try { + /* + * Limit trace size to between 100kB and 400kB, since Unicode characters can be 1-4 bytes each. + */ + int fullTracesLength = fullTraces.length(); + String truncatedTraces = fullTraces.substring(Math.max(0, fullTracesLength - 100000), fullTracesLength); + shareTracesInternal(truncatedTraces); + return true; + } catch (RuntimeException exception2) { // Likely cause is a TransactionTooLargeException on API levels 15+. + return false; + } + } + } + + @Override public void notifyShareTracesFailed() { + Toast.makeText(getContext(), "Share failed", Toast.LENGTH_SHORT).show(); } @Override public void disableAutoScroll() { @@ -378,6 +398,13 @@ private void updateScrollPosition(int removedTraces) { } } + private void shareTracesInternal(final String plainTraces) { + Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); + sharingIntent.setType(SHARE_INTENT_TYPE); + sharingIntent.putExtra(Intent.EXTRA_TEXT, plainTraces); + getContext().startActivity(Intent.createChooser(sharingIntent, SHARE_INTENT_TITLE)); + } + /** * Backdoor used to replace the presenter used in this view. This method should be used just for * testing purposes. diff --git a/lynx/src/main/java/com/github/pedrovgs/lynx/presenter/LynxPresenter.java b/lynx/src/main/java/com/github/pedrovgs/lynx/presenter/LynxPresenter.java index 0808150..ef509a3 100644 --- a/lynx/src/main/java/com/github/pedrovgs/lynx/presenter/LynxPresenter.java +++ b/lynx/src/main/java/com/github/pedrovgs/lynx/presenter/LynxPresenter.java @@ -16,6 +16,8 @@ package com.github.pedrovgs.lynx.presenter; +import android.support.annotation.CheckResult; + import com.github.pedrovgs.lynx.LynxConfig; import com.github.pedrovgs.lynx.model.Lynx; import com.github.pedrovgs.lynx.model.Trace; @@ -121,7 +123,9 @@ public void updateFilterTraceLevel(TraceLevel level) { public void onShareButtonClicked() { List tracesToShare = new LinkedList(traceBuffer.getTraces()); String plainTraces = generatePlainTracesToShare(tracesToShare); - view.shareTraces(plainTraces); + if (!view.shareTraces(plainTraces)) { + view.notifyShareTracesFailed(); + } } /** @@ -212,7 +216,9 @@ public interface View { void clear(); - void shareTraces(String plainTraces); + @CheckResult boolean shareTraces(String plainTraces); + + void notifyShareTracesFailed(); void disableAutoScroll();