Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

Commit

Permalink
more device context (deviceId, connectionType and language) (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto authored Jan 16, 2020
1 parent 0fabfbe commit 581fb79
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,17 @@ private Device getDevice() {
device.setBootTime(getBootTime());
device.setTimezone(getTimeZone());

if (device.getId() == null) {
device.setId(getDeviceId());
}
if (device.getLanguage() == null) {
device.setLanguage(Locale.getDefault().toString()); // eg en_US
}
if (device.getConnectionType() == null) {
// wifi, ethernet or cellular, null if none
device.setConnectionType(ConnectivityChecker.getConnectionType(context, options.getLogger()));
}

return device;
}

Expand Down Expand Up @@ -838,18 +849,22 @@ private String getApplicationName() {

public User getUser() {
User user = new User();
user.setId(getDeviceId());

return user;
}

private String getDeviceId() {
try {
Object androidId = contextData.get().get(ANDROID_ID);

if (androidId != null) {
user.setId((String) androidId);
return (String) androidId;
}
} catch (Exception e) {
log(SentryLevel.ERROR, "Error getting androidId.", e);
log(SentryLevel.ERROR, "Error getting deviceId.", e);
}

return user;
return null;
}

@SuppressWarnings("HardwareIds")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package io.sentry.android.core.util;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.os.Build;
import io.sentry.core.ILogger;
import io.sentry.core.SentryLevel;
import org.jetbrains.annotations.ApiStatus;
Expand All @@ -20,13 +24,11 @@ private ConnectivityChecker() {}
* @return true if the application has internet access
*/
public static @Nullable Boolean isConnected(@NotNull Context context, @NotNull ILogger logger) {
ConnectivityManager connectivityManager =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
ConnectivityManager connectivityManager = getConnectivityManager(context, logger);
if (connectivityManager == null) {
logger.log(SentryLevel.INFO, "ConnectivityManager is null and cannot check network status");
return null;
}
return getActiveNetworkInfo(context, connectivityManager, logger);
return isConnected(context, connectivityManager, logger);
// getActiveNetworkInfo might return null if VPN doesn't specify its
// underlying network

Expand All @@ -35,20 +37,81 @@ private ConnectivityChecker() {}
}

@SuppressWarnings({"deprecation", "MissingPermission"})
private static Boolean getActiveNetworkInfo(
private static Boolean isConnected(
Context context, ConnectivityManager connectivityManager, ILogger logger) {
android.net.NetworkInfo activeNetwork =
getActiveNetworkInfo(context, connectivityManager, logger);

if (activeNetwork == null) {
logger.log(SentryLevel.INFO, "NetworkInfo is null and cannot check network status");
return null;
}
return activeNetwork.isConnected();
}

@SuppressWarnings({"deprecation", "MissingPermission"})
private static @Nullable android.net.NetworkInfo getActiveNetworkInfo(
@NotNull Context context,
@NotNull ConnectivityManager connectivityManager,
@NotNull ILogger logger) {
if (!Permissions.hasPermission(context, Manifest.permission.ACCESS_NETWORK_STATE)) {
logger.log(SentryLevel.INFO, "No permission (ACCESS_NETWORK_STATE) to check network status.");
return null;
}

// do not import class or deprecation lint will throw
android.net.NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
return connectivityManager.getActiveNetworkInfo();
}

if (activeNetwork != null) {
return activeNetwork.isConnected();
/**
* Check the connection type of the active network
*
* @param context the App. context
* @param logger the logger from options
* @return the connection type wifi, ethernet, cellular or null
*/
@SuppressLint({"ObsoleteSdkInt", "MissingPermission"})
public static String getConnectionType(@NotNull Context context, @NotNull ILogger logger) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
ConnectivityManager connectivityManager = getConnectivityManager(context, logger);
if (connectivityManager == null) {
return null;
}
if (!Permissions.hasPermission(context, Manifest.permission.ACCESS_NETWORK_STATE)) {
logger.log(
SentryLevel.INFO, "No permission (ACCESS_NETWORK_STATE) to check network status.");
return null;
}
Network activeNetwork = connectivityManager.getActiveNetwork();
if (activeNetwork == null) {
logger.log(SentryLevel.INFO, "Network is null and cannot check network status");
return null;
}
NetworkCapabilities networkCapabilities =
connectivityManager.getNetworkCapabilities(activeNetwork);
if (networkCapabilities == null) {
logger.log(SentryLevel.INFO, "NetworkCapabilities is null and cannot check network type");
return null;
}
if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
return "wifi";
}
if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) {
return "ethernet";
}
if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) {
return "cellular";
}
}
logger.log(SentryLevel.INFO, "NetworkInfo is null and cannot check network status");
return null;
}

private static @Nullable ConnectivityManager getConnectivityManager(
@NotNull Context context, @NotNull ILogger logger) {
ConnectivityManager connectivityManager =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager == null) {
logger.log(SentryLevel.INFO, "ConnectivityManager is null and cannot check network status");
}
return connectivityManager;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ class ConnectivityCheckerTest {
fun `isConnected won't throw exception`() {
ConnectivityChecker.isConnected(context, mock())
}

@Test
fun `getConnectionType won't throw exception`() {
ConnectivityChecker.getConnectionType(context, mock())
}
}
27 changes: 27 additions & 0 deletions sentry-core/src/main/java/io/sentry/core/protocol/Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public final class Device implements IUnknownPropertiesConsumer {
private Integer screenDpi;
private Date bootTime;
private TimeZone timezone;
private String id;
private String language;
private String connectionType;

@SuppressWarnings("unused")
private Map<String, Object> unknown;
Expand Down Expand Up @@ -290,6 +293,30 @@ public void setScreenHeightPixels(Integer screenHeightPixels) {
this.screenHeightPixels = screenHeightPixels;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getLanguage() {
return language;
}

public void setLanguage(String language) {
this.language = language;
}

public String getConnectionType() {
return connectionType;
}

public void setConnectionType(String connectionType) {
this.connectionType = connectionType;
}

public enum DeviceOrientation {
PORTRAIT,
LANDSCAPE
Expand Down

0 comments on commit 581fb79

Please sign in to comment.