易胜博 v5.4.2版本的 MD5 值为:c151c34758927d86cba9be014d144084

以下内容为反编译后的 CodePush.java 源代码,内容仅作参考


package com.microsoft.codepush.react;

import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.JavaScriptModule;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.devsupport.DevInternalSettings;
import com.facebook.react.devsupport.interfaces.DevSupportManager;
import com.facebook.react.uimanager.ViewManager;
import java.io.File;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;

public class CodePush implements ReactPackage {
    private static CodePush mCurrentInstance = null;
    private static String mPublicKey = null;
    private static ReactInstanceHolder mReactInstanceHolder = null;
    private static String mServerUrl = "https://codepush.appcenter.ms/";
    private static String sAppVersion;
    private static boolean sIsRunningBinaryVersion;
    private static boolean sNeedToReportRollback;
    private static boolean sTestConfigurationFlag;
    private String mAssetsBundleFileName;
    private Context mContext;
    private String mDeploymentKey;
    private boolean mDidUpdate;
    private final boolean mIsDebugMode;
    private SettingsManager mSettingsManager;
    private CodePushTelemetryManager mTelemetryManager;
    private CodePushUpdateManager mUpdateManager;

    public CodePush(String deploymentKey, Context context) {
        this(deploymentKey, context, false);
    }

    public static String getServiceUrl() {
        return mServerUrl;
    }

    public CodePush(String deploymentKey, Context context, boolean isDebugMode) {
        this.mDidUpdate = false;
        this.mContext = context.getApplicationContext();
        this.mUpdateManager = new CodePushUpdateManager(context.getFilesDir().getAbsolutePath());
        this.mTelemetryManager = new CodePushTelemetryManager(this.mContext);
        this.mDeploymentKey = deploymentKey;
        this.mIsDebugMode = isDebugMode;
        this.mSettingsManager = new SettingsManager(this.mContext);
        if (sAppVersion == null) {
            try {
                sAppVersion = this.mContext.getPackageManager().getPackageInfo(this.mContext.getPackageName(), 0).versionName;
            } catch (PackageManager.NameNotFoundException e) {
                throw new CodePushUnknownException("Unable to get package info for " + this.mContext.getPackageName(), e);
            }
        }
        mCurrentInstance = this;
        String customPropertyFromStringsIfExist = getCustomPropertyFromStringsIfExist("PublicKey");
        if (customPropertyFromStringsIfExist != null) {
            mPublicKey = customPropertyFromStringsIfExist;
        }
        String customPropertyFromStringsIfExist2 = getCustomPropertyFromStringsIfExist("ServerUrl");
        if (customPropertyFromStringsIfExist2 != null) {
            mServerUrl = customPropertyFromStringsIfExist2;
        }
        clearDebugCacheIfNeeded(null);
        initializeUpdateAfterRestart();
    }

    public CodePush(String deploymentKey, Context context, boolean isDebugMode, String serverUrl) {
        this(deploymentKey, context, isDebugMode);
        mServerUrl = serverUrl;
    }

    public CodePush(String deploymentKey, Context context, boolean isDebugMode, int publicKeyResourceDescriptor) {
        this(deploymentKey, context, isDebugMode);
        mPublicKey = getPublicKeyByResourceDescriptor(publicKeyResourceDescriptor);
    }

    public CodePush(String deploymentKey, Context context, boolean isDebugMode, String serverUrl, Integer publicKeyResourceDescriptor) {
        this(deploymentKey, context, isDebugMode);
        if (publicKeyResourceDescriptor != null) {
            mPublicKey = getPublicKeyByResourceDescriptor(publicKeyResourceDescriptor.intValue());
        }
        mServerUrl = serverUrl;
    }

    private String getPublicKeyByResourceDescriptor(int publicKeyResourceDescriptor) {
        try {
            String string = this.mContext.getString(publicKeyResourceDescriptor);
            if (string.isEmpty()) {
                throw new CodePushInvalidPublicKeyException("Specified public key is empty");
            }
            return string;
        } catch (Resources.NotFoundException e) {
            throw new CodePushInvalidPublicKeyException("Unable to get public key, related resource descriptor " + publicKeyResourceDescriptor + " can not be found", e);
        }
    }

    private String getCustomPropertyFromStringsIfExist(String propertyName) {
        String packageName = this.mContext.getPackageName();
        int identifier = this.mContext.getResources().getIdentifier("CodePush" + propertyName, "string", packageName);
        if (identifier == 0) {
            return null;
        }
        String string = this.mContext.getString(identifier);
        if (!string.isEmpty()) {
            return string;
        }
        CodePushUtils.log("Specified " + propertyName + " is empty");
        return null;
    }

