Skip to content

Commit

Permalink
Merge pull request #610 from bugsnag/version-code-override
Browse files Browse the repository at this point in the history
Allow overriding the versionCode via Configuration
  • Loading branch information
fractalwrench authored Oct 9, 2019
2 parents 9ef0b05 + c52ff1e commit 3471dde
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## TBD

* Allow overriding the versionCode via Configuration
[#610](https://github.com/bugsnag/bugsnag-android/pull/610)

## 4.20.0 (2019-09-25)

* Record StorageManager cache behaviour in internal error reports
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public void setUp() throws Exception {
Context context = ApplicationProvider.getApplicationContext();
PackageManager packageManager = context.getPackageManager();
Configuration config = new Configuration("api-key");
config.setVersionCode(1);
AppData obj = new AppData(context, packageManager, config, sessionTracker);
this.appData = obj.getAppDataSummary();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public void setUp() throws Exception {
Context context = ApplicationProvider.getApplicationContext();
PackageManager packageManager = context.getPackageManager();
Configuration config = new Configuration("api-key");
config.setVersionCode(1);
AppData obj = new AppData(context, packageManager, config, sessionTracker);
this.appData = obj.getAppData();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ public void testEmptyManifestConfig() {
ConfigFactory.populateConfigFromManifest(protoConfig, data);

assertEquals(config.getApiKey(), protoConfig.getApiKey());
assertEquals(config.getVersionCode(), protoConfig.getVersionCode());
assertEquals(config.getBuildUUID(), protoConfig.getBuildUUID());
assertEquals(config.getAppVersion(), protoConfig.getAppVersion());
assertEquals(config.getReleaseStage(), protoConfig.getReleaseStage());
Expand All @@ -198,13 +199,15 @@ public void testEmptyManifestConfig() {
public void testFullManifestConfig() {
String buildUuid = "123";
String appVersion = "v1.0";
Integer versionCode = 27;
String releaseStage = "debug";
String endpoint = "http://example.com";
String sessionEndpoint = "http://session-example.com";

Bundle data = new Bundle();
data.putString("com.bugsnag.android.BUILD_UUID", buildUuid);
data.putString("com.bugsnag.android.APP_VERSION", appVersion);
data.putInt("com.bugsnag.android.VERSION_CODE", versionCode);
data.putString("com.bugsnag.android.RELEASE_STAGE", releaseStage);
data.putString("com.bugsnag.android.SESSIONS_ENDPOINT", sessionEndpoint);
data.putString("com.bugsnag.android.ENDPOINT", endpoint);
Expand All @@ -219,6 +222,7 @@ public void testFullManifestConfig() {
ConfigFactory.populateConfigFromManifest(protoConfig, data);
assertEquals(buildUuid, protoConfig.getBuildUUID());
assertEquals(appVersion, protoConfig.getAppVersion());
assertEquals(versionCode, protoConfig.getVersionCode());
assertEquals(releaseStage, protoConfig.getReleaseStage());
assertEquals(endpoint, protoConfig.getEndpoint());
assertEquals(sessionEndpoint, protoConfig.getSessionEndpoint());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,16 @@ private String calculateNotifierType() {
@Nullable
@SuppressWarnings("deprecation")
private Integer calculateVersionCode() {
if (packageInfo != null) {
return packageInfo.versionCode;
Integer versionCode = config.getVersionCode();

if (versionCode != null) {
return versionCode;
} else {
return null;
if (packageInfo != null) {
return packageInfo.versionCode;
} else {
return null;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class ConfigFactory {

private static final String BUGSNAG_NAMESPACE = "com.bugsnag.android";
private static final String MF_APP_VERSION = BUGSNAG_NAMESPACE + ".APP_VERSION";
private static final String MF_VERSION_CODE = BUGSNAG_NAMESPACE + ".VERSION_CODE";
private static final String MF_ENDPOINT = BUGSNAG_NAMESPACE + ".ENDPOINT";
private static final String MF_SESSIONS_ENDPOINT = BUGSNAG_NAMESPACE + ".SESSIONS_ENDPOINT";
private static final String MF_RELEASE_STAGE = BUGSNAG_NAMESPACE + ".RELEASE_STAGE";
Expand Down Expand Up @@ -92,6 +93,9 @@ static void populateConfigFromManifest(@NonNull Configuration config,
config.setAppVersion(data.getString(MF_APP_VERSION));
config.setReleaseStage(data.getString(MF_RELEASE_STAGE));

if (data.containsKey(MF_VERSION_CODE)) {
config.setVersionCode(data.getInt(MF_VERSION_CODE));
}
if (data.containsKey(MF_ENDPOINT)) {
String endpoint = data.getString(MF_ENDPOINT);
String sessionEndpoint = data.getString(MF_SESSIONS_ENDPOINT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class Configuration extends Observable implements Observer {
private final String apiKey;
private String buildUuid;
private String appVersion;
private Integer versionCode;
private String context;
private volatile String endpoint = "https://notify.bugsnag.com";
private volatile String sessionEndpoint = "https://sessions.bugsnag.com";
Expand Down Expand Up @@ -129,6 +130,26 @@ public void setAppVersion(@NonNull String appVersion) {
NativeInterface.MessageType.UPDATE_APP_VERSION, appVersion));
}

/**
* Gets the version code sent to Bugsnag.
*
* @return Version Code
*/
@Nullable
public Integer getVersionCode() {
return versionCode;
}

/**
* Set the version code sent to Bugsnag. By default we'll pull this
* from your AndroidManifest.xml
*
* @param versionCode the version code to send
*/
public void setVersionCode(@Nullable Integer versionCode) {
this.versionCode = versionCode;
}

/**
* Gets the context to be sent to Bugsnag.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,12 @@ public void deliver(@NonNull Report report,
public void testSetNullDelivery() {
config.setDelivery(null);
}

@Test
public void testVersionCode() {
Configuration configuration = new Configuration("api-key");
assertNull(configuration.getVersionCode()); // populated in client ctor if null
configuration.setVersionCode(577);
assertEquals(577, (int) configuration.getVersionCode());
}
}

0 comments on commit 3471dde

Please sign in to comment.