From 374f7249d3e4a9777ddb674dcb2203cd3fba1f6a Mon Sep 17 00:00:00 2001 From: Manuel Martin Date: Fri, 5 Jul 2019 17:49:46 +0200 Subject: [PATCH] User enum in widget show (#1352) * User enum in widget show Refactored the show methods into a single method that uses a enum for readability. * Fixed Suggestions widget show override --- .../mozilla/vrbrowser/VRBrowserActivity.java | 3 ++- .../vrbrowser/ui/widgets/KeyboardWidget.java | 2 +- .../ui/widgets/NavigationBarWidget.java | 4 ++-- .../ui/widgets/SuggestionsWidget.java | 4 ++-- .../vrbrowser/ui/widgets/TrayWidget.java | 6 ++--- .../vrbrowser/ui/widgets/UIWidget.java | 18 +++++++++------ .../vrbrowser/ui/widgets/WindowWidget.java | 23 +++++++++++-------- .../ui/widgets/dialogs/CrashDialogWidget.java | 4 ++-- .../ui/widgets/dialogs/PermissionWidget.java | 6 ++--- .../ui/widgets/dialogs/VoiceSearchWidget.java | 8 +++---- .../widgets/prompts/ChoicePromptWidget.java | 4 ++-- .../ui/widgets/prompts/PromptWidget.java | 11 ++++----- .../ui/widgets/settings/SettingsWidget.java | 12 +++++----- 13 files changed, 55 insertions(+), 50 deletions(-) diff --git a/app/src/common/shared/org/mozilla/vrbrowser/VRBrowserActivity.java b/app/src/common/shared/org/mozilla/vrbrowser/VRBrowserActivity.java index 257bf215e..26ee34475 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/VRBrowserActivity.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/VRBrowserActivity.java @@ -55,6 +55,7 @@ import org.mozilla.vrbrowser.ui.widgets.TopBarWidget; import org.mozilla.vrbrowser.ui.widgets.TrayListener; import org.mozilla.vrbrowser.ui.widgets.TrayWidget; +import org.mozilla.vrbrowser.ui.widgets.UIWidget; import org.mozilla.vrbrowser.ui.widgets.VideoProjectionMenuWidget; import org.mozilla.vrbrowser.ui.widgets.Widget; import org.mozilla.vrbrowser.ui.widgets.WidgetManagerDelegate; @@ -447,7 +448,7 @@ private void handleCrashIntent(final Intent intent) { mCrashDialog.setCrashDialogDelegate(() -> sendCrashData(intent)); } - mCrashDialog.show(); + mCrashDialog.show(UIWidget.REQUEST_FOCUS); } } diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/KeyboardWidget.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/KeyboardWidget.java index efd4c0633..40073746e 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/KeyboardWidget.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/KeyboardWidget.java @@ -714,7 +714,7 @@ private void handleVoiceInput() { } mIsInVoiceInput = true; TelemetryWrapper.voiceInputEvent(); - mVoiceSearchWidget.show(false); + mVoiceSearchWidget.show(CLEAR_FOCUS); mWidgetPlacement.visible = false; mWidgetManager.updateWidget(this); } diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/NavigationBarWidget.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/NavigationBarWidget.java index 665357d43..008db13b7 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/NavigationBarWidget.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/NavigationBarWidget.java @@ -824,7 +824,7 @@ public void OnVoiceSearchClicked() { mVoiceSearchWidget.hide(REMOVE_WIDGET); } else { - mVoiceSearchWidget.show(); + mVoiceSearchWidget.show(REQUEST_FOCUS); } } @@ -881,7 +881,7 @@ public void OnShowSearchPopup() { if (!mPopup.isVisible()) { mPopup.updatePlacement((int)WidgetPlacement.convertPixelsToDp(getContext(), mURLBar.getWidth())); - mPopup.show(); + mPopup.show(CLEAR_FOCUS); } } ); diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/SuggestionsWidget.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/SuggestionsWidget.java index 60bf3cf46..5bdc35561 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/SuggestionsWidget.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/SuggestionsWidget.java @@ -113,8 +113,8 @@ protected void initializeWidgetPlacement(WidgetPlacement aPlacement) { } @Override - public void show() { - super.show(false); + public void show(@ShowFlags int aShowFlags) { + super.show(aShowFlags); mList.startAnimation(mScaleUpAnimation); } diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/TrayWidget.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/TrayWidget.java index d02abe889..7b99a9deb 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/TrayWidget.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/TrayWidget.java @@ -268,7 +268,7 @@ private void toggleSettingsDialog() { if (widget.isVisible()) { widget.hide(REMOVE_WIDGET); } else { - widget.show(); + widget.show(REQUEST_FOCUS); } } @@ -281,14 +281,14 @@ public void setTrayVisible(boolean aVisible) { private void updateVisibility() { if (mTrayVisible && !mKeyboardVisible) { - this.show(); + this.show(REQUEST_FOCUS); } else { this.hide(UIWidget.KEEP_WIDGET); } } @Override - public void show() { + public void show(@ShowFlags int aShowFlags) { if (!mWidgetPlacement.visible) { mWidgetPlacement.visible = true; mWidgetManager.addWidget(this); diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/UIWidget.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/UIWidget.java index d78cb3dac..c1b51d307 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/UIWidget.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/UIWidget.java @@ -248,24 +248,28 @@ public void toggle() { hide(REMOVE_WIDGET); } else { - show(); + show(REQUEST_FOCUS); } } - public void show() { - show(true); - } + @IntDef(value = { REQUEST_FOCUS, CLEAR_FOCUS }) + public @interface ShowFlags {} + public static final int REQUEST_FOCUS = 0; + public static final int CLEAR_FOCUS = 1; - public void show(boolean focus) { + public void show(@ShowFlags int aShowFlags) { if (!mWidgetPlacement.visible) { mWidgetPlacement.visible = true; mWidgetManager.addWidget(this); mWidgetManager.pushBackHandler(mBackHandler); } - if (focus) { - setFocusableInTouchMode(true); + setFocusableInTouchMode(false); + if (aShowFlags == REQUEST_FOCUS) { requestFocusFromTouch(); + + } else { + clearFocus(); } } diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/WindowWidget.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/WindowWidget.java index a2cd4157b..d2c94a7f9 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/WindowWidget.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/WindowWidget.java @@ -103,16 +103,19 @@ protected void initializeWidgetPlacement(WidgetPlacement aPlacement) { } @Override - public void show(boolean focus) { + public void show(@ShowFlags int aShowFlags) { if (!mWidgetPlacement.visible) { mWidgetPlacement.visible = true; } mWidgetManager.updateWidget(this); - if (focus) { - setFocusableInTouchMode(true); + setFocusableInTouchMode(false); + if (aShowFlags == REQUEST_FOCUS) { requestFocusFromTouch(); + + } else { + clearFocus(); } } @@ -581,7 +584,7 @@ public void setNoInternetToastVisible(boolean aVisible) { mNoInternetToast.mWidgetPlacement.parentHandle = getHandle(); } if (aVisible && !mNoInternetToast.isVisible()) { - mNoInternetToast.show(); + mNoInternetToast.show(REQUEST_FOCUS); } else if (!aVisible && mNoInternetToast.isVisible()) { mNoInternetToast.hide(REMOVE_WIDGET); } @@ -593,7 +596,7 @@ public void showAlert(String title, @NonNull String msg, @NonNull AlertCallback mAlertPrompt.setTitle(title); mAlertPrompt.setMessage(msg); mAlertPrompt.setDelegate(callback); - mAlertPrompt.show(); + mAlertPrompt.show(REQUEST_FOCUS); } // PromptDelegate @@ -605,7 +608,7 @@ public void onAlert(GeckoSession session, String title, String msg, AlertCallbac mAlertPrompt.setTitle(title); mAlertPrompt.setMessage(msg); mAlertPrompt.setDelegate(callback); - mAlertPrompt.show(); + mAlertPrompt.show(REQUEST_FOCUS); } @Override @@ -616,7 +619,7 @@ public void onButtonPrompt(GeckoSession session, String title, String msg, Strin mConfirmPrompt.setMessage(msg); mConfirmPrompt.setButtons(btnMsg); mConfirmPrompt.setDelegate(callback); - mConfirmPrompt.show(); + mConfirmPrompt.show(REQUEST_FOCUS); } @Override @@ -627,7 +630,7 @@ public void onTextPrompt(GeckoSession session, String title, String msg, String mTextPrompt.setMessage(msg); mTextPrompt.setDefaultText(value); mTextPrompt.setDelegate(callback); - mTextPrompt.show(); + mTextPrompt.show(REQUEST_FOCUS); } @Override @@ -637,7 +640,7 @@ public void onAuthPrompt(GeckoSession session, String title, String msg, AuthOpt mAuthPrompt.setTitle(title); mAuthPrompt.setMessage(msg); mAuthPrompt.setAuthOptions(options, callback); - mAuthPrompt.show(); + mAuthPrompt.show(REQUEST_FOCUS); } @Override @@ -649,7 +652,7 @@ public void onChoicePrompt(GeckoSession session, String title, String msg, int t mChoicePrompt.setChoices(choices); mChoicePrompt.setMenuType(type); mChoicePrompt.setDelegate(callback); - mChoicePrompt.show(); + mChoicePrompt.show(REQUEST_FOCUS); } @Override diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/dialogs/CrashDialogWidget.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/dialogs/CrashDialogWidget.java index e221d3c17..59f251e9c 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/dialogs/CrashDialogWidget.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/dialogs/CrashDialogWidget.java @@ -134,8 +134,8 @@ protected void initializeWidgetPlacement(WidgetPlacement aPlacement) { } @Override - public void show() { - super.show(); + public void show(@ShowFlags int aShowFlags) { + super.show(aShowFlags); mWidgetManager.pushWorldBrightness(this, WidgetManagerDelegate.DEFAULT_DIM_BRIGHTNESS); } diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/dialogs/PermissionWidget.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/dialogs/PermissionWidget.java index 415f3d0fc..f82c1cacb 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/dialogs/PermissionWidget.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/dialogs/PermissionWidget.java @@ -82,8 +82,8 @@ protected void initializeWidgetPlacement(WidgetPlacement aPlacement) { } @Override - public void show() { - super.show(); + public void show(@ShowFlags int aShowFlags) { + super.show(aShowFlags); mWidgetManager.pushWorldBrightness(this, WidgetManagerDelegate.DEFAULT_DIM_BRIGHTNESS); } @@ -141,7 +141,7 @@ public void showPrompt(String aUri, PermissionType aType, GeckoSession.Permissio mPermissionMessage.setText(str); mPermissionIcon.setImageResource(iconId); - show(); + show(REQUEST_FOCUS); } String getRequesterName(String aUri) { diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/dialogs/VoiceSearchWidget.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/dialogs/VoiceSearchWidget.java index 5ab57959f..95a663bc2 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/dialogs/VoiceSearchWidget.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/dialogs/VoiceSearchWidget.java @@ -258,18 +258,18 @@ public void onRequestPermissionsResult(int requestCode, String[] permissions, in } if (granted) { - show(); + show(REQUEST_FOCUS); } else { - super.show(true); + super.show(REQUEST_FOCUS); setPermissionNotGranted(); } } } @Override - public void show(boolean aFocus) { - super.show(aFocus); + public void show(@ShowFlags int aShowFlags) { + super.show(aShowFlags); setStartListeningState(); diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/prompts/ChoicePromptWidget.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/prompts/ChoicePromptWidget.java index 062dcc088..09eae0e98 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/prompts/ChoicePromptWidget.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/prompts/ChoicePromptWidget.java @@ -145,8 +145,8 @@ protected void onDismiss() { } @Override - public void show() { - show(true); + public void show(@ShowFlags int aShowFlags) { + show(aShowFlags); for (int i = 0; i < mListItems.length; i++) { mList.setItemChecked(i, mListItems[i].mChoice.selected); } diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/prompts/PromptWidget.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/prompts/PromptWidget.java index b468a0067..399890c53 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/prompts/PromptWidget.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/prompts/PromptWidget.java @@ -65,11 +65,13 @@ protected void initializeWidgetPlacement(WidgetPlacement aPlacement) { @Override - public void show() { + public void show(@ShowFlags int aShowFlags) { mLayout.measure(View.MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); mWidgetPlacement.height = (int)(mLayout.getMeasuredHeight()/mWidgetPlacement.density); - super.show(); + super.show(aShowFlags); + + mWidgetManager.pushWorldBrightness(this, WidgetManagerDelegate.DEFAULT_DIM_BRIGHTNESS); ViewTreeObserver viewTreeObserver = mLayout.getViewTreeObserver(); if (viewTreeObserver.isAlive()) { @@ -84,11 +86,6 @@ public void onGlobalLayout() { } } - public void show(boolean focus) { - super.show(focus); - mWidgetManager.pushWorldBrightness(this, WidgetManagerDelegate.DEFAULT_DIM_BRIGHTNESS); - } - public void hide(@HideFlags int aHideFlags) { super.hide(aHideFlags); mWidgetManager.popWorldBrightness(this); diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/settings/SettingsWidget.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/settings/SettingsWidget.java index 946e1107d..add3baae3 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/settings/SettingsWidget.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/settings/SettingsWidget.java @@ -370,8 +370,8 @@ public void onGlobalFocusChanged(View oldFocus, View newFocus) { } @Override - public void show() { - super.show(); + public void show(@ShowFlags int aShowFlags) { + super.show(aShowFlags); mWidgetManager.pushWorldBrightness(this, WidgetManagerDelegate.DEFAULT_DIM_BRIGHTNESS); } @@ -413,10 +413,10 @@ public void showRestartDialog() { if (widget == null) { widget = createChild(RestartDialogWidget.class, false); mRestartDialogHandle = widget.getHandle(); - widget.setDelegate(this::show); + widget.setDelegate(() -> show(REQUEST_FOCUS)); } - widget.show(); + widget.show(REQUEST_FOCUS); } @Override @@ -427,13 +427,13 @@ public void showAlert(String aTitle, String aMessage) { if (widget == null) { widget = createChild(AlertPromptWidget.class, false); mAlertDialogHandle = widget.getHandle(); - widget.setDelegate(this::show); + widget.setDelegate(() -> show(REQUEST_FOCUS)); } widget.getPlacement().translationZ = 0; widget.getPlacement().parentHandle = mHandle; widget.setTitle(aTitle); widget.setMessage(aMessage); - widget.show(); + widget.show(REQUEST_FOCUS); } }