-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
include the permissions granted at crash time into the report (which …
…the user can choose to send, this is not done automatically)
- Loading branch information
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
app/src/main/java/org/zephyrsoft/trackworktime/util/PermissionCollector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String> getGrantedPermissions(Context context) { | ||
List<String> 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; | ||
} | ||
} |