Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Merge pull request #985 from MozillaReality/basic_auth
Browse files Browse the repository at this point in the history
Implement basic auth prompt
  • Loading branch information
MortimerGoro authored Mar 13, 2019
2 parents fc40026 + 54fd41d commit 06e4d41
Show file tree
Hide file tree
Showing 5 changed files with 342 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.mozilla.vrbrowser.browser.SettingsStore;
import org.mozilla.vrbrowser.ui.views.BookmarksView;
import org.mozilla.vrbrowser.ui.widgets.prompts.AlertPromptWidget;
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.TextPromptWidget;
Expand All @@ -50,6 +51,7 @@ public class WindowWidget extends UIWidget implements SessionStore.SessionChange
private AlertPromptWidget mAlertPrompt;
private ConfirmPromptWidget mConfirmPrompt;
private TextPromptWidget mTextPrompt;
private AuthPromptWidget mAuthPrompt;
private int mWidthBackup;
private int mHeightBackup;
private int mBorderWidth;
Expand Down Expand Up @@ -605,7 +607,12 @@ public void onTextPrompt(GeckoSession session, String title, String msg, String

@Override
public void onAuthPrompt(GeckoSession session, String title, String msg, AuthOptions options, AuthCallback callback) {

mAuthPrompt = new AuthPromptWidget(getContext());
mAuthPrompt.mWidgetPlacement.parentHandle = getHandle();
mAuthPrompt.setTitle(title);
mAuthPrompt.setMessage(msg);
mAuthPrompt.setAuthOptions(options, callback);
mAuthPrompt.show();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package org.mozilla.vrbrowser.ui.widgets.prompts;

import android.content.Context;
import android.text.InputType;
import android.text.method.ScrollingMovementMethod;
import android.util.AttributeSet;
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;
import org.mozilla.vrbrowser.R;
import org.mozilla.vrbrowser.audio.AudioEngine;
import org.mozilla.vrbrowser.ui.views.settings.SettingsEditText;

public class AuthPromptWidget extends PromptWidget {

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);
initialize(aContext);
}

public AuthPromptWidget(Context aContext, AttributeSet aAttrs) {
super(aContext, aAttrs);
initialize(aContext);
}

public AuthPromptWidget(Context aContext, AttributeSet aAttrs, int aDefStyle) {
super(aContext, aAttrs, aDefStyle);
initialize(aContext);
}

@Override
protected void initialize(Context aContext) {
super.initialize(aContext);

inflate(aContext, R.layout.prompt_auth, this);

mAudio = AudioEngine.fromContext(aContext);

mLayout = findViewById(R.id.layout);

mTitle = findViewById(R.id.textTitle);
mMessage = findViewById(R.id.textMessage);
mMessage.setMovementMethod(new ScrollingMovementMethod());
mUsernameText = findViewById(R.id.authUsername);
mUsernameText.setShowSoftInputOnFocus(false);
mUsernameTextLabel = findViewById(R.id.authUsernameLabel);
mPasswordText = findViewById(R.id.authPassword);
mPasswordText.setShowSoftInputOnFocus(false);
mPasswordTextLabel = findViewById(R.id.authPasswordLabel);

mOkButton = findViewById(R.id.positiveButton);
mOkButton.setSoundEffectsEnabled(false);
mOkButton.setOnClickListener(view -> {
if (mAudio != null) {
mAudio.playSound(AudioEngine.Sound.CLICK);
}

if (mCallback != null) {
mCallback.confirm(mUsernameText.getText().toString(), mPasswordText.getText().toString());
}

hide(REMOVE_WIDGET);
});

mCancelButton = findViewById(R.id.negativeButton);
mCancelButton.setSoundEffectsEnabled(false);
mCancelButton.setOnClickListener(view -> {
if (mAudio != null) {
mAudio.playSound(AudioEngine.Sound.CLICK);
}

onDismiss();
});

CheckBox showPassword = findViewById(R.id.showPasswordCheckbox);
showPassword.setOnCheckedChangeListener((buttonView, isChecked) -> {
if (isChecked) {
mPasswordText.setInputType(InputType.TYPE_CLASS_TEXT);
} else {
mPasswordText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
}
});
}

@Override
protected void onDismiss() {
hide(REMOVE_WIDGET);

if (mCallback != null) {
mCallback.dismiss();
}
}

public void setAuthOptions(GeckoSession.PromptDelegate.AuthOptions aOptions, GeckoSession.PromptDelegate.AuthCallback aCallback) {
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) {
// Hide the username input if basic auth dialog only requests a password.
mUsernameText.setVisibility(View.GONE);
mUsernameTextLabel.setVisibility(View.GONE);
}
mCallback = aCallback;
}

