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 #984 from MozillaReality/settings_scale
Browse files Browse the repository at this point in the history
Update Settings Dialog to allow for better scaling
  • Loading branch information
MortimerGoro authored Mar 13, 2019
2 parents 7ec6316 + 675218e commit a626f1d
Show file tree
Hide file tree
Showing 44 changed files with 1,444 additions and 163 deletions.
18 changes: 11 additions & 7 deletions app/src/common/shared/org/mozilla/vrbrowser/VRBrowserActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public void run() {
private LinkedList<Pair<Object, Float>> mBrightnessQueue;
private Pair<Object, Float> mCurrentBrightness;
private SearchEngineWrapper mSearchEngineWrapper;
private SettingsStore mSettings;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -181,6 +182,8 @@ protected void onCreate(Bundle savedInstanceState) {
});
mAudioUpdateRunnable = () -> mAudioEngine.update();

mSettings = SettingsStore.getInstance(this);

loadFromIntent(getIntent());
queueRunnable(() -> createOffscreenDisplay());
final String tempPath = getCacheDir().getAbsolutePath();
Expand Down Expand Up @@ -531,7 +534,8 @@ void handleScrollEvent(final int aHandle, final int aDevice, final float aX, fin
runOnUiThread(() -> {
Widget widget = mWidgets.get(aHandle);
if (widget != null) {
MotionEventGenerator.dispatchScroll(widget, aDevice, aX, aY);
float scrollDirection = mSettings.getScrollDirection() == 0 ? 1.0f : -1.0f;
MotionEventGenerator.dispatchScroll(widget, aDevice, aX * scrollDirection, aY * scrollDirection);
} else {
Log.e(LOGTAG, "Failed to find widget for scroll event: " + aHandle);
}
Expand Down Expand Up @@ -965,12 +969,12 @@ public boolean isPermissionGranted(@NonNull String permission) {
}

@Override
public void requestPermission(@NonNull String uri, @NonNull String permission, GeckoSession.PermissionDelegate.Callback aCallback) {
mPermissionDelegate.onAppPermissionRequest(
SessionStore.get().getCurrentSession(),
uri,
permission,
aCallback);
public void requestPermission(String uri, @NonNull String permission, GeckoSession.PermissionDelegate.Callback aCallback) {
if (uri != null && !uri.isEmpty()) {
mPermissionDelegate.onAppPermissionRequest(SessionStore.get().getCurrentSession(), uri, permission, aCallback);
} else {
mPermissionDelegate.onAndroidPermissionsRequest(SessionStore.get().getCurrentSession(), new String[]{permission}, aCallback);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public interface SessionChangeListener {
class SessionSettings {
boolean multiprocess = SettingsStore.getInstance(mContext).isMultiprocessEnabled();
boolean privateMode = false;
boolean trackingProtection = true;
boolean trackingProtection = SettingsStore.getInstance(mContext).isTrackingProtectionEnabled();
boolean suspendMediaWhenInactive = true;
int userAgentMode = SettingsStore.getInstance(mContext).getUaMode();
boolean servo = false;
Expand Down Expand Up @@ -872,7 +872,7 @@ public void setServo(final boolean enabled) {
}
}

public void setMultiprocess(final boolean enabled) {
private void recreateSession(SessionStore.SessionSettings aSettings) {
if (mCurrentSession != null) {
final GeckoResult<GeckoSession.SessionState> state = mCurrentSession.saveState();
state.then(new GeckoResult.OnValueListener<GeckoSession.SessionState, Object>() {
Expand All @@ -884,9 +884,7 @@ public GeckoResult<Object> onValue(@Nullable GeckoSession.SessionState value) th
mCurrentSession.close();

int oldSessionId = getCurrentSessionId();
SessionStore.SessionSettings settings = new SessionStore.SessionSettings();
settings.multiprocess = enabled;
int sessionId = createSession(settings);
int sessionId = createSession(aSettings);
GeckoSession session = getSession(sessionId);
session.restoreState(value);
setCurrentSession(sessionId);
Expand All @@ -906,6 +904,29 @@ public GeckoResult<Object> onException(@NonNull Throwable exception) throws Thro
}
}

private State getCurrentState() {
if (mCurrentSession != null) {
return mSessions.get(mCurrentSession.hashCode());
}
return null;
}

public void setMultiprocess(final boolean aEnabled) {
State state = getCurrentState();
if (state != null && state.mSettings.multiprocess != aEnabled) {
state.mSettings.multiprocess = aEnabled;
recreateSession(state.mSettings);
}
}

public void setTrackingProtection(final boolean aEnabled) {
State state = getCurrentState();
if (state != null && state.mSettings.trackingProtection != aEnabled) {
state.mSettings.trackingProtection = aEnabled;
recreateSession(state.mSettings);
}
}

public void setRemoteDebugging(final boolean enabled) {
if (mRuntime != null) {
mRuntime.getSettings().setRemoteDebuggingEnabled(enabled);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ SettingsStore getInstance(final @NonNull Context aContext) {
public final static boolean CONSOLE_LOGS_DEFAULT = false;
public final static boolean ENV_OVERRIDE_DEFAULT = false;
public final static boolean MULTIPROCESS_DEFAULT = false;
public final static boolean TRACKING_DEFAULT = true;
public final static boolean SERVO_DEFAULT = false;
public final static int UA_MODE_DEFAULT = GeckoSessionSettings.USER_AGENT_MODE_VR;
public final static int INPUT_MODE_DEFAULT = 1;
Expand All @@ -50,6 +51,7 @@ SettingsStore getInstance(final @NonNull Context aContext) {
public final static int MAX_WINDOW_WIDTH_DEFAULT = 1200;
public final static int MAX_WINDOW_HEIGHT_DEFAULT = 1200;
public final static int POINTER_COLOR_DEFAULT_DEFAULT = Color.parseColor("#FFFFFF");
public final static int SCROLL_DIRECTION_DEFAULT = 0;
public final static String ENV_DEFAULT = "cave";
public final static float BROWSER_WORLD_WIDTH_DEFAULT = 4.0f;
public final static float BROWSER_WORLD_HEIGHT_DEFAULT = 2.25f;
Expand All @@ -61,6 +63,8 @@ SettingsStore getInstance(final @NonNull Context aContext) {
private final static boolean enableCrashReportingByDefault = false;
private final static boolean enableTelemetryByDefault = true;

private int mCachedScrollDirection = -1;

public SettingsStore(Context aContext) {
mContext = aContext;
mPrefs = PreferenceManager.getDefaultSharedPreferences(aContext);
Expand Down Expand Up @@ -134,6 +138,17 @@ public void setConsoleLogsEnabled(boolean isEnabled) {
editor.commit();
}

public boolean isTrackingProtectionEnabled() {
return mPrefs.getBoolean(
mContext.getString(R.string.settings_key_tracking_protection), TRACKING_DEFAULT);
}

public void setTrackingProtectionEnabled(boolean isEnabled) {
SharedPreferences.Editor editor = mPrefs.edit();
editor.putBoolean(mContext.getString(R.string.settings_key_tracking_protection), isEnabled);
editor.commit();
}

public boolean isEnvironmentOverrideEnabled() {
return mPrefs.getBoolean(
mContext.getString(R.string.settings_key_environment_override), ENV_OVERRIDE_DEFAULT);
Expand All @@ -145,6 +160,7 @@ public void setEnvironmentOverrideEnabled(boolean isEnabled) {
editor.commit();
}


public boolean isMultiprocessEnabled() {
return mPrefs.getBoolean(
mContext.getString(R.string.settings_key_multiprocess), MULTIPROCESS_DEFAULT);
Expand Down Expand Up @@ -314,6 +330,21 @@ public void setPointerColor(int color) {
editor.commit();
}

public int getScrollDirection() {
if (mCachedScrollDirection < 0) {
mCachedScrollDirection = mPrefs.getInt(mContext.getString(R.string.settings_key_scroll_direction), SCROLL_DIRECTION_DEFAULT);
}
return mCachedScrollDirection;
}

public void setScrollDirection(int aScrollDirection) {
mCachedScrollDirection = aScrollDirection;
SharedPreferences.Editor editor = mPrefs.edit();
editor.putInt(mContext.getString(R.string.settings_key_scroll_direction), aScrollDirection);
editor.commit();
}


public int getMSAALevel() {
return mPrefs.getInt(
mContext.getString(R.string.settings_key_msaa), MSAA_DEFAULT_LEVEL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
Expand All @@ -17,6 +18,8 @@ public class ButtonSetting extends LinearLayout {
private String mButtonText;
private TextView mButton;
private OnClickListener mListener;
private Drawable mButtonBackground;
private Drawable mButtonForeground;

public ButtonSetting(Context context, AttributeSet attrs) {
this(context, attrs, 0);
Expand Down Expand Up @@ -44,6 +47,8 @@ private void initialize(Context aContext) {
mButton = findViewById(R.id.button);
mButton.setText(mButtonText);
mButton.setOnClickListener(mInternalClickListener);
mButtonBackground = mButton.getBackground();
mButtonForeground = mButton.getForeground();
}

private View.OnClickListener mInternalClickListener = v -> onClickListener(v);
Expand All @@ -64,4 +69,23 @@ public void setOnClickListener(OnClickListener aListener) {
mListener = aListener;
}

public void setShowAsLabel(boolean aShowAsLabel) {
if (aShowAsLabel) {
mButton.setBackground(null);
mButton.setForeground(null);
mButton.setTextColor(getContext().getColor(R.color.fog));
} else {
mButton.setBackground(mButtonBackground);
mButton.setForeground(mButtonForeground);
}
}

public void setButtonText(String aText) {
mButton.setText(aText);
}

public String getDescription() {
return mDescription;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package org.mozilla.vrbrowser.ui.views.settings;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.InputType;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;

import org.mozilla.vrbrowser.R;
import org.mozilla.vrbrowser.audio.AudioEngine;

import androidx.annotation.IdRes;

public class ImageRadioGroupSetting extends LinearLayout {

public interface OnCheckedChangeListener {
void onCheckedChanged(@IdRes int checkedId, boolean apply);
}

private AudioEngine mAudio;
private CharSequence[] mOptions;
private Object[] mValues;
private Drawable[] mImages;
private OnCheckedChangeListener mRadioGroupListener;
private ImageRadioButton[] mItems;

public ImageRadioGroupSetting(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

public ImageRadioGroupSetting(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);

TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.RadioGroupSetting, defStyleAttr, 0);
mOptions = attributes.getTextArray(R.styleable.RadioGroupSetting_options);
int id = attributes.getResourceId(R.styleable.RadioGroupSetting_values, 0);
TypedArray array = context.getResources().obtainTypedArray(id);
if (array.getType(0) == TypedValue.TYPE_STRING) {
mValues = getResources().getStringArray(id);

} else if (array.getType(0) == TypedValue.TYPE_INT_HEX ||
array.getType(0) == TypedValue.TYPE_INT_DEC) {
int [] values = getResources().getIntArray(id);
mValues = new Integer[values.length];
for (int i=0; i<values.length; i++) {
mValues[i] = values[i];
}
}
array.recycle();

id = attributes.getResourceId(R.styleable.RadioGroupSetting_images, 0);

array = context.getResources().obtainTypedArray(id);
mImages = new Drawable[mOptions.length];
for (int i = 0; i < mOptions.length; ++i) {
mImages[i] = array.getDrawable(i);
}
array.recycle();

attributes.recycle();
initialize(context);
}

protected void initialize(Context aContext) {
mAudio = AudioEngine.fromContext(aContext);
setOrientation(LinearLayout.VERTICAL);
mItems = new ImageRadioButton[mOptions.length];

for (int i= 0; i < mOptions.length; i++) {
ImageRadioButton button = new ImageRadioButton(aContext);
button.setValues(i, mOptions[i].toString(), mImages[i]);
button.setChecked(false);
final int checkedId = i;
button.setOnClickListener(v -> {
if (mAudio != null) {
mAudio.playSound(AudioEngine.Sound.CLICK);
}
setChecked(checkedId, true);

});
addView(button);
mItems[i] = button;
}
}

static class ImageRadioButton extends LinearLayout {
private ImageView mImage;
private RadioButton mRadioButton;
public ImageRadioButton(Context context) {
super(context);
initialize(context);
}

private void initialize(Context aContext) {
inflate(aContext, R.layout.setting_radio_item, this);
mImage = findViewById(R.id.radioItemImage);
mRadioButton = findViewById(R.id.radioItemButton);
mRadioButton.setInputType(InputType.TYPE_NULL);
mRadioButton.setSoundEffectsEnabled(false);
}

public void setValues(int aItemId, String aText, Drawable aImage) {
mRadioButton.setId(aItemId);
mRadioButton.setText(aText);
if (aImage != null) {
mImage.setImageDrawable(aImage);
}
}

public void setChecked(boolean aChecked) {
mRadioButton.setChecked(aChecked);
}

public boolean isChecked() {
return mRadioButton.isChecked();
}

@Override
public void setOnClickListener(View.OnClickListener aListener) {
super.setOnClickListener(aListener);
mRadioButton.setOnClickListener(aListener);
mImage.setOnClickListener(aListener);
}
}

public void setChecked(@IdRes int checkedId, boolean doApply) {
for (int i = 0; i < mItems.length; i++) {
mItems[i].setChecked(i == checkedId);
}

if (mRadioGroupListener != null && doApply) {
mRadioGroupListener.onCheckedChanged(checkedId, doApply);
}
}

public void setOnCheckedChangeListener(OnCheckedChangeListener aListener) {
mRadioGroupListener = aListener;
}

public Object getValueForId(@IdRes int checkId) {
return mValues[checkId];
}

public int getIdForValue(Object value) {
for (int i = 0; i < mValues.length; i++) {
if (mValues[i].equals(value)) {
return i;
}
}

return 0;
}

public int getCheckedRadioButtonId() {
for (int i = 0; i < mItems.length; ++i) {
if (mItems[i].isChecked()) {
return i;
}
}
return -1;
}

}
Loading

0 comments on commit a626f1d

Please sign in to comment.