Skip to content

Commit

Permalink
Add support for display/voice/content languages (#1474)
Browse files Browse the repository at this point in the history
* Add support for display/voice/content languages

* Fixed crash when running using an applicationId suffix
  • Loading branch information
keianhzo committed Aug 2, 2019
1 parent c03ef4b commit 451ce73
Show file tree
Hide file tree
Showing 56 changed files with 2,034 additions and 155 deletions.
Expand Up @@ -63,6 +63,7 @@
import org.mozilla.vrbrowser.ui.widgets.WindowWidget;
import org.mozilla.vrbrowser.ui.widgets.dialogs.CrashDialogWidget;
import org.mozilla.vrbrowser.utils.ConnectivityReceiver;
import org.mozilla.vrbrowser.utils.LocaleUtils;
import org.mozilla.vrbrowser.utils.ServoUtils;

import java.io.IOException;
Expand Down Expand Up @@ -173,6 +174,11 @@ public void onGlobalFocusChanged(View oldFocus, View newFocus) {
}
};

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

@Override
protected void onCreate(Bundle savedInstanceState) {
// Fix for infinite restart on startup crashes.
Expand All @@ -189,6 +195,8 @@ protected void onCreate(Bundle savedInstanceState) {
// Set a global exception handler as soon as possible
GlobalExceptionHandler.register(this.getApplicationContext());

LocaleUtils.init(this);

if (DeviceType.isOculusBuild()) {
workaroundGeckoSigAction();
}
Expand Down
Expand Up @@ -6,10 +6,13 @@
package org.mozilla.vrbrowser;

import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;

import org.mozilla.vrbrowser.browser.Places;
import org.mozilla.vrbrowser.db.AppDatabase;
import org.mozilla.vrbrowser.telemetry.TelemetryWrapper;
import org.mozilla.vrbrowser.utils.LocaleUtils;

public class VRBrowserApplication extends Application {

Expand All @@ -26,6 +29,18 @@ public void onCreate() {
TelemetryWrapper.init(this);
}

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

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
LocaleUtils.setLocale(this);
}

public AppDatabase getDatabase() {
return AppDatabase.getInstance(this, mAppExecutors);
}
Expand Down
Expand Up @@ -1000,6 +1000,12 @@ public boolean getAutoplayEnabled() {
return false;
}

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

// NavigationDelegate

@Override
Expand Down
Expand Up @@ -6,6 +6,8 @@
import android.os.StrictMode;
import android.preference.PreferenceManager;

import org.json.JSONArray;
import org.json.JSONException;
import org.mozilla.geckoview.GeckoSessionSettings;
import org.mozilla.telemetry.TelemetryHolder;
import org.mozilla.vrbrowser.R;
Expand All @@ -16,6 +18,8 @@

import androidx.annotation.NonNull;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import static org.mozilla.vrbrowser.utils.ServoUtils.isServoAvailable;
Expand Down Expand Up @@ -408,21 +412,63 @@ public void setAudioEnabled(boolean isEnabled) {
editor.commit();
}

public String getVoiceSearchLanguage() {
public String getVoiceSearchLocale() {
String language = mPrefs.getString(
mContext.getString(R.string.settings_key_voice_search_language), null);
if (language == null) {
return LocaleUtils.getDefaultVoiceSearchLanguage(mContext);
return LocaleUtils.getDefaultVoiceSearchLocale(mContext);
}
return language;
}

public void setVoiceSearchLanguage(String language) {
public void setVoiceSearchLocale(String language) {
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString(mContext.getString(R.string.settings_key_voice_search_language), language);
editor.commit();
}

public String getDisplayLocale() {
String language = mPrefs.getString(
mContext.getString(R.string.settings_key_display_language), null);
if (language == null) {
return LocaleUtils.getDefaultDisplayLocale(mContext);
}
return language;
}

public void setDisplayLocale(String language) {
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString(mContext.getString(R.string.settings_key_display_language), language);
editor.commit();
}

public ArrayList<String> getContentLocales() {
ArrayList<String> result = new ArrayList<>();

String json = mPrefs.getString(
mContext.getString(R.string.settings_key_content_languages),
new JSONArray().toString());

try {
JSONArray jsonArray = new JSONArray(json);
for (int i=0; i<jsonArray.length(); i++) {
result.add(jsonArray.getString(i));
}

} catch (JSONException e) {
e.printStackTrace();
}

return result;
}

public void setContentLocales(List<String> languages) {
JSONArray json = new JSONArray(languages);
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString(mContext.getString(R.string.settings_key_content_languages), json.toString());
editor.commit();
}

public float getCylinderDensity() {
return mPrefs.getFloat(mContext.getString(R.string.settings_key_cylinder_density), 0);
}
Expand Down
@@ -1,13 +1,33 @@
package org.mozilla.vrbrowser.ui.adapters;

import android.graphics.Typeface;
import android.view.View;
import android.widget.TextView;

import androidx.databinding.BindingAdapter;


public class BindingAdapters {

@BindingAdapter("visibleGone")
public static void showHide(View view, boolean show) {
view.setVisibility(show ? View.VISIBLE : View.GONE);
}

@BindingAdapter("visibleInvisible")
public static void showInvisible(View view, boolean show) {
view.setVisibility(show ? View.VISIBLE : View.INVISIBLE);
}

@BindingAdapter("typeface")
public static void setTypeface(TextView v, String style) {
switch (style) {
case "bold":
v.setTypeface(null, Typeface.BOLD);
break;
default:
v.setTypeface(null, Typeface.NORMAL);
break;
}
}
}
@@ -0,0 +1,25 @@
package org.mozilla.vrbrowser.ui.adapters;

public class Language {

public Language(String id, String name) {
this.id = id;
this.name = name;
}

private String name;
private String id;

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

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

@Override
public int hashCode() {
return id.hashCode();
}
}

0 comments on commit 451ce73

Please sign in to comment.