From b3ee43a7f467c55efa920b07d8e1838b7248047a Mon Sep 17 00:00:00 2001 From: Zaraki Kenpachi <7empest_pot@proton.me> Date: Sat, 8 Apr 2023 11:00:28 +0530 Subject: [PATCH 1/5] feat: add an option to immediately remote verify --- .../auditor/AttestationActivity.java | 15 +++++++++++- .../attestation/auditor/RemoteVerifyJob.java | 23 ++++++++++++------- app/src/main/res/menu/menu_attestation.xml | 3 +++ app/src/main/res/values/strings.xml | 2 ++ 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/app/attestation/auditor/AttestationActivity.java b/app/src/main/java/app/attestation/auditor/AttestationActivity.java index c84cec43..9f9d9f20 100644 --- a/app/src/main/java/app/attestation/auditor/AttestationActivity.java +++ b/app/src/main/java/app/attestation/auditor/AttestationActivity.java @@ -502,6 +502,7 @@ public boolean onPrepareOptionsMenu(final Menu menu) { menu.findItem(R.id.action_enable_remote_verify) .setEnabled(isSupportedAuditee && !isRemoteVerifyEnabled); menu.findItem(R.id.action_disable_remote_verify).setEnabled(isRemoteVerifyEnabled); + menu.findItem(R.id.action_remote_verify_now).setEnabled(isRemoteVerifyEnabled); menu.findItem(R.id.action_submit_sample).setEnabled(canSubmitSample && !SubmitSampleJob.isScheduled(this)); return true; @@ -544,7 +545,19 @@ public boolean onOptionsItemSelected(final MenuItem item) { stage = Stage.EnableRemoteVerify; startQrScanner(); return true; - } else if (itemId == R.id.action_disable_remote_verify) { + } + else if (itemId == R.id.action_remote_verify_now) { + if (checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) { + requestPermissions(new String[]{Manifest.permission.POST_NOTIFICATIONS}, + PERMISSIONS_REQUEST_POST_NOTIFICATIONS_SUBMIT_SAMPLE); + } else { + RemoteVerifyJob.schedule(this, -1); + snackbar.setText(R.string.remote_verify_now).show(); + } + return true; + } + + else if (itemId == R.id.action_disable_remote_verify) { new AlertDialog.Builder(this) .setMessage(getString(R.string.action_disable_remote_verify) + "?") .setPositiveButton(R.string.disable, (dialogInterface, i) -> { diff --git a/app/src/main/java/app/attestation/auditor/RemoteVerifyJob.java b/app/src/main/java/app/attestation/auditor/RemoteVerifyJob.java index fc4a66e9..1944c00d 100644 --- a/app/src/main/java/app/attestation/auditor/RemoteVerifyJob.java +++ b/app/src/main/java/app/attestation/auditor/RemoteVerifyJob.java @@ -76,12 +76,15 @@ static void restore(final Context context) { } static void schedule(final Context context, int interval) { - if (interval < MIN_INTERVAL) { - interval = MIN_INTERVAL; - Log.e(TAG, "invalid interval " + interval + " clamped to MIN_INTERVAL " + MIN_INTERVAL); - } else if (interval > MAX_INTERVAL) { - interval = MAX_INTERVAL; - Log.e(TAG, "invalid interval " + interval + " clamped to MAX_INTERVAL " + MAX_INTERVAL); + boolean scheduleNow = interval == -1; // if interval = -1, the job needs to be scheduled now and only once. + if (!scheduleNow) { + if (interval < MIN_INTERVAL) { + interval = MIN_INTERVAL; + Log.e(TAG, "invalid interval " + interval + " clamped to MIN_INTERVAL " + MIN_INTERVAL); + } else if (interval > MAX_INTERVAL) { + interval = MAX_INTERVAL; + Log.e(TAG, "invalid interval " + interval + " clamped to MAX_INTERVAL " + MAX_INTERVAL); + } } final JobScheduler scheduler = context.getSystemService(JobScheduler.class); final JobInfo jobInfo = scheduler.getPendingJob(PERIODIC_JOB_ID); @@ -91,7 +94,8 @@ static void schedule(final Context context, int interval) { jobInfo.getEstimatedNetworkDownloadBytes() == ESTIMATED_DOWNLOAD_BYTES && jobInfo.getEstimatedNetworkUploadBytes() == ESTIMATED_UPLOAD_BYTES && jobInfo.getIntervalMillis() == intervalMillis && - jobInfo.getFlexMillis() == flexMillis) { + jobInfo.getFlexMillis() == flexMillis && + !scheduleNow) { Log.d(TAG, "job already registered"); return; } @@ -112,10 +116,13 @@ static void schedule(final Context context, int interval) { } } final JobInfo.Builder builder = new JobInfo.Builder(PERIODIC_JOB_ID, serviceName) - .setPeriodic(intervalMillis, flexMillis) .setPersisted(true) .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY) .setEstimatedNetworkBytes(ESTIMATED_DOWNLOAD_BYTES, ESTIMATED_UPLOAD_BYTES); + + if(!scheduleNow) { + builder.setPeriodic(intervalMillis, flexMillis); + } if (scheduler.schedule(builder.build()) == JobScheduler.RESULT_FAILURE) { throw new RuntimeException("job schedule failed"); } diff --git a/app/src/main/res/menu/menu_attestation.xml b/app/src/main/res/menu/menu_attestation.xml index 26c7f7bf..58ee0051 100644 --- a/app/src/main/res/menu/menu_attestation.xml +++ b/app/src/main/res/menu/menu_attestation.xml @@ -14,6 +14,9 @@ + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 30b3a932..d1d90642 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -20,11 +20,13 @@ Clear Auditor pairings Enable remote verification Disable remote verification + Remote verify Now Submit sample data Help Scheduled regular remote verification Disabled remote verification + Scheduled immeidate remote verification Scheduled submitting sample data Cleared Auditee pairings Failed to fully clear Auditee pairings From 3f70cb4c16b8295e4e10115c57b98e53038c3781 Mon Sep 17 00:00:00 2001 From: Zaraki Kenpachi <7empest_pot@proton.me> Date: Sat, 8 Apr 2023 19:41:52 +0530 Subject: [PATCH 2/5] fix: typos --- app/src/main/res/values/strings.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d1d90642..1ba55247 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -20,13 +20,13 @@ Clear Auditor pairings Enable remote verification Disable remote verification - Remote verify Now + Remote verify now Submit sample data Help Scheduled regular remote verification Disabled remote verification - Scheduled immeidate remote verification + Scheduled immediate remote verification Scheduled submitting sample data Cleared Auditee pairings Failed to fully clear Auditee pairings From 860aff41357100b990dc125a2aece5b8d91740e1 Mon Sep 17 00:00:00 2001 From: Zaraki Kenpachi <7empest_pot@proton.me> Date: Sun, 23 Apr 2023 20:48:12 +0530 Subject: [PATCH 3/5] rebase --- .../attestation/auditor/AttestationActivity.java | 2 +- .../app/attestation/auditor/RemoteVerifyJob.java | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/app/attestation/auditor/AttestationActivity.java b/app/src/main/java/app/attestation/auditor/AttestationActivity.java index 9f9d9f20..8f31955e 100644 --- a/app/src/main/java/app/attestation/auditor/AttestationActivity.java +++ b/app/src/main/java/app/attestation/auditor/AttestationActivity.java @@ -549,7 +549,7 @@ public boolean onOptionsItemSelected(final MenuItem item) { else if (itemId == R.id.action_remote_verify_now) { if (checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[]{Manifest.permission.POST_NOTIFICATIONS}, - PERMISSIONS_REQUEST_POST_NOTIFICATIONS_SUBMIT_SAMPLE); + PERMISSIONS_REQUEST_POST_NOTIFICATIONS_REMOTE_VERIFY); } else { RemoteVerifyJob.schedule(this, -1); snackbar.setText(R.string.remote_verify_now).show(); diff --git a/app/src/main/java/app/attestation/auditor/RemoteVerifyJob.java b/app/src/main/java/app/attestation/auditor/RemoteVerifyJob.java index 1944c00d..8deec5e1 100644 --- a/app/src/main/java/app/attestation/auditor/RemoteVerifyJob.java +++ b/app/src/main/java/app/attestation/auditor/RemoteVerifyJob.java @@ -39,6 +39,8 @@ public class RemoteVerifyJob extends JobService { private static final String TAG = "RemoteVerifyJob"; private static final int PERIODIC_JOB_ID = 0; private static final int FIRST_RUN_JOB_ID = 1; + + private static final int FIRE_ONCE_JOB_ID = 2; static final String DOMAIN = "attestation.app"; private static final String CHALLENGE_URL = "https://" + DOMAIN + "/challenge"; private static final String VERIFY_URL = "https://" + DOMAIN + "/verify"; @@ -115,13 +117,24 @@ static void schedule(final Context context, int interval) { throw new RuntimeException("job schedule failed"); } } - final JobInfo.Builder builder = new JobInfo.Builder(PERIODIC_JOB_ID, serviceName) + final JobInfo.Builder builder = scheduleNow ? + new JobInfo.Builder(FIRE_ONCE_JOB_ID, serviceName) + .setPersisted(true) + .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY) + : new JobInfo.Builder(PERIODIC_JOB_ID, serviceName) .setPersisted(true) .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY) +<<<<<<< HEAD .setEstimatedNetworkBytes(ESTIMATED_DOWNLOAD_BYTES, ESTIMATED_UPLOAD_BYTES); if(!scheduleNow) { builder.setPeriodic(intervalMillis, flexMillis); +======= + .setPeriodic(intervalMillis, flexMillis); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + builder.setEstimatedNetworkBytes(ESTIMATED_DOWNLOAD_BYTES, ESTIMATED_UPLOAD_BYTES); +>>>>>>> 9a0a5f1 (review changes) } if (scheduler.schedule(builder.build()) == JobScheduler.RESULT_FAILURE) { throw new RuntimeException("job schedule failed"); From 3adc5f9ab63cbf8051b2d6f12149378b93c4bece Mon Sep 17 00:00:00 2001 From: Zaraki Kenpachi <7empest_pot@proton.me> Date: Sun, 23 Apr 2023 20:35:47 +0530 Subject: [PATCH 4/5] add new flow in the `OnRequestPermissionsResult` to handle immediate remote verify --- .../java/app/attestation/auditor/AttestationActivity.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/app/attestation/auditor/AttestationActivity.java b/app/src/main/java/app/attestation/auditor/AttestationActivity.java index 8f31955e..8dc1d2b7 100644 --- a/app/src/main/java/app/attestation/auditor/AttestationActivity.java +++ b/app/src/main/java/app/attestation/auditor/AttestationActivity.java @@ -63,8 +63,10 @@ public class AttestationActivity extends AppCompatActivity { private static final int PERMISSIONS_REQUEST_CAMERA = 0; private static final int PERMISSIONS_REQUEST_POST_NOTIFICATIONS_REMOTE_VERIFY = 1; + private static final int PERMISSIONS_REQUEST_POST_NOTIFICATIONS_SUBMIT_SAMPLE = 2; + private static final int PERMISSIONS_REQUEST_POST_NOTIFICATIONS_IMMEDIATE_REMOTE_VERIFY = 3; private static final ExecutorService executor = Executors.newSingleThreadExecutor(); private ActivityAttestationBinding binding; @@ -468,6 +470,9 @@ public void onRequestPermissionsResult(int requestCode, } } else if (requestCode == PERMISSIONS_REQUEST_POST_NOTIFICATIONS_REMOTE_VERIFY) { QRScannerActivityLauncher.launch(new Intent(this, QRScannerActivity.class)); + } else if (requestCode == PERMISSIONS_REQUEST_POST_NOTIFICATIONS_IMMEDIATE_REMOTE_VERIFY) { + RemoteVerifyJob.schedule(this, -1); + snackbar.setText(R.string.remote_verify_now).show(); } else if (requestCode == PERMISSIONS_REQUEST_POST_NOTIFICATIONS_SUBMIT_SAMPLE) { SubmitSampleJob.schedule(this); snackbar.setText(R.string.schedule_submit_sample_success).show(); @@ -549,7 +554,7 @@ public boolean onOptionsItemSelected(final MenuItem item) { else if (itemId == R.id.action_remote_verify_now) { if (checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[]{Manifest.permission.POST_NOTIFICATIONS}, - PERMISSIONS_REQUEST_POST_NOTIFICATIONS_REMOTE_VERIFY); + PERMISSIONS_REQUEST_POST_NOTIFICATIONS_IMMEDIATE_REMOTE_VERIFY); } else { RemoteVerifyJob.schedule(this, -1); snackbar.setText(R.string.remote_verify_now).show(); From da9741a934cb831a6f2b91bfe9f855b6de51786c Mon Sep 17 00:00:00 2001 From: Zaraki Kenpachi <7empest_pot@proton.me> Date: Sun, 23 Apr 2023 20:52:24 +0530 Subject: [PATCH 5/5] rebase changes --- .../java/app/attestation/auditor/RemoteVerifyJob.java | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/app/src/main/java/app/attestation/auditor/RemoteVerifyJob.java b/app/src/main/java/app/attestation/auditor/RemoteVerifyJob.java index 8deec5e1..8098955a 100644 --- a/app/src/main/java/app/attestation/auditor/RemoteVerifyJob.java +++ b/app/src/main/java/app/attestation/auditor/RemoteVerifyJob.java @@ -124,18 +124,9 @@ static void schedule(final Context context, int interval) { : new JobInfo.Builder(PERIODIC_JOB_ID, serviceName) .setPersisted(true) .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY) -<<<<<<< HEAD - .setEstimatedNetworkBytes(ESTIMATED_DOWNLOAD_BYTES, ESTIMATED_UPLOAD_BYTES); - - if(!scheduleNow) { - builder.setPeriodic(intervalMillis, flexMillis); -======= + .setEstimatedNetworkBytes(ESTIMATED_DOWNLOAD_BYTES, ESTIMATED_UPLOAD_BYTES) .setPeriodic(intervalMillis, flexMillis); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { - builder.setEstimatedNetworkBytes(ESTIMATED_DOWNLOAD_BYTES, ESTIMATED_UPLOAD_BYTES); ->>>>>>> 9a0a5f1 (review changes) - } if (scheduler.schedule(builder.build()) == JobScheduler.RESULT_FAILURE) { throw new RuntimeException("job schedule failed"); }