diff --git a/app/build.gradle b/app/build.gradle index 7faf0a4..9a3ca55 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,13 +1,15 @@ apply plugin: 'com.android.application' +apply plugin: 'kotlin-android-extensions' +apply plugin: 'kotlin-android' android { - compileSdkVersion 23 - buildToolsVersion "23.0.2" + compileSdkVersion 29 + buildToolsVersion '29.0.3' defaultConfig { applicationId "com.ahmedjazzar.rosetta.app" - minSdkVersion 14 - targetSdkVersion 23 + minSdkVersion 15 + targetSdkVersion 29 versionCode 1 versionName "0.9.3" } @@ -20,12 +22,15 @@ android { } dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) - compile 'com.android.support:appcompat-v7:23.1.1' - compile 'com.android.support:design:23.1.1' - - testCompile 'junit:junit:4.12' - testCompile 'org.mockito:mockito-core:1.10.19' - - compile project(':rosetta') + implementation fileTree(dir: 'libs', include: ['*.jar']) + implementation 'androidx.appcompat:appcompat:1.1.0' + implementation 'com.google.android.material:material:1.1.0' + testImplementation 'junit:junit:4.13' + testImplementation 'org.mockito:mockito-core:2.23.0' + implementation project(':rosetta') + implementation "androidx.core:core-ktx:1.2.0" + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" +} +repositories { + mavenCentral() } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 7b04df9..badd523 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -3,12 +3,12 @@ package="com.ahmedjazzar.rosetta.app"> + android:theme="@style/AppTheme"> - + diff --git a/app/src/main/java/com/ahmedjazzar/rosetta/app/BaseActivity.java b/app/src/main/java/com/ahmedjazzar/rosetta/app/BaseActivity.java new file mode 100644 index 0000000..ba79b27 --- /dev/null +++ b/app/src/main/java/com/ahmedjazzar/rosetta/app/BaseActivity.java @@ -0,0 +1,29 @@ +package com.ahmedjazzar.rosetta.app; + +import android.content.Context; +import android.content.SharedPreferences; +import android.os.Bundle; +import android.preference.PreferenceManager; +import androidx.appcompat.app.AppCompatActivity; + +import com.ahmedjazzar.rosetta.ContextWrapper; + +import java.util.Locale; + +public abstract class BaseActivity extends AppCompatActivity { + SharedPreferences mSharedPreferences; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + } + + @Override + protected void attachBaseContext(Context newBase) { + mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(newBase); + Locale locale = new Locale(mSharedPreferences.getString("user_preferred_language", "en")); + Context context = ContextWrapper.wrap(newBase, locale); + super.attachBaseContext(context); + } +} diff --git a/app/src/main/java/com/ahmedjazzar/rosetta/app/MainActivity.java b/app/src/main/java/com/ahmedjazzar/rosetta/app/MainActivity.java index a50dc17..78e10ca 100644 --- a/app/src/main/java/com/ahmedjazzar/rosetta/app/MainActivity.java +++ b/app/src/main/java/com/ahmedjazzar/rosetta/app/MainActivity.java @@ -1,27 +1,23 @@ package com.ahmedjazzar.rosetta.app; import android.os.Bundle; -import android.support.design.widget.FloatingActionButton; -import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.Toolbar; +import com.google.android.material.floatingactionbutton.FloatingActionButton; +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; import android.view.View; import android.view.Menu; import android.view.MenuItem; -import com.ahmedjazzar.rosetta.LanguageSwitcher; - -import java.util.Locale; - -public class MainActivity extends AppCompatActivity { +public class MainActivity extends BaseActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); + Toolbar toolbar = findViewById(R.id.toolbar); // This floating button switching between Arabic and English Locales manually upon click - final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); + final FloatingActionButton fab = findViewById(R.id.fab); setSupportActionBar(toolbar); if (MainApplication.languageSwitcher.getCurrentLocale().getLanguage().equals("ar")) { diff --git a/app/src/main/java/com/ahmedjazzar/rosetta/app/MainApplication.java b/app/src/main/java/com/ahmedjazzar/rosetta/app/MainApplication.java index 02db5e2..899249b 100644 --- a/app/src/main/java/com/ahmedjazzar/rosetta/app/MainApplication.java +++ b/app/src/main/java/com/ahmedjazzar/rosetta/app/MainApplication.java @@ -1,7 +1,11 @@ package com.ahmedjazzar.rosetta.app; import android.app.Application; +import android.content.Context; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; +import com.ahmedjazzar.rosetta.ContextWrapper; import com.ahmedjazzar.rosetta.LanguageSwitcher; import java.util.HashSet; diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index f554cbe..df1113e 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,5 +1,5 @@ - - - - + - - + diff --git a/app/src/main/res/values-v21/styles.xml b/app/src/main/res/values-v21/styles.xml index 251fb9f..05c4277 100644 --- a/app/src/main/res/values-v21/styles.xml +++ b/app/src/main/res/values-v21/styles.xml @@ -4,6 +4,6 @@ false true true - @android:color/transparent + @color/colorPrimaryDark diff --git a/build.gradle b/build.gradle index e0b366a..91e48dd 100644 --- a/build.gradle +++ b/build.gradle @@ -1,11 +1,14 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { + ext.kotlin_version = '1.3.71' repositories { jcenter() + google() } dependencies { - classpath 'com.android.tools.build:gradle:1.5.0' + classpath 'com.android.tools.build:gradle:3.6.1' + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files @@ -15,6 +18,7 @@ buildscript { allprojects { repositories { jcenter() + google() } } diff --git a/gradle.properties b/gradle.properties index 1d3591c..915f0e6 100644 --- a/gradle.properties +++ b/gradle.properties @@ -15,4 +15,6 @@ # When configured, Gradle will run in incubating parallel mode. # This option should only be used with decoupled projects. More details, visit # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects -# org.gradle.parallel=true \ No newline at end of file +# org.gradle.parallel=true +android.enableJetifier=true +android.useAndroidX=true \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index f23df6e..a7653d6 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Wed Oct 21 11:34:03 PDT 2015 +#Mon Mar 30 15:47:02 GST 2020 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.8-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip diff --git a/rosetta/build.gradle b/rosetta/build.gradle index 82563f1..c97175d 100644 --- a/rosetta/build.gradle +++ b/rosetta/build.gradle @@ -1,4 +1,6 @@ apply plugin: 'com.android.library' +apply plugin: 'kotlin-android-extensions' +apply plugin: 'kotlin-android' ext { bintrayRepo = 'maven' @@ -14,7 +16,7 @@ ext { siteUrl = 'https://github.com/ahmedaljazzar/rosetta' gitUrl = 'https://github.com/ahmedaljazzar/rosetta.git' - libraryVersion = '1.0.1' + libraryVersion = '1.0.2' developerId = 'ahmedaljazzar' developerName = 'Ahmed Jazzar' @@ -29,11 +31,12 @@ buildscript { repositories { jcenter() mavenCentral() + google() } dependencies { - classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.4' - classpath 'com.android.tools.build:gradle:1.2.3' - classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3' + classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4' + classpath 'com.android.tools.build:gradle:3.6.1' + classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1' } } @@ -45,12 +48,12 @@ android { maven { url 'http://repo1.maven.org/maven2' } } - compileSdkVersion 23 - buildToolsVersion "23.0.2" + compileSdkVersion 29 + buildToolsVersion '29.0.3' defaultConfig { - minSdkVersion 14 - targetSdkVersion 23 + minSdkVersion 15 + targetSdkVersion 29 versionCode 3 versionName "1.0.1" } @@ -64,11 +67,17 @@ android { } dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) - testCompile 'junit:junit:4.12' - testCompile 'org.mockito:mockito-core:1.10.19' - compile 'com.android.support:appcompat-v7:23.1.1' + implementation fileTree(dir: 'libs', include: ['*.jar']) + testImplementation 'junit:junit:4.13' + testImplementation 'org.mockito:mockito-core:2.23.0' + implementation 'androidx.appcompat:appcompat:1.1.0' + implementation 'com.jakewharton:process-phoenix:2.0.0' + implementation "androidx.core:core-ktx:1.2.0" + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" +} +repositories { + mavenCentral() } -apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/installv1.gradle' -apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/bintrayv1.gradle' \ No newline at end of file +//apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/installv1.gradle' +//apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/bintrayv1.gradle' \ No newline at end of file diff --git a/rosetta/src/main/AndroidManifest.xml b/rosetta/src/main/AndroidManifest.xml index f1b2707..6cdeb08 100644 --- a/rosetta/src/main/AndroidManifest.xml +++ b/rosetta/src/main/AndroidManifest.xml @@ -1,5 +1,5 @@ - + diff --git a/rosetta/src/main/java/com/ahmedjazzar/rosetta/ContextWrapper.java b/rosetta/src/main/java/com/ahmedjazzar/rosetta/ContextWrapper.java new file mode 100644 index 0000000..b94a94f --- /dev/null +++ b/rosetta/src/main/java/com/ahmedjazzar/rosetta/ContextWrapper.java @@ -0,0 +1,42 @@ +package com.ahmedjazzar.rosetta; + +import android.content.Context; +import android.content.res.Configuration; +import android.content.res.Resources; +import android.os.Build; +import android.os.LocaleList; + +import java.util.Locale; + +public class ContextWrapper extends android.content.ContextWrapper { + + public ContextWrapper(Context base) { + super(base); + } + + public static ContextWrapper wrap(Context context, Locale newLocale) { + + Resources res = context.getResources(); + Configuration configuration = res.getConfiguration(); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + configuration.setLocale(newLocale); + + LocaleList localeList = new LocaleList(newLocale); + LocaleList.setDefault(localeList); + configuration.setLocales(localeList); + + context = context.createConfigurationContext(configuration); + + } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { + configuration.setLocale(newLocale); + context = context.createConfigurationContext(configuration); + + } else { + configuration.locale = newLocale; + res.updateConfiguration(configuration, res.getDisplayMetrics()); + } + + return new ContextWrapper(context); + } +} diff --git a/rosetta/src/main/java/com/ahmedjazzar/rosetta/LanguageSwitcher.java b/rosetta/src/main/java/com/ahmedjazzar/rosetta/LanguageSwitcher.java index 3585593..927455d 100644 --- a/rosetta/src/main/java/com/ahmedjazzar/rosetta/LanguageSwitcher.java +++ b/rosetta/src/main/java/com/ahmedjazzar/rosetta/LanguageSwitcher.java @@ -2,8 +2,8 @@ import android.app.Activity; import android.content.Context; -import android.support.annotation.NonNull; -import android.support.v4.app.FragmentActivity; +import androidx.annotation.NonNull; +import androidx.fragment.app.FragmentActivity; import java.util.HashSet; import java.util.Locale; diff --git a/rosetta/src/main/java/com/ahmedjazzar/rosetta/LanguagesListDialogFragment.java b/rosetta/src/main/java/com/ahmedjazzar/rosetta/LanguagesListDialogFragment.java index 07f3a5f..3781e67 100644 --- a/rosetta/src/main/java/com/ahmedjazzar/rosetta/LanguagesListDialogFragment.java +++ b/rosetta/src/main/java/com/ahmedjazzar/rosetta/LanguagesListDialogFragment.java @@ -4,20 +4,23 @@ import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; -import android.content.Intent; import android.os.Bundle; -import android.support.v4.app.DialogFragment; -import android.support.v4.app.FragmentActivity; + +import androidx.fragment.app.DialogFragment; +import androidx.fragment.app.FragmentActivity; + import android.widget.Button; import android.widget.TextView; +import com.jakewharton.processphoenix.ProcessPhoenix; + import java.util.ArrayList; import java.util.Locale; /** * This fragment is responsible for displaying the supported locales and performing any necessary * action that allows user to select, cancel, and commit changes. - * + *

