package cgeo.geocaching;

import cgeo.geocaching.network.Cookies;
import cgeo.geocaching.settings.Settings;
import cgeo.geocaching.storage.DataStore;
import cgeo.geocaching.utils.Log;
import cgeo.geocaching.utils.OOMDumpingUncaughtExceptionHandler;

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.ComponentCallbacks2;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.UserManager;
import android.view.ViewConfiguration;

import androidx.annotation.NonNull;

import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class CgeoApplication extends Application {

    private static CgeoApplication instance;

    public CgeoApplication() {
        setInstance(this);
    }

    private static void setInstance(@NonNull final CgeoApplication application) {
        instance = application;
    }

    public static Application getInstance() {
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        OOMDumpingUncaughtExceptionHandler.installUncaughtExceptionHandler();

        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O) {
            fixUserManagerMemoryLeak();
        }

        fixGoogleMapZoomDataBug();

        showOverflowMenu();

        initApplicationLocale();

        // ensure initialization of lists
        DataStore.getLists();

        // Restore cookies
        Cookies.restoreCookies();
    }

    /**
     * https://code.google.com/p/android/issues/detail?id=173789
     * introduced with JELLY_BEAN_MR2 / fixed in October 2016
     */
    private void fixUserManagerMemoryLeak() {
        try {
            // invoke UserManager.get() via reflection
            final Method m = UserManager.class.getMethod("get", Context.class);
            m.setAccessible(true);
            m.invoke(null, this);
        } catch (final Throwable e) {
            if (BuildConfig.DEBUG) {
                throw new IllegalStateException("Cannot fix UserManager memory leak", e);
            }
        }
    }

    /**
     * https://issuetracker.google.com/issues/154855417
     * delete corrupted map zoom data files once
     * bug appeared 2020-04-22
     * workaround according to https://issuetracker.google.com/issues/154855417#comment398
     */
    private void fixGoogleMapZoomDataBug() {
        try {
            final SharedPreferences hasFixedGoogleBug154855417 = getSharedPreferences("google_bug_154855417", Context.MODE_PRIVATE);
            if (!hasFixedGoogleBug154855417.contains("fixed")) {
                final File corruptedZoomTables = new File(getFilesDir(), "ZoomTables.data");
                final File corruptedSavedClientParameters = new File(getFilesDir(), "SavedClientParameters.data.cs");
                final File corruptedClientParametersData = new File(getFilesDir(), "DATA_ServerControlledParametersManager.data.v1." + getBaseContext().getPackageName());
                corruptedZoomTables.delete();
                corruptedSavedClientParameters.delete();
                corruptedClientParametersData.delete();
                hasFixedGoogleBug154855417.edit().putBoolean("fixed", true).apply();
            }
        } catch (Exception e) {
        }
    }

    @Override
    public void onConfigurationChanged(final Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        initApplicationLocale();
    }

    private void showOverflowMenu() {
        try {
            final ViewConfiguration config = ViewConfiguration.get(this);
            final Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
            menuKeyField.setAccessible(true);
            menuKeyField.setBoolean(config, false);
        } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException ignored) {
        }
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        onTrimMemory(ComponentCallbacks2.TRIM_MEMORY_COMPLETE);
    }

    @SuppressLint("NewApi")
    @Override
    public void onTrimMemory(final int level) {
        super.onTrimMemory(level);
        if (level >= TRIM_MEMORY_MODERATE) {
            Log.i("Cleaning applications cache to trim memory");
            DataStore.removeAllFromCache();
        }
    }

    /**
     * Enforce language to be English if the user decided so.
     */
    private void initApplicationLocale() {
        final Configuration config = new Configuration();
        config.locale = Settings.getApplicationLocale();
        final Resources resources = getResources();
        resources.updateConfiguration(config, resources.getDisplayMetrics());
    }

}

@@ -395,50 +395,8 @@
 xt;%0A
-import android.content.SharedPreferences;%0A
 impo
@@ -429,24 +429,24 @@
 figuration;%0A
+
 import andro
@@ -608,29 +608,8 @@
 l;%0A%0A
-import java.io.File;%0A
 impo
@@ -1298,44 +1298,8 @@
  %7D%0A%0A
-        fixGoogleMapZoomDataBug();%0A%0A
     
@@ -2066,24 +2066,24 @@
  leak%22, e);%0A
+
             
@@ -2105,1197 +2105,8 @@
  %7D%0A%0A
-    /**%0A     * https://issuetracker.google.com/issues/154855417%0A     * delete corrupted map zoom data files once%0A     * bug appeared 2020-04-22%0A     * workaround according to https://issuetracker.google.com/issues/154855417#comment398%0A     */%0A    private void fixGoogleMapZoomDataBug() %7B%0A        try %7B%0A            final SharedPreferences hasFixedGoogleBug154855417 = getSharedPreferences(%22google_bug_154855417%22, Context.MODE_PRIVATE);%0A            if (!hasFixedGoogleBug154855417.contains(%22fixed%22)) %7B%0A                final File corruptedZoomTables = new File(getFilesDir(), %22ZoomTables.data%22);%0A                final File corruptedSavedClientParameters = new File(getFilesDir(), %22SavedClientParameters.data.cs%22);%0A                final File corruptedClientParametersData = new File(getFilesDir(), %22DATA_ServerControlledParametersManager.data.v1.%22 + getBaseContext().getPackageName());%0A                corruptedZoomTables.delete();%0A                corruptedSavedClientParameters.delete();%0A                corruptedClientParametersData.delete();%0A                hasFixedGoogleBug154855417.edit().putBoolean(%22fixed%22, true).apply();%0A            %7D%0A        %7D catch (Exception e) %7B%0A        %7D%0A    %7D%0A%0A
     

remove google maps zoom data fix