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

Commit

Permalink
Fixes #2693 Fix system language change crash and support for language…
Browse files Browse the repository at this point in the history
… hot reload (#2705)

* Support for config changes and language hot reload

* ViewModel migration


ViewModel migration
  • Loading branch information
keianhzo authored Jan 31, 2020
1 parent 854287c commit 8503280
Show file tree
Hide file tree
Showing 62 changed files with 2,107 additions and 1,340 deletions.
55 changes: 51 additions & 4 deletions app/src/common/shared/org/mozilla/vrbrowser/VRBrowserActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
import androidx.annotation.Keep;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleRegistry;
import androidx.lifecycle.ViewModelStore;
import androidx.lifecycle.ViewModelStoreOwner;

import org.mozilla.geckoview.GeckoRuntime;
import org.mozilla.geckoview.GeckoSession;
Expand Down Expand Up @@ -81,11 +86,12 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Consumer;

public class VRBrowserActivity extends PlatformActivity implements WidgetManagerDelegate, ComponentCallbacks2 {
public class VRBrowserActivity extends PlatformActivity implements WidgetManagerDelegate, ComponentCallbacks2, LifecycleOwner, ViewModelStoreOwner {

private BroadcastReceiver mCrashReceiver = new BroadcastReceiver() {
@Override
Expand All @@ -97,6 +103,29 @@ public void onReceive(Context context, Intent intent) {
}
};

private final LifecycleRegistry mLifeCycle;

@NonNull
@Override
public Lifecycle getLifecycle() {
return mLifeCycle;
}

private final ViewModelStore mViewModelStore;

@NonNull
@Override
public ViewModelStore getViewModelStore() {
return mViewModelStore;
}

public VRBrowserActivity() {
mLifeCycle = new LifecycleRegistry(this);
mLifeCycle.setCurrentState(Lifecycle.State.INITIALIZED);

mViewModelStore = new ViewModelStore();
}

class SwipeRunnable implements Runnable {
boolean mCanceled = false;
@Override
Expand Down Expand Up @@ -269,6 +298,8 @@ protected void onCreate(Bundle savedInstanceState) {
mConnectivityReceiver = new ConnectivityReceiver();
mPoorPerformanceWhiteList = new HashSet<>();
checkForCrash();

mLifeCycle.setCurrentState(Lifecycle.State.CREATED);
}

protected void initializeWidgets() {
Expand Down Expand Up @@ -362,6 +393,7 @@ protected void onStart() {
super.onStart();
TelemetryWrapper.start();
UISurfaceTextureRenderer.setRenderActive(true);
mLifeCycle.setCurrentState(Lifecycle.State.STARTED);
}

@Override
Expand Down Expand Up @@ -423,6 +455,7 @@ protected void onResume() {
((VRBrowserApplication)getApplicationContext()).getAccounts().pollForEventsAsync();

super.onResume();
mLifeCycle.setCurrentState(Lifecycle.State.RESUMED);
}

@Override
Expand Down Expand Up @@ -455,6 +488,8 @@ protected void onDestroy() {


super.onDestroy();
mLifeCycle.setCurrentState(Lifecycle.State.DESTROYED);
mViewModelStore.clear();
}

@Override
Expand All @@ -473,6 +508,11 @@ protected void onNewIntent(final Intent intent) {

@Override
public void onConfigurationChanged(Configuration newConfig) {
getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());

LocaleUtils.refresh();
mWidgets.forEach((i, widget) -> widget.onConfigurationChanged(newConfig));

SessionStore.get().onConfigurationChanged(newConfig);

super.onConfigurationChanged(newConfig);
Expand Down Expand Up @@ -502,7 +542,7 @@ void loadFromIntent(final Intent intent) {
SettingsStore.getInstance(this).setHomepage(homepageUri.toString());
}

// Enable/Disbale e10s
// Enable/Disable e10s
if (extras.containsKey("e10s")) {
boolean wasEnabled = SettingsStore.getInstance(this).isMultiprocessEnabled();
boolean enabled = extras.getBoolean("e10s", wasEnabled);
Expand Down Expand Up @@ -1445,7 +1485,7 @@ public boolean canOpenNewWindow() {
@Override
public void openNewWindow(String uri) {
WindowWidget newWindow = mWindows.addWindow();
if (newWindow != null) {
if ((newWindow != null) && (newWindow.getSession() != null)) {
newWindow.getSession().loadUri(uri);
}
}
Expand Down Expand Up @@ -1475,6 +1515,13 @@ public void saveState() {
mWindows.saveState();
}

@Override
public void updateLocale(@NonNull Context context) {
Configuration configuration = context.getResources().getConfiguration();
configuration.setLocale(Locale.forLanguageTag(LocaleUtils.getDisplayLanguage(this).getId()));
onConfigurationChanged(new Configuration(context.getResources().getConfiguration()));
}

private native void addWidgetNative(int aHandle, WidgetPlacement aPlacement);
private native void updateWidgetNative(int aHandle, WidgetPlacement aPlacement);
private native void updateVisibleWidgetsNative();
Expand All @@ -1483,7 +1530,7 @@ public void saveState() {
private native void finishWidgetResizeNative(int aHandle);
private native void startWidgetMoveNative(int aHandle, int aMoveBehaviour);
private native void finishWidgetMoveNative();
private native void setWorldBrightnessNative(float aBrigthness);
private native void setWorldBrightnessNative(float aBrightness);
private native void setTemporaryFilePath(String aPath);
private native void exitImmersiveNative();
private native void workaroundGeckoSigAction();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ protected void attachBaseContext(Context base) {
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
LocaleUtils.setDisplayLocale(this, newConfig.getLocales().get(0).toLanguageTag());
LocaleUtils.setLocale(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ public void onPlaybackStateChange(MediaElement mediaElement, int playbackState)
mPlaying = false;
} else if (playbackState == MediaElement.MEDIA_STATE_ENDED) {
mEnded = true;
} else if (playbackState == MediaElement.MEDIA_STATE_EMPTIED) {
mEnded = true;
mPlaying = false;
mIsUnloaded = true;
}
mMediaListeners.forEach(listener -> listener.onPlaybackStateChange(mediaElement, playbackState));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@ private void showPopUp(int sessionId, @NonNull Pair<String, LinkedList<PopUpRequ
}

mPopUpPrompt.hide(UIWidget.REMOVE_WIDGET);
mPopUpPrompt.releaseWidget();
mPopUpPrompt = null;
});
mPopUpPrompt.setDelegate(() -> mExecutors.mainThread().execute(() -> {
if (mPopupDelegate != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.mozilla.vrbrowser.utils.BitmapCache;
import org.mozilla.vrbrowser.utils.InternalPages;
import org.mozilla.vrbrowser.utils.SystemUtils;
import org.mozilla.vrbrowser.utils.UrlUtils;

import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -569,9 +570,7 @@ public String getHomeUri() {
}

public Boolean isHomeUri(String aUri) {
return aUri != null && aUri.toLowerCase().startsWith(
SettingsStore.getInstance(mContext).getHomepage()
);
return UrlUtils.isHomeUri(mContext, aUri);
}

public String getCurrentUri() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ public void registerForUpdates() {

public void unregisterForUpdates() {
if (mContext != null) {
mContext.unregisterReceiver(mLocaleChangedReceiver);
try {
mContext.unregisterReceiver(mLocaleChangedReceiver);

} catch(IllegalArgumentException ignored) {}
if (mPrefs != null) {
mPrefs.unregisterOnSharedPreferenceChangeListener(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.DimenRes;
import androidx.annotation.Dimension;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.databinding.BindingAdapter;

import org.mozilla.vrbrowser.R;
import org.mozilla.vrbrowser.ui.views.HoneycombButton;
import org.mozilla.vrbrowser.ui.views.UIButton;
import org.mozilla.vrbrowser.ui.views.UITextButton;
import org.mozilla.vrbrowser.ui.views.settings.ButtonSetting;
import org.mozilla.vrbrowser.ui.views.settings.SwitchSetting;

Expand Down Expand Up @@ -154,4 +153,15 @@ public static void setLayoutWidth(@NonNull ImageView view, @NonNull @Dimension f
params.height = (int)dimen;
view.setLayoutParams(params);
}
}

@BindingAdapter("privateMode")
public static void setPrivateMode(@NonNull UIButton button, boolean isPrivateMode) {
button.setPrivateMode(isPrivateMode);
}

@BindingAdapter("privateMode")
public static void setPrivateMode(@NonNull UITextButton button, boolean isPrivateMode) {
button.setPrivateMode(isPrivateMode);
}

}
Loading

0 comments on commit 8503280

Please sign in to comment.