    private boolean isLiveReloadEnabled(ReactInstanceManager instanceManager) {
        DevSupportManager devSupportManager;
        if (instanceManager != null && (devSupportManager = instanceManager.getDevSupportManager()) != null) {
            DevInternalSettings devInternalSettings = (DevInternalSettings) devSupportManager.getDevSettings();
            for (Method method : devInternalSettings.getClass().getMethods()) {
                if (method.getName().equals("isReloadOnJSChangeEnabled")) {
                    try {
                        return ((Boolean) method.invoke(devInternalSettings, new Object[0])).booleanValue();
                    } catch (Exception unused) {
                        return false;
                    }
                }
            }
        }
        return false;
    }

    public void clearDebugCacheIfNeeded(ReactInstanceManager instanceManager) {
        if (this.mIsDebugMode && this.mSettingsManager.isPendingUpdate(null) && !isLiveReloadEnabled(instanceManager)) {
            File file = new File(this.mContext.getFilesDir(), "ReactNativeDevBundle.js");
            if (file.exists()) {
                file.delete();
            }
        }
    }

    public boolean didUpdate() {
        return this.mDidUpdate;
    }

    public String getAppVersion() {
        return sAppVersion;
    }

    public String getAssetsBundleFileName() {
        return this.mAssetsBundleFileName;
    }

    public String getPublicKey() {
        return mPublicKey;
    }

    public long getBinaryResourcesModifiedTime() {
        try {
            return Long.parseLong(this.mContext.getResources().getString(this.mContext.getResources().getIdentifier(CodePushConstants.CODE_PUSH_APK_BUILD_TIME_KEY, "string", this.mContext.getPackageName())).replaceAll("\"", ""));
        } catch (Exception e) {
            throw new CodePushUnknownException("Error in getting binary resources modified time", e);
        }
    }

    public String getPackageFolder() {
        JSONObject currentPackage = this.mUpdateManager.getCurrentPackage();
        if (currentPackage == null) {
            return null;
        }
        return this.mUpdateManager.getPackageFolderPath(currentPackage.optString("packageHash"));
    }

    @Deprecated
    public static String getBundleUrl() {
        return getJSBundleFile();
    }

    @Deprecated
    public static String getBundleUrl(String assetsBundleFileName) {
        return getJSBundleFile(assetsBundleFileName);
    }

    public Context getContext() {
        return this.mContext;
    }

    public String getDeploymentKey() {
        return this.mDeploymentKey;
    }

    public static String getJSBundleFile() {
        return getJSBundleFile(CodePushConstants.DEFAULT_JS_BUNDLE_NAME);
    }

    public static String getJSBundleFile(String assetsBundleFileName) {
        CodePush codePush = mCurrentInstance;
        if (codePush == null) {
            throw new CodePushNotInitializedException("A CodePush instance has not been created yet. Have you added it to your app's list of ReactPackages?");
        }
        return codePush.getJSBundleFileInternal(assetsBundleFileName);
    }

    public String getJSBundleFileInternal(String assetsBundleFileName) {
        String str;
        this.mAssetsBundleFileName = assetsBundleFileName;
        String str2 = CodePushConstants.ASSETS_BUNDLE_PREFIX + assetsBundleFileName;
        try {
            str = this.mUpdateManager.getCurrentPackageBundlePath(this.mAssetsBundleFileName);
        } catch (CodePushMalformedDataException e) {
            CodePushUtils.log(e.getMessage());
            clearUpdates();
            str = null;
        }
        if (str == null) {
            CodePushUtils.logBundleUrl(str2);
            sIsRunningBinaryVersion = true;
            return str2;
        }
        JSONObject currentPackage = this.mUpdateManager.getCurrentPackage();
        if (isPackageBundleLatest(currentPackage)) {
            CodePushUtils.logBundleUrl(str);
            sIsRunningBinaryVersion = false;
            return str;
        }
        this.mDidUpdate = false;
        if (!this.mIsDebugMode || hasBinaryVersionChanged(currentPackage)) {
            clearUpdates();
        }
        CodePushUtils.logBundleUrl(str2);
        sIsRunningBinaryVersion = true;
        return str2;
    }

    public String getServerUrl() {
        return mServerUrl;
    }

