Skip to content

Commit

Permalink
Merge pull request #231 from louisg1337/work_profile_fix
Browse files Browse the repository at this point in the history
Work profile fix
  • Loading branch information
shankari authored May 16, 2024
2 parents 1ffad35 + 7a3ee6e commit 24d9aa5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
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);
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

0 comments on commit 24d9aa5

Please sign in to comment.