<commit_before>package com.android.support.application;

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.Looper;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * application获取辅助类
 *
 * @author lizhangqu
 * @version V1.0
 * @since 2017-07-06 20:42
 */
public class ApplicationCompat {
    private static final Object LOCK = new Object();
    @SuppressLint("StaticFieldLeak")
    private static Application sApplication;

    /**
     * 返回全局Application
     *
     * @return Application
     */
    public static Application getApplication() {
        if (sApplication != null) {
            return sApplication;
        }
        synchronized (ApplicationCompat.class) {
            try {
                if (sApplication == null) {
                    sApplication = getSystemApp();
                    if (sApplication == null && Thread.currentThread().getId() != Looper.getMainLooper().getThread().getId()) {
                        synchronized (LOCK) {
                            //主线程直接获取
                            Handler handler = new Handler(Looper.getMainLooper());
                            handler.post(new ApplicationGetter());
                            LOCK.wait();
                        }
                    }
                }
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
        return sApplication;
    }

    /**
     * 获得Application
     *
     * @return Application
     * @throws ClassNotFoundException    ClassNotFoundException
     * @throws NoSuchMethodException     NoSuchMethodException
     * @throws NoSuchFieldException      NoSuchFieldException
     * @throws InvocationTargetException InvocationTargetException
     * @throws IllegalAccessException    IllegalAccessException
     */
    private static Application getSystemApp() throws ClassNotFoundException, NoSuchMethodException, NoSuchFieldException, InvocationTargetException, IllegalAccessException {
        Class<?> cls = Class.forName("android.app.ActivityThread");
        Method declaredMethod = cls.getDeclaredMethod("currentActivityThread");
        Field declaredField = cls.getDeclaredField("mInitialApplication");
        declaredField.setAccessible(true);
        Object object = declaredField.get(declaredMethod.invoke(null));
        if (object instanceof Application) {
            return (Application) object;
        }
        return null;
    }

    /**
     * 阻塞子线程，切换到主线程获取
     */
    private static class ApplicationGetter implements Runnable {
        ApplicationGetter() {
        }

        @Override
        public void run() {
            try {
                sApplication = getSystemApp();
            } catch (Throwable e) {
                e.printStackTrace();
            }
            synchronized (LOCK) {
                LOCK.notify();
            }
        }
    }

    /**
     * 返回app名字
     *
     * @return appName
     */
    public static String getAppName() {
        try {
            PackageManager packageManager = getApplication().getPackageManager();
            ApplicationInfo applicationInfo = packageManager.getApplicationInfo(getApplication().getPackageName(), 0);
            return packageManager.getApplicationLabel(applicationInfo).toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 返回versionName
     *
     * @return versionName
     */
    public static String getVersionName() {
        try {
            return getApplication().getPackageManager().getPackageInfo(getApplication().getPackageName(), 0).versionName;
        } catch (Exception e) {
            e.printStackTrace();
        }<commit_msg>* add method to get Application Context<commit_after>package com.android.support.application;

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.Looper;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * application获取辅助类
 *
 * @author lizhangqu
 * @version V1.0
 * @since 2017-07-06 20:42
 */
public class ApplicationCompat {
    private static final Object LOCK = new Object();
    @SuppressLint("StaticFieldLeak")
    private static Application sApplication;

    /**
     * 返回全局Application
     *
     * @return Application
     */
    public static Application getApplication() {
        if (sApplication != null) {
            return sApplication;
        }
        synchronized (ApplicationCompat.class) {
            try {
                if (sApplication == null) {
                    sApplication = getSystemApp();
                    if (sApplication == null && Thread.currentThread().getId() != Looper.getMainLooper().getThread().getId()) {
                        synchronized (LOCK) {
                            //主线程直接获取
                            Handler handler = new Handler(Looper.getMainLooper());
                            handler.post(new ApplicationGetter());
                            LOCK.wait();
                        }
                    }
                }
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
        return sApplication;
    }

    /**
     * 获得Application
     *
     * @return Application
     * @throws ClassNotFoundException    ClassNotFoundException
     * @throws NoSuchMethodException     NoSuchMethodException
     * @throws NoSuchFieldException      NoSuchFieldException
     * @throws InvocationTargetException InvocationTargetException
     * @throws IllegalAccessException    IllegalAccessException
     */
    private static Application getSystemApp() throws ClassNotFoundException, NoSuchMethodException, NoSuchFieldException, InvocationTargetException, IllegalAccessException {
        Class<?> cls = Class.forName("android.app.ActivityThread");
        Method declaredMethod = cls.getDeclaredMethod("currentActivityThread");
        Field declaredField = cls.getDeclaredField("mInitialApplication");
        declaredField.setAccessible(true);
        Object object = declaredField.get(declaredMethod.invoke(null));
        if (object instanceof Application) {
            return (Application) object;
        }
        return null;
    }

    /**
     * 阻塞子线程，切换到主线程获取
     */
    private static class ApplicationGetter implements Runnable {
        ApplicationGetter() {
        }

        @Override
        public void run() {
            try {
                sApplication = getSystemApp();
            } catch (Throwable e) {
                e.printStackTrace();
            }
            synchronized (LOCK) {
                LOCK.notify();
            }
        }
    }

    /**
     * 获取applicationContext
     *
     * @return
     */
    public static Context getApplicationContext() {
        try {
            return getApplication().getApplicationContext();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 返回app名字
     *
     * @return appName
     */
    public static String getAppName() {
        try {
            PackageManager packageManager = getApplication().getPackageManager();
            ApplicationInfo applicationInfo = packageManager.getApplicationInfo(getApplication().getPackageName(), 0);
            return packageManager.getApplicationLabel(applicationInfo).toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 返回versionName
     *
     * @return versionName
     */
    public static String getVersionName() {
        try {
            return getApplication().getPackageManager().getPackageInfo(getApplication().getPackageName(), 0).versionName;
        } catch (Exception e) {
            e.printStackTrace();
        }
{'fraction_non_alphanumeric': 0.0923374425912497, 'fraction_numerical': 0.0038675368624607204, 'mean_word_length': 2.058019216555802, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1466366', 'n_tokens_mistral': 2047, 'n_tokens_neox': 2044, 'n_words': 415}