AndroidAPS/app/src/main/java/info/nightscout/utils/FabricPrivacy.java

128 lines
4.5 KiB
Java
Raw Normal View History

2018-02-22 13:30:36 +01:00
package info.nightscout.utils;
import com.crashlytics.android.Crashlytics;
import com.crashlytics.android.answers.Answers;
import com.crashlytics.android.answers.CustomEvent;
2018-07-09 23:05:48 +02:00
import info.nightscout.androidaps.BuildConfig;
import info.nightscout.androidaps.Config;
2018-07-09 23:05:48 +02:00
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.interfaces.PluginBase;
import java.util.Date;
2018-02-22 13:30:36 +01:00
/**
* Created by jamorham on 21/02/2018.
*
* Some users do not wish to be tracked, Fabric Answers and Crashlytics do not provide an easy way
* to disable them and make calls from a potentially invalid singleton reference. This wrapper
* emulates the methods but ignores the request if the instance is null or invalid.
*
*/
public class FabricPrivacy {
private static final String TAG = "FabricPrivacy";
private static volatile FabricPrivacy instance;
public static FabricPrivacy getInstance() {
if (instance == null) {
initSelf();
}
return instance;
}
private static synchronized void initSelf() {
if (instance == null) {
instance = new FabricPrivacy();
}
}
// Crashlytics logException
public static void logException(Throwable throwable) {
try {
final Crashlytics crashlytics = Crashlytics.getInstance();
crashlytics.core.logException(throwable);
} catch (NullPointerException | IllegalStateException e) {
android.util.Log.d(TAG, "Ignoring opted out non-initialized log: " + throwable);
}
}
// Crashlytics log
public static void log(String msg) {
try {
final Crashlytics crashlytics = Crashlytics.getInstance();
crashlytics.core.log(msg);
} catch (NullPointerException | IllegalStateException e) {
android.util.Log.d(TAG, "Ignoring opted out non-initialized log: " + msg);
}
}
// Crashlytics log
public static void log(int priority, String tag, String msg) {
try {
final Crashlytics crashlytics = Crashlytics.getInstance();
crashlytics.core.log(priority, tag, msg);
} catch (NullPointerException | IllegalStateException e) {
android.util.Log.d(TAG, "Ignoring opted out non-initialized log: " + msg);
}
}
public static boolean fabricEnabled() {
return SP.getBoolean("enable_fabric", true);
}
// Answers logCustom
public void logCustom(CustomEvent event) {
try {
final Answers answers = Answers.getInstance();
if (fabricEnabled()) {
answers.logCustom(event);
} else {
android.util.Log.d(TAG, "Ignoring recently opted-out event: " + event.toString());
}
} catch (NullPointerException | IllegalStateException e) {
android.util.Log.d(TAG, "Ignoring opted-out non-initialized event: " + event.toString());
}
}
public static void logAppStart() {
if (Config.NSCLIENT)
getInstance().logCustom(new CustomEvent("AppStart-NSClient"));
else if (Config.G5UPLOADER)
getInstance().logCustom(new CustomEvent("AppStart-G5Uploader"));
else if (Config.PUMPCONTROL)
getInstance().logCustom(new CustomEvent("AppStart-PumpControl"));
else if (MainApp.getConstraintChecker().isClosedLoopAllowed().value())
getInstance().logCustom(new CustomEvent("AppStart-ClosedLoop"));
else
getInstance().logCustom(new CustomEvent("AppStart-OpenLoop"));
}
2018-07-09 23:05:48 +02:00
public static void reportPluginStats() {
if (!fabricEnabled()) return;
2018-07-09 23:05:48 +02:00
long lastUploadDay = SP.getLong(MainApp.gs(R.string.key_plugin_stats_report_timestamp), 0L);
Date date = new Date();
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
long today = date.getTime() - date.getTime() % 1000;
if (today > lastUploadDay) {
CustomEvent pluginStats = new CustomEvent("PluginStats");
pluginStats.putCustomAttribute("version", BuildConfig.VERSION);
for (PluginBase plugin : MainApp.getPluginsList()) {
if (plugin.isEnabled(plugin.getType()) && !plugin.pluginDescription.alwaysEnabled) {
pluginStats.putCustomAttribute(plugin.getName(), "enabled");
}
}
FabricPrivacy.getInstance().logCustom(pluginStats);
SP.putLong(MainApp.gs(R.string.key_plugin_stats_report_timestamp), today);
}
}
2018-02-22 13:30:36 +01:00
}