Skip to content

Commit

Permalink
Follow system language (#2781)
Browse files Browse the repository at this point in the history
* Languages refactor

* Fixes first launch crash

* Stop using a hardcoded locale in the sumo pages

* Fix capitalizing string exception.

Co-authored-by: Daosheng Mu <daoshengmu@gmail.com>
  • Loading branch information
keianhzo and daoshengmu committed Feb 14, 2020
1 parent a81f0f0 commit 8a820da
Show file tree
Hide file tree
Showing 19 changed files with 397 additions and 364 deletions.
Expand Up @@ -58,6 +58,7 @@
import org.mozilla.vrbrowser.telemetry.GleanMetricsService;
import org.mozilla.vrbrowser.telemetry.TelemetryWrapper;
import org.mozilla.vrbrowser.ui.OffscreenDisplay;
import org.mozilla.vrbrowser.ui.adapters.Language;
import org.mozilla.vrbrowser.ui.widgets.KeyboardWidget;
import org.mozilla.vrbrowser.ui.widgets.NavigationBarWidget;
import org.mozilla.vrbrowser.ui.widgets.RootWidget;
Expand Down Expand Up @@ -86,7 +87,6 @@
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;
Expand Down Expand Up @@ -210,7 +210,8 @@ public void onGlobalFocusChanged(View oldFocus, View newFocus) {

@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(LocaleUtils.setLocale(base));
Context newContext = LocaleUtils.init(base);
super.attachBaseContext(newContext);
}

@Override
Expand All @@ -230,8 +231,6 @@ protected void onCreate(Bundle savedInstanceState) {
// Set a global exception handler as soon as possible
GlobalExceptionHandler.register(this.getApplicationContext());

LocaleUtils.init();

if (DeviceType.isOculusBuild()) {
workaroundGeckoSigAction();
}
Expand All @@ -243,7 +242,7 @@ protected void onCreate(Bundle savedInstanceState) {
SessionStore.get().setContext(this, extras);
SessionStore.get().initializeServices();
SessionStore.get().initializeStores(this);
SessionStore.get().setLocales(LocaleUtils.getPreferredLocales(this));
SessionStore.get().setLocales(LocaleUtils.getPreferredLanguageTags(this));

// Create broadcast receiver for getting crash messages from crash process
IntentFilter intentFilter = new IntentFilter();
Expand Down Expand Up @@ -508,9 +507,12 @@ protected void onNewIntent(final Intent intent) {

@Override
public void onConfigurationChanged(Configuration newConfig) {
Language language = LocaleUtils.getDisplayLanguage(this);
newConfig.setLocale(language.getLocale());
getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());

LocaleUtils.refresh();
LocaleUtils.update(this, language);

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

SessionStore.get().onConfigurationChanged(newConfig);
Expand Down Expand Up @@ -1527,9 +1529,7 @@ public void 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()));
onConfigurationChanged(context.getResources().getConfiguration());
}

private native void addWidgetNative(int aHandle, WidgetPlacement aPlacement);
Expand Down
Expand Up @@ -16,6 +16,7 @@
import org.mozilla.vrbrowser.db.DataRepository;
import org.mozilla.vrbrowser.telemetry.GleanMetricsService;
import org.mozilla.vrbrowser.telemetry.TelemetryWrapper;
import org.mozilla.vrbrowser.ui.adapters.Language;
import org.mozilla.vrbrowser.utils.BitmapCache;
import org.mozilla.vrbrowser.utils.LocaleUtils;

Expand Down Expand Up @@ -43,15 +44,16 @@ public void onCreate() {

@Override
protected void attachBaseContext(Context base) {
LocaleUtils.saveSystemLocale();
super.attachBaseContext(LocaleUtils.setLocale(base));
Context context = LocaleUtils.init(base);
super.attachBaseContext(context);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
Context context = LocaleUtils.init(this);
Language language = LocaleUtils.getDisplayLanguage(context);
newConfig.setLocale(language.getLocale());
super.onConfigurationChanged(newConfig);
LocaleUtils.setDisplayLocale(this, newConfig.getLocales().get(0).toLanguageTag());
LocaleUtils.setLocale(this);
}

public Services getServices() {
Expand Down
Expand Up @@ -279,7 +279,7 @@ public void setRemoteDebugging(final boolean enabled) {

public void setLocales(List<String> locales) {
if (mRuntime != null) {
mRuntime.getSettings().setLocales(locales.stream().toArray(String[]::new));
mRuntime.getSettings().setLocales(locales.toArray(new String[0]));
}
}

Expand Down
@@ -1,23 +1,40 @@
package org.mozilla.vrbrowser.ui.adapters;

import org.mozilla.vrbrowser.utils.StringUtils;

import java.util.Locale;

public class Language {

public Language(String id, String name) {
this.id = id;
this.name = name;
public Language(Locale locale) {
this.locale = locale;
this.isPreferred = false;

String languageId = locale.toLanguageTag();
String displayName = StringUtils.capitalize(locale.getDisplayName());
this.displayName = displayName + " [" + languageId + "]";
}

public Language(Locale locale, String displayName) {
this.locale = locale;
this.isPreferred = false;
this.displayName = StringUtils.capitalize(displayName);
}

private String name;
private String id;
private Locale locale;
private boolean isPreferred;
private String displayName;

public Locale getLocale() {
return this.locale;
}

public String getId() {
return this.id;
public String getDisplayName() {
return this.displayName;
}

public String getName() {
return this.name;
public String getLanguageTag() {
return this.locale.toLanguageTag();
}

public void setPreferred(boolean isPreferred) {
Expand All @@ -30,6 +47,6 @@ public boolean isPreferred() {

@Override
public int hashCode() {
return id.hashCode();
return locale.hashCode();
}
}
Expand Up @@ -72,7 +72,7 @@ public void addItem(Language language) {

public void addItemAlphabetical(Language language) {
int index = Collections.binarySearch(mLanguagesList, language,
(ob1, ob2) -> ob1.getName().compareToIgnoreCase(ob2.getName()));
(ob1, ob2) -> ob1.getLanguageTag().compareToIgnoreCase(ob2.getLanguageTag()));

if (index < 0) {
index = (index * -1) - 1;
Expand Down
Expand Up @@ -94,7 +94,7 @@ public WindowViewModel(Application application) {
isTopBarVisible.addSource(isResizeMode, mIsTopBarVisibleObserver);
isTopBarVisible.addSource(isPrivateSession, mIsTopBarVisibleObserver);
isTopBarVisible.addSource(isWindowVisible, mIsTopBarVisibleObserver);
isTopBarVisible.setValue(new ObservableBoolean(false));
isTopBarVisible.setValue(new ObservableBoolean(true));

showClearButton = new MediatorLiveData<>();
showClearButton.addSource(isOnlyWindow, mShowClearButtonObserver);
Expand Down Expand Up @@ -160,14 +160,14 @@ public WindowViewModel(Application application) {
@Override
public void onChanged(ObservableBoolean o) {
if (isFullscreen.getValue().get() || isResizeMode.getValue().get() || !isWindowVisible.getValue().get()) {
isTopBarVisible.setValue(new ObservableBoolean(false));
isTopBarVisible.postValue(new ObservableBoolean(false));

} else {
if (isOnlyWindow.getValue().get()) {
isTopBarVisible.setValue(new ObservableBoolean(isPrivateSession.getValue().get()));
isTopBarVisible.postValue(new ObservableBoolean(isPrivateSession.getValue().get()));

} else {
isTopBarVisible.setValue(new ObservableBoolean(true));
isTopBarVisible.postValue(new ObservableBoolean(true));
}
}
}
Expand All @@ -176,7 +176,7 @@ public void onChanged(ObservableBoolean o) {
private Observer<ObservableBoolean> mShowClearButtonObserver = new Observer<ObservableBoolean>() {
@Override
public void onChanged(ObservableBoolean o) {
showClearButton.setValue(new ObservableBoolean(isWindowVisible.getValue().get() &&
showClearButton.postValue(new ObservableBoolean(isWindowVisible.getValue().get() &&
isPrivateSession.getValue().get() && isOnlyWindow.getValue().get() &&
!isResizeMode.getValue().get() && !isFullscreen.getValue().get()));
}
Expand All @@ -186,21 +186,21 @@ public void onChanged(ObservableBoolean o) {
@Override
public void onChanged(ObservableBoolean o) {
if (isFullscreen.getValue().get() || isResizeMode.getValue().get() || isActiveWindow.getValue().get()) {
isTitleBarVisible.setValue(new ObservableBoolean(false));
isTitleBarVisible.postValue(new ObservableBoolean(false));

} else {
isTitleBarVisible.setValue(new ObservableBoolean(isWindowVisible.getValue().get() && !isOnlyWindow.getValue().get()));
isTitleBarVisible.postValue(new ObservableBoolean(isWindowVisible.getValue().get() && !isOnlyWindow.getValue().get()));
}
}
};

private Observer<ObservableBoolean> mIsLibraryVisibleObserver = new Observer<ObservableBoolean>() {
@Override
public void onChanged(ObservableBoolean o) {
isLibraryVisible.setValue(new ObservableBoolean(isBookmarksVisible.getValue().get() || isHistoryVisible.getValue().get()));
isLibraryVisible.postValue(new ObservableBoolean(isBookmarksVisible.getValue().get() || isHistoryVisible.getValue().get()));

// We use this to force dispatch a title bar and navigation bar URL refresh when library is opened
url.setValue(url.getValue());
url.postValue(url.getValue());
}
};

Expand Down Expand Up @@ -236,7 +236,7 @@ public void onChanged(Spannable aUrl) {
}
}

titleBarUrl.setValue(UrlUtils.titleBarUrl(url));
titleBarUrl.postValue(UrlUtils.titleBarUrl(url));
}
};

Expand All @@ -250,14 +250,14 @@ public void onChanged(ObservableBoolean o) {
UrlUtils.isHomeUri(getApplication(), aUrl) ||
isLibraryVisible.getValue().get() ||
UrlUtils.isBlankUri(getApplication(), aUrl)) {
isInsecureVisible.setValue(new ObservableBoolean(false));
isInsecureVisible.postValue(new ObservableBoolean(false));

} else {
isInsecureVisible.setValue(new ObservableBoolean(true));
isInsecureVisible.postValue(new ObservableBoolean(true));
}

} else {
isInsecureVisible.setValue(new ObservableBoolean(false));
isInsecureVisible.postValue(new ObservableBoolean(false));
}
}
};
Expand All @@ -271,20 +271,20 @@ public void onChanged(Spannable aUrl) {
UrlUtils.isHomeUri(getApplication(), aUrl.toString()) ||
isLibraryVisible.getValue().get() ||
UrlUtils.isBlankUri(getApplication(), aUrl.toString())) {
navigationBarUrl.setValue("");
navigationBarUrl.postValue("");

} else {
navigationBarUrl.setValue(url);
navigationBarUrl.postValue(url);
}

if (isBookmarksVisible.getValue().get()) {
hint.setValue(getApplication().getString(R.string.url_bookmarks_title));
hint.postValue(getApplication().getString(R.string.url_bookmarks_title));

} else if (isHistoryVisible.getValue().get()) {
hint.setValue(getApplication().getString(R.string.url_history_title));
hint.postValue(getApplication().getString(R.string.url_history_title));

} else {
hint.setValue(getApplication().getString(R.string.search_placeholder));
hint.postValue(getApplication().getString(R.string.search_placeholder));
}
}
};
Expand Down Expand Up @@ -365,21 +365,21 @@ 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.setValue(new SpannableString(aURL));
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.setValue(url);
this.url.postValue(url);

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

this.url.setValue(url);
this.url.postValue(url);
}

@NonNull
Expand Down Expand Up @@ -532,7 +532,7 @@ public MutableLiveData<ObservableBoolean> getIsBookmarked() {
}

public void setIsBookmarked(boolean isBookmarked) {
this.isBookmarked.setValue(new ObservableBoolean(isBookmarked));
this.isBookmarked.postValue(new ObservableBoolean(isBookmarked));
}

@NonNull
Expand Down
Expand Up @@ -240,7 +240,6 @@ public void detachFromWindow() {
if (mViewModel != null) {
mViewModel.getIsLoading().removeObserver(mIsLoadingObserver);
mViewModel.getIsBookmarked().removeObserver(mIsBookmarkedObserver);
mViewModel.getHint().removeObserver(mHintObserver);
mViewModel = null;
}
}
Expand All @@ -255,7 +254,6 @@ public void attachToWindow(@NonNull WindowWidget aWindow) {

mViewModel.getIsLoading().observe((VRBrowserActivity)getContext(), mIsLoadingObserver);
mViewModel.getIsBookmarked().observe((VRBrowserActivity)getContext(), mIsBookmarkedObserver);
mViewModel.getHint().observe((VRBrowserActivity)getContext(), mHintObserver);
}

public void setSession(Session session) {
Expand Down Expand Up @@ -316,8 +314,6 @@ private void handleBookmarkClick() {

private Observer<ObservableBoolean> mIsBookmarkedObserver = aBoolean -> mBinding.bookmarkButton.clearFocus();

private Observer<String> mHintObserver = hint -> mBinding.urlEditText.setHint(hint);

public String getText() {
return mBinding.urlEditText.getText().toString();
}
Expand Down
Expand Up @@ -93,6 +93,8 @@ private void initialize(Context aContext) {
}

public void updateUI() {
removeAllViews();

LayoutInflater inflater = LayoutInflater.from(getContext());

// Inflate this data binding layout
Expand Down
Expand Up @@ -228,7 +228,7 @@ public void startVoiceSearch() {
ActivityCompat.requestPermissions((Activity)getContext(), new String[]{Manifest.permission.RECORD_AUDIO},
VOICE_SEARCH_AUDIO_REQUEST_CODE);
} else {
String locale = LocaleUtils.getVoiceSearchLocale(getContext());
String locale = LocaleUtils.getVoiceSearchLanguageTag(getContext());
mMozillaSpeechService.setLanguage(LocaleUtils.mapToMozillaSpeechLocales(locale));
boolean storeData = SettingsStore.getInstance(getContext()).isSpeechDataCollectionEnabled();
if (SessionStore.get().getActiveSession().isPrivateMode()) {
Expand Down

0 comments on commit 8a820da

Please sign in to comment.