Skip to content

Commit

Permalink
Request battery optimization exemption
Browse files Browse the repository at this point in the history
Requests an exemption to battery optimization when enabling remote verification
  • Loading branch information
t895 committed Oct 31, 2024
1 parent 5d78ae1 commit 9f722db
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<uses-permission android:name="android.permission.USE_FINGERPRINT" android:maxSdkVersion="28" />
<uses-permission android:name="android.permission.USE_BIOMETRIC" />
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />

<application android:label="@string/app_name"
android:useEmbeddedDex="true"
Expand Down
45 changes: 35 additions & 10 deletions app/src/main/java/app/attestation/auditor/AttestationActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.provider.Settings;
import android.text.Html;
import android.text.Spanned;
import android.util.Log;
Expand Down Expand Up @@ -294,6 +297,10 @@ protected void onResume() {
stage = Stage.EnableRemoteVerify;
startQrScanner();
});

if (isIgnoringBatteryOptimizations() && stage == Stage.EnableRemoteVerify) {
startQrScanner();
}
}

@Override
Expand Down Expand Up @@ -434,17 +441,17 @@ private Bitmap createQrCode(final byte[] contents) {

@SuppressLint("InlinedApi")
private void startQrScanner() {
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
if (!isIgnoringBatteryOptimizations()) {
requestIgnoreBatteryOptimizations();
} else if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA},
PERMISSIONS_REQUEST_CAMERA);
} else if (checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.POST_NOTIFICATIONS},
PERMISSIONS_REQUEST_POST_NOTIFICATIONS_REMOTE_VERIFY);
} else {
if (stage == Stage.EnableRemoteVerify &&
checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.POST_NOTIFICATIONS},
PERMISSIONS_REQUEST_POST_NOTIFICATIONS_REMOTE_VERIFY);
} else {
QRScannerActivityLauncher.launch(new Intent(this, QRScannerActivity.class));
}
QRScannerActivityLauncher.launch(new Intent(this, QRScannerActivity.class));
stage = Stage.None;
}
}

Expand All @@ -459,8 +466,6 @@ public void onRequestPermissionsResult(int requestCode,
} else {
snackbar.setText(R.string.camera_permission_denied).show();
}
} else if (requestCode == PERMISSIONS_REQUEST_POST_NOTIFICATIONS_REMOTE_VERIFY) {
QRScannerActivityLauncher.launch(new Intent(this, QRScannerActivity.class));
} else if (requestCode == PERMISSIONS_REQUEST_POST_NOTIFICATIONS_SUBMIT_SAMPLE) {
SubmitSampleJob.schedule(this);
snackbar.setText(R.string.schedule_submit_sample_success).show();
Expand Down Expand Up @@ -577,4 +582,24 @@ public boolean onOptionsItemSelected(final MenuItem item) {
}
return super.onOptionsItemSelected(item);
}

private boolean isIgnoringBatteryOptimizations() {
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (powerManager != null) {
return powerManager.isIgnoringBatteryOptimizations(getPackageName());
}
return true;
}

@SuppressLint("BatteryLife")
private void requestIgnoreBatteryOptimizations() {
try {
Log.d(TAG, "Requesting battery optimization exemption");
Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivity(intent);
} catch (Exception e) {
Log.e(TAG, "Failed to request to ignore battery optimizations: " + e.getMessage());
}
}
}

0 comments on commit 9f722db

Please sign in to comment.