    public void initializeUpdateAfterRestart() {
        this.mDidUpdate = false;
        JSONObject pendingUpdate = this.mSettingsManager.getPendingUpdate();
        if (pendingUpdate != null) {
            JSONObject currentPackage = this.mUpdateManager.getCurrentPackage();
            if (currentPackage == null || (!isPackageBundleLatest(currentPackage) && hasBinaryVersionChanged(currentPackage))) {
                CodePushUtils.log("Skipping initializeUpdateAfterRestart(), binary version is newer");
                return;
            }
            try {
                if (pendingUpdate.getBoolean(CodePushConstants.PENDING_UPDATE_IS_LOADING_KEY)) {
                    CodePushUtils.log("Update did not finish loading the last time, rolling back to a previous version.");
                    sNeedToReportRollback = true;
                    rollbackPackage();
                } else {
                    this.mDidUpdate = true;
                    this.mSettingsManager.savePendingUpdate(pendingUpdate.getString(CodePushConstants.PENDING_UPDATE_HASH_KEY), true);
                }
            } catch (JSONException e) {
                throw new CodePushUnknownException("Unable to read pending update metadata stored in SharedPreferences", e);
            }
        }
    }

    public void invalidateCurrentInstance() {
        mCurrentInstance = null;
    }

    public boolean isDebugMode() {
        return this.mIsDebugMode;
    }

    public boolean isRunningBinaryVersion() {
        return sIsRunningBinaryVersion;
    }

    private boolean isPackageBundleLatest(JSONObject packageMetadata) {
        try {
            String optString = packageMetadata.optString(CodePushConstants.BINARY_MODIFIED_TIME_KEY, null);
            Long valueOf = optString != null ? Long.valueOf(Long.parseLong(optString)) : null;
            String optString2 = packageMetadata.optString("appVersion", null);
            long binaryResourcesModifiedTime = getBinaryResourcesModifiedTime();
            if (valueOf != null && valueOf.longValue() == binaryResourcesModifiedTime) {
                if (!isUsingTestConfiguration()) {
                    if (sAppVersion.equals(optString2)) {
                    }
                }
                return true;
            }
            return false;
        } catch (NumberFormatException e) {
            throw new CodePushUnknownException("Error in reading binary modified date from package metadata", e);
        }
    }

    private boolean hasBinaryVersionChanged(JSONObject packageMetadata) {
        return !sAppVersion.equals(packageMetadata.optString("appVersion", null));
    }

    public boolean needToReportRollback() {
        return sNeedToReportRollback;
    }

    public static void overrideAppVersion(String appVersionOverride) {
        sAppVersion = appVersionOverride;
    }

    private void rollbackPackage() {
        this.mSettingsManager.saveFailedUpdate(this.mUpdateManager.getCurrentPackage());
        this.mUpdateManager.rollbackPackage();
        this.mSettingsManager.removePendingUpdate();
    }

    public void setNeedToReportRollback(boolean needToReportRollback) {
        sNeedToReportRollback = needToReportRollback;
    }

    public static boolean isUsingTestConfiguration() {
        return sTestConfigurationFlag;
    }

    public void setDeploymentKey(String deploymentKey) {
        this.mDeploymentKey = deploymentKey;
    }

    public static void setUsingTestConfiguration(boolean shouldUseTestConfiguration) {
        sTestConfigurationFlag = shouldUseTestConfiguration;
    }

    public void clearUpdates() {
        this.mUpdateManager.clearUpdates();
        this.mSettingsManager.removePendingUpdate();
        this.mSettingsManager.removeFailedUpdates();
    }

    public static void setReactInstanceHolder(ReactInstanceHolder reactInstanceHolder) {
        mReactInstanceHolder = reactInstanceHolder;
    }

    public static ReactInstanceManager getReactInstanceManager() {
        ReactInstanceHolder reactInstanceHolder = mReactInstanceHolder;
        if (reactInstanceHolder == null) {
            return null;
        }
        return reactInstanceHolder.getReactInstanceManager();
    }

    @Override
    public List<NativeModule> createNativeModules(ReactApplicationContext reactApplicationContext) {
        CodePushNativeModule codePushNativeModule = new CodePushNativeModule(reactApplicationContext, this, this.mUpdateManager, this.mTelemetryManager, this.mSettingsManager);
        CodePushDialog codePushDialog = new CodePushDialog(reactApplicationContext);
        ArrayList arrayList = new ArrayList();
        arrayList.add(codePushNativeModule);
        arrayList.add(codePushDialog);
        return arrayList;
    }

    public List<Class<? extends JavaScriptModule>> createJSModules() {
        return new ArrayList();
    }

    @Override
    public List<ViewManager> createViewManagers(ReactApplicationContext reactApplicationContext) {
        return new ArrayList();
    }
}