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

added executor for caching values out of the main thread #201

Merged
merged 5 commits into from
Dec 17, 2019
Merged
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,15 @@
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.TimeZone;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.jetbrains.annotations.Nullable;

final class DefaultAndroidEventProcessor implements EventProcessor {
Expand All @@ -59,12 +64,43 @@ final class DefaultAndroidEventProcessor implements EventProcessor {
final Context context;
private final SentryOptions options;

private final Future<Map<String, Object>> contextData;

public DefaultAndroidEventProcessor(Context context, SentryOptions options) {
this.context =
Objects.requireNonNull(
context != null ? context.getApplicationContext() : null,
"The application context is required.");
this.options = Objects.requireNonNull(options, "The SentryOptions is required.");

ExecutorService executorService = Executors.newSingleThreadExecutor();
contextData = executorService.submit(() -> loadContextData());
marandaneto marked this conversation as resolved.
Show resolved Hide resolved

executorService.shutdown();
}

private Map<String, Object> loadContextData() {
Map<String, Object> map = new HashMap<>();
String[] proGuardUuids = getProGuardUuids();
if (proGuardUuids != null) {
map.put("proGuardUuids", proGuardUuids);
}

map.put("rooted", isRooted());

String androidId = getAndroidId();
if (androidId != null) {
map.put("androidId", androidId);
marandaneto marked this conversation as resolved.
Show resolved Hide resolved
}

String kernelVersion = getKernelVersion();
if (kernelVersion != null) {
map.put("kernelVersion", kernelVersion);
}

map.put("emulator", isEmulator());

return map;
}

@Override
Expand Down Expand Up @@ -118,7 +154,16 @@ private void processNonCachedEvent(SentryEvent event) {
}

private List<DebugImage> getDebugImages() {
String[] uuids = getProGuardUuids();
String[] uuids = null;
try {
Object proGuardUuids = contextData.get().get("proGuardUuids");
if (proGuardUuids != null) {
uuids = (String[]) proGuardUuids;
}
} catch (Exception e) {
log(SentryLevel.ERROR, "Error getting proGuardUuids.", e);
return null;
}

if (uuids == null || uuids.length == 0) {
return null;
Expand Down Expand Up @@ -252,7 +297,15 @@ private Device getDevice() {
}
device.setOnline(isConnected());
device.setOrientation(getOrientation());
device.setSimulator(isEmulator());

try {
Object emulator = contextData.get().get("emulator");
if (emulator != null) {
device.setSimulator((Boolean) emulator);
}
} catch (Exception e) {
log(SentryLevel.ERROR, "Error getting emulator.", e);
}

ActivityManager.MemoryInfo memInfo = getMemInfo();
if (memInfo != null) {
Expand Down Expand Up @@ -583,11 +636,11 @@ private StatFs getExternalStorageStat(File internalStorage) {
}

@SuppressWarnings("ObsoleteSdkInt")
public File[] getExternalFilesDirs(String type) {
private File[] getExternalFilesDirs() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
return context.getExternalFilesDirs(type);
return context.getExternalFilesDirs(null);
} else {
File single = context.getExternalFilesDir(type);
File single = context.getExternalFilesDir(null);
if (single != null) {
return new File[] {single};
}
Expand All @@ -596,7 +649,7 @@ public File[] getExternalFilesDirs(String type) {
}

private File getExternalStorageDep(File internalStorage) {
File[] externalFilesDirs = getExternalFilesDirs(null);
File[] externalFilesDirs = getExternalFilesDirs();

if (externalFilesDirs != null) {
// return the 1st file which is not the emulated internal storage
Expand Down Expand Up @@ -677,8 +730,20 @@ private OperatingSystem getOperatingSystem() {
os.setName("Android");
os.setVersion(Build.VERSION.RELEASE);
os.setBuild(Build.DISPLAY);
os.setKernelVersion(getKernelVersion());
os.setRooted(isRooted());

try {
Object kernelVersion = contextData.get().get("kernelVersion");
if (kernelVersion != null) {
os.setKernelVersion((String) kernelVersion);
}

Object rooted = contextData.get().get("rooted");
if (rooted != null) {
os.setRooted((Boolean) rooted);
}
} catch (Exception e) {
log(SentryLevel.ERROR, "Error getting OperatingSystem.", e);
}

return os;
}
Expand Down Expand Up @@ -794,7 +859,15 @@ private String getApplicationName() {
public User getUser() {
User user = new User();

user.setId(getAndroidId());
try {
Object androidId = contextData.get().get("androidId");

if (androidId != null) {
user.setId((String) androidId);
bruno-garcia marked this conversation as resolved.
Show resolved Hide resolved
}
} catch (Exception e) {
log(SentryLevel.ERROR, "Error getting androidId.", e);
}

return user;
}
Expand Down