<commit_before>package com.limpoxe.fairy.manager.mapping;

import android.content.ContextWrapper;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.content.res.TypedArray;

import com.limpoxe.fairy.content.LoadedPlugin;
import com.limpoxe.fairy.content.PluginActivityInfo;
import com.limpoxe.fairy.content.PluginDescriptor;
import com.limpoxe.fairy.core.FairyGlobal;
import com.limpoxe.fairy.core.PluginLauncher;
import com.limpoxe.fairy.util.LogUtil;
import com.limpoxe.fairy.util.ResourceUtil;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
import static com.limpoxe.fairy.manager.mapping.PluginStubBinding.buildDefaultAction;

public class StubActivityMappingProcessor implements StubMappingProcessor {

    /**
     * key:stub Activity Name
     * value:plugin Activity Name
     */
    private static HashMap<String, String> singleTaskActivityMapping = new HashMap<String, String>();
    private static HashMap<String, String> singleTopActivityMapping = new HashMap<String, String>();
    private static HashMap<String, String> singleInstanceActivityMapping = new HashMap<String, String>();
    private static String standardActivity = null;
    private static String standardLandspaceActivity = null;
    private static String standardActivityTranslucent = null;

    private static boolean isPoolInited = false;

    private static int sResId = -1;

    @Override
    public String bindStub(PluginDescriptor pluginDescriptor, String pluginActivityClassName) {
        if (StubExact.isExact(pluginActivityClassName, PluginDescriptor.ACTIVITY)) {
            return pluginActivityClassName;
        }
        initStubPool();

        PluginActivityInfo info = pluginDescriptor.getActivityInfos().get(pluginActivityClassName);

        HashMap<String, String> bindingMapping = null;
        int launchMode = Integer.parseInt(info.getLaunchMode());

        if (launchMode == ActivityInfo.LAUNCH_MULTIPLE) {

            if (info.getTheme() != null) {
                LoadedPlugin loadedPlugin = PluginLauncher.instance().getRunningPlugin(pluginDescriptor.getPackageName());
                if (loadedPlugin != null) {
                    try {
                        if (sResId == -1) {
                            Class r = Class.forName("com.android.internal.R$attr");
                            Field f = r.getDeclaredField("windowIsTranslucent");
                            f.setAccessible(true);
                            sResId = (int)f.get(null);
                        }
                        int styleId = ResourceUtil.parseResId(info.getTheme());
                        if (styleId != 0) {
                            //maybe need cache
                            //根据目标Activity的主题id构造一个主题对象，
                            //并尝试从此主题中取出用于配置透明的属性：windowIsTranslucent
                            //如果取到了，说明目标Activity是使用的透明主题
                            //则返回透明主题的stubActivity
                            Resources.Theme theme = loadedPlugin.pluginResource.newTheme();
                            Resources.Theme baseTheme = ((ContextWrapper)loadedPlugin.pluginContext).getBaseContext().getTheme();
                            if (baseTheme != null) {
                                theme.setTo(baseTheme);
                            }
                            theme.applyStyle(styleId, true);
                            TypedArray a = theme.obtainStyledAttributes(null, new int[]{sResId}, 0, 0);
                            if (a.hasValue(0)) {
                                return standardActivityTranslucent;
                            }
                        }
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (NoSuchFieldException e) {
                        e.printStackTrace();
                    }
                }
            }

            if (Integer.parseInt(info.getScreenOrientation()) == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
                return standardLandspaceActivity;
            }

            return standardActivity;

        } else if (launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {

            bindingMapping = singleTaskActivityMapping;

        } else if (launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {

            bindingMapping = singleTopActivityMapping;

        } else if (launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {

            bindingMapping = singleInstanceActivityMapping;

        }<commit_msg>stub mapping NPE<commit_after>package com.limpoxe.fairy.manager.mapping;

import android.content.ContextWrapper;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.content.res.TypedArray;

import com.limpoxe.fairy.content.LoadedPlugin;
import com.limpoxe.fairy.content.PluginActivityInfo;
import com.limpoxe.fairy.content.PluginDescriptor;
import com.limpoxe.fairy.core.FairyGlobal;
import com.limpoxe.fairy.core.PluginLauncher;
import com.limpoxe.fairy.util.LogUtil;
import com.limpoxe.fairy.util.ResourceUtil;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
import static com.limpoxe.fairy.manager.mapping.PluginStubBinding.buildDefaultAction;

public class StubActivityMappingProcessor implements StubMappingProcessor {

    /**
     * key:stub Activity Name
     * value:plugin Activity Name
     */
    private static HashMap<String, String> singleTaskActivityMapping = new HashMap<String, String>();
    private static HashMap<String, String> singleTopActivityMapping = new HashMap<String, String>();
    private static HashMap<String, String> singleInstanceActivityMapping = new HashMap<String, String>();
    private static String standardActivity = null;
    private static String standardLandspaceActivity = null;
    private static String standardActivityTranslucent = null;

    private static boolean isPoolInited = false;

    private static int sResId = -1;

    @Override
    public String bindStub(PluginDescriptor pluginDescriptor, String pluginActivityClassName) {
        if (StubExact.isExact(pluginActivityClassName, PluginDescriptor.ACTIVITY)) {
            return pluginActivityClassName;
        }
        initStubPool();

        PluginActivityInfo info = pluginDescriptor.getActivityInfos().get(pluginActivityClassName);

        HashMap<String, String> bindingMapping = null;
        int launchMode = Integer.parseInt(info.getLaunchMode());

        if (launchMode == ActivityInfo.LAUNCH_MULTIPLE) {

            if (info.getTheme() != null) {
                LoadedPlugin loadedPlugin = PluginLauncher.instance().getRunningPlugin(pluginDescriptor.getPackageName());
                if (loadedPlugin != null) {
                    try {
                        if (sResId == -1) {
                            Class r = Class.forName("com.android.internal.R$attr");
                            Field f = r.getDeclaredField("windowIsTranslucent");
                            f.setAccessible(true);
                            sResId = (int)f.get(null);
                        }
                        int styleId = ResourceUtil.parseResId(info.getTheme());
                        if (styleId != 0) {
                            //maybe need cache
                            //根据目标Activity的主题id构造一个主题对象，
                            //并尝试从此主题中取出用于配置透明的属性：windowIsTranslucent
                            //如果取到了，说明目标Activity是使用的透明主题
                            //则返回透明主题的stubActivity
                            Resources.Theme theme = loadedPlugin.pluginResource.newTheme();
                            Resources.Theme baseTheme = ((ContextWrapper)loadedPlugin.pluginContext).getBaseContext().getTheme();
                            if (baseTheme != null) {
                                theme.setTo(baseTheme);
                            }
                            theme.applyStyle(styleId, true);
                            TypedArray a = theme.obtainStyledAttributes(null, new int[]{sResId}, 0, 0);
                            if (a.hasValue(0)) {
                                return standardActivityTranslucent;
                            }
                        }
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (NoSuchFieldException e) {
                        e.printStackTrace();
                    }
                }
            }

            if (info.getScreenOrientation() != null && Integer.parseInt(info.getScreenOrientation()) == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
                return standardLandspaceActivity;
            }

            return standardActivity;

        } else if (launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {

            bindingMapping = singleTaskActivityMapping;

        } else if (launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {

            bindingMapping = singleTopActivityMapping;

        } else if (launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {

            bindingMapping = singleInstanceActivityMapping;

        }
{'fraction_non_alphanumeric': 0.08363895680521598, 'fraction_numerical': 0.0012224938875305623, 'mean_word_length': 2.069731081926204, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '6127021', 'n_tokens_mistral': 2514, 'n_tokens_neox': 2486, 'n_words': 458}