<commit_before>package com.jsonrpclib.observers;

import android.content.Context;
import android.database.DataSetObserver;
import android.util.Pair;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.TextView;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * User: jbogacki
 * Date: 08.04.2013
 * Time: 22:23
 */
public class ObserverHelper {
    private List<Pair<ObservableWrapper, WrapObserver>> dataObservers = new ArrayList<Pair<ObservableWrapper, WrapObserver>>();
    private List<Pair<Adapter, DataSetObserver>> dataAdapters = new ArrayList<Pair<Adapter, DataSetObserver>>();
    private Map<View, Pair<ObservableWrapper, WrapObserver>> viewObservers = new HashMap<View, Pair<ObservableWrapper, WrapObserver>>();

    private static final String splitter = "\\.";
    private static final Pattern pattern = Pattern.compile("\\[[^\\]]*\\]");
    private static final String convention = "Changed";
    private Context context;


    private static Object dataObject;
    private static Class<?> dataClass;

    public static void setDataObject(Object data) {
        dataObject = data;
        dataClass = data.getClass();
    }

    public static void setDataClass(Class<?> dataClass) {
        dataObject = null;
        ObserverHelper.dataClass = dataClass;
    }

    public ObserverHelper(Context context) {
        this.context = context;
    }

    @SuppressWarnings("unchecked")
    public void start(final Object object, View view) {
        dataObservers.clear();
        viewObservers.clear();
        dataAdapters.clear();
        if (ObserverHelper.dataClass != null) {
            findDataObserver(object);
        }
        if (view != null) {
            findViewObserver(view,object);
        }
    }

    private void findViewObserver(View view,Object object) {
        if (view instanceof ViewGroup) {
            ViewGroup group = (ViewGroup) view;
            for (int i = 0; i < group.getChildCount(); i++) {
                View viewElem = group.getChildAt(i);
                findViewObserver(viewElem, object);
            }
        } else {
            linkViewObserver(view,object);
        }
    }

    @SuppressWarnings("unchecked")
    private void linkViewObserver(final View view,final Object object) {
        try {
            if (view.getTag() != null && view.getTag() instanceof String) {
                final String tag = (String) view.getTag();

                final List<ObservableWrapper> results = findObservablesByTag(tag);

                WrapObserver observer = new WrapObserver() {
                    @Override
                    public void update(Object data) {
                        try {
                            if (view instanceof TextView) {
                                TextView textView = (TextView) view;<commit_msg>bug fixes<commit_after>package com.jsonrpclib.observers;

import android.app.Activity;
import android.content.Context;
import android.database.DataSetObserver;
import android.os.Build;
import android.support.v4.app.FragmentActivity;
import android.util.Pair;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ListView;
import android.widget.TextView;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * User: jbogacki
 * Date: 08.04.2013
 * Time: 22:23
 */
public class ObserverHelper {
    private List<Pair<ObservableWrapper, WrapObserver>> dataObservers = new ArrayList<Pair<ObservableWrapper, WrapObserver>>();
    private List<Pair<Adapter, DataSetObserver>> dataAdapters = new ArrayList<Pair<Adapter, DataSetObserver>>();
    private Map<View, Pair<ObservableWrapper, WrapObserver>> viewObservers = new HashMap<View, Pair<ObservableWrapper, WrapObserver>>();

    private static final String splitter = "\\.";
    private static final Pattern pattern = Pattern.compile("\\[[^\\]]*\\]");
    private static final String convention = "Changed";
    private Context context;


    private static Object dataObject;
    private static Class<?> dataClass;

    public static void setDataObject(Object data) {
        dataObject = data;
        dataClass = data.getClass();
    }

    public static void setDataClass(Class<?> dataClass) {
        dataObject = null;
        ObserverHelper.dataClass = dataClass;
    }

    public ObserverHelper(Context context) {
        this.context = context;
    }

    @SuppressWarnings("unchecked")
    public void start(final Object object, View view) {
        dataObservers.clear();
        viewObservers.clear();
        dataAdapters.clear();
        if (ObserverHelper.dataClass != null) {
            findDataObserver(object);
        }
        if (view != null) {
            findViewObserver(view,object);
        }
    }

    private void findViewObserver(View view,Object object) {
        if (view instanceof ViewGroup && !(view instanceof ListView) && !isFromFragment(view,object)) {
            ViewGroup group = (ViewGroup) view;
            for (int i = 0; i < group.getChildCount(); i++) {
                View viewElem = group.getChildAt(i);
                findViewObserver(viewElem, object);
            }
        } else {
            linkViewObserver(view,object);
        }
    }


    private boolean isFromFragment(View view,Object object)
    {
        if(view.getId()==-1)
        {
            return false;
        }
        if(object instanceof FragmentActivity)
        {
            return ((FragmentActivity)object).getSupportFragmentManager().findFragmentById(view.getId())!=null;
        }
        else if (Build.VERSION.SDK_INT>Build.VERSION_CODES.HONEYCOMB && object instanceof Activity)
        {
            return ((Activity)object).getFragmentManager().findFragmentById(view.getId())!=null;
        }
        else
        {
            return false;
        }
    }

    @SuppressWarnings("unchecked")
    private void linkViewObserver(final View view,final Object object) {
        try {
            if (view.getTag() != null && view.getTag() instanceof String) {
                final String tag = (String) view.getTag();

                final List<ObservableWrapper> results = findObservablesByTag(tag);

                WrapObserver observer = new WrapObserver() {
                    @Override
                    public void update(Object data) {
                        try {
                            if (view instanceof TextView) {
                                TextView textView = (TextView) view;
{'fraction_non_alphanumeric': 0.10483056957462149, 'fraction_numerical': 0.004037490987743331, 'mean_word_length': 2.5919212843086483, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '2973077', 'n_tokens_mistral': 1880, 'n_tokens_neox': 1821, 'n_words': 450}