Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work profile fix #231

Merged
merged 3 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
id="cordova-plugin-em-datacollection"
version="1.8.6">
version="1.8.7">

<name>DataCollection</name>
<description>Background data collection FTW! This is the part that I really
Expand Down
34 changes: 32 additions & 2 deletions src/android/verification/SensorControlChecks.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import android.os.Build;
import android.os.PowerManager;
import android.content.pm.PackageManager;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;

import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationManagerCompat;
Expand Down Expand Up @@ -113,15 +115,43 @@ public static boolean checkNotificationsUnpaused(final Context ctxt) {
}
return appUnpaused;
}

/**
* Check if the app is installed on a work profile.
*
* We need this check as Android automatically exempts apps installed on a workprofile from hibernation.
* This means that the "Pause app activity if unused" option is greyed out, which blocks users from continuing
* past the permissions screen.
*/
public static boolean checkWorkProfile(final Context ctxt) {
DevicePolicyManager devicePolicyManager = (DevicePolicyManager) ctxt.getSystemService(ctxt.DEVICE_POLICY_SERVICE);
Comment on lines +126 to +127
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am fine with merging this as-is so that we don't over-engineer this, but in the future, we may want to/need to return the admin information. But it is fine for now, since it follows the format of the other checks as well.

List<ComponentName> activeAdmins = devicePolicyManager.getActiveAdmins();
boolean workProfile = false;
if (activeAdmins != null){
for (ComponentName admin : activeAdmins){
String packageName = admin.getPackageName();
boolean profileOwner = devicePolicyManager.isProfileOwnerApp(packageName);
Log.d(ctxt, TAG, "admin: " + packageName + " profile: " + profileOwner);
if (profileOwner){
workProfile = true;
}
}
}
return workProfile;
}

public static boolean checkUnusedAppsUnrestricted(final Context ctxt) {
ListenableFuture<Integer> future = PackageManagerCompat.getUnusedAppRestrictionsStatus(ctxt);
// Check to see if we are on a work profile first
if (checkWorkProfile(ctxt)){
return true;
}
ListenableFuture<Integer> future = PackageManagerCompat.getUnusedAppRestrictionsStatus(ctxt);
try {
Log.i(ctxt, TAG, "About to call future.get to read the restriction status");
Integer appRestrictionStatus = future.get();
Log.i(ctxt, TAG, "Received "+appRestrictionStatus+" from future.get");
switch(appRestrictionStatus) {
case UnusedAppRestrictionsConstants.ERROR: return true;
case UnusedAppRestrictionsConstants.ERROR: return false;
case UnusedAppRestrictionsConstants.FEATURE_NOT_AVAILABLE: return true;
case UnusedAppRestrictionsConstants.DISABLED: return true;
case UnusedAppRestrictionsConstants.API_30_BACKPORT:
Expand Down