This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #985 from MozillaReality/basic_auth
Implement basic auth prompt
- Loading branch information
Showing
5 changed files
with
342 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/prompts/AuthPromptWidget.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters