Skip to content
This repository was archived by the owner on May 18, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions CodePush.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Alert } from "./AlertAdapter";
import requestFetchAdapter from "./request-fetch-adapter";
import { AppState, Platform } from "react-native";
import log from "./logging";
import hoistStatics from 'hoist-non-react-statics';
import hoistStatics from "hoist-non-react-statics";

let NativeCodePush = require("react-native").NativeModules.CodePush;
const PackageMixins = require("./package-mixins")(NativeCodePush);
Expand All @@ -26,7 +26,22 @@ async function checkForUpdate(deploymentKey = null, handleBinaryVersionMismatchC
* dynamically "redirecting" end-users at different
* deployments (e.g. an early access deployment for insiders).
*/
const config = deploymentKey ? { ...nativeConfig, ...{ deploymentKey } } : nativeConfig;
const config = deploymentKey
? { ...nativeConfig, ...{ deploymentKey } }
: nativeConfig;

log("deploymentKey: " + deploymentKey);

const optionsEntries = Object.entries(nativeConfig);
optionsEntries.forEach(([key, value]) => {
log(key + ":", value);
});

const option = Object.entries(config);
option.forEach(([key, value]) => {
log(key + ":", value);
});

const sdk = getPromisifiedSdk(requestFetchAdapter, config);

// Use dynamically overridden getCurrentPackage() during tests.
Expand Down Expand Up @@ -92,15 +107,18 @@ async function checkForUpdate(deploymentKey = null, handleBinaryVersionMismatchC
const getConfiguration = (() => {
let config;
return async function getConfiguration() {
if (config) {
return config;
} else if (testConfig) {
return testConfig;
} else {
config = await NativeCodePush.getConfiguration();
return config;
}
}
// TODO: Check if we can rely on previous config
config = await NativeCodePush.getConfiguration();
return config;
// if (config) {
// return config;
// } else if (testConfig) {
// return testConfig;
// } else {
// config = await NativeCodePush.getConfiguration();
// return config;
// }
};
})();

async function getCurrentPackage() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.util.Log;

import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactPackage;
Expand All @@ -24,10 +25,10 @@

public class CodePush implements ReactPackage {

private static boolean sIsRunningBinaryVersion = false;
private static boolean sNeedToReportRollback = false;
private boolean sIsRunningBinaryVersion = false;
private boolean sNeedToReportRollback = false;
private static boolean sTestConfigurationFlag = false;
private static String sAppVersion = null;
private String sAppVersion = null;

private boolean mDidUpdate = false;

Expand All @@ -45,27 +46,32 @@ public class CodePush implements ReactPackage {
private Context mContext;
private final boolean mIsDebugMode;

private static String mPublicKey;
private String mPublicKey;

private static ReactInstanceHolder mReactInstanceHolder;
private static CodePush mCurrentInstance;
private ReactInstanceHolder mReactInstanceHolder;
private CodePush mCurrentInstance;

public CodePush(String deploymentKey, Context context) {
this(deploymentKey, context, false);
private CodePushConstants codePushConstants;

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

public static String getServiceUrl() {
return mServerUrl;
}

public CodePush(String deploymentKey, Context context, boolean isDebugMode) {
public CodePush(String type, String deploymentKey, Context context, boolean isDebugMode) {
mContext = context.getApplicationContext();
codePushConstants = new CodePushConstants();
codePushConstants.setCodePushFolderPrefix(type);

mUpdateManager = new CodePushUpdateManager(context.getFilesDir().getAbsolutePath());
mTelemetryManager = new CodePushTelemetryManager(mContext);
mUpdateManager = new CodePushUpdateManager(context.getFilesDir().getAbsolutePath(), codePushConstants);
mTelemetryManager = new CodePushTelemetryManager(mContext, codePushConstants);
mDeploymentKey = deploymentKey;
CodePushUtils.log("Codepush: " + this + " deploymentKey: " + mDeploymentKey);
mIsDebugMode = isDebugMode;
mSettingsManager = new SettingsManager(mContext);
mSettingsManager = new SettingsManager(mContext, codePushConstants);

if (sAppVersion == null) {
try {
Expand All @@ -77,8 +83,9 @@ public CodePush(String deploymentKey, Context context, boolean isDebugMode) {
}

mCurrentInstance = this;
CodePushUtils.log("mCurrentInstance: " + mCurrentInstance);

String publicKeyFromStrings = getCustomPropertyFromStringsIfExist("PublicKey");
String publicKeyFromStrings = getCustomPropertyFromStringsIfExist("PublicKey");
if (publicKeyFromStrings != null) mPublicKey = publicKeyFromStrings;

String serverUrlFromStrings = getCustomPropertyFromStringsIfExist("ServerUrl");
Expand All @@ -88,19 +95,19 @@ public CodePush(String deploymentKey, Context context, boolean isDebugMode) {
initializeUpdateAfterRestart();
}

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

public CodePush(String deploymentKey, Context context, boolean isDebugMode, int publicKeyResourceDescriptor) {
this(deploymentKey, context, isDebugMode);
public CodePush(String type, String deploymentKey, Context context, boolean isDebugMode, int publicKeyResourceDescriptor) {
this(type, deploymentKey, context, isDebugMode);

mPublicKey = getPublicKeyByResourceDescriptor(publicKeyResourceDescriptor);
}

public CodePush(String deploymentKey, Context context, boolean isDebugMode, String serverUrl, Integer publicKeyResourceDescriptor) {
this(deploymentKey, context, isDebugMode);
public CodePush(String type, String deploymentKey, Context context, boolean isDebugMode, String serverUrl, Integer publicKeyResourceDescriptor) {
this(type, deploymentKey, context, isDebugMode);

if (publicKeyResourceDescriptor != null) {
mPublicKey = getPublicKeyByResourceDescriptor(publicKeyResourceDescriptor);
Expand Down Expand Up @@ -129,18 +136,18 @@ private String getPublicKeyByResourceDescriptor(int publicKeyResourceDescriptor)

private String getCustomPropertyFromStringsIfExist(String propertyName) {
String property;

String packageName = mContext.getPackageName();
int resId = mContext.getResources().getIdentifier("CodePush" + propertyName, "string", packageName);

if (resId != 0) {
property = mContext.getString(resId);

if (!property.isEmpty()) {
return property;
} else {
CodePushUtils.log("Specified " + propertyName + " is empty");
}
}
}

return null;
Expand Down Expand Up @@ -217,12 +224,12 @@ public String getPackageFolder() {
}

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

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

Expand All @@ -234,11 +241,11 @@ public String getDeploymentKey() {
return mDeploymentKey;
}

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

public static String getJSBundleFile(String assetsBundleFileName) {
public String getJSBundleFile(String assetsBundleFileName) {
if (mCurrentInstance == null) {
throw new CodePushNotInitializedException("A CodePush instance has not been created yet. Have you added it to your app's list of ReactPackages?");
}
Expand Down Expand Up @@ -364,7 +371,7 @@ boolean needToReportRollback() {
return sNeedToReportRollback;
}

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

Expand All @@ -376,7 +383,7 @@ private void rollbackPackage() {
}

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

/* The below 3 methods are used for running tests.*/
Expand All @@ -388,7 +395,7 @@ public void setDeploymentKey(String deploymentKey) {
mDeploymentKey = deploymentKey;
}

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

Expand All @@ -398,11 +405,11 @@ public void clearUpdates() {
mSettingsManager.removeFailedUpdates();
}

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

static ReactInstanceManager getReactInstanceManager() {
ReactInstanceManager getReactInstanceManager() {
if (mReactInstanceHolder == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ public class CodePushBuilder {
private boolean mIsDebugMode;
private String mServerUrl;
private Integer mPublicKeyResourceDescriptor;

public CodePushBuilder(String deploymentKey, Context context) {
private String mType;
public CodePushBuilder(String type, String deploymentKey, Context context) {
this.mDeploymentKey = deploymentKey;
this.mContext = context;
this.mServerUrl = CodePush.getServiceUrl();
this.mType = type;
}

public CodePushBuilder setIsDebugMode(boolean isDebugMode) {
Expand All @@ -32,6 +33,6 @@ public CodePushBuilder setPublicKeyResourceDescriptor(int publicKeyResourceDescr
}

public CodePush build() {
return new CodePush(this.mDeploymentKey, this.mContext, this.mIsDebugMode, this.mServerUrl, this.mPublicKeyResourceDescriptor);
return new CodePush(this.mType, this.mDeploymentKey, this.mContext, this.mIsDebugMode, this.mServerUrl, this.mPublicKeyResourceDescriptor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
public class CodePushConstants {
public static final String ASSETS_BUNDLE_PREFIX = "assets://";
public static final String BINARY_MODIFIED_TIME_KEY = "binaryModifiedTime";
public static final String CODE_PUSH_FOLDER_PREFIX = "CodePush";
public String CODE_PUSH_FOLDER_PREFIX = "CodePush";
public static final String CODE_PUSH_HASH_FILE_NAME = "CodePushHash";
public static final String CODE_PUSH_OLD_HASH_FILE_NAME = "CodePushHash.json";
public static final String CODE_PUSH_PREFERENCES = "CodePush";
public String CODE_PUSH_PREFERENCES = "CodePush";
public static final String CURRENT_PACKAGE_KEY = "currentPackage";
public static final String DEFAULT_JS_BUNDLE_NAME = "index.android.bundle";
public static final String DIFF_MANIFEST_FILE_NAME = "hotcodepush.json";
Expand All @@ -31,4 +31,9 @@ public class CodePushConstants {
public static final String LATEST_ROLLBACK_PACKAGE_HASH_KEY = "packageHash";
public static final String LATEST_ROLLBACK_TIME_KEY = "time";
public static final String LATEST_ROLLBACK_COUNT_KEY = "count";

public void setCodePushFolderPrefix(String mType) {
CODE_PUSH_FOLDER_PREFIX = "CodePush/" + mType;
CODE_PUSH_PREFERENCES = "CodePush" + "_" + mType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ private void clearLifecycleEventListener() {

// Use reflection to find the ReactInstanceManager. See #556 for a proposal for a less brittle way to approach this.
private ReactInstanceManager resolveInstanceManager() throws NoSuchFieldException, IllegalAccessException {
ReactInstanceManager instanceManager = CodePush.getReactInstanceManager();
ReactInstanceManager instanceManager = mCodePush.getReactInstanceManager();
if (instanceManager != null) {
return instanceManager;
}
Expand Down Expand Up @@ -285,6 +285,7 @@ public void downloadUpdate(final ReadableMap updatePackage, final boolean notify
@Override
protected Void doInBackground(Void... params) {
try {
CodePushUtils.log("Update Package - " + updatePackage);
JSONObject mutableUpdatePackage = CodePushUtils.convertReadableToJsonObject(updatePackage);
CodePushUtils.setJSONValueForKey(mutableUpdatePackage, CodePushConstants.BINARY_MODIFIED_TIME_KEY, "" + mCodePush.getBinaryResourcesModifiedTime());
mUpdateManager.downloadPackage(mutableUpdatePackage, mCodePush.getAssetsBundleFileName(), new DownloadProgressCallback() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public class CodePushTelemetryManager {
private final String RETRY_DEPLOYMENT_REPORT_KEY = "CODE_PUSH_RETRY_DEPLOYMENT_REPORT";
private final String STATUS_KEY = "status";

public CodePushTelemetryManager(Context applicationContext) {
mSettings = applicationContext.getSharedPreferences(CodePushConstants.CODE_PUSH_PREFERENCES, 0);
public CodePushTelemetryManager(Context applicationContext, CodePushConstants codePushConstants) {
mSettings = applicationContext.getSharedPreferences(codePushConstants.CODE_PUSH_PREFERENCES, 0);
}

public WritableMap getBinaryUpdateReport(String appVersion) {
Expand Down
Loading