Skip to content

Commit

Permalink
Merge pull request #209 from louisg1337/battery_optimization_plugin_r…
Browse files Browse the repository at this point in the history
…ework

New battery optimization request permissions popup.
  • Loading branch information
shankari authored Sep 29, 2023
2 parents c21f835 + c2d5824 commit 6a4d0d1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
1 change: 1 addition & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<!-- COARSE_LOCATION obfuscates the location to a city block, change to FINE_LOCATION for accuracy -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
</config-file>

<config-file target="AndroidManifest.xml" parent="/manifest/application">
Expand Down
2 changes: 2 additions & 0 deletions src/android/verification/SensorControlConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class SensorControlConstants {
public static String LOCATION_PERMISSION = Manifest.permission.ACCESS_FINE_LOCATION;
public static String BACKGROUND_LOC_PERMISSION = Manifest.permission.ACCESS_BACKGROUND_LOCATION;
public static String MOTION_ACTIVITY_PERMISSION = Manifest.permission.ACTIVITY_RECOGNITION;
public static String REQUEST_BATTERY_PERMISSION = Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS;

public static final int ENABLE_LOCATION_SETTINGS = 362253738;
public static final int ENABLE_LOCATION_SETTINGS_MANUAL = 362253736;
Expand All @@ -18,6 +19,7 @@ public class SensorControlConstants {
public static final int REMOVE_UNUSED_APP_RESTRICTIONS = 362253743;
public static final int OPEN_APP_STATUS_PAGE = 362253744;
public static final int OPEN_BATTERY_OPTIMIZATION_PAGE = 362253745;
public static final int IGNORE_BATTERY_OPTIMIZATIONS = 362253746;

public static final String ENABLE_LOCATION_PERMISSION_ACTION = "ENABLE_LOCATION_PERMISSION";
public static final String ENABLE_BACKGROUND_LOC_PERMISSION_ACTION = "ENABLE_BACKGROUND_LOC_PERMISSION";
Expand Down
57 changes: 43 additions & 14 deletions src/android/verification/SensorControlForegroundDelegate.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ void requestPermission() {
String.format("After iterating over all entries in %s shouldShowRequest = %s", currPermissions, shouldShowRequestRationaleBefore));
if (openAppSettings) {
SensorControlForegroundDelegate.this.openAppSettingsPage(cordovaCallback, permissionStatusConstant);
} else if (permissionStatusConstant == SensorControlConstants.IGNORE_BATTERY_OPTIMIZATIONS) {
SensorControlForegroundDelegate.this.openRequestBatteryOptimization(cordovaCallback, permissionStatusConstant);
} else {
if (currPermissions.length > 1) {
cordova.requestPermissions(plugin, permissionStatusConstant, currPermissions);
Expand Down Expand Up @@ -418,20 +420,47 @@ public void checkIgnoreBatteryOptimizations(CallbackContext cordovaCallback) {
}
}


public void checkAndPromptIgnoreBatteryOptimizations(CallbackContext cordovaCallback) {
boolean unrestricted = SensorControlChecks.checkIgnoreBatteryOptimizations(cordova.getActivity());
if (unrestricted) {
SensorControlBackgroundChecker.restartFSMIfStartState(cordova.getActivity());
cordovaCallback.success();
} else {
Log.i(cordova.getActivity(), TAG, "Battery optimizations enforced, asking user to ignore");
this.cordovaCallback = cordovaCallback;
cordova.setActivityResultCallback(plugin);
Intent intent = new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
cordova.getActivity().startActivityForResult(intent, SensorControlConstants.OPEN_BATTERY_OPTIMIZATION_PAGE);
/**
* @param cordovaCallback
*
* Driver function that gets called to ignore battery optimizations. Uses PermissionPopupChecker to generate the
* intent and handle the user's input.
*/
public void checkAndPromptIgnoreBatteryOptimizations(CallbackContext cordovaCallback) {
boolean unrestricted = SensorControlChecks.checkIgnoreBatteryOptimizations(cordova.getActivity());
if (unrestricted) {
SensorControlBackgroundChecker.restartFSMIfStartState(cordova.getActivity());
cordovaCallback.success();
return;
} else {
this.cordovaCallback = cordovaCallback;
this.permissionChecker = getPermissionChecker(
SensorControlConstants.IGNORE_BATTERY_OPTIMIZATIONS,
SensorControlConstants.REQUEST_BATTERY_PERMISSION,
"Insufficient battery permissions, please fix!",
"Insufficient battery permissions, please fix!"
);
this.permissionChecker.requestPermission();
return;
}
}
}

/**
* @param cordovaCallback
* @param requestCode
*
* Creates and specifies an intent to open ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATION. Used as a helper function inside
* of PermissionPopupChecker.requestPermissions()
*/
public void openRequestBatteryOptimization(CallbackContext cordovaCallback, int requestCode) {
Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
String packageName = cordova.getActivity().getPackageName();
intent.setData(Uri.parse("package:" + packageName));
this.cordovaCallback = cordovaCallback;
cordova.setActivityResultCallback(plugin);
// cordova.getActivity().startActivityForResult(intent, SensorControlConstants.OPEN_BATTERY_OPTIMIZATION_PAGE);
cordova.getActivity().startActivityForResult(intent, requestCode);
}

private void displayResolution(PendingIntent resolution) {
if (resolution != null) {
Expand Down Expand Up @@ -590,7 +619,7 @@ public void run() {
}
});
break;
case SensorControlConstants.OPEN_BATTERY_OPTIMIZATION_PAGE:
case SensorControlConstants.IGNORE_BATTERY_OPTIMIZATIONS:
Log.d(mAct, TAG, requestCode + " is our code, handling callback");
Log.d(mAct, TAG, "Got ignore battery optimization callback from launching optimization page");
AsyncTask.execute(new Runnable() {
Expand Down

0 comments on commit 6a4d0d1

Please sign in to comment.