ProxyApplication.java 源代码


package com.x8zs.shell;

import android.app.Application;
import android.content.Context;
import android.content.ContextWrapper;
import android.os.Bundle;
import android.text.TextUtils;
import com.anythink.basead.b.a;
import com.anythink.core.api.ATAdConst;
import com.umeng.commonsdk.framework.UMModuleRegister;
import com.x8zs.morgoo.helper.Log;
import com.x8zs.plugin.client.ClientInfo;
import com.x8zs.plugin.mta.MtaManager;
import com.x8zs.plugin.patcher.IncrementalClassLoader;
import com.x8zs.plugin.patcher.MonkeyPatcher;
import com.x8zs.plugin.utils.IOUtil;
import com.x8zs.plugin.utils.OSUtil;
import java.io.File;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;

public class ProxyApplication extends Application {
    private Application mRealApp;

    private static Application createOriginalApplication(Context context) {
        String originalAppClassName = getOriginalAppClassName(context);
        if (!TextUtils.isEmpty(originalAppClassName)) {
            return (Application) Class.forName(originalAppClassName).getConstructor(new Class[0]).newInstance(new Object[0]);
        }
        return null;
    }

    public static void doAttachBaseContext(Context context) {
        if (isOurProcess(context)) {
            X8MiscApplication.attachBaseContext(context);
        } else if (OSUtil.isMainProcess(context)) {
            MainApplication.attachBaseContext(context);
        }
    }

    public static void doOnCreate(Application application) {
        if (isOurProcess(application)) {
            X8MiscApplication.onCreate(application);
        } else if (OSUtil.isMainProcess(application)) {
            MainApplication.onCreate(application);
        }
    }

    private static String getOriginalAppClassName(Context context) {
        Bundle bundle = context.getPackageManager().getApplicationInfo(context.getPackageName(), 128).metaData;
        if (bundle != null && bundle.containsKey("x8zs_class_name")) {
            String string = bundle.getString("x8zs_class_name");
            String packageName = context.getPackageName();
            if (!string.contains(".")) {
                if (!string.startsWith(".")) {
                    return packageName + "." + string;
                }
                return string;
            }
            if (string.startsWith(".")) {
                return packageName + string;
            }
            return string;
        }
        return null;
    }

    public static boolean isOurProcess(Context context) {
        String currentProcessName = OSUtil.getCurrentProcessName(context);
        if (currentProcessName != null && currentProcessName.endsWith(":x8process")) {
            return true;
        }
        return false;
    }

    private static boolean isShellBuildPkg(Context context) {
        try {
            String[] list = context.getAssets().list(Log.TAG);
            if (list == null) {
                return false;
            }
            if (list.length <= 0) {
                return false;
            }
            return true;
        } catch (Throwable unused) {
            return false;
        }
    }

    private static void loadMultiDexAndSetupClassloader(Context context) {
        String[] strArr;
        try {
            strArr = context.getAssets().list(Log.TAG);
        } catch (Throwable unused) {
            strArr = null;
        }
        if (strArr != null && strArr.length != 0) {
            StringBuilder sb = new StringBuilder();
            boolean z6 = false;
            for (String str : strArr) {
                sb.append(str);
                sb.append(",");
            }
            android.util.Log.d("ProxyApplication", "[loadMultiDexAndSetupClassloader] start, dexNames = " + sb.toString());
            ArrayList arrayList = new ArrayList();
            for (String str2 : strArr) {
                String str3 = context.getDir("x8zsdex", 0) + "/" + str2;
                File file = new File(str3);
                if (!file.exists()) {
                    IOUtil.copyAsset(context, "x8zs/" + str2, file);
                }
                arrayList.add(str3);
            }
            String absolutePath = context.getDir("x8zsodex", 0).getAbsolutePath();
            try {
                String[] strArr2 = context.getApplicationInfo().sharedLibraryFiles;
                if (strArr2 != null) {
                    int length = strArr2.length;
                    int i6 = 0;
                    while (true) {
                        if (i6 >= length) {
                            break;
                        }
                        if (strArr2[i6].equals("/system/framework/org.apache.http.legacy.jar")) {
                            z6 = true;
                            break;
                        }
                        i6++;
                    }
                }
            } catch (Throwable th) {
                th.printStackTrace();
            }
            IncrementalClassLoader.setupClassLoaders(context, context.getClassLoader(), absolutePath, arrayList, z6);
            android.util.Log.d("ProxyApplication", "[loadMultiDexAndSetupClassloader] done");
            return;
        }
        android.util.Log.d("ProxyApplication", "[loadMultiDexAndSetupClassloader] start, no dex files");
    }

