Skip to content

Commit

Permalink
Better tracking of the specific window content type
Browse files Browse the repository at this point in the history
Replace the integer constants for the library sections in WindowWidget
with an enum that also includes the about: URL for each one.

WindowViewModel.currentContentType keeps track of the specific content
being shown in the window at each moment, which will make it easier to
implement the native UI New Tab page and reorganise the library.

WindowViewModel.isNativeContentVisible depends on the previous field
and provides a boolean value so the current bindings keep working.

This PR should not cause any major behaviour changes.
  • Loading branch information
felipeerias committed Nov 26, 2024
1 parent c764170 commit 998f673
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
import com.igalia.wolvic.ui.widgets.WindowWidget;
import com.igalia.wolvic.ui.widgets.Windows;
import com.igalia.wolvic.ui.widgets.dialogs.CrashDialogWidget;
import com.igalia.wolvic.ui.widgets.dialogs.DeprecatedVersionDialogWidget;
import com.igalia.wolvic.ui.widgets.dialogs.LegalDocumentDialogWidget;
import com.igalia.wolvic.ui.widgets.dialogs.PromptDialogWidget;
import com.igalia.wolvic.ui.widgets.dialogs.SendTabDialogWidget;
Expand Down Expand Up @@ -1104,7 +1103,7 @@ void handleMotionEvent(final int aHandle, final int aDevice, final boolean aFocu
// We shouldn't divide the scale factor when we pass the motion event to the web engine
if (widget instanceof WindowWidget) {
WindowWidget windowWidget = (WindowWidget) widget;
if (!windowWidget.isLibraryVisible()) {
if (!windowWidget.isNativeContentVisible()) {
scale = 1.0f;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.util.TypedValue;
import android.webkit.URLUtil;

Expand All @@ -22,13 +23,16 @@
import com.igalia.wolvic.browser.SettingsStore;
import com.igalia.wolvic.browser.api.WContentBlocking;
import com.igalia.wolvic.ui.widgets.Windows;
import com.igalia.wolvic.utils.SystemUtils;
import com.igalia.wolvic.utils.UrlUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

public class WindowViewModel extends AndroidViewModel {

private static final String LOGTAG = SystemUtils.createLogtag(WindowViewModel.class);

private int mURLProtocolColor;
private int mURLWebsiteColor;

Expand All @@ -48,7 +52,8 @@ public class WindowViewModel extends AndroidViewModel {
private MutableLiveData<ObservableBoolean> isInsecure;
private MutableLiveData<ObservableBoolean> isActiveWindow;
private MediatorLiveData<ObservableBoolean> isTitleBarVisible;
private MutableLiveData<ObservableBoolean> isLibraryVisible;
private MutableLiveData<Windows.ContentType> currentContentType;
private MediatorLiveData<ObservableBoolean> isNativeContentVisible;
private MutableLiveData<ObservableBoolean> isLoading;
private MutableLiveData<ObservableBoolean> isMicrophoneEnabled;
private MutableLiveData<ObservableBoolean> isBookmarked;
Expand Down Expand Up @@ -128,7 +133,12 @@ public WindowViewModel(Application application) {
isTitleBarVisible.addSource(isOnlyWindow, mIsTitleBarVisibleObserver);
isTitleBarVisible.setValue(new ObservableBoolean(true));

isLibraryVisible = new MutableLiveData<>(new ObservableBoolean(false));
currentContentType = new MutableLiveData<>(Windows.ContentType.WEB_CONTENT);
isNativeContentVisible = new MediatorLiveData<>();
isNativeContentVisible.addSource(currentContentType, contentType ->
isNativeContentVisible.setValue(new ObservableBoolean(contentType != Windows.ContentType.WEB_CONTENT))
);
isNativeContentVisible.setValue(new ObservableBoolean(currentContentType.getValue() != Windows.ContentType.WEB_CONTENT));

isLoading = new MutableLiveData<>(new ObservableBoolean(false));
isMicrophoneEnabled = new MutableLiveData<>(new ObservableBoolean(true));
Expand All @@ -151,7 +161,7 @@ public WindowViewModel(Application application) {
isInsecureVisible = new MediatorLiveData<>();
isInsecureVisible.addSource(isInsecure, mIsInsecureVisibleObserver);
isInsecureVisible.addSource(isPrivateSession, mIsInsecureVisibleObserver);
isInsecureVisible.addSource(isLibraryVisible, mIsInsecureVisibleObserver);
isInsecureVisible.addSource(isNativeContentVisible, mIsInsecureVisibleObserver);
isInsecureVisible.setValue(new ObservableBoolean(false));

isMediaAvailable = new MutableLiveData<>(new ObservableBoolean(false));
Expand All @@ -172,13 +182,14 @@ public WindowViewModel(Application application) {
isUrlBarButtonsVisible.addSource(isDrmUsed, mIsUrlBarButtonsVisibleObserver);
isUrlBarButtonsVisible.addSource(isPopUpAvailable, mIsUrlBarButtonsVisibleObserver);
isUrlBarButtonsVisible.addSource(isWebXRUsed, mIsUrlBarButtonsVisibleObserver);
isUrlBarButtonsVisible.addSource(isLibraryVisible, mIsUrlBarButtonsVisibleObserver);
isUrlBarButtonsVisible.addSource(isNativeContentVisible, mIsUrlBarButtonsVisibleObserver);
isUrlBarButtonsVisible.addSource(isFocused, mIsUrlBarButtonsVisibleObserver);
isUrlBarButtonsVisible.setValue(new ObservableBoolean(false));

isUrlBarIconsVisible = new MediatorLiveData<>();
isUrlBarIconsVisible.addSource(isLoading, mIsUrlBarIconsVisibleObserver);
isUrlBarIconsVisible.addSource(isInsecureVisible, mIsUrlBarIconsVisibleObserver);
isUrlBarIconsVisible.addSource(isNativeContentVisible, mIsUrlBarIconsVisibleObserver);
isUrlBarIconsVisible.setValue(new ObservableBoolean(false));

mWidth = new MutableLiveData<>(new ObservableInt());
Expand Down Expand Up @@ -228,7 +239,7 @@ public void onChanged(ObservableBoolean o) {
@Override
public void onChanged(Spannable aUrl) {
String url = aUrl.toString();
if (isLibraryVisible.getValue().get()) {
if (isNativeContentVisible.getValue().get()) {
url = getApplication().getString(R.string.url_library_title);

} else {
Expand Down Expand Up @@ -260,7 +271,7 @@ public void onChanged(ObservableBoolean o) {
(UrlUtils.isDataUri(aUrl) && isPrivateSession.getValue().get()) ||
UrlUtils.isFileUri(aUrl) ||
UrlUtils.isHomeUri(getApplication(), aUrl) ||
isLibraryVisible.getValue().get() ||
isNativeContentVisible.getValue().get() ||
UrlUtils.isBlankUri(getApplication(), aUrl)) {
isInsecureVisible.postValue(new ObservableBoolean(false));

Expand All @@ -281,7 +292,7 @@ public void onChanged(Spannable aUrl) {
if (UrlUtils.isPrivateAboutPage(getApplication(), url) ||
(UrlUtils.isDataUri(url) && isPrivateSession.getValue().get()) ||
UrlUtils.isHomeUri(getApplication(), aUrl.toString()) ||
isLibraryVisible.getValue().get() ||
isNativeContentVisible.getValue().get() ||
UrlUtils.isBlankUri(getApplication(), aUrl.toString())) {
navigationBarUrl.postValue("");

Expand All @@ -297,7 +308,7 @@ public void onChanged(ObservableBoolean o) {
String aUrl = url.getValue().toString();
isUrlBarButtonsVisible.postValue(new ObservableBoolean(
!isFocused.getValue().get() &&
!isLibraryVisible.getValue().get() &&
!isNativeContentVisible.getValue().get() &&
!UrlUtils.isContentFeed(getApplication(), aUrl) &&
!UrlUtils.isPrivateAboutPage(getApplication(), aUrl) &&
(URLUtil.isHttpUrl(aUrl) || URLUtil.isHttpsUrl(aUrl)) &&
Expand All @@ -316,7 +327,7 @@ public void onChanged(ObservableBoolean o) {
@Override
public void onChanged(ObservableBoolean o) {
isUrlBarIconsVisible.postValue(new ObservableBoolean(
!isLibraryVisible.getValue().get() &&
!isNativeContentVisible.getValue().get() &&
(isLoading.getValue().get() ||
isInsecureVisible.getValue().get())
));
Expand Down Expand Up @@ -370,21 +381,20 @@ public void setUrl(@Nullable String url) {
setUrl(new SpannableString(url));
}

public void setUrl(@Nullable Spannable url) {
private void setUrl(@Nullable Spannable url) {
if (url == null) {
return;
}

String aURL = url.toString();

int index = -1;
String aURL;
try {
aURL = URLDecoder.decode(aURL, "UTF-8");
aURL = URLDecoder.decode(url.toString(), "UTF-8");

} catch (UnsupportedEncodingException | IllegalArgumentException e) {
e.printStackTrace();
Log.w(LOGTAG, "Unable to decode URL [ " + url + " ] : " + e);
aURL = "";
}
int index = -1;
if (aURL.startsWith("jar:")) {
return;

Expand All @@ -404,21 +414,18 @@ public void setUrl(@Nullable Spannable url) {
// Update the URL bar only if the URL is different than the current one and
// the URL bar is not focused to avoid override user input
if (!getUrl().getValue().toString().equalsIgnoreCase(aURL) && !getIsFocused().getValue().get()) {
this.url.postValue(new SpannableString(aURL));
if (index > 0) {
SpannableString spannable = new SpannableString(aURL);
ForegroundColorSpan color1 = new ForegroundColorSpan(mURLProtocolColor);
ForegroundColorSpan color2 = new ForegroundColorSpan(mURLWebsiteColor);
spannable.setSpan(color1, 0, index + 3, 0);
spannable.setSpan(color2, index + 3, aURL.length(), 0);
this.url.postValue(url);
this.url.postValue(spannable);

} else {
this.url.postValue(url);
}
}

this.url.postValue(url);
}

@NonNull
Expand All @@ -427,7 +434,7 @@ public MutableLiveData<String> getHint() {
}

private String getHintValue() {
if (isLibraryVisible.getValue().get()) {
if (isNativeContentVisible.getValue().get()) {
return getApplication().getString(R.string.url_library_title);

} else {
Expand Down Expand Up @@ -563,18 +570,17 @@ public void setIsActiveWindow(boolean isActiveWindow) {
this.isActiveWindow.setValue(new ObservableBoolean(isActiveWindow));
}

public void setIsLibraryVisible(boolean isLibraryVisible) {
this.isLibraryVisible.postValue(new ObservableBoolean(isLibraryVisible));
this.url.postValue(this.getUrl().getValue());
public void setCurrentContentType(Windows.ContentType contentType) {
currentContentType.postValue(contentType);
}

public void setIsPanelVisible(boolean isVisible) {
setIsLibraryVisible(isVisible);
@NonNull
public MutableLiveData<Windows.ContentType> getCurrentContentType() {
return currentContentType;
}

@NonNull
public MutableLiveData<ObservableBoolean> getIsLibraryVisible() {
return isLibraryVisible;
public MutableLiveData<ObservableBoolean> getIsNativeContentVisible() {
return isNativeContentVisible;
}

@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class LibraryPanel extends FrameLayout {
private AddonsView mAddonsView;
private SystemNotificationsView mSystemNotificationsView;
private LibraryView mCurrentView;
private @Windows.PanelType int mCurrentPanel;
private Windows.ContentType mCurrentPanel;

public LibraryPanel(@NonNull Context context) {
super(context);
Expand All @@ -64,7 +64,7 @@ protected void initialize() {
mDownloadsView = new DownloadsView(getContext(), this);
mAddonsView = new AddonsView(getContext(), this);
mSystemNotificationsView = new SystemNotificationsView(getContext(), this);
mCurrentPanel = Windows.BOOKMARKS;
mCurrentPanel = Windows.ContentType.BOOKMARKS;

updateUI();
}
Expand Down Expand Up @@ -177,27 +177,27 @@ public void onDestroy() {
mSystemNotificationsView.onDestroy();
}

public @Windows.PanelType int getSelectedPanelType() {
public Windows.ContentType getSelectedPanelType() {
if (mCurrentView == mBookmarksView) {
return Windows.BOOKMARKS;
return Windows.ContentType.BOOKMARKS;

} else if (mCurrentView == mWebAppsView) {
return Windows.WEB_APPS;
return Windows.ContentType.WEB_APPS;

} else if (mCurrentView == mHistoryView) {
return Windows.HISTORY;
return Windows.ContentType.HISTORY;

} else if (mCurrentView == mDownloadsView) {
return Windows.DOWNLOADS;
return Windows.ContentType.DOWNLOADS;

} else if (mCurrentView == mAddonsView) {
return Windows.ADDONS;
return Windows.ContentType.ADDONS;

} else if (mCurrentView == mSystemNotificationsView) {
return Windows.NOTIFICATIONS;
return Windows.ContentType.NOTIFICATIONS;

} else {
return Windows.NONE;
return Windows.ContentType.WEB_CONTENT;
}
}

Expand Down Expand Up @@ -241,30 +241,30 @@ private void selectTab(@NonNull View view) {
mBinding.searchBar.setVisibility(mCurrentView.supportsSearch() ? View.VISIBLE : View.INVISIBLE);
}

public void selectPanel(@Windows.PanelType int panelType) {
public void selectPanel(Windows.ContentType panelType) {
mCurrentPanel = panelType;

if (panelType == Windows.NONE) {
if (panelType == Windows.ContentType.WEB_CONTENT) {
panelType = getSelectedPanelType();
}
switch (panelType) {
case Windows.NONE:
case Windows.BOOKMARKS:
case WEB_CONTENT:
case BOOKMARKS:
selectTab(mBinding.bookmarks);
break;
case Windows.WEB_APPS:
case WEB_APPS:
selectTab(mBinding.webApps);
break;
case Windows.HISTORY:
case HISTORY:
selectTab(mBinding.history);
break;
case Windows.DOWNLOADS:
case DOWNLOADS:
selectTab(mBinding.downloads);
break;
case Windows.ADDONS:
case ADDONS:
selectTab(mBinding.addons);
break;
case Windows.NOTIFICATIONS:
case NOTIFICATIONS:
selectTab(mBinding.notifications);
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
import android.content.res.Configuration;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.net.Uri;
import androidx.preference.PreferenceManager;
import android.util.AttributeSet;
import android.util.Log;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
Expand Down Expand Up @@ -1379,12 +1377,7 @@ public void onSwitchMode() {
public void onAddons() {
hideMenu();

if (!mAttachedWindow.isLibraryVisible()) {
mAttachedWindow.switchPanel(Windows.ADDONS);

} else if (mAttachedWindow.getSelectedPanel() != Windows.ADDONS) {
mAttachedWindow.showPanel(Windows.ADDONS);
}
mAttachedWindow.showPanel(Windows.ContentType.ADDONS);
}

@Override
Expand Down Expand Up @@ -1460,7 +1453,7 @@ public void showPopUpsBlockedNotification() {
mBlockedCount++;
final int currentCount = mBlockedCount;
postDelayed(() -> {
if (currentCount == mBlockedCount && !mViewModel.getIsLibraryVisible().getValue().get()) {
if (currentCount == mBlockedCount && !mViewModel.getIsNativeContentVisible().getValue().get()) {
showNotification(POPUP_NOTIFICATION_ID,
mBinding.navigationBarNavigation.urlBar.getPopUpButton(),
NotificationManager.Notification.TOP,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.igalia.wolvic.BuildConfig;
import com.igalia.wolvic.R;
import com.igalia.wolvic.VRBrowserActivity;
import com.igalia.wolvic.VRBrowserApplication;
Expand Down Expand Up @@ -578,7 +577,7 @@ public void detachFromWindow() {
mWidgetPlacement.parentHandle = -1;

if (mViewModel != null) {
mViewModel.getIsLibraryVisible().removeObserver(mIsLibraryVisible);
mViewModel.getIsNativeContentVisible().removeObserver(mIsNativeContentVisible);
mViewModel.getIsPrivateSession().removeObserver(mIsPrivateSession);
mViewModel = null;
}
Expand All @@ -601,7 +600,7 @@ public void attachToWindow(@NonNull WindowWidget aWindow) {
(VRBrowserActivity)getContext(),
ViewModelProvider.AndroidViewModelFactory.getInstance(((VRBrowserActivity) getContext()).getApplication()))
.get(String.valueOf(mAttachedWindow.hashCode()), WindowViewModel.class);
mViewModel.getIsLibraryVisible().observe((VRBrowserActivity)getContext(), mIsLibraryVisible);
mViewModel.getIsNativeContentVisible().observe((VRBrowserActivity)getContext(), mIsNativeContentVisible);
mViewModel.getIsPrivateSession().observe((VRBrowserActivity)getContext(), mIsPrivateSession);

mBinding.setViewmodel(mViewModel);
Expand All @@ -611,7 +610,7 @@ public void attachToWindow(@NonNull WindowWidget aWindow) {
mIsWindowAttached = true;
}

private Observer<ObservableBoolean> mIsLibraryVisible = aBoolean -> {
private Observer<ObservableBoolean> mIsNativeContentVisible = aBoolean -> {
if (mBinding.libraryButton.isHovered()) {
return;
}
Expand Down
Loading

0 comments on commit 998f673

Please sign in to comment.