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

144 lines
5.9 KiB
Java
Raw Normal View History

2019-02-26 20:38:27 +01:00
package info.nightscout.androidaps.utils;
2018-02-22 13:30:36 +01:00
2019-04-05 00:32:26 +02:00
import android.os.Bundle;
2018-02-22 13:30:36 +01:00
import com.crashlytics.android.Crashlytics;
2019-04-05 00:32:26 +02:00
import com.google.firebase.analytics.FirebaseAnalytics;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2018-08-17 10:27:46 +02:00
2018-07-09 23:05:48 +02:00
import info.nightscout.androidaps.BuildConfig;
import info.nightscout.androidaps.MainApp;
2019-04-05 00:32:26 +02:00
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin;
2018-02-22 13:30:36 +01:00
/**
* Created by jamorham on 21/02/2018.
2018-08-17 10:27:46 +02:00
* <p>
2018-02-22 13:30:36 +01:00
* 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 {
2019-04-05 00:32:26 +02:00
private static Logger log = LoggerFactory.getLogger(L.CORE);
2018-02-22 13:30:36 +01:00
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) {
2019-04-05 00:32:26 +02:00
if (L.isEnabled(L.CORE))
log.debug("Ignoring opted out non-initialized log: " + throwable);
2018-02-22 13:30:36 +01:00
}
}
// Crashlytics log
public static void log(String msg) {
try {
final Crashlytics crashlytics = Crashlytics.getInstance();
crashlytics.core.log(msg);
} catch (NullPointerException | IllegalStateException e) {
2019-04-05 00:32:26 +02:00
if (L.isEnabled(L.CORE))
log.debug("Ignoring opted out non-initialized log: " + msg);
2018-02-22 13:30:36 +01:00
}
}
// 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) {
2019-04-05 00:32:26 +02:00
if (L.isEnabled(L.CORE))
log.debug("Ignoring opted out non-initialized log: " + msg);
2018-02-22 13:30:36 +01:00
}
}
public static boolean fabricEnabled() {
return SP.getBoolean("enable_fabric", true);
}
2019-04-05 00:32:26 +02:00
// Analytics logCustom
public void logCustom(Bundle event) {
2018-02-22 13:30:36 +01:00
try {
if (fabricEnabled()) {
2019-04-05 00:32:26 +02:00
MainApp.getFirebaseAnalytics().logEvent(FirebaseAnalytics.Event.SELECT_CONTENT, event);
2018-02-22 13:30:36 +01:00
} else {
2019-04-05 00:32:26 +02:00
if (L.isEnabled(L.CORE))
log.debug("Ignoring recently opted-out event: " + event.toString());
2018-02-22 13:30:36 +01:00
}
} catch (NullPointerException | IllegalStateException e) {
2019-04-05 00:32:26 +02:00
if (L.isEnabled(L.CORE))
log.debug("Ignoring opted-out non-initialized event: " + event.toString());
2018-02-22 13:30:36 +01:00
}
}
2019-04-05 00:32:26 +02:00
// Analytics logCustom
public void logCustom(String event) {
try {
if (fabricEnabled()) {
MainApp.getFirebaseAnalytics().logEvent(event, new Bundle());
} else {
if (L.isEnabled(L.CORE))
log.debug("Ignoring recently opted-out event: " + event);
}
} catch (NullPointerException | IllegalStateException e) {
if (L.isEnabled(L.CORE))
log.debug("Ignoring opted-out non-initialized event: " + event);
2018-07-09 23:05:48 +02:00
}
}
2018-07-11 20:19:42 +02:00
2019-04-05 00:32:26 +02:00
public static void setUserStats() {
if (!fabricEnabled()) return;
2018-07-11 20:19:42 +02:00
2019-04-05 00:32:26 +02:00
String closedLoopEnabled = MainApp.getConstraintChecker().isClosedLoopAllowed().value() ? "CLOSED_LOOP_ENABLED" : "CLOSED_LOOP_DISABLED";
2019-04-11 10:16:52 +02:00
// Size is limited to 36 chars
String remote = BuildConfig.REMOTE
.replace("https://","")
.replace("http://","")
.replace(".git", "")
.replace(".com/", ":")
.replace(".org/", ":")
.replace(".net/", ":");
2019-04-05 00:32:26 +02:00
MainApp.getFirebaseAnalytics().setUserProperty("Mode", BuildConfig.APPLICATION_ID + "-" + closedLoopEnabled);
MainApp.getFirebaseAnalytics().setUserProperty("Language", LocaleHelper.getLanguage(MainApp.instance()));
MainApp.getFirebaseAnalytics().setUserProperty("Version", BuildConfig.VERSION);
MainApp.getFirebaseAnalytics().setUserProperty("HEAD", BuildConfig.HEAD);
2019-04-11 10:16:52 +02:00
MainApp.getFirebaseAnalytics().setUserProperty("Remote", remote);
2019-04-05 00:32:26 +02:00
if (ConfigBuilderPlugin.getPlugin().getActivePump() != null)
MainApp.getFirebaseAnalytics().setUserProperty("Pump", ConfigBuilderPlugin.getPlugin().getActivePump().getClass().getSimpleName());
if (ConfigBuilderPlugin.getPlugin().getActiveAPS() != null)
MainApp.getFirebaseAnalytics().setUserProperty("Aps", ConfigBuilderPlugin.getPlugin().getActiveAPS().getClass().getSimpleName());
if (ConfigBuilderPlugin.getPlugin().getActiveBgSource() != null)
MainApp.getFirebaseAnalytics().setUserProperty("BgSource", ConfigBuilderPlugin.getPlugin().getActiveBgSource().getClass().getSimpleName());
if (ConfigBuilderPlugin.getPlugin().getActiveProfileInterface() != null)
MainApp.getFirebaseAnalytics().setUserProperty("Profile", ConfigBuilderPlugin.getPlugin().getActiveProfileInterface().getClass().getSimpleName());
if (ConfigBuilderPlugin.getPlugin().getActiveSensitivity() != null)
MainApp.getFirebaseAnalytics().setUserProperty("Sensitivity", ConfigBuilderPlugin.getPlugin().getActiveSensitivity().getClass().getSimpleName());
if (ConfigBuilderPlugin.getPlugin().getActiveInsulin() != null)
MainApp.getFirebaseAnalytics().setUserProperty("Insulin", ConfigBuilderPlugin.getPlugin().getActiveInsulin().getClass().getSimpleName());
2018-07-11 20:19:42 +02:00
}
2018-02-22 13:30:36 +01:00
}