From 31076d370074213e818277715141218b569dabe4 Mon Sep 17 00:00:00 2001 From: Manuel Martin Date: Mon, 12 Aug 2019 12:49:25 +0200 Subject: [PATCH] New Prompt API --- .../mozilla/vrbrowser/VRBrowserActivity.java | 25 ++-- .../browser/engine/SessionStack.java | 129 ++++++++-------- .../browser/engine/SessionStore.java | 4 +- .../vrbrowser/ui/widgets/WindowWidget.java | 138 +++++++++++++----- .../ui/widgets/prompts/AlertPromptWidget.java | 19 --- .../ui/widgets/prompts/AuthPromptWidget.java | 31 ++-- .../widgets/prompts/ChoicePromptWidget.java | 44 ++---- .../widgets/prompts/ConfirmPromptWidget.java | 39 +---- .../ui/widgets/prompts/PromptWidget.java | 25 +++- .../ui/widgets/prompts/TextPromptWidget.java | 23 +-- app/src/main/res/layout/prompt_confirm.xml | 14 -- versions.gradle | 2 +- 12 files changed, 249 insertions(+), 244 deletions(-) diff --git a/app/src/common/shared/org/mozilla/vrbrowser/VRBrowserActivity.java b/app/src/common/shared/org/mozilla/vrbrowser/VRBrowserActivity.java index 1cb21dfbc..807854d39 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/VRBrowserActivity.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/VRBrowserActivity.java @@ -48,6 +48,8 @@ import org.mozilla.vrbrowser.crashreporting.CrashReporterService; import org.mozilla.vrbrowser.crashreporting.GlobalExceptionHandler; import org.mozilla.vrbrowser.geolocation.GeolocationWrapper; +import org.mozilla.vrbrowser.ui.widgets.prompts.AlertPromptWidget; +import org.mozilla.vrbrowser.ui.widgets.prompts.ConfirmPromptWidget; import org.mozilla.vrbrowser.utils.DeviceType; import org.mozilla.vrbrowser.input.MotionEventGenerator; import org.mozilla.vrbrowser.search.SearchEngineWrapper; @@ -900,12 +902,10 @@ private void setDeviceType(int aType) { private void haltActivity(final int aReason) { runOnUiThread(() -> { if (mConnectionAvailable && mWindows.getFocusedWindow() != null) { - mWindows.getFocusedWindow().showAlert(getString(R.string.not_entitled_title), getString(R.string.not_entitled_message, getString(R.string.app_name)), new GeckoSession.PromptDelegate.AlertCallback() { - @Override - public void dismiss() { - VRBrowserActivity.this.finish(); - } - }); + mWindows.getFocusedWindow().showAlert( + getString(R.string.not_entitled_title), + getString(R.string.not_entitled_message, getString(R.string.app_name)), + () -> VRBrowserActivity.this.finish()); } }); } @@ -930,15 +930,20 @@ private void handlePoorPerformance() { return; } window.getSessionStack().loadUri("about:blank"); - final String[] buttons = {getString(R.string.ok_button), null, getString(R.string.performance_unblock_page)}; - window.showButtonPrompt(getString(R.string.performance_title), getString(R.string.performance_message), buttons, new GeckoSession.PromptDelegate.ButtonCallback() { + final String[] buttons = {getString(R.string.ok_button), getString(R.string.performance_unblock_page)}; + window.showButtonPrompt(getString(R.string.performance_title), getString(R.string.performance_message), buttons, new ConfirmPromptWidget.ConfirmPromptDelegate() { @Override - public void confirm(int button) { - if (button == GeckoSession.PromptDelegate.BUTTON_TYPE_NEGATIVE) { + public void confirm(int index) { + if (index == GeckoSession.PromptDelegate.ButtonPrompt.Type.NEGATIVE) { mPoorPerformanceWhiteList.add(originalUrl); window.getSessionStack().loadUri(originalUrl); } } + + @Override + public void dismiss() { + + } }); }); } diff --git a/app/src/common/shared/org/mozilla/vrbrowser/browser/engine/SessionStack.java b/app/src/common/shared/org/mozilla/vrbrowser/browser/engine/SessionStack.java index 6df8cd72b..dcce4cb4f 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/browser/engine/SessionStack.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/browser/engine/SessionStack.java @@ -65,7 +65,6 @@ public class SessionStack implements ContentBlocking.Delegate, GeckoSession.Navi private transient LinkedList mContentListeners; private transient LinkedList mSessionChangeListeners; private transient LinkedList mTextInputListeners; - private transient LinkedList mPromptListeners; private transient LinkedList mVideoAvailabilityListeners; private transient UserAgentOverride mUserAgentOverride; @@ -73,6 +72,7 @@ public class SessionStack implements ContentBlocking.Delegate, GeckoSession.Navi private HashMap mSessions; private Deque mSessionsStack; private transient GeckoSession.PermissionDelegate mPermissionDelegate; + private transient GeckoSession.PromptDelegate mPromptDelegate; private int mPreviousGeckoSessionId = NO_SESSION; private String mRegion; private transient Context mContext; @@ -91,7 +91,6 @@ protected SessionStack(Context context, GeckoRuntime runtime, boolean usePrivate mContentListeners = new LinkedList<>(); mSessionChangeListeners = new LinkedList<>(); mTextInputListeners = new LinkedList<>(); - mPromptListeners = new LinkedList<>(); mVideoAvailabilityListeners = new LinkedList<>(); if (mPrefs != null) { @@ -201,6 +200,13 @@ public void setPermissionDelegate(GeckoSession.PermissionDelegate aDelegate) { } } + public void setPromptDelegate(GeckoSession.PromptDelegate aDelegate) { + mPromptDelegate = aDelegate; + for (HashMap.Entry entry : mSessions.entrySet()) { + entry.getValue().mSession.setPromptDelegate(aDelegate); + } + } + public void addNavigationListener(GeckoSession.NavigationDelegate aListener) { mNavigationListeners.add(aListener); dumpState(mCurrentSession, aListener); @@ -244,14 +250,6 @@ public void removeTextInputListener(GeckoSession.TextInputDelegate aListener) { mTextInputListeners.remove(aListener); } - public void addPromptListener(GeckoSession.PromptDelegate aListener) { - mPromptListeners.add(aListener); - } - - public void removePromptListener(GeckoSession.PromptDelegate aListener) { - mPromptListeners.remove(aListener); - } - public void addVideoAvailabilityListener(VideoAvailabilityListener aListener) { mVideoAvailabilityListeners.add(aListener); } @@ -300,10 +298,10 @@ public void restore(SessionStack store, int currentSessionId) { state.mSession.setNavigationDelegate(this); state.mSession.setProgressDelegate(this); - state.mSession.setPromptDelegate(this); state.mSession.setContentDelegate(this); state.mSession.getTextInput().setDelegate(this); state.mSession.setPermissionDelegate(mPermissionDelegate); + state.mSession.setPromptDelegate(mPromptDelegate); state.mSession.setContentBlockingDelegate(this); state.mSession.setMediaDelegate(this); for (SessionChangeListener listener: mSessionChangeListeners) { @@ -360,10 +358,10 @@ private int createSession(@NonNull SessionSettings aSettings) { state.mSession.getSettings().setUserAgentMode(aSettings.getUserAgentMode()); state.mSession.setNavigationDelegate(this); state.mSession.setProgressDelegate(this); - state.mSession.setPromptDelegate(this); state.mSession.setContentDelegate(this); state.mSession.getTextInput().setDelegate(this); state.mSession.setPermissionDelegate(mPermissionDelegate); + state.mSession.setPromptDelegate(mPromptDelegate); state.mSession.setContentBlockingDelegate(this); state.mSession.setMediaDelegate(this); for (SessionChangeListener listener: mSessionChangeListeners) { @@ -1149,100 +1147,95 @@ public void updateCursorAnchorInfo(@NonNull GeckoSession aSession, @NonNull Curs @Override public void onContentBlocked(@NonNull final GeckoSession session, @NonNull final ContentBlocking.BlockEvent event) { - if ((event.categories & ContentBlocking.AT_AD) != 0) { + if ((event.getAntiTrackingCategory() & ContentBlocking.AntiTracking.AD) != 0) { Log.i(LOGTAG, "Blocking Ad: " + event.uri); } - if ((event.categories & ContentBlocking.AT_ANALYTIC) != 0) { + if ((event.getAntiTrackingCategory() & ContentBlocking.AntiTracking.ANALYTIC) != 0) { Log.i(LOGTAG, "Blocking Analytic: " + event.uri); } - if ((event.categories & ContentBlocking.AT_CONTENT) != 0) { + if ((event.getAntiTrackingCategory() & ContentBlocking.AntiTracking.CONTENT) != 0) { Log.i(LOGTAG, "Blocking Content: " + event.uri); } - if ((event.categories & ContentBlocking.AT_SOCIAL) != 0) { + if ((event.getAntiTrackingCategory() & ContentBlocking.AntiTracking.SOCIAL) != 0) { Log.i(LOGTAG, "Blocking Social: " + event.uri); } } // PromptDelegate + @Nullable @Override - public void onAlert(@NonNull GeckoSession session, String title, String msg, @NonNull AlertCallback callback) { - if (session == mCurrentSession) { - for (GeckoSession.PromptDelegate listener : mPromptListeners) { - listener.onAlert(session, title, msg, callback); - } - } - } + public GeckoResult onAlertPrompt(@NonNull GeckoSession geckoSession, @NonNull AlertPrompt alertPrompt) { + if (mPromptDelegate != null) + return mPromptDelegate.onAlertPrompt(geckoSession, alertPrompt); - @Override - public void onButtonPrompt(@NonNull GeckoSession session, String title, String msg, String[] btnMsg, @NonNull ButtonCallback callback) { - if (session == mCurrentSession) { - for (GeckoSession.PromptDelegate listener : mPromptListeners) { - listener.onButtonPrompt(session, title, msg, btnMsg, callback); - } - } + return GeckoResult.fromValue(alertPrompt.dismiss()); } + @Nullable @Override - public void onTextPrompt(@NonNull GeckoSession session, String title, String msg, String value, @NonNull TextCallback callback) { - if (session == mCurrentSession) { - for (GeckoSession.PromptDelegate listener : mPromptListeners) { - listener.onTextPrompt(session, title, msg, value, callback); - } - } + public GeckoResult onButtonPrompt(@NonNull GeckoSession geckoSession, @NonNull ButtonPrompt buttonPrompt) { + if (mPromptDelegate != null) + return mPromptDelegate.onButtonPrompt(geckoSession, buttonPrompt); + + return GeckoResult.fromValue(buttonPrompt.dismiss()); } + @Nullable @Override - public void onAuthPrompt(@NonNull GeckoSession session, String title, String msg, @NonNull AuthOptions options, @NonNull AuthCallback callback) { - if (session == mCurrentSession) { - for (GeckoSession.PromptDelegate listener : mPromptListeners) { - listener.onAuthPrompt(session, title, msg, options, callback); - } - } + public GeckoResult onTextPrompt(@NonNull GeckoSession geckoSession, @NonNull TextPrompt textPrompt) { + if (mPromptDelegate != null) + return mPromptDelegate.onTextPrompt(geckoSession, textPrompt); + + return GeckoResult.fromValue(textPrompt.dismiss()); } + @Nullable @Override - public void onChoicePrompt(@NonNull GeckoSession session, String title, String msg, int type, @NonNull Choice[] choices, @NonNull ChoiceCallback callback) { - if (session == mCurrentSession) { - for (GeckoSession.PromptDelegate listener : mPromptListeners) { - listener.onChoicePrompt(session, title, msg, type, choices, callback); - } - } + public GeckoResult onAuthPrompt(@NonNull GeckoSession geckoSession, @NonNull AuthPrompt authPrompt) { + if (mPromptDelegate != null) + return mPromptDelegate.onAuthPrompt(geckoSession, authPrompt); + + return GeckoResult.fromValue(authPrompt.dismiss()); } + @Nullable @Override - public void onColorPrompt(@NonNull GeckoSession session, String title, String value, @NonNull TextCallback callback) { - if (session == mCurrentSession) { - for (GeckoSession.PromptDelegate listener : mPromptListeners) { - listener.onColorPrompt(session, title, value, callback); - } - } + public GeckoResult onChoicePrompt(@NonNull GeckoSession geckoSession, @NonNull ChoicePrompt choicePrompt) { + if (mPromptDelegate != null) + return mPromptDelegate.onChoicePrompt(geckoSession, choicePrompt); + + return GeckoResult.fromValue(choicePrompt.dismiss()); } + @Nullable @Override - public void onDateTimePrompt(@NonNull GeckoSession session, String title, int type, String value, String min, String max, @NonNull TextCallback callback) { - if (session == mCurrentSession) { - for (GeckoSession.PromptDelegate listener : mPromptListeners) { - listener.onDateTimePrompt(session, title, type, value, min, max, callback); - } - } + public GeckoResult onColorPrompt(@NonNull GeckoSession geckoSession, @NonNull ColorPrompt colorPrompt) { + if (mPromptDelegate != null) + return mPromptDelegate.onColorPrompt(geckoSession, colorPrompt); + + return GeckoResult.fromValue(colorPrompt.dismiss()); } + @Nullable @Override - public void onFilePrompt(@NonNull GeckoSession session, String title, int type, String[] mimeTypes, int capture, @NonNull FileCallback callback) { - if (session == mCurrentSession) { - for (GeckoSession.PromptDelegate listener : mPromptListeners) { - listener.onFilePrompt(session, title, type, mimeTypes, capture, callback); - } - } + public GeckoResult onDateTimePrompt(@NonNull GeckoSession geckoSession, @NonNull DateTimePrompt dateTimePrompt) { + if (mPromptDelegate != null) + return mPromptDelegate.onDateTimePrompt(geckoSession, dateTimePrompt); + + return GeckoResult.fromValue(dateTimePrompt.dismiss()); } + @Nullable @Override - public GeckoResult onPopupRequest(@NonNull final GeckoSession session, final String targetUri) { - return GeckoResult.fromValue(AllowOrDeny.DENY); + public GeckoResult onFilePrompt(@NonNull GeckoSession geckoSession, @NonNull FilePrompt filePrompt) { + if (mPromptDelegate != null) + return mPromptDelegate.onFilePrompt(geckoSession, filePrompt); + + return GeckoResult.fromValue(filePrompt.dismiss()); } // MediaDelegate diff --git a/app/src/common/shared/org/mozilla/vrbrowser/browser/engine/SessionStore.java b/app/src/common/shared/org/mozilla/vrbrowser/browser/engine/SessionStore.java index d165d8b67..da48a6930 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/browser/engine/SessionStore.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/browser/engine/SessionStore.java @@ -63,8 +63,8 @@ public void setContext(Context context, Bundle aExtras) { GeckoRuntimeSettings.Builder runtimeSettingsBuilder = new GeckoRuntimeSettings.Builder(); runtimeSettingsBuilder.crashHandler(CrashReporterService.class); - runtimeSettingsBuilder.contentBlocking((new ContentBlocking.Settings.Builder()) - .categories(ContentBlocking.AT_AD | ContentBlocking.AT_SOCIAL | ContentBlocking.AT_ANALYTIC) + runtimeSettingsBuilder.contentBlocking((new ContentBlocking.Settings.Builder() + .antiTracking(ContentBlocking.AntiTracking.AD | ContentBlocking.AntiTracking.SOCIAL| ContentBlocking.AntiTracking.ANALYTIC)) .build()); runtimeSettingsBuilder.consoleOutput(SettingsStore.getInstance(context).isConsoleLogsEnabled()); runtimeSettingsBuilder.displayDensityOverride(SettingsStore.getInstance(context).getDisplayDensity()); 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 62ccb38c2..1200c32f2 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 @@ -43,6 +43,7 @@ import org.mozilla.vrbrowser.ui.widgets.prompts.AuthPromptWidget; import org.mozilla.vrbrowser.ui.widgets.prompts.ChoicePromptWidget; import org.mozilla.vrbrowser.ui.widgets.prompts.ConfirmPromptWidget; +import org.mozilla.vrbrowser.ui.widgets.prompts.PromptWidget; import org.mozilla.vrbrowser.ui.widgets.prompts.TextPromptWidget; import org.mozilla.vrbrowser.utils.InternalPages; @@ -99,8 +100,8 @@ public WindowWidget(Context aContext, int windowId, boolean privateMode) { mWindowId = windowId; mSessionStack = SessionStore.get().createSessionStack(mWindowId, privateMode); + mSessionStack.setPromptDelegate(this); mSessionStack.addSessionChangeListener(this); - mSessionStack.addPromptListener(this); mSessionStack.addContentListener(this); mSessionStack.addVideoAvailabilityListener(this); mSessionStack.addNavigationListener(this); @@ -530,8 +531,8 @@ public void handleResizeEvent(float aWorldWidth, float aWorldHeight) { @Override public void releaseWidget() { + mSessionStack.setPromptDelegate(null); mSessionStack.removeSessionChangeListener(this); - mSessionStack.removePromptListener(this); mSessionStack.removeContentListener(this); mSessionStack.removeVideoAvailabilityListener(this); mSessionStack.removeNavigationListener(this); @@ -712,22 +713,22 @@ public void setNoInternetToastVisible(boolean aVisible) { } } - public void showAlert(String title, @NonNull String msg, @NonNull AlertCallback callback) { + public void showAlert(String title, @NonNull String msg, @NonNull PromptWidget.PromptDelegate callback) { mAlertPrompt = new AlertPromptWidget(getContext()); mAlertPrompt.mWidgetPlacement.parentHandle = getHandle(); mAlertPrompt.setTitle(title); mAlertPrompt.setMessage(msg); - mAlertPrompt.setDelegate(callback); + mAlertPrompt.setPromptDelegate(callback); mAlertPrompt.show(REQUEST_FOCUS); } - public void showButtonPrompt(String title, @NonNull String msg, @NonNull String[] btnMsg, @NonNull ButtonCallback callback) { + public void showButtonPrompt(String title, @NonNull String msg, @NonNull String[] btnMsg, @NonNull ConfirmPromptWidget.ConfirmPromptDelegate callback) { mConfirmPrompt = new ConfirmPromptWidget(getContext()); mConfirmPrompt.mWidgetPlacement.parentHandle = getHandle(); mConfirmPrompt.setTitle(title); mConfirmPrompt.setMessage(msg); mConfirmPrompt.setButtons(btnMsg); - mConfirmPrompt.setDelegate(callback); + mConfirmPrompt.setPromptDelegate(callback); mConfirmPrompt.show(REQUEST_FOCUS); } @@ -797,63 +798,132 @@ private int getWindowWidth(float aWorldWidth) { // PromptDelegate + @Nullable @Override - public void onAlert(GeckoSession session, String title, String msg, AlertCallback callback) { + public GeckoResult onAlertPrompt(@NonNull GeckoSession geckoSession, @NonNull AlertPrompt alertPrompt) { + final GeckoResult result = new GeckoResult<>(); + mAlertPrompt = new AlertPromptWidget(getContext()); mAlertPrompt.mWidgetPlacement.parentHandle = getHandle(); - mAlertPrompt.setTitle(title); - mAlertPrompt.setMessage(msg); - mAlertPrompt.setDelegate(callback); + mAlertPrompt.setTitle(alertPrompt.title); + mAlertPrompt.setMessage(alertPrompt.message); + mAlertPrompt.setPromptDelegate(() -> result.complete(alertPrompt.dismiss())); mAlertPrompt.show(REQUEST_FOCUS); + + return result; } + @Nullable @Override - public void onButtonPrompt(GeckoSession session, String title, String msg, String[] btnMsg, ButtonCallback callback) { + public GeckoResult onButtonPrompt(@NonNull GeckoSession geckoSession, @NonNull ButtonPrompt buttonPrompt) { + final GeckoResult result = new GeckoResult<>(); + mConfirmPrompt = new ConfirmPromptWidget(getContext()); mConfirmPrompt.mWidgetPlacement.parentHandle = getHandle(); - mConfirmPrompt.setTitle(title); - mConfirmPrompt.setMessage(msg); - mConfirmPrompt.setButtons(btnMsg); - mConfirmPrompt.setDelegate(callback); + mConfirmPrompt.setTitle(buttonPrompt.title); + mConfirmPrompt.setMessage(buttonPrompt.message); + mConfirmPrompt.setButtons(new String[] { + getResources().getText(R.string.ok_button).toString(), + getResources().getText(R.string.cancel_button).toString() + }); + mConfirmPrompt.setPromptDelegate(new ConfirmPromptWidget.ConfirmPromptDelegate() { + @Override + public void confirm(int index) { + result.complete(buttonPrompt.confirm(index)); + } + + @Override + public void dismiss() { + result.complete(buttonPrompt.dismiss()); + } + }); mConfirmPrompt.show(REQUEST_FOCUS); + + return result; } + @Nullable @Override - public void onTextPrompt(GeckoSession session, String title, String msg, String value, TextCallback callback) { + public GeckoResult onTextPrompt(@NonNull GeckoSession geckoSession, @NonNull TextPrompt textPrompt) { + final GeckoResult result = new GeckoResult<>(); + mTextPrompt = new TextPromptWidget(getContext()); mTextPrompt.mWidgetPlacement.parentHandle = getHandle(); - mTextPrompt.setTitle(title); - mTextPrompt.setMessage(msg); - mTextPrompt.setDefaultText(value); - mTextPrompt.setDelegate(callback); + mTextPrompt.setTitle(textPrompt.title); + mTextPrompt.setMessage(textPrompt.message); + mTextPrompt.setDefaultText(textPrompt.defaultValue); + mTextPrompt.setPromptDelegate(new TextPromptWidget.TextPromptDelegate() { + @Override + public void confirm(String message) { + result.complete(textPrompt.confirm(message)); + } + + @Override + public void dismiss() { + result.complete(textPrompt.dismiss()); + } + }); mTextPrompt.show(REQUEST_FOCUS); + + return result; } + @Nullable @Override - public void onAuthPrompt(GeckoSession session, String title, String msg, AuthOptions options, AuthCallback callback) { + public GeckoResult onAuthPrompt(@NonNull GeckoSession geckoSession, @NonNull AuthPrompt authPrompt) { + final GeckoResult result = new GeckoResult<>(); + mAuthPrompt = new AuthPromptWidget(getContext()); mAuthPrompt.mWidgetPlacement.parentHandle = getHandle(); - mAuthPrompt.setTitle(title); - mAuthPrompt.setMessage(msg); - mAuthPrompt.setAuthOptions(options, callback); + mAuthPrompt.setTitle(authPrompt.title); + mAuthPrompt.setMessage(authPrompt.message); + mAuthPrompt.setAuthOptions(authPrompt.authOptions); + mAuthPrompt.setPromptDelegate(new AuthPromptWidget.AuthPromptDelegate() { + @Override + public void dismiss() { + result.complete(authPrompt.dismiss()); + } + + @Override + public void confirm(String password) { + result.complete(authPrompt.confirm(password)); + } + + @Override + public void confirm(String username, String password) { + result.complete(authPrompt.confirm(username, password)); + } + }); mAuthPrompt.show(REQUEST_FOCUS); + + return result; } + @Nullable @Override - public void onChoicePrompt(GeckoSession session, String title, String msg, int type, final Choice[] choices, final ChoiceCallback callback) { + public GeckoResult onChoicePrompt(@NonNull GeckoSession geckoSession, @NonNull ChoicePrompt choicePrompt) { + final GeckoResult result = new GeckoResult<>(); + mChoicePrompt = new ChoicePromptWidget(getContext()); mChoicePrompt.mWidgetPlacement.parentHandle = getHandle(); - mChoicePrompt.setTitle(title); - mChoicePrompt.setMessage(msg); - mChoicePrompt.setChoices(choices); - mChoicePrompt.setMenuType(type); - mChoicePrompt.setDelegate(callback); + mChoicePrompt.setTitle(choicePrompt.title); + mChoicePrompt.setMessage(choicePrompt.message); + mChoicePrompt.setChoices(choicePrompt.choices); + mChoicePrompt.setMenuType(choicePrompt.type); + mChoicePrompt.setPromptDelegate(new ChoicePromptWidget.ChoicePromptDelegate() { + @Override + public void confirm(String[] choices) { + result.complete(choicePrompt.confirm(choices)); + } + + @Override + public void dismiss() { + + } + }); mChoicePrompt.show(REQUEST_FOCUS); - } - @Override - public GeckoResult onPopupRequest(final GeckoSession session, final String targetUri) { - return GeckoResult.fromValue(AllowOrDeny.ALLOW); + return result; } // GeckoSession.ContentDelegate diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/prompts/AlertPromptWidget.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/prompts/AlertPromptWidget.java index 3e6377f29..eb1ca5c30 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/prompts/AlertPromptWidget.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/prompts/AlertPromptWidget.java @@ -5,7 +5,6 @@ import android.util.AttributeSet; import android.widget.Button; -import org.mozilla.geckoview.GeckoSession; import org.mozilla.vrbrowser.R; import org.mozilla.vrbrowser.audio.AudioEngine; @@ -13,7 +12,6 @@ public class AlertPromptWidget extends PromptWidget { private AudioEngine mAudio; private Button mOkButton; - private GeckoSession.PromptDelegate.AlertCallback mCallback; public AlertPromptWidget(Context aContext) { super(aContext); @@ -51,21 +49,4 @@ protected void initialize(Context aContext) { }); } - @Override - protected void onDismiss() { - hide(REMOVE_WIDGET); - - if (mCallback != null) { - mCallback.dismiss(); - } - - if (mDelegate != null) { - mDelegate.onDismiss(); - } - } - - public void setDelegate(GeckoSession.PromptDelegate.AlertCallback delegate) { - mCallback = delegate; - } - } diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/prompts/AuthPromptWidget.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/prompts/AuthPromptWidget.java index f3c2bfb02..704978840 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/prompts/AuthPromptWidget.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/prompts/AuthPromptWidget.java @@ -7,8 +7,6 @@ import android.view.View; import android.widget.Button; import android.widget.CheckBox; -import android.widget.CompoundButton; -import android.widget.Switch; import android.widget.TextView; import org.mozilla.geckoview.GeckoSession; @@ -18,14 +16,17 @@ public class AuthPromptWidget extends PromptWidget { + public interface AuthPromptDelegate extends PromptDelegate { + void confirm(String password); + void confirm(String username, String password); + } + private AudioEngine mAudio; private SettingsEditText mUsernameText; private SettingsEditText mPasswordText; private TextView mUsernameTextLabel; - private TextView mPasswordTextLabel; private Button mOkButton; private Button mCancelButton; - private GeckoSession.PromptDelegate.AuthCallback mCallback; public AuthPromptWidget(Context aContext) { super(aContext); @@ -57,7 +58,6 @@ protected void initialize(Context aContext) { mUsernameTextLabel = findViewById(R.id.authUsernameLabel); mPasswordText = findViewById(R.id.authPassword); mPasswordText.setShowSoftInputOnFocus(false); - mPasswordTextLabel = findViewById(R.id.authPasswordLabel); mOkButton = findViewById(R.id.positiveButton); mOkButton.setOnClickListener(view -> { @@ -65,8 +65,11 @@ protected void initialize(Context aContext) { mAudio.playSound(AudioEngine.Sound.CLICK); } - if (mCallback != null) { - mCallback.confirm(mUsernameText.getText().toString(), mPasswordText.getText().toString()); + if (mPromptDelegate != null && mPromptDelegate instanceof AuthPromptDelegate) { + if (mUsernameText.getVisibility() == VISIBLE) + ((AuthPromptDelegate)mPromptDelegate).confirm(mUsernameText.getText().toString(), mPasswordText.getText().toString()); + else + ((AuthPromptDelegate)mPromptDelegate).confirm(mPasswordText.getText().toString()); } hide(REMOVE_WIDGET); @@ -91,28 +94,18 @@ protected void initialize(Context aContext) { }); } - @Override - protected void onDismiss() { - hide(REMOVE_WIDGET); - - if (mCallback != null) { - mCallback.dismiss(); - } - } - - public void setAuthOptions(GeckoSession.PromptDelegate.AuthOptions aOptions, GeckoSession.PromptDelegate.AuthCallback aCallback) { + public void setAuthOptions(GeckoSession.PromptDelegate.AuthPrompt.AuthOptions aOptions) { if (aOptions.username != null) { mUsernameText.setText(aOptions.username); } if (aOptions.password != null) { mPasswordText.setText(aOptions.password); } - if ((aOptions.flags & GeckoSession.PromptDelegate.AuthOptions.AUTH_FLAG_ONLY_PASSWORD) != 0) { + if ((aOptions.flags & GeckoSession.PromptDelegate.AuthPrompt.AuthOptions.Flags.ONLY_PASSWORD) != 0) { // Hide the username input if basic auth dialog only requests a password. mUsernameText.setVisibility(View.GONE); mUsernameTextLabel.setVisibility(View.GONE); } - mCallback = aCallback; } @Override 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 7ed0f91f1..e09061226 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 @@ -2,9 +2,7 @@ import android.content.Context; import android.graphics.Typeface; -import android.os.Handler; import android.util.AttributeSet; -import android.util.Log; import android.util.SparseBooleanArray; import android.view.LayoutInflater; import android.view.MotionEvent; @@ -17,8 +15,11 @@ import android.widget.RadioButton; import android.widget.TextView; +import androidx.annotation.NonNull; + import org.mozilla.geckoview.GeckoSession; -import org.mozilla.geckoview.GeckoSession.PromptDelegate.Choice; +import org.mozilla.geckoview.GeckoSession.PromptDelegate.ChoicePrompt.Choice; +import org.mozilla.geckoview.GeckoSession.PromptDelegate.ChoicePrompt.Type; import org.mozilla.vrbrowser.R; import org.mozilla.vrbrowser.audio.AudioEngine; import org.mozilla.vrbrowser.ui.widgets.WidgetPlacement; @@ -26,10 +27,12 @@ import java.util.ArrayList; import java.util.Arrays; -import androidx.annotation.NonNull; - public class ChoicePromptWidget extends PromptWidget { + public interface ChoicePromptDelegate extends PromptDelegate { + void confirm(String[] choices); + } + private static final int DIALOG_CLOSE_DELAY = 250; private static final int LISTVIEW_ITEM_HEIGHT = 20; @@ -38,9 +41,7 @@ public class ChoicePromptWidget extends PromptWidget { private Button mCloseButton; private Button mOkButton; private ChoiceWrapper[] mListItems; - private GeckoSession.PromptDelegate.ChoiceCallback mCallback; private ChoiceAdapter mAdapter; - private final Handler handler = new Handler(); public ChoicePromptWidget(Context aContext) { super(aContext); @@ -77,8 +78,8 @@ protected void initialize(Context aContext) { postDelayed(() -> { ChoiceWrapper selectedItem = mListItems[position]; if (mList.getChoiceMode() == ListView.CHOICE_MODE_SINGLE) { - if (mCallback != null) { - mCallback.confirm(new String[]{selectedItem.getChoice().id}); + if (mPromptDelegate != null && mPromptDelegate instanceof ChoicePromptDelegate) { + ((ChoicePromptDelegate)mPromptDelegate).confirm(new String[]{selectedItem.getChoice().id}); hide(REMOVE_WIDGET); } } @@ -97,7 +98,7 @@ protected void initialize(Context aContext) { switch (mList.getChoiceMode()) { case ListView.CHOICE_MODE_SINGLE: case ListView.CHOICE_MODE_MULTIPLE: { - if (mCallback != null) { + if (mPromptDelegate != null) { onDismiss(); } } @@ -114,7 +115,7 @@ protected void initialize(Context aContext) { switch (mList.getChoiceMode()) { case ListView.CHOICE_MODE_SINGLE: case ListView.CHOICE_MODE_MULTIPLE: { - if (mCallback != null) { + if (mPromptDelegate != null && mPromptDelegate instanceof ChoicePromptDelegate) { int len = mList.getCount(); SparseBooleanArray selected = mList.getCheckedItemPositions(); ArrayList selectedChoices = new ArrayList<>(); @@ -123,7 +124,7 @@ protected void initialize(Context aContext) { selectedChoices.add(mListItems[i].getChoice().id); } } - mCallback.confirm(selectedChoices.toArray(new String[selectedChoices.size()])); + ((ChoicePromptDelegate)mPromptDelegate).confirm(selectedChoices.toArray(new String[selectedChoices.size()])); } } break; @@ -135,15 +136,6 @@ protected void initialize(Context aContext) { mListItems = new ChoiceWrapper[]{}; } - @Override - protected void onDismiss() { - hide(REMOVE_WIDGET); - - if (mCallback != null) { - mCallback.dismiss(); - } - } - @Override public void show(@ShowFlags int aShowFlags) { super.show(aShowFlags); @@ -158,10 +150,6 @@ public int getMinHeight() { return WidgetPlacement.dpDimension(getContext(), R.dimen.prompt_min_height); } - public void setDelegate(GeckoSession.PromptDelegate.ChoiceCallback delegate) { - mCallback = delegate; - } - public void setChoices(Choice[] choices) { mListItems = getWrappedChoices(choices); mAdapter = new ChoiceAdapter(getContext(), R.layout.prompt_choice_item, mListItems); @@ -174,14 +162,14 @@ public void setChoices(Choice[] choices) { public void setMenuType(int type) { switch (type) { - case Choice.CHOICE_TYPE_SINGLE: - case Choice.CHOICE_TYPE_MENU: { + case Type.SINGLE: + case Type.MENU: { mList.setChoiceMode(ListView.CHOICE_MODE_SINGLE); mCloseButton.setVisibility(View.VISIBLE); mOkButton.setVisibility(View.GONE); } break; - case Choice.CHOICE_TYPE_MULTIPLE: { + case Type.MULTIPLE: { mList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); mCloseButton.setVisibility(View.VISIBLE); mOkButton.setVisibility(View.VISIBLE); diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/prompts/ConfirmPromptWidget.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/prompts/ConfirmPromptWidget.java index 301bedaec..3f7181a1a 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/prompts/ConfirmPromptWidget.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/prompts/ConfirmPromptWidget.java @@ -6,19 +6,20 @@ import android.view.View; import android.widget.Button; -import org.mozilla.geckoview.GeckoSession; import org.mozilla.vrbrowser.R; import org.mozilla.vrbrowser.audio.AudioEngine; public class ConfirmPromptWidget extends PromptWidget { + public interface ConfirmPromptDelegate extends PromptDelegate { + void confirm(int index); + } + private static final int POSITIVE = 0; - private static final int NEUTRAL = 1; - private static final int NEGATIVE = 2; + private static final int NEGATIVE = 1; private AudioEngine mAudio; private Button[] mButtons; - private GeckoSession.PromptDelegate.ButtonCallback mCallback; public ConfirmPromptWidget(Context aContext) { super(aContext); @@ -48,16 +49,12 @@ protected void initialize(Context aContext) { mMessage = findViewById(R.id.confirmMessage); mMessage.setMovementMethod(new ScrollingMovementMethod()); - mButtons = new Button[3]; + mButtons = new Button[2]; mButtons[POSITIVE] = findViewById(R.id.positiveButton); mButtons[POSITIVE].setOnClickListener(mButtonClickListener); mButtons[POSITIVE].setVisibility(GONE); - mButtons[NEUTRAL] = findViewById(R.id.neutralButton); - mButtons[NEUTRAL].setOnClickListener(mButtonClickListener); - mButtons[NEUTRAL].setVisibility(GONE); - mButtons[NEGATIVE] = findViewById(R.id.negativeButton); mButtons[NEGATIVE].setOnClickListener(mButtonClickListener); mButtons[NEGATIVE].setVisibility(GONE); @@ -70,40 +67,20 @@ public void onClick(View view) { mAudio.playSound(AudioEngine.Sound.CLICK); } - if (mCallback != null) { - mCallback.confirm((int)view.getTag()); + if (mPromptDelegate != null && mPromptDelegate instanceof ConfirmPromptDelegate) { + ((ConfirmPromptDelegate)mPromptDelegate).confirm((int)view.getTag()); } hide(REMOVE_WIDGET); } }; - @Override - protected void onDismiss() { - hide(REMOVE_WIDGET); - - if (mCallback != null) { - mCallback.dismiss(); - } - } - - public void setDelegate(GeckoSession.PromptDelegate.ButtonCallback delegate) { - mCallback = delegate; - } - public void setButtons(String[] btnMsg) { - // NOTE: For some reason Gecko handles positive and negative internally reversed. - // Returning 0 should be Ok but is in fact Cancel. if (btnMsg[POSITIVE] != null) { mButtons[POSITIVE].setText(btnMsg[POSITIVE]); mButtons[POSITIVE].setTag(POSITIVE); mButtons[POSITIVE].setVisibility(VISIBLE); } - if (btnMsg[NEUTRAL] != null) { - mButtons[NEUTRAL].setText(btnMsg[NEUTRAL]); - mButtons[NEUTRAL].setTag(NEUTRAL); - mButtons[NEUTRAL].setVisibility(VISIBLE); - } if (btnMsg[NEGATIVE] != null) { mButtons[NEGATIVE].setText(btnMsg[NEGATIVE]); mButtons[NEGATIVE].setTag(NEGATIVE); 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 2c350a5f5..62eceb51e 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 @@ -7,18 +7,24 @@ import android.view.ViewTreeObserver; import android.widget.TextView; +import androidx.annotation.NonNull; + import org.mozilla.vrbrowser.R; -import org.mozilla.vrbrowser.ui.widgets.UIWidget; import org.mozilla.vrbrowser.ui.widgets.WidgetManagerDelegate; import org.mozilla.vrbrowser.ui.widgets.WidgetPlacement; import org.mozilla.vrbrowser.ui.widgets.dialogs.UIDialog; public class PromptWidget extends UIDialog { + public interface PromptDelegate { + void dismiss(); + } + protected TextView mTitle; protected TextView mMessage; protected ViewGroup mLayout; private int mMaxHeight; + protected PromptDelegate mPromptDelegate; public PromptWidget(Context aContext) { super(aContext); @@ -32,6 +38,10 @@ public PromptWidget(Context aContext, AttributeSet aAttrs, int aDefStyle) { super(aContext, aAttrs, aDefStyle); } + public void setPromptDelegate(@NonNull PromptDelegate delegate) { + mPromptDelegate = delegate; + } + public void setTitle(String title) { if (title == null || title.isEmpty()) { mTitle.setVisibility(View.GONE); @@ -93,6 +103,19 @@ public void hide(@HideFlags int aHideFlags) { mWidgetManager.popWorldBrightness(this); } + @Override + protected void onDismiss() { + hide(REMOVE_WIDGET); + + if (mPromptDelegate != null) { + mPromptDelegate.dismiss(); + } + + if (mDelegate != null) { + mDelegate.onDismiss(); + } + } + // WidgetManagerDelegate.FocusChangeListener @Override public void onGlobalFocusChanged(View oldFocus, View newFocus) { diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/prompts/TextPromptWidget.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/prompts/TextPromptWidget.java index 8324d99ea..46c294090 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/prompts/TextPromptWidget.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/prompts/TextPromptWidget.java @@ -5,18 +5,20 @@ import android.util.AttributeSet; import android.widget.Button; -import org.mozilla.geckoview.GeckoSession; import org.mozilla.vrbrowser.R; import org.mozilla.vrbrowser.audio.AudioEngine; import org.mozilla.vrbrowser.ui.views.settings.SettingsEditText; public class TextPromptWidget extends PromptWidget { + public interface TextPromptDelegate extends PromptDelegate { + void confirm(String message); + } + private AudioEngine mAudio; private SettingsEditText mPromptText; private Button mOkButton; private Button mCancelButton; - private GeckoSession.PromptDelegate.TextCallback mCallback; public TextPromptWidget(Context aContext) { super(aContext); @@ -51,8 +53,8 @@ protected void initialize(Context aContext) { mAudio.playSound(AudioEngine.Sound.CLICK); } - if (mCallback != null) { - mCallback.confirm(mPromptText.getText().toString()); + if (mPromptDelegate != null && mPromptDelegate instanceof TextPromptDelegate) { + ((TextPromptDelegate)mPromptDelegate).confirm(mPromptText.getText().toString()); } hide(REMOVE_WIDGET); @@ -68,19 +70,6 @@ protected void initialize(Context aContext) { }); } - @Override - protected void onDismiss() { - hide(REMOVE_WIDGET); - - if (mCallback != null) { - mCallback.dismiss(); - } - } - - public void setDelegate(GeckoSession.PromptDelegate.TextCallback delegate) { - mCallback = delegate; - } - public void setDefaultText(String text) { mPromptText.setText(text); } diff --git a/app/src/main/res/layout/prompt_confirm.xml b/app/src/main/res/layout/prompt_confirm.xml index 24ec5483e..c34bb5988 100644 --- a/app/src/main/res/layout/prompt_confirm.xml +++ b/app/src/main/res/layout/prompt_confirm.xml @@ -70,20 +70,6 @@ android:textSize="10pt" android:textStyle="bold" /> -