strip down firstInstall and lastUpdateTime events to simple counts

This commit is contained in:
Hans-Christoph Steiner 2021-03-01 16:41:28 +01:00
parent d558d396ed
commit 8e8a7c0b74

View File

@ -22,7 +22,7 @@ import android.content.Context;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.Build; import android.os.Build;
import android.system.Os; import android.text.TextUtils;
import android.text.format.DateUtils; import android.text.format.DateUtils;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.work.Constraints; import androidx.work.Constraints;
@ -79,6 +79,8 @@ public class FDroidMetricsWorker extends Worker {
static SimpleDateFormat weekFormatter = new SimpleDateFormat("yyyy ww", Locale.ENGLISH); static SimpleDateFormat weekFormatter = new SimpleDateFormat("yyyy ww", Locale.ENGLISH);
private static final ArrayList<MatomoEvent> events = new ArrayList<>();
public FDroidMetricsWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) { public FDroidMetricsWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams); super(context, workerParams);
} }
@ -257,7 +259,6 @@ public class FDroidMetricsWorker extends Worker {
} }
}); });
App[] installedApps = InstalledAppProvider.Helper.all(context); App[] installedApps = InstalledAppProvider.Helper.all(context);
final ArrayList<MatomoEvent> events = new ArrayList<>();
events.add(getDeviceEvent(weekStart, "isPrivilegedInstallerEnabled", events.add(getDeviceEvent(weekStart, "isPrivilegedInstallerEnabled",
Preferences.get().isPrivilegedInstallerEnabled())); Preferences.get().isPrivilegedInstallerEnabled()));
events.add(getDeviceEvent(weekStart, "Build.VERSION.SDK_INT", Build.VERSION.SDK_INT)); events.add(getDeviceEvent(weekStart, "Build.VERSION.SDK_INT", Build.VERSION.SDK_INT));
@ -276,21 +277,10 @@ public class FDroidMetricsWorker extends Worker {
if (!found) continue; if (!found) continue;
if (isTimestampInReportingWeek(weekStart, packageInfo.firstInstallTime)) { if (isTimestampInReportingWeek(weekStart, packageInfo.firstInstallTime)) {
events.add(getFirstInstallEvent(packageInfo)); addFirstInstallEvent(pm, packageInfo);
} }
if (isTimestampInReportingWeek(weekStart, packageInfo.lastUpdateTime)) { if (isTimestampInReportingWeek(weekStart, packageInfo.lastUpdateTime)) {
events.add(getInstallerEvent(pm, packageInfo)); addLastUpdateTimeEvent(pm, packageInfo);
}
if (Build.VERSION.SDK_INT >= 21) {
try {
long atime = Os.lstat(packageInfo.applicationInfo.sourceDir).st_atime;
if (isTimestampInReportingWeek(atime)) {
events.add(getApkOpenedEvent(atime, packageInfo));
}
} catch (Exception e) {
// TODO replace with ErrnoException when using minSdkVersion 19 or higher
e.printStackTrace();
}
} }
} }
events.addAll(parseInstallHistoryCsv(context, weekStart)); events.addAll(parseInstallHistoryCsv(context, weekStart));
@ -325,31 +315,28 @@ public class FDroidMetricsWorker extends Worker {
final String ua = Utils.getUserAgent(); final String ua = Utils.getUserAgent();
} }
private static MatomoEvent getApkOpenedEvent(long timestamp, PackageInfo packageInfo) { private static void addFirstInstallEvent(PackageManager pm, PackageInfo packageInfo) {
return getApkEvent(timestamp, packageInfo, "opened"); addInstallerEvent(pm, packageInfo, "PackageInfo.firstInstall", packageInfo.firstInstallTime);
} }
private static MatomoEvent getFirstInstallEvent(PackageInfo packageInfo) { private static void addLastUpdateTimeEvent(PackageManager pm, PackageInfo packageInfo) {
return getApkEvent(packageInfo.firstInstallTime, packageInfo, "PackageInfo.firstInstall"); addInstallerEvent(pm, packageInfo, "PackageInfo.lastUpdateTime", packageInfo.lastUpdateTime);
} }
private static MatomoEvent getApkEvent(long timestamp, PackageInfo packageInfo, String action) { private static void addInstallerEvent(
PackageManager pm, PackageInfo packageInfo, String action, long timestamp) {
MatomoEvent matomoEvent = new MatomoEvent(timestamp); MatomoEvent matomoEvent = new MatomoEvent(timestamp);
matomoEvent.category = "APK"; matomoEvent.category = "APK";
matomoEvent.action = action; matomoEvent.action = action;
matomoEvent.name = packageInfo.packageName; matomoEvent.name = pm.getInstallerPackageName(packageInfo.packageName);
return matomoEvent; matomoEvent.times = 1;
} for (MatomoEvent me : events) {
if (me.equals(matomoEvent)) {
/** me.times++;
* Which app store installed APKs. return;
*/ }
private static MatomoEvent getInstallerEvent(PackageManager pm, PackageInfo packageInfo) { }
MatomoEvent matomoEvent = new MatomoEvent(packageInfo.lastUpdateTime); events.add(matomoEvent);
matomoEvent.category = "getInstallerPackageName";
matomoEvent.action = pm.getInstallerPackageName(packageInfo.packageName);
matomoEvent.name = packageInfo.packageName;
return matomoEvent;
} }
/** /**
@ -360,6 +347,7 @@ public class FDroidMetricsWorker extends Worker {
matomoEvent.category = "device"; matomoEvent.category = "device";
matomoEvent.action = action; matomoEvent.action = action;
matomoEvent.name = String.valueOf(name); matomoEvent.name = String.valueOf(name);
matomoEvent.times = 1;
return matomoEvent; return matomoEvent;
} }
@ -384,7 +372,7 @@ public class FDroidMetricsWorker extends Worker {
@JsonProperty @JsonProperty
final long period_end; final long period_end;
@JsonProperty @JsonProperty
final long times = 1; // NOPMD long times = 0;
@JsonProperty @JsonProperty
String value; String value;
@ -398,6 +386,19 @@ public class FDroidMetricsWorker extends Worker {
category = "package"; category = "package";
action = rawEvent.action; action = rawEvent.action;
name = rawEvent.applicationId; name = rawEvent.applicationId;
times = 1;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MatomoEvent that = (MatomoEvent) o;
return period_start == that.period_start &&
period_end == that.period_end &&
TextUtils.equals(category, that.category) &&
TextUtils.equals(action, that.action) &&
TextUtils.equals(name, that.name);
} }
} }