11package com .codedead .deadpix .domain ;
22
3+ import android .annotation .SuppressLint ;
34import android .content .Context ;
45import android .content .SharedPreferences ;
56import android .content .res .Configuration ;
6- import android .preference .PreferenceManager ;
7+
8+ import androidx .preference .PreferenceManager ;
79
810import java .util .Locale ;
911
1012public class LocaleHelper {
1113
1214 /**
13- * Method that is called when a Context object is going to be attached
15+ * Method that is called when a Context object is attached
1416 *
15- * @param context The Context object that is going to be attached
16- * @return The Context object that should be attached
17+ * @param context The Context object that is attached
18+ * @return The Context object containing correct resource properties
1719 */
18- public static Context onAttach (Context context ) {
20+ public static Context onAttach (final Context context ) {
1921 final String lang = getPersistedData (context , Locale .getDefault ().getLanguage ());
2022 return setLocale (context , lang );
2123 }
2224
2325 /**
24- * Method that is called when a Context object is going to be attached
26+ * Method that is called when a Context object is attached
2527 *
26- * @param context The Context object that is going to be attached
27- * @param defaultLanguage The default language
28- * @return The Context object that should be attached
28+ * @param context The Context object that is attached
29+ * @param defaultLanguage The default language code
30+ * @return The Context object containing correct resource properties
2931 */
30- public static Context onAttach (Context context , String defaultLanguage ) {
32+ public static Context onAttach (final Context context , final String defaultLanguage ) {
3133 final String lang = getPersistedData (context , defaultLanguage );
3234 return setLocale (context , lang );
3335 }
3436
3537 /**
36- * Set the locale
38+ * Set the Locale depending on the language
3739 *
38- * @param context The Context object that should be updated with the latest language
39- * @param language The language that should be applied to the Context object
40- * @return The updated Context object
40+ * @param context The Context object that can be used to change resource properties
41+ * @param language The language that should be attached
42+ * @return The Context object containing correct resource properties
4143 */
42- public static Context setLocale (Context context , String language ) {
44+ public static Context setLocale (final Context context , final String language ) {
4345 persist (context , language );
4446 return updateResources (context , language );
4547 }
4648
4749 /**
48- * Get the language from the shared preferences
50+ * Get the language code that is stored in the shared preferences
4951 *
50- * @param context The Context object that can be used to store shared preferences
51- * @param defaultLanguage The default language
52- * @return The language from the shared preferences
52+ * @param context The Context object that can be used to load the preferences
53+ * @param defaultLanguage The default language code
54+ * @return The language code that is stored in the shared preferences
5355 */
54- private static String getPersistedData (Context context , String defaultLanguage ) {
55- final SharedPreferences preferences = context . getSharedPreferences ( "deadpixsettings" , Context . MODE_PRIVATE );
56+ private static String getPersistedData (final Context context , final String defaultLanguage ) {
57+ final SharedPreferences preferences = PreferenceManager . getDefaultSharedPreferences ( context );
5658 return preferences .getString ("language" , defaultLanguage );
5759 }
5860
5961 /**
60- * Store the language in the shared preferences
62+ * Store the language code in the shared preferences
6163 *
62- * @param context The Context object that can be used to store the shared preferences
63- * @param language The language that should be stored
64+ * @param context The Context object that can be used to store preferences
65+ * @param language The language code that should be stored
6466 */
65- private static void persist (Context context , String language ) {
67+ private static void persist (final Context context , final String language ) {
6668 final SharedPreferences preferences = PreferenceManager .getDefaultSharedPreferences (context );
6769 final SharedPreferences .Editor editor = preferences .edit ();
6870
@@ -71,13 +73,14 @@ private static void persist(Context context, String language) {
7173 }
7274
7375 /**
74- * Update the context with the latest language
76+ * Update the resources of a Context object
7577 *
76- * @param context The Context object that should be updated
77- * @param language The language that should be applied to the Context object
78- * @return The updated Context object
78+ * @param context The Context object that should be configured
79+ * @param language The language code that should be applied to the Context object
80+ * @return The Context object containing correct resource properties
7981 */
80- private static Context updateResources (Context context , String language ) {
82+ @ SuppressLint ("AppBundleLocaleChanges" )
83+ private static Context updateResources (final Context context , final String language ) {
8184 final Locale locale = new Locale (language );
8285 Locale .setDefault (locale );
8386
0 commit comments