diff --git a/app/build.gradle b/app/build.gradle index 899f427..a878977 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -87,6 +87,9 @@ dependencies { implementation 'net.sf.supercsv:super-csv:2.4.0' implementation 'org.tinylog:tinylog:1.3.6' + compileOnly "com.google.auto.service:auto-service-annotations:1.1.1" + annotationProcessor "com.google.auto.service:auto-service:1.1.1" + runtimeOnly 'androidx.lifecycle:lifecycle-viewmodel:2.6.2' runtimeOnly 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.2' diff --git a/app/src/main/java/org/zephyrsoft/trackworktime/util/PermissionCollector.java b/app/src/main/java/org/zephyrsoft/trackworktime/util/PermissionCollector.java new file mode 100644 index 0000000..43c94e5 --- /dev/null +++ b/app/src/main/java/org/zephyrsoft/trackworktime/util/PermissionCollector.java @@ -0,0 +1,49 @@ +package org.zephyrsoft.trackworktime.util; + +import android.content.Context; +import android.content.pm.PackageInfo; +import android.content.pm.PackageManager; + +import androidx.annotation.NonNull; + +import com.google.auto.service.AutoService; + +import org.acra.builder.ReportBuilder; +import org.acra.collector.Collector; +import org.acra.collector.CollectorException; +import org.acra.config.CoreConfiguration; +import org.acra.data.CrashReportData; +import org.pmw.tinylog.Logger; + +import java.util.ArrayList; +import java.util.List; + +@AutoService(Collector.class) +public class PermissionCollector implements Collector { + + @Override + public void collect(@NonNull Context context, @NonNull CoreConfiguration coreConfiguration, @NonNull ReportBuilder reportBuilder, @NonNull CrashReportData crashReportData) throws CollectorException { + String granted = String.join(", ", getGrantedPermissions(context)); + crashReportData.put("GRANTED_PERMISSIONS", granted); + } + + private List getGrantedPermissions(Context context) { + List granted = new ArrayList<>(); + try { + PackageInfo pi = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_PERMISSIONS); + for (int i = 0; i < pi.requestedPermissions.length; i++) { + if ((pi.requestedPermissionsFlags[i] & PackageInfo.REQUESTED_PERMISSION_GRANTED) != 0) { + granted.add(pi.requestedPermissions[i]); + } + } + } catch (Exception e) { + Logger.warn(e, "could not determine the granted permissions"); + } + return granted; + } + + @Override + public boolean enabled(@NonNull CoreConfiguration config) { + return true; + } +}