From 093ab1fe5732f7177c278cd6484f24c41b805cc9 Mon Sep 17 00:00:00 2001 From: Pascal Hartig Date: Thu, 15 Nov 2018 09:46:55 -0800 Subject: [PATCH] Add report bug callback for diagnostics screen Summary: Adds an optional interface that, when implemented, adds a "Report Bug" button to the top of the screen. This is a weird Android-ism that's sadly still the recommended way of providing hooks into Fragments: https://developer.android.com/guide/components/fragments#EventCallbacks In order to keep things vanilla here, let's follow that pattern. Reviewed By: jknoxville Differential Revision: D13045578 fbshipit-source-id: 1506fcfc630c2d1183d7d1eaf470d23954e0c097 --- .../FlipperDiagnosticFragment.java | 44 ++++++++++++++++--- .../FlipperDiagnosticReportListener.java | 25 +++++++++++ 2 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 android/src/main/java/com/facebook/flipper/android/diagnostics/FlipperDiagnosticReportListener.java diff --git a/android/src/main/java/com/facebook/flipper/android/diagnostics/FlipperDiagnosticFragment.java b/android/src/main/java/com/facebook/flipper/android/diagnostics/FlipperDiagnosticFragment.java index 2caddc55c..51084b0ca 100644 --- a/android/src/main/java/com/facebook/flipper/android/diagnostics/FlipperDiagnosticFragment.java +++ b/android/src/main/java/com/facebook/flipper/android/diagnostics/FlipperDiagnosticFragment.java @@ -7,6 +7,7 @@ */ package com.facebook.flipper.android.diagnostics; +import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; import android.os.Bundle; @@ -16,6 +17,7 @@ import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.Button; import android.widget.LinearLayout; import android.widget.ScrollView; import android.widget.TextView; @@ -27,15 +29,27 @@ import com.facebook.flipper.core.StateSummary.StateElement; public class FlipperDiagnosticFragment extends Fragment implements FlipperStateUpdateListener { - private TextView mSummaryView; - private TextView mLogView; - private ScrollView mScrollView; + TextView mSummaryView; + TextView mLogView; + ScrollView mScrollView; + Button mReportButton; + + @Nullable FlipperDiagnosticReportListener mReportCallback; + + private final View.OnClickListener mOnBugReportClickListener = + new View.OnClickListener() { + @Override + public void onClick(View v) { + mReportCallback.report( + AndroidFlipperClient.getInstance(getContext()).getState(), getSummary()); + } + }; public static FlipperDiagnosticFragment newInstance() { - final FlipperDiagnosticFragment fragment = new FlipperDiagnosticFragment(); - return fragment; + return new FlipperDiagnosticFragment(); } + @SuppressLint("SetTextI18n") @Nullable @Override public View onCreateView( @@ -46,10 +60,18 @@ public class FlipperDiagnosticFragment extends Fragment implements FlipperStateU final LinearLayout root = new LinearLayout(getContext()); root.setOrientation(LinearLayout.VERTICAL); + if (mReportCallback != null) { + mReportButton = new Button(getContext()); + mReportButton.setText("Report Bug"); + mReportButton.setOnClickListener(mOnBugReportClickListener); + } mSummaryView = new TextView(getContext()); mLogView = new TextView(getContext()); mScrollView = new ScrollView(getContext()); mScrollView.addView(mLogView); + if (mReportButton != null) { + root.addView(mReportButton); + } root.addView(mSummaryView); root.addView(mScrollView); return root; @@ -90,7 +112,7 @@ public class FlipperDiagnosticFragment extends Fragment implements FlipperStateU } } - private String getSummary() { + String getSummary() { final Context context = getContext(); final StateSummary summary = AndroidFlipperClient.getInstance(context).getStateSummary(); final StringBuilder stateText = new StringBuilder(); @@ -115,9 +137,19 @@ public class FlipperDiagnosticFragment extends Fragment implements FlipperStateU return stateText.toString(); } + @Override public void onStop() { super.onStop(); final FlipperClient client = AndroidFlipperClient.getInstance(getContext()); client.unsubscribe(); } + + @Override + public void onAttach(Context context) { + super.onAttach(context); + + if (context instanceof FlipperDiagnosticReportListener) { + mReportCallback = (FlipperDiagnosticReportListener) context; + } + } } diff --git a/android/src/main/java/com/facebook/flipper/android/diagnostics/FlipperDiagnosticReportListener.java b/android/src/main/java/com/facebook/flipper/android/diagnostics/FlipperDiagnosticReportListener.java new file mode 100644 index 000000000..4c4fc1265 --- /dev/null +++ b/android/src/main/java/com/facebook/flipper/android/diagnostics/FlipperDiagnosticReportListener.java @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2018-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the LICENSE + * file in the root directory of this source tree. + * + */ +package com.facebook.flipper.android.diagnostics; + +import com.facebook.flipper.core.FlipperClient; + +/** + * Implement this interface on your activity hosting {@link FlipperDiagnosticFragment} to enable the + * "Report Bug" button and receive a callback for it. + */ +public interface FlipperDiagnosticReportListener { + + /** + * Called when a bug report is requested, including the flipper diagnostic information. + * + * @param state See {@link FlipperClient#getState()} + * @param summary See {@link FlipperClient#getStateSummary()} + */ + void report(String state, String summary); +}