<commit_before>package org.mosspaper;

import org.mosspaper.objects.MossObject;
import org.mosspaper.objects.DataProvider;
import org.mosspaper.objects.Interval;
import org.mosspaper.objects.UpdateManager;
import org.mosspaper.prefs.PrefUtils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.Handler;
import android.os.FileObserver;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;

public class Env {

    /**
     * Singleton holds the current Env
     */
    public enum Current {
        INSTANCE;

        public Env env;
    }

    class ConfigWatcher extends FileObserver {

        private String mFilename;
        private Context mContext;

        public ConfigWatcher(Context context, String directory, String filename) {
            super(directory, FileObserver.MODIFY);
            this.mFilename = filename;
            this.mContext = context;
        }

        public void onEvent(int event, String path) {
            if ((event & FileObserver.MODIFY) != 0 && path.equals(mFilename)) {
                mHandler.post(new Runnable() {
                    public void run() {
                        Env.reload(mContext);
                    }
                });
            }
        }
    }

    public static Env getEnv() {
        return Current.INSTANCE.env;
    }

    public Env() {
        paint = new Paint();
        paint.setColor(0xffffffff);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(1);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setStrokeWidth(0f); // Hairline mode
        paint.setTypeface(Typeface.MONOSPACE);

        exs = new ArrayList<MossException>();
    }

    public void destroy() {
        stopFileWatcher();
    }

    public static void reload(Context context) {
        reload(context, true);
    }

    public static void load(Context context, Handler handler) {
        mHandler = handler;
        reload(context, true);
    }

    public static void reload(Context context, boolean resetPrefs) {
        SharedPreferences prefs =
            context.getSharedPreferences(WallPaper.SHARED_PREFS_NAME, 0);
        Env oldEnv = Current.INSTANCE.env;
        Env newEnv = null;
        try {
            if (Config.CUSTOM.equals(prefs.getString("sample_config_file", ""))
                    && null != prefs.getString("config_file", null)
                    && new File(prefs.getString("config_file", "")).exists()) {

                try {
                    Parser p = new Parser();
                    String cfile = prefs.getString("config_file", "");
                    InputStream in = new FileInputStream(cfile);
                    newEnv = new Env();
                    newEnv.configFile = new File(cfile);
                    p.buildEnv(newEnv, in);
                } catch (IOException e) {
                    newEnv = loadDefaultConfig(context, prefs);
                }<commit_msg>Fixed bug in Env.load which reset the environment<commit_after>package org.mosspaper;

import org.mosspaper.objects.MossObject;
import org.mosspaper.objects.DataProvider;
import org.mosspaper.objects.Interval;
import org.mosspaper.objects.UpdateManager;
import org.mosspaper.prefs.PrefUtils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.Handler;
import android.os.FileObserver;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;

public class Env {

    /**
     * Singleton holds the current Env
     */
    public enum Current {
        INSTANCE;

        public Env env;
    }

    class ConfigWatcher extends FileObserver {

        private String mFilename;
        private Context mContext;

        public ConfigWatcher(Context context, String directory, String filename) {
            super(directory, FileObserver.MODIFY);
            this.mFilename = filename;
            this.mContext = context;
        }

        public void onEvent(int event, String path) {
            if ((event & FileObserver.MODIFY) != 0 && path.equals(mFilename)) {
                mHandler.post(new Runnable() {
                    public void run() {
                        Env.reload(mContext);
                    }
                });
            }
        }
    }

    public static Env getEnv() {
        return Current.INSTANCE.env;
    }

    public Env() {
        paint = new Paint();
        paint.setColor(0xffffffff);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(1);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setStrokeWidth(0f); // Hairline mode
        paint.setTypeface(Typeface.MONOSPACE);

        exs = new ArrayList<MossException>();
    }

    public void destroy() {
        stopFileWatcher();
    }

    public static void reload(Context context) {
        reload(context, true);
    }

    public static void load(Context context, Handler handler) {
        mHandler = handler;
        reload(context, false);
    }

    public static void reload(Context context, boolean resetPrefs) {
        SharedPreferences prefs =
            context.getSharedPreferences(WallPaper.SHARED_PREFS_NAME, 0);
        Env oldEnv = Current.INSTANCE.env;
        Env newEnv = null;
        try {
            if (Config.CUSTOM.equals(prefs.getString("sample_config_file", ""))
                    && null != prefs.getString("config_file", null)
                    && new File(prefs.getString("config_file", "")).exists()) {

                try {
                    Parser p = new Parser();
                    String cfile = prefs.getString("config_file", "");
                    InputStream in = new FileInputStream(cfile);
                    newEnv = new Env();
                    newEnv.configFile = new File(cfile);
                    p.buildEnv(newEnv, in);
                } catch (IOException e) {
                    newEnv = loadDefaultConfig(context, prefs);
                }
{'fraction_non_alphanumeric': 0.09851273744662052, 'fraction_numerical': 0.0014725371815638344, 'mean_word_length': 2.1902301550023484, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4249742', 'n_tokens_mistral': 1882, 'n_tokens_neox': 1810, 'n_words': 411}