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

Request battery optimization exemption #283

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 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());
}
}
}