package com.kazucocoa.droidtesthelper;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Locale;

public class HandleLocaleActivity extends AppCompatActivity {

    private static String languageExt = "LANG";

    private static String countryExt = "COUNTRY";

    public static boolean hasExtraRegardingLocal(Intent intent) {
        return intent.hasExtra(languageExt) && intent.hasExtra(countryExt);
    }

    public static Intent buildLaunchHandleLocalActivityIntent(Context context, Intent intent) {
        Intent returnIntent = new Intent(context, HandleLocaleActivity.class);
        returnIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        returnIntent.setAction(Intent.ACTION_VIEW);

        if(!hasExtraRegardingLocal(intent)) {
            returnIntent.putExtra(languageExt, "en");
            returnIntent.putExtra(countryExt, "US");
            return returnIntent;
        }

        String language = intent.getStringExtra(languageExt);
        String country = intent.getStringExtra(countryExt);
        returnIntent.putExtra(languageExt, language);
        returnIntent.putExtra(countryExt, country);

        return returnIntent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_handle_locale);

        Intent intent = this.getIntent();
        TextView textView = (TextView) findViewById(R.id.set_for_change_locale_text);

        if (hasExtraRegardingLocal(intent)) {
            String lang = intent.getStringExtra(languageExt);
            String coun = intent.getStringExtra(countryExt);

            assert textView != null;
            textView.setText("set with: " + lang + "_" + coun);

            Locale locale = new Locale(lang, coun);
            setLocale(locale);
        } else {
            assert textView != null;
            textView.setText("set with: en_US(Default)");

            Locale locale = new Locale("en", "US");
            setLocale(locale);
        }
    }

    private void setLocale(Locale locale) {
        try {
            Class amnClass = Class.forName("android.app.ActivityManagerNative");
            Object amn = null;
            Configuration config = null;

            // amn = ActivityManagerNative.getDefault();
            Method methodGetDefault = amnClass.getMethod("getDefault");
            methodGetDefault.setAccessible(true);
            amn = methodGetDefault.invoke(amnClass);

            // config = amn.getConfiguration();
            Method methodGetConfiguration = amnClass.getMethod("getConfiguration");
            methodGetConfiguration.setAccessible(true);
            config = (Configuration) methodGetConfiguration.invoke(amn);

            // config.userSetLocale = true;
            Class configClass = config.getClass();
            Field f = configClass.getField("userSetLocale");
            f.setBoolean(config, true);

            // set the locale to the new value
            config.locale = locale;

            // amn.updateConfiguration(config);
            Method methodUpdateConfiguration = amnClass.getMethod("updateConfiguration", Configuration.class);
            methodUpdateConfiguration.setAccessible(true);
            methodUpdateConfiguration.invoke(amn, config);
        } catch (Exception e) {
            // error
        }
    }
}

@@ -278,16 +278,68 @@
 .Field;%0A
+import java.lang.reflect.InvocationTargetException;%0A
 import j
@@ -2091,34 +2091,143 @@
         
-setLocale(locale);
+try %7B%0A                setLocale(locale);%0A            %7D catch (Exception e) %7B%0A                e.printStackTrace();%0A            %7D
 %0A       
@@ -2376,32 +2376,54 @@
 le(%22en%22, %22US%22);%0A
+            try %7B%0A    
             setL
@@ -2449,79 +2449,401 @@
     
-%7D%0A    %7D%0A%0A    private void setLocale(Locale locale) %7B%0A        try %7B%0A    
+    %7D catch (Exception e) %7B%0A                e.printStackTrace();%0A            %7D%0A        %7D%0A    %7D%0A%0A    // ref: http://stackoverflow.com/questions/2900023/change-language-programmatically-in-android/4683532#4683532%0A    private void setLocale(Locale locale) throws%0A            ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, NoSuchFieldException %7B%0A
     
@@ -2911,28 +2911,24 @@
 erNative%22);%0A
-    
         Obje
@@ -2950,20 +2950,16 @@
         
-    
 Configur
@@ -2980,20 +2980,16 @@
  null;%0A%0A
-    
         
@@ -3033,36 +3033,32 @@
 ault();%0A        
-    
 Method methodGet
@@ -3101,36 +3101,32 @@
 ault%22);%0A        
-   
- 
 methodGetDefault
@@ -3139,36 +3139,32 @@
 cessible(true);%0A
-    
         amn = me
@@ -3197,36 +3197,32 @@
 lass);%0A%0A        
-   
- 
 // config = amn.
@@ -3237,28 +3237,24 @@
 guration();%0A
-    
         Meth
@@ -3325,28 +3325,24 @@
 %22);%0A        
-   
- 
 methodGetCon
@@ -3365,36 +3365,32 @@
 cessible(true);%0A
-    
         config =
@@ -3447,28 +3447,24 @@
 );%0A%0A        
-   
- 
 // config.us
@@ -3483,20 +3483,16 @@
 = true;%0A
-    
         
@@ -3538,20 +3538,16 @@
         
-  
-  
 Field f 
@@ -3595,20 +3595,16 @@
         
-    
 f.setBoo
@@ -3632,20 +3632,16 @@
         
-  
-  
 // set t
@@ -3671,28 +3671,24 @@
 lue%0A        
-    
 config.local
@@ -3704,28 +3704,24 @@
 e;%0A%0A        
-  
-  
 // amn.updat
@@ -3740,28 +3740,24 @@
 on(config);%0A
-    
         Meth
@@ -3851,36 +3851,32 @@
 class);%0A        
-  
-  
 methodUpdateConf
@@ -3910,28 +3910,24 @@
 e);%0A        
-    
 methodUpdate
@@ -3965,71 +3965,8 @@
 g);%0A
-        %7D catch (Exception e) %7B%0A            // error%0A        %7D%0A
     

correct error