    @Override
    protected void attachBaseContext(Context context) {
        String str;
        MtaManager.init(context);
        try {
            loadMultiDexAndSetupClassloader(context);
            Application createOriginalApplication = createOriginalApplication(context);
            this.mRealApp = createOriginalApplication;
            if (createOriginalApplication != null) {
                android.util.Log.d("ProxyApplication", "[attachBaseContext] app = " + this.mRealApp);
                Method declaredMethod = ContextWrapper.class.getDeclaredMethod("attachBaseContext", Context.class);
                declaredMethod.setAccessible(true);
                declaredMethod.invoke(this.mRealApp, context);
                MonkeyPatcher.disableContentProviders();
            } else {
                android.util.Log.d("ProxyApplication", "[attachBaseContext] no app");
            }
            if (isShellBuildPkg(context)) {
                str = "101";
            } else {
                str = ATAdConst.BIDDING_TYPE.BIDDING_LOSS_WITH_LOW_PRICE_IN_NORMAL;
            }
            ClientInfo.init(str, "3.3.6.8", "gp");
            doAttachBaseContext(context);
            super.attachBaseContext(context);
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put(a.C0030a.A, context.getPackageName());
            hashMap.put(UMModuleRegister.PROCESS, OSUtil.getCurrentProcessName(context));
            MtaManager.getInstance().track("App_AttachBaseContext_Success", hashMap);
        } catch (Throwable th) {
            th.printStackTrace();
            android.util.Log.e("ProxyApplication", "[attachBaseContext] ex: " + th.getMessage());
            HashMap<String, String> hashMap2 = new HashMap<>();
            hashMap2.put(a.C0030a.A, context.getPackageName());
            hashMap2.put(UMModuleRegister.PROCESS, OSUtil.getCurrentProcessName(context));
            hashMap2.put("error", th.getClass().getSimpleName() + ": " + th.getMessage());
            MtaManager.getInstance().track("App_AttachBaseContext_Fail", hashMap2);
            throw new RuntimeException("[attachBaseContext] ex: " + th.getMessage());
        }
    }

    @Override
    public void onCreate() {
        try {
            if (this.mRealApp != null) {
                android.util.Log.d("ProxyApplication", "[onCreate] app = " + this.mRealApp);
                MonkeyPatcher.monkeyPatchApplication(this, this, this.mRealApp, null);
                MonkeyPatcher.enableContentProviders(this.mRealApp);
                this.mRealApp.onCreate();
            } else {
                android.util.Log.d("ProxyApplication", "[onCreate] no app");
            }
            Application application = this.mRealApp;
            if (application == null) {
                application = this;
            }
            doOnCreate(application);
            super.onCreate();
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put(a.C0030a.A, getPackageName());
            hashMap.put(UMModuleRegister.PROCESS, OSUtil.getCurrentProcessName(this));
            MtaManager.getInstance().track("App_OnCreate_Success", hashMap);
        } catch (Throwable th) {
            th.printStackTrace();
            android.util.Log.e("ProxyApplication", "[onCreate] ex: " + th.getMessage());
            HashMap<String, String> hashMap2 = new HashMap<>();
            hashMap2.put(a.C0030a.A, getPackageName());
            hashMap2.put(UMModuleRegister.PROCESS, OSUtil.getCurrentProcessName(this));
            hashMap2.put("error", th.getClass().getSimpleName() + ": " + th.getMessage());
            MtaManager.getInstance().track("App_OnCreate_Fail", hashMap2);
            throw new RuntimeException("[onCreate] ex: " + th.getMessage());
        }
    }
}