This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
Add Metrics #27
Merged
Merged
Add Metrics #27
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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 @@ | ||
/build |
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,37 @@ | ||
apply plugin: 'com.android.library' | ||
|
||
android { | ||
compileSdkVersion project.ext.targetSdkVersion | ||
|
||
defaultConfig { | ||
minSdkVersion project.ext.minSdkVersion | ||
targetSdkVersion project.ext.targetSdkVersion | ||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" | ||
} | ||
|
||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
} | ||
} | ||
|
||
testOptions { | ||
unitTests.returnDefaultValues = true | ||
unitTests.includeAndroidResources = true | ||
} | ||
|
||
compileOptions { | ||
sourceCompatibility JavaVersion.VERSION_1_8 | ||
targetCompatibility JavaVersion.VERSION_1_8 | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation project(path: ':core') | ||
|
||
testImplementation 'junit:junit' | ||
testImplementation 'org.mockito:mockito-core' | ||
testImplementation 'org.json:json' | ||
} | ||
|
||
apply from: '../gradle-mvn-push.gradle' |
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,3 @@ | ||
POM_NAME=AeroGear Android SDK | ||
POM_ARTIFACT_ID=metrics | ||
POM_PACKAGING=aar |
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,2 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="org.aerogear.mobile.metrics" /> |
120 changes: 120 additions & 0 deletions
120
metrics/src/main/java/org/aerogear/mobile/metrics/MetricsService.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,120 @@ | ||
package org.aerogear.mobile.metrics; | ||
|
||
import android.content.Context; | ||
import android.content.SharedPreferences; | ||
|
||
import org.aerogear.mobile.core.MobileCore; | ||
import org.aerogear.mobile.core.ServiceModule; | ||
import org.aerogear.mobile.core.configuration.ServiceConfiguration; | ||
import org.aerogear.mobile.core.http.HttpRequest; | ||
import org.aerogear.mobile.core.http.HttpResponse; | ||
import org.aerogear.mobile.core.http.HttpServiceModule; | ||
import org.aerogear.mobile.core.logging.Logger; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
import java.util.UUID; | ||
|
||
public class MetricsService implements ServiceModule { | ||
public final static String STORAGE_NAME = "org.aerogear.mobile.metrics"; | ||
public final static String STORAGE_KEY = "metrics-sdk-installation-id"; | ||
|
||
private final static String MODULE_NAME = "metrics"; | ||
private final static String TAG = "AEROGEAR/METRICS"; | ||
|
||
private HttpServiceModule httpService; | ||
private Logger logger; | ||
|
||
private String appVersion = null; | ||
private String metricsUrl = null; | ||
|
||
/** | ||
* Get or create the client ID that identifies a device as long as the user doesn't | ||
* reinstall the app or delete the app storage. A random UUID is created and stored in the | ||
* application shared preferences. | ||
* | ||
* Can be overridden to provide a different implementation for identification. | ||
* | ||
* @param context Android app context | ||
* @return String Client ID | ||
*/ | ||
protected String getOrCreateClientId(final Context context) { | ||
final SharedPreferences preferences = context | ||
.getSharedPreferences(STORAGE_NAME, Context.MODE_PRIVATE); | ||
|
||
String clientId = preferences.getString(STORAGE_KEY, null); | ||
if (clientId == null) { | ||
clientId = UUID.randomUUID().toString(); | ||
|
||
logger.info(TAG, "Generated a new client ID: " + clientId); | ||
|
||
SharedPreferences.Editor editor = preferences.edit(); | ||
editor.putString(STORAGE_KEY, clientId); | ||
editor.commit(); | ||
} | ||
|
||
return clientId; | ||
} | ||
|
||
/** | ||
* This method is called to create the JSON object containing the metrics data. | ||
* Can be overridden to add more data points. | ||
* | ||
* @param context Android app context | ||
* @return JSONObject Metrics data | ||
* @throws JSONException when any of the data results in invalid JSON | ||
*/ | ||
protected JSONObject metricsData(final Context context) throws JSONException { | ||
final JSONObject result = new JSONObject(); | ||
result.put("clientId", getOrCreateClientId(context)); | ||
result.put("appId", context.getPackageName()); | ||
result.put("appVersion", appVersion); | ||
result.put("sdkVersion", MobileCore.getSdkVersion()); | ||
return result; | ||
} | ||
|
||
/** | ||
* Send the metrics data to the server. The data is contained in a JSON object with the | ||
* following properties: clientId, appId, sdkVersion and appVersion | ||
* | ||
* Should not be overridden. Users can change the target URL in mobile-services.json | ||
* | ||
* @param context Android application context | ||
*/ | ||
public final void init(final Context context) { | ||
try { | ||
final JSONObject data = metricsData(context); | ||
|
||
// Send request to backend | ||
HttpRequest request = httpService.newRequest(); | ||
request.post(metricsUrl, data.toString().getBytes()); | ||
HttpResponse response = request.execute(); | ||
|
||
// Async response handling | ||
response.onComplete(() -> { | ||
if (response.getStatus() != 200) { | ||
logger.error(TAG, "Error sending metrics data"); | ||
} | ||
}); | ||
} catch (JSONException e) { | ||
logger.error(TAG, e); | ||
} | ||
} | ||
|
||
@Override | ||
public void configure(final MobileCore core, final ServiceConfiguration serviceConfiguration) { | ||
metricsUrl = serviceConfiguration.getUri(); | ||
appVersion = core.getAppVersion(); | ||
httpService = core.getHttpLayer(); | ||
logger = MobileCore.getLogger(); | ||
} | ||
|
||
@Override | ||
public String type() { | ||
return MODULE_NAME; | ||
} | ||
|
||
@Override | ||
public void destroy() { | ||
} | ||
} |
Oops, something went wrong.
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.
For some reason those exclusions don't work with
.java
and gradle gives out warnings. That's why i've changed it, it seems to work as expected this way.