From 97ce240a271bb156a0cb84f4b6b5ea3bf84da1dc Mon Sep 17 00:00:00 2001 From: Xin Chen Date: Mon, 7 Feb 2022 15:25:21 -0800 Subject: [PATCH] Update RedBoxDialog to separate content view and dialog Summary: This diff separates the content view creation logic from existing `RedBoxDialog` logic. After the change, `RedBoxDialog` is no longer a subclass of `Dialog`, but behaves like a dialog with forwarding pattern to delegate dialog API to internal member. This will keep the APIs consistent with dependent components. The motivation of the change is to make the content view reusable. This is important in VR surface where we don't have activities and necessary implementations for Dialog to show. Changelog: [Android][Internal] Reviewed By: javache Differential Revision: D34016503 fbshipit-source-id: 261594bda9f6fb2d83764a1e5ec2e9e60d8d39a3 --- .../react/devsupport/RedBoxDialog.java | 56 +++++++++++++------ 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/devsupport/RedBoxDialog.java b/ReactAndroid/src/main/java/com/facebook/react/devsupport/RedBoxDialog.java index ad549755b0d..b4827f6bb28 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/devsupport/RedBoxDialog.java +++ b/ReactAndroid/src/main/java/com/facebook/react/devsupport/RedBoxDialog.java @@ -42,12 +42,15 @@ import okhttp3.RequestBody; import org.json.JSONObject; /** Dialog for displaying JS errors in an eye-catching form (red box). */ -/* package */ class RedBoxDialog extends Dialog implements AdapterView.OnItemClickListener { +/* package */ class RedBoxDialog implements AdapterView.OnItemClickListener { private final DevSupportManager mDevSupportManager; private final DoubleTapReloadRecognizer mDoubleTapReloadRecognizer; private final @Nullable RedBoxHandler mRedBoxHandler; + private final View mContentView; + private final Context mContext; + private @Nullable Dialog mDialog; private ListView mStackView; private Button mReloadJsButton; private Button mDismissButton; @@ -238,20 +241,17 @@ import org.json.JSONObject; protected RedBoxDialog( Context context, DevSupportManager devSupportManager, @Nullable RedBoxHandler redBoxHandler) { - super(context, R.style.Theme_Catalyst_RedBox); - - requestWindowFeature(Window.FEATURE_NO_TITLE); - - setContentView(R.layout.redbox_view); + mContext = context; + mContentView = (View) LayoutInflater.from(context).inflate(R.layout.redbox_view, null); mDevSupportManager = devSupportManager; mDoubleTapReloadRecognizer = new DoubleTapReloadRecognizer(); mRedBoxHandler = redBoxHandler; - mStackView = (ListView) findViewById(R.id.rn_redbox_stack); + mStackView = (ListView) mContentView.findViewById(R.id.rn_redbox_stack); mStackView.setOnItemClickListener(this); - mReloadJsButton = (Button) findViewById(R.id.rn_redbox_reload_button); + mReloadJsButton = (Button) mContentView.findViewById(R.id.rn_redbox_reload_button); mReloadJsButton.setOnClickListener( new View.OnClickListener() { @Override @@ -259,7 +259,7 @@ import org.json.JSONObject; mDevSupportManager.handleReloadJS(); } }); - mDismissButton = (Button) findViewById(R.id.rn_redbox_dismiss_button); + mDismissButton = (Button) mContentView.findViewById(R.id.rn_redbox_dismiss_button); mDismissButton.setOnClickListener( new View.OnClickListener() { @Override @@ -269,12 +269,12 @@ import org.json.JSONObject; }); if (mRedBoxHandler != null && mRedBoxHandler.isReportEnabled()) { - mLoadingIndicator = (ProgressBar) findViewById(R.id.rn_redbox_loading_indicator); - mLineSeparator = (View) findViewById(R.id.rn_redbox_line_separator); - mReportTextView = (TextView) findViewById(R.id.rn_redbox_report_label); + mLoadingIndicator = (ProgressBar) mContentView.findViewById(R.id.rn_redbox_loading_indicator); + mLineSeparator = (View) mContentView.findViewById(R.id.rn_redbox_line_separator); + mReportTextView = (TextView) mContentView.findViewById(R.id.rn_redbox_report_label); mReportTextView.setMovementMethod(LinkMovementMethod.getInstance()); mReportTextView.setHighlightColor(Color.TRANSPARENT); - mReportButton = (Button) findViewById(R.id.rn_redbox_report_button); + mReportButton = (Button) mContentView.findViewById(R.id.rn_redbox_report_button); mReportButton.setOnClickListener(mReportButtonOnClickListener); } } @@ -303,15 +303,39 @@ import org.json.JSONObject; AsyncTask.THREAD_POOL_EXECUTOR, (StackFrame) mStackView.getAdapter().getItem(position)); } - @Override public boolean onKeyUp(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_MENU) { mDevSupportManager.showDevOptionsDialog(); return true; } - if (mDoubleTapReloadRecognizer.didDoubleTapR(keyCode, getCurrentFocus())) { + if (mDoubleTapReloadRecognizer.didDoubleTapR(keyCode, mDialog.getCurrentFocus())) { mDevSupportManager.handleReloadJS(); } - return super.onKeyUp(keyCode, event); + return mDialog.onKeyUp(keyCode, event); + } + + public Context getContext() { + return mContext; + } + + public View getContentView() { + return mContentView; + } + + public boolean isShowing() { + return mDialog.isShowing(); + } + + public void show() { + if (mDialog == null) { + mDialog = new Dialog(mContext, R.style.Theme_Catalyst_RedBox); + mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); + } + mDialog.setContentView(mContentView); + mDialog.show(); + } + + public void dismiss() { + mDialog.dismiss(); } }