@Override
public void setTitle(String title) {
if (title == null || title.isEmpty()) {
mTitle.setText(getContext().getString(R.string.authentication_required));

} else {
mTitle.setText(title);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ 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);
}

// WidgetManagerDelegate.FocusChangeListener
@Override
public void onGlobalFocusChanged(View oldFocus, View newFocus) {
Expand Down
182 changes: 182 additions & 0 deletions app/src/main/res/layout/prompt_auth.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/prompt_background">

<TextView
android:id="@+id/textTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:gravity="center_horizontal"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Title" />

<TextView
android:id="@+id/textMessage"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="10dp"
android:gravity="top|start"
android:maxHeight="@dimen/prompt_content_max_height"
android:scrollbars="vertical"
android:textAlignment="viewStart"
android:textSize="18sp"
app:layout_constraintTop_toBottomOf="@+id/textTitle"
app:layout_constraintBottom_toTopOf="@+id/authInput"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
tools:text="Message" />

<LinearLayout
android:id="@+id/authInput"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
app:layout_constraintTop_toBottomOf="@+id/textMessage"
app:layout_constraintBottom_toTopOf="@+id/buttonsLayout"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<TextView
android:id="@+id/authUsernameLabel"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="top|end"
android:maxHeight="@dimen/prompt_content_max_height"
android:scrollbars="vertical"
android:textAlignment="viewEnd"
android:textSize="18sp"
android:text="@string/authentication_username" />

<org.mozilla.vrbrowser.ui.views.settings.SettingsEditText
android:id="@+id/authUsername"
style="@style/settingsEdit"
android:layout_width="match_parent"
android:layout_weight="100"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
app:highlightedTextColor="@color/fog" />
</LinearLayout>


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/authPasswordLabel"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:gravity="top|end"
android:maxHeight="@dimen/prompt_content_max_height"
android:scrollbars="vertical"
android:textAlignment="viewEnd"
android:textSize="18sp"
android:text="@string/authentication_password" />

<org.mozilla.vrbrowser.ui.views.settings.SettingsEditText
android:id="@+id/authPassword"
style="@style/settingsEdit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="100"
android:layout_marginStart="5dp"
android:inputType="textPassword"
app:highlightedTextColor="@color/fog" />

</LinearLayout>

<LinearLayout
android:id="@+id/switch_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"
android:orientation="horizontal">

<TextView
android:layout_width="110dp"
android:layout_height="wrap_content"
android:textSize="14sp"
android:gravity="top|end"
android:text="@string/authentication_show_password" />

<CheckBox
android:id="@+id/showPasswordCheckbox"
android:layout_marginStart="5dp"
style="@style/settingsSwitch" />

</LinearLayout>

</LinearLayout>


<LinearLayout
android:id="@+id/buttonsLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent">

<Button
android:id="@+id/negativeButton"
android:layout_width="wrap_content"
android:minWidth="110dp"
android:layout_height="wrap_content"
android:background="@drawable/prompt_button_background"
android:fontFamily="sans-serif"
android:padding="10dp"
android:scaleType="fitCenter"
android:text="@string/cancel_button"
android:textAllCaps="true"
android:textColor="@drawable/prompt_button_text_color"
android:textSize="10pt"
android:textStyle="bold" />

<Button
android:id="@+id/positiveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="110dp"
android:background="@drawable/prompt_button_background"
android:fontFamily="sans-serif"
android:padding="10dp"
android:scaleType="fitCenter"
android:text="@string/ok_button"
android:textAllCaps="true"
android:textColor="@drawable/prompt_button_text_color"
android:textSize="10pt"
android:textStyle="bold" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
9 changes: 9 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -570,4 +570,13 @@

<!-- This string is displayed in any button used for removing all the items in the current context. -->
<string name="homepage_hint">Firefox Home (Default)</string>

<!-- This string is displayed in the title of an authentication prompt, which requests a username and a password. -->
<string name="authentication_required">Authentication Required</string>
<!-- This string is displayed as the label of a username input in an authentication prompt. -->
<string name="authentication_username">Username:</string>
<!-- This string is displayed as the label of a password input in an authentication prompt. -->
<string name="authentication_password">Password:</string>
<!-- This string is displayed as the label of a switch of an authentication prompt in order to show or hide the text in a password field. -->
<string name="authentication_show_password">Show password:</string>
</resources>

0 comments on commit 06e4d41

Please sign in to comment.