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

New battery optimization request permissions popup. #209

Merged
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
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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Neat! I like the reuse of the existing permissions mechanism. In a future cleanup, we may want to also change openAppSettings from a separate boolean to using a separate constant and clean up that interface...

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