/*
 * Copyright (c)  2017  Francisco José Montiel Navarro.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.franmontiel.localechanger.utils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * A helper class with methods to detect Locale changes and recreate the Activities accordingly.
 */
public class ActivityRecreationHelper {

    private static final Map<String, Locale> localesOnActivities = new HashMap<>();

    private ActivityRecreationHelper(){
    }

    /**
     * Call this method on the Activity onResume. It will recreate the Activity if a Locale change is detected.
     * @param activity
     */
    public static void onResume(Activity activity) {
        Locale previousLocale = localesOnActivities.get(activity.toString());
        boolean shouldRestartActivity = previousLocale != null && !previousLocale.equals(Locale.getDefault());
        if (shouldRestartActivity) {
            recreate(activity, false);
        } else {
            localesOnActivities.put(activity.toString(), Locale.getDefault());
        }
    }

    /**
     * Call this method on the Activity onDestroy.
     * @param activity
     */
    public static void onDestroy(Activity activity) {
        localesOnActivities.remove(activity.toString());
    }

    /**
     * Helper method to recreate the Activity. This method should be called after a Locale change.
     * @param activity the Activity that will be recreated
     * @param animate a flag indicating if the recreation will be animated or not
     */
    public static void recreate(Activity activity, boolean animate) {
        if (animate) {
            Intent restartIntent = new Intent(activity, activity.getClass());

            Bundle extras = activity.getIntent().getExtras();
            if (extras != null) {
                restartIntent.putExtras(extras);
            }
            ActivityCompat.startActivity(
                    activity,
                    restartIntent,
                    ActivityOptionsCompat
                            .makeCustomAnimation(activity, android.R.anim.fade_in, android.R.anim.fade_out)
                            .toBundle()
            );
            activity.finish();
        } else {
            activity.recreate();
        }
    }
}
@@ -1591,24 +1591,25 @@
 ult());%0A
+%0A
         
 if (shou
@@ -1604,84 +1604,84 @@
     
-if (shouldRestartActivity) %7B%0A            recreate(activity, false
+localesOnActivities.put(activity.toString(), Locale.getDefault()
 );%0A
+%0A
         
 %7D el
@@ -1680,93 +1680,73 @@
     
-%7D else %7B%0A            localesOnActivities.put(activity.toString(), Locale.getDefault()
+if (shouldRestartActivity) %7B%0A            recreate(activity, false
 );%0A 
@@ -2299,35 +2299,8 @@
 ) %7B%0A
-        if (animate) %7B%0A    
     
@@ -2366,28 +2366,24 @@
 tClass());%0A%0A
-    
         Bund
@@ -2432,28 +2432,24 @@
 ();%0A        
-    
 if (extras !
@@ -2454,28 +2454,24 @@
  != null) %7B%0A
-    
             
@@ -2507,29 +2507,49 @@
 s);%0A        
-    %7D
+%7D%0A%0A        if (animate) %7B
 %0A           
@@ -2847,24 +2847,39 @@
        );%0A  
+      %7D else %7B%0A
           ac
@@ -2868,32 +2868,34 @@
 lse %7B%0A          
+  
 activity.finish(
@@ -2891,77 +2891,130 @@
 ity.
-finish();%0A        %7D else %7B%0A            activity.recreate();%0A        %7D
+startActivity(restartIntent);%0A            activity.overridePendingTransition(0, 0);%0A        %7D%0A%0A        activity.finish();%0A
 %0A   

Fix problem with Activity recreation (without animation) on Oreo