Skip to content

Commit

Permalink
Toast rather than crash when sharing giant traces (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
stkent authored and pedrovgs committed Apr 20, 2017
1 parent 5d1c89f commit 641619f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
37 changes: 32 additions & 5 deletions lynx/src/main/java/com/github/pedrovgs/lynx/LynxView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -121,7 +123,9 @@ public void updateFilterTraceLevel(TraceLevel level) {
public void onShareButtonClicked() {
List<Trace> tracesToShare = new LinkedList<Trace>(traceBuffer.getTraces());
String plainTraces = generatePlainTracesToShare(tracesToShare);
view.shareTraces(plainTraces);
if (!view.shareTraces(plainTraces)) {
view.notifyShareTracesFailed();
}
}

/**
Expand Down Expand Up @@ -212,7 +216,9 @@ public interface View {

void clear();

void shareTraces(String plainTraces);
@CheckResult boolean shareTraces(String plainTraces);

void notifyShareTracesFailed();

void disableAutoScroll();

Expand Down

0 comments on commit 641619f

Please sign in to comment.