* Created by ahmedjazzar on 1/19/16. */ @@ -31,12 +34,11 @@ public class LanguagesListDialogFragment extends DialogFragment { private int mSelectedLanguage = -1; private Logger mLogger; - public LanguagesListDialogFragment() { + public LanguagesListDialogFragment() { this.mLogger = new Logger(TAG); } /** - * * @param savedInstanceState * @return a Dialog fragment */ @@ -75,19 +77,19 @@ public void onClick(DialogInterface dialogInterface, int which) { } /** - * * @param which the position of the selected locale */ - protected void onLanguageSelected(int which) { + protected void onLanguageSelected(int which) { // just update the selected locale mSelectedLanguage = which; } /** * Localizing the dialog buttons and title + * * @param which the position of the selected locale */ - protected void onLanguageSelectedLocalized(int which) { + protected void onLanguageSelectedLocalized(int which) { // update the selected locale mSelectedLanguage = which; @@ -105,13 +107,14 @@ protected void onLanguageSelectedLocalized(int which) { /** * the position of the selected locale given the ids - * @param which the position of the selected locale - * @param titleView dialog's title text view + * + * @param which the position of the selected locale + * @param titleView dialog's title text view * @param positiveButton positive button * @param negativeButton negative button */ protected void onLanguageSelectedLocalized(int which, TextView titleView, Button positiveButton, - Button negativeButton) { + Button negativeButton) { // update the selected locale mSelectedLanguage = which; @@ -123,10 +126,10 @@ protected void onLanguageSelectedLocalized(int which, TextView titleView, Button "locale"); String LocalizedTitle = LocalesUtils.getInSpecificLocale(activity, locale, DIALOG_TITLE_ID); - if(titleView == null) { + if (titleView == null) { // Display dialog title in the selected locale dialog.setTitle(LocalizedTitle); - } else { + } else { titleView.setText(LocalizedTitle); } @@ -153,12 +156,13 @@ protected void onPositiveClick() { getActivity(), mSelectedLanguage)) { mLogger.info("App locale changed successfully."); - LocalesUtils.refreshApplication(getActivity()); + //LocalesUtils.refreshApplication(getActivity()); + ProcessPhoenix.triggerRebirth(getActivity()); } else { mLogger.error("Unsuccessful trial to change the App locale."); // TODO: notify the user that his request not placed } - } else { + } else { dismiss(); } } @@ -173,19 +177,17 @@ protected void onNegativeClick() { } /** - * * @return available languages */ protected String[] getLanguages() { - ArrayList languages = LocalesUtils.getLocalesWithDisplayName(); + ArrayList languages = LocalesUtils.getLocalesWithDisplayName(); return languages.toArray(new String[languages.size()]); } /** - * * @return the index of the locale that app is using now */ - protected int getCurrentLocaleIndex() { + protected int getCurrentLocaleIndex() { return LocalesUtils.getCurrentLocaleIndex(); } diff --git a/rosetta/src/main/java/com/ahmedjazzar/rosetta/LocalesPreferenceManager.java b/rosetta/src/main/java/com/ahmedjazzar/rosetta/LocalesPreferenceManager.java index 7228a3b..04d30e8 100644 --- a/rosetta/src/main/java/com/ahmedjazzar/rosetta/LocalesPreferenceManager.java +++ b/rosetta/src/main/java/com/ahmedjazzar/rosetta/LocalesPreferenceManager.java @@ -10,47 +10,47 @@ * This class is responsible for setting and getting the preferred locale and manage any related * actions. I think that there's no need for logging here because the utils class already handles * logs for these actions based on their returned results. - * + *

* Created by ahmedjazzar on 1/22/16. */ -class LocalesPreferenceManager { +class LocalesPreferenceManager { private SharedPreferences mSharedPreferences; private SharedPreferences.Editor mEditor; - + static final int BASE_LOCALE = 1; private final String BASE_LANGUAGE_KEY = "base_language"; private final String BASE_COUNTRY_KEY = "base_country"; - + static final int LAUNCH_LOCALE = 2; private final String LAUNCH_LANGUAGE_KEY = "launch_language"; private final String LAUNCH_COUNTRY_KEY = "launch_country"; - + static final int USER_PREFERRED_LOCALE = 3; private final String USER_PREFERRED_LANGUAGE_KEY = "user_preferred_language"; private final String USER_PREFERRED_COUNTRY_KEY = "user_preferred_country"; - LocalesPreferenceManager(Context context, Locale firstLaunchLocale, Locale baseLocale) { + LocalesPreferenceManager(Context context, Locale firstLaunchLocale, Locale baseLocale) { this.mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); this.mEditor = this.mSharedPreferences.edit(); - if (!isLocaleExists(BASE_LOCALE)) { + if (!isLocaleExists(BASE_LOCALE)) { this.setPreferredLocale(BASE_LOCALE, baseLocale); } - if (!isLocaleExists(LAUNCH_LOCALE)) { + if (!isLocaleExists(LAUNCH_LOCALE)) { this.setPreferredLocale(LAUNCH_LOCALE, firstLaunchLocale); } - if (!isLocaleExists(USER_PREFERRED_LOCALE)) { + if (!isLocaleExists(USER_PREFERRED_LOCALE)) { this.setPreferredLocale(USER_PREFERRED_LOCALE, firstLaunchLocale); } } - boolean isLocaleExists(int key) { + boolean isLocaleExists(int key) { - switch (key) { + switch (key) { case BASE_LOCALE: return mSharedPreferences.contains(this.BASE_LANGUAGE_KEY); case LAUNCH_LOCALE: @@ -68,20 +68,19 @@ boolean isLocaleExists(int key) { * @param locale user desired locale * @return true if the preference updated */ - boolean setPreferredLocale(int key, Locale locale) { + boolean setPreferredLocale(int key, Locale locale) { return this.setPreferredLocale(key, locale.getLanguage(), locale.getCountry()); } /** - * * @return preferred locale after concatenating language and country */ - Locale getPreferredLocale(int key) { + Locale getPreferredLocale(int key) { String languageKey; String countryKey; - switch (key) { + switch (key) { case BASE_LOCALE: languageKey = this.BASE_LANGUAGE_KEY; countryKey = this.BASE_COUNTRY_KEY; @@ -101,7 +100,7 @@ Locale getPreferredLocale(int key) { String language = getPreferredLanguage(languageKey); String country = getPreferredCountry(countryKey); - if (language == null) { + if (language == null) { return null; } @@ -111,16 +110,17 @@ Locale getPreferredLocale(int key) { /** * Sets user preferred locale by setting a language preference and a country preference since * there's no supported preferences for locales + * * @param language of the locale; ex. en - * @param country of the locale; ex. US + * @param country of the locale; ex. US * @return true if the preferences updated */ - private boolean setPreferredLocale(int key, String language, String country) { - + private boolean setPreferredLocale(int key, String language, String country) { + String languageKey; String countryKey; - switch (key) { + switch (key) { case BASE_LOCALE: languageKey = this.BASE_LANGUAGE_KEY; countryKey = this.BASE_COUNTRY_KEY; @@ -136,7 +136,7 @@ private boolean setPreferredLocale(int key, String language, String country) { default: return false; } - + mEditor.putString(languageKey, language); mEditor.putString(countryKey, country); @@ -144,18 +144,16 @@ private boolean setPreferredLocale(int key, String language, String country) { } /** - * * @return preferred language */ - private String getPreferredLanguage(String key) { + private String getPreferredLanguage(String key) { return mSharedPreferences.getString(key, null); } /** - * * @return preferred country */ - private String getPreferredCountry(String key) { + private String getPreferredCountry(String key) { return mSharedPreferences.getString(key, null); } } diff --git a/rosetta/src/main/java/com/ahmedjazzar/rosetta/LocalesUtils.java b/rosetta/src/main/java/com/ahmedjazzar/rosetta/LocalesUtils.java index b06b178..658e6c9 100644 --- a/rosetta/src/main/java/com/ahmedjazzar/rosetta/LocalesUtils.java +++ b/rosetta/src/main/java/com/ahmedjazzar/rosetta/LocalesUtils.java @@ -6,10 +6,14 @@ import android.content.res.Configuration; import android.content.res.Resources; import android.os.Build; -import android.support.annotation.NonNull; -import android.support.v4.app.FragmentActivity; + +import androidx.annotation.NonNull; +import androidx.fragment.app.FragmentActivity; + import android.util.DisplayMetrics; +import com.jakewharton.processphoenix.ProcessPhoenix; + import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; @@ -20,7 +24,7 @@ * This class is a helper class that connects all library classes activities together and make it * easier for every class in the library to use and look at the shared info without a need to * initialize a new object from the desired class - * + *

* Created by ahmedjazzar on 1/19/16. */ final class LocalesUtils { @@ -36,7 +40,6 @@ final class LocalesUtils { private static Logger sLogger = new Logger(TAG); /** - * * @param detector just a setter because I don't want to declare any constructors in this class */ static void setDetector(@NonNull LocalesDetector detector) { @@ -44,83 +47,77 @@ static void setDetector(@NonNull LocalesDetector detector) { } /** - * * @param localesPreferenceManager just a setter because I don't want to declare any * constructors in this class */ static void setLocalesPreferenceManager( - @NonNull LocalesPreferenceManager localesPreferenceManager) { + @NonNull LocalesPreferenceManager localesPreferenceManager) { LocalesUtils.sLocalesPreferenceManager = localesPreferenceManager; } /** - * * @param stringId a string to start discovering sLocales in * @return a HashSet of discovered sLocales */ - static HashSet fetchAvailableLocales(int stringId) { + static HashSet fetchAvailableLocales(int stringId) { return sDetector.fetchAvailableLocales(stringId); } /** - * * @param localesSet sLocales user wanna use */ - static void setSupportedLocales(HashSet localesSet) { + static void setSupportedLocales(HashSet localesSet) { LocalesUtils.sLocales = sDetector.validateLocales(localesSet); sLogger.debug("Locales have been changed"); } /** - * * @return a HashSet of the available sLocales discovered in the application */ - static HashSet getLocales() { + static HashSet getLocales() { return LocalesUtils.sLocales; } /** - * * @return a list of locales for displaying on the layout purposes */ - static ArrayList getLocalesWithDisplayName() { + static ArrayList getLocalesWithDisplayName() { ArrayList stringLocales = new ArrayList<>(); - for (Locale loc: LocalesUtils.getLocales()) { + for (Locale loc : LocalesUtils.getLocales()) { stringLocales.add(loc.getDisplayName(loc)); } return stringLocales; } /** - * * @return the index of the current app locale */ - static int getCurrentLocaleIndex() { + static int getCurrentLocaleIndex() { Locale locale = LocalesUtils.getCurrentLocale(); int index = -1; int itr = 0; - for (Locale l : sLocales) { - if(locale.equals(l)) { + for (Locale l : sLocales) { + if (locale.equals(l)) { index = itr; break; } itr++; } - if (index == -1) { + if (index == -1) { //TODO: change the index to the most closer available locale sLogger.warn("Current device locale '" + locale.toString() + "' does not appear in your given supported locales"); index = sDetector.detectMostClosestLocale(locale); - if(index == -1) { + if (index == -1) { index = 0; sLogger.warn("Current locale index changed to 0 as the current locale '" + - locale.toString() + - "' not supported." + locale.toString() + + "' not supported." ); } } @@ -129,40 +126,36 @@ static int getCurrentLocaleIndex() { } /** - * - * @see Pseudolocalization for - * more information about pseudo localization * @return pseudo locales list + * @see Pseudolocalization for + * more information about pseudo localization */ - static List getPseudoLocales() { + static List getPseudoLocales() { return Arrays.asList(LocalesUtils.PSEUDO_LOCALES); } /** - * * @param index * @return the locale at the given index */ - static Locale getLocaleFromIndex(int index) { + static Locale getLocaleFromIndex(int index) { return LocalesUtils.sLocales.toArray(new Locale[LocalesUtils.sLocales.size()])[index]; } /** - * * @param context - * @param index the selected locale position + * @param index the selected locale position * @return true if the application locale changed */ - static boolean setAppLocale(Context context, int index) { + static boolean setAppLocale(Context context, int index) { return setAppLocale(context, getLocaleFromIndex(index)); } /** - * * @param context * @return true if the application locale changed */ - static boolean setAppLocale(Context context, Locale newLocale) { + static boolean setAppLocale(Context context, Locale newLocale) { Resources resources = context.getResources(); DisplayMetrics displayMetrics = resources.getDisplayMetrics(); @@ -177,14 +170,14 @@ static boolean setAppLocale(Context context, Locale newLocale) { } resources.updateConfiguration(configuration, displayMetrics); - if(oldLocale.equals(newLocale)) { + if (oldLocale.equals(newLocale)) { return false; } - if (LocalesUtils.updatePreferredLocale(newLocale)) { + if (LocalesUtils.updatePreferredLocale(newLocale)) { sLogger.info("Locale preferences updated to: " + newLocale); Locale.setDefault(newLocale); - } else { + } else { sLogger.error("Failed to update locale preferences."); } @@ -192,15 +185,13 @@ static boolean setAppLocale(Context context, Locale newLocale) { } /** - * * @return application's base locale */ - static Locale getBaseLocale() { + static Locale getBaseLocale() { return LocalesUtils.sLocalesPreferenceManager.getPreferredLocale(LocalesPreferenceManager.BASE_LOCALE); } /** - * * @param activity * @param locale * @param stringId the target string @@ -217,7 +208,7 @@ static String getInSpecificLocale(FragmentActivity activity, Locale locale, int Resources resources = new Resources(activity.getAssets(), metrics, conf); conf.locale = old; - return resources.getString(stringId); + return resources.getString(stringId); } /** @@ -244,28 +235,28 @@ static void refreshApplication(Activity activity) { } /** - * * @return the first launch locale */ - static Locale getLaunchLocale() { + static Locale getLaunchLocale() { return sLocalesPreferenceManager.getPreferredLocale(LocalesPreferenceManager.LAUNCH_LOCALE); } /** * Setting the application locale manually - * @param newLocale the desired locale - * @param activity the current activity in order to refresh the app * + * @param newLocale the desired locale + * @param activity the current activity in order to refresh the app * @return true if the operation succeed, false otherwise */ static boolean setLocale(Locale newLocale, Activity activity) { - if (newLocale == null || !getLocales().contains(newLocale)) { + if (newLocale == null || !getLocales().contains(newLocale)) { return false; } - if (LocalesUtils.setAppLocale(activity.getApplicationContext(), newLocale)) { - LocalesUtils.refreshApplication(activity); + if (LocalesUtils.setAppLocale(activity.getApplicationContext(), newLocale)) { + //LocalesUtils.refreshApplication(activity); + ProcessPhoenix.triggerRebirth(activity); return true; } @@ -285,18 +276,16 @@ public static Locale getCurrentLocale(Context context) { } /** - * * @param locale the new preferred locale * @return true if the preferred locale updated */ - private static boolean updatePreferredLocale(Locale locale) { + private static boolean updatePreferredLocale(Locale locale) { return LocalesUtils.sLocalesPreferenceManager .setPreferredLocale(LocalesPreferenceManager.USER_PREFERRED_LOCALE, locale); } /** - * * @return current application locale */ private static Locale getCurrentLocale() {