-
Notifications
You must be signed in to change notification settings - Fork 205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: Moves configuration creation for manifest to separate class #356
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
sdk/src/main/java/com/bugsnag/android/ConfigFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package com.bugsnag.android; | ||
|
||
import android.content.Context; | ||
import android.content.pm.ApplicationInfo; | ||
import android.content.pm.PackageManager; | ||
import android.os.Bundle; | ||
import android.support.annotation.NonNull; | ||
import android.text.TextUtils; | ||
|
||
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_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"; | ||
private static final String MF_SEND_THREADS = BUGSNAG_NAMESPACE + ".SEND_THREADS"; | ||
private static final String MF_ENABLE_EXCEPTION_HANDLER = | ||
BUGSNAG_NAMESPACE + ".ENABLE_EXCEPTION_HANDLER"; | ||
private static final String MF_PERSIST_USER_BETWEEN_SESSIONS = | ||
BUGSNAG_NAMESPACE + ".PERSIST_USER_BETWEEN_SESSIONS"; | ||
private static final String MF_AUTO_CAPTURE_SESSIONS = | ||
BUGSNAG_NAMESPACE + ".AUTO_CAPTURE_SESSIONS"; | ||
private static final String MF_API_KEY = BUGSNAG_NAMESPACE + ".API_KEY"; | ||
static final String MF_BUILD_UUID = BUGSNAG_NAMESPACE + ".BUILD_UUID"; | ||
|
||
/** | ||
* Creates a new configuration object based on the provided parameters | ||
* will read the API key and other configuration values from the manifest if it is not provided | ||
* | ||
* @param androidContext The context of the application | ||
* @param apiKey The API key to use | ||
* @param enableExceptionHandler should we automatically handle uncaught exceptions? | ||
* @return The created config | ||
*/ | ||
@NonNull | ||
static Configuration createNewConfiguration(@NonNull Context androidContext, | ||
String apiKey, | ||
boolean enableExceptionHandler) { | ||
Context appContext = androidContext.getApplicationContext(); | ||
|
||
// Attempt to load API key and other config from AndroidManifest.xml, if not passed in | ||
boolean loadFromManifest = TextUtils.isEmpty(apiKey); | ||
|
||
if (loadFromManifest) { | ||
try { | ||
PackageManager packageManager = appContext.getPackageManager(); | ||
String packageName = appContext.getPackageName(); | ||
ApplicationInfo ai = | ||
packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA); | ||
Bundle data = ai.metaData; | ||
apiKey = data.getString(MF_API_KEY); | ||
} catch (Exception ignore) { | ||
Logger.warn("Bugsnag is unable to read api key from manifest."); | ||
} | ||
} | ||
|
||
if (apiKey == null) { | ||
throw new NullPointerException("You must provide a Bugsnag API key"); | ||
} | ||
|
||
// Build a configuration object | ||
Configuration newConfig = new Configuration(apiKey); | ||
newConfig.setEnableExceptionHandler(enableExceptionHandler); | ||
|
||
if (loadFromManifest) { | ||
try { | ||
PackageManager packageManager = appContext.getPackageManager(); | ||
String packageName = appContext.getPackageName(); | ||
ApplicationInfo ai = | ||
packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA); | ||
Bundle data = ai.metaData; | ||
populateConfigFromManifest(newConfig, data); | ||
} catch (Exception ignore) { | ||
Logger.warn("Bugsnag is unable to read config from manifest."); | ||
} | ||
} | ||
return newConfig; | ||
} | ||
|
||
/** | ||
* Populates the config with meta-data values supplied from the manifest as a Bundle. | ||
* | ||
* @param config the config to mutate | ||
* @param data the manifest bundle | ||
*/ | ||
static void populateConfigFromManifest(@NonNull Configuration config, | ||
@NonNull Bundle data) { | ||
config.setBuildUUID(data.getString(MF_BUILD_UUID)); | ||
config.setAppVersion(data.getString(MF_APP_VERSION)); | ||
config.setReleaseStage(data.getString(MF_RELEASE_STAGE)); | ||
|
||
if (data.containsKey(MF_ENDPOINT)) { | ||
String endpoint = data.getString(MF_ENDPOINT); | ||
String sessionEndpoint = data.getString(MF_SESSIONS_ENDPOINT); | ||
//noinspection ConstantConditions (pass in null/empty as this function will warn) | ||
config.setEndpoints(endpoint, sessionEndpoint); | ||
} | ||
|
||
config.setSendThreads(data.getBoolean(MF_SEND_THREADS, true)); | ||
config.setPersistUserBetweenSessions( | ||
data.getBoolean(MF_PERSIST_USER_BETWEEN_SESSIONS, false)); | ||
|
||
if (data.containsKey(MF_AUTO_CAPTURE_SESSIONS)) { | ||
config.setAutoCaptureSessions(data.getBoolean(MF_AUTO_CAPTURE_SESSIONS)); | ||
} | ||
|
||
config.setEnableExceptionHandler( | ||
data.getBoolean(MF_ENABLE_EXCEPTION_HANDLER, true)); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's the default access modifier that's applied here? Does moving this have any external implications?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The package-level modifier will be applied, so anything with the
com.bugsnag.android
namespace can access this. I don't believe this has any external implications as I'm not aware of any other notifiers which call this method.