Skip to content

Commit 1372a65

Browse files
Merge pull request #3329 from dimagi/integrity_error_proceed
Integrity token workaround for Connect
2 parents 6e51e97 + 6557de6 commit 1372a65

File tree

6 files changed

+35
-17
lines changed

6 files changed

+35
-17
lines changed

app/src/org/commcare/android/integrity/IntegrityReporterWorker.kt

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ import androidx.work.NetworkType
1010
import androidx.work.OneTimeWorkRequest
1111
import androidx.work.WorkManager
1212
import androidx.work.WorkerParameters
13-
import com.google.android.play.core.integrity.StandardIntegrityException
1413
import com.google.common.base.Strings
15-
import org.commcare.android.database.connect.models.PersonalIdSessionData
1614
import org.commcare.android.integrity.IntegrityTokenApiRequestHelper.Companion.fetchIntegrityToken
1715
import org.commcare.android.logging.ReportingUtils
1816
import org.commcare.connect.network.connectId.PersonalIdApiHandler
@@ -63,13 +61,8 @@ class IntegrityReporterWorker(appContext: Context, workerParams: WorkerParameter
6361
val requestHash = org.commcare.utils.HashUtils.computeHash(jsonBody, org.commcare.utils.HashUtils.HashAlgorithm.SHA256)
6462
val tokenResult = fetchIntegrityToken(requestHash)
6563
val (integrityToken, hash) = tokenResult.getOrElse {
66-
val errorCode: String = if(it is StandardIntegrityException) {
67-
it.errorCode.toString();
68-
} else {
69-
it.message ?: "Unknown error"
70-
}
71-
72-
FirebaseAnalyticsUtil.reportPersonalIdIntegritySubmission(requestId, errorCode)
64+
val errorCode = IntegrityTokenApiRequestHelper.getCodeForException(it)
65+
FirebaseAnalyticsUtil.reportPersonalIdHeartbeatIntegritySubmission(requestId, errorCode)
7366

7467
body["device_error"] = errorCode
7568
makeReportIntegrityCall(context, null, null, body, requestId)
@@ -100,7 +93,7 @@ class IntegrityReporterWorker(appContext: Context, workerParams: WorkerParameter
10093
cont.resume(success)
10194
}
10295
override fun onFailure(errorCode: PersonalIdOrConnectApiErrorCodes, t: Throwable?) {
103-
FirebaseAnalyticsUtil.reportPersonalIdIntegritySubmission(requestId, "SendError")
96+
FirebaseAnalyticsUtil.reportPersonalIdHeartbeatIntegritySubmission(requestId, "SendError")
10497
cont.resume(false)
10598
}
10699
}

app/src/org/commcare/android/integrity/IntegrityTokenApiRequestHelper.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,5 +152,13 @@ class IntegrityTokenApiRequestHelper(
152152
Result.failure(e)
153153
}
154154
}
155+
156+
fun getCodeForException(exception: Throwable): String {
157+
return if (exception is StandardIntegrityException) {
158+
exception.errorCode.toString()
159+
} else {
160+
exception.message ?: "UnknownError"
161+
}
162+
}
155163
}
156164
}

app/src/org/commcare/connect/network/connectId/parser/ReportIntegrityResponseParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class ReportIntegrityResponseParser<T> implements BaseApiResponseParser<T
2020
@Override
2121
public T parse(int responseCode, @NonNull InputStream responseData, @Nullable Object anyInputObject) throws IOException,JSONException {
2222
JSONObject json = new JSONObject(new String(StreamsUtil.inputStreamToByteArray(responseData)));
23-
FirebaseAnalyticsUtil.reportPersonalIdIntegritySubmission(((String)anyInputObject),
23+
FirebaseAnalyticsUtil.reportPersonalIdHeartbeatIntegritySubmission(((String)anyInputObject),
2424
json.optString("result_code", "NoCodeFromServer"));
2525
return (T)Boolean.TRUE;
2626
}

app/src/org/commcare/fragments/personalId/PersonalIdPhoneFragment.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import android.Manifest;
44
import android.app.Activity;
5-
import android.app.Dialog;
65
import android.content.DialogInterface;
76
import android.location.Location;
87
import android.os.Bundle;
@@ -268,8 +267,10 @@ public void onTokenReceived(@NotNull String requestHash,
268267

269268
@Override
270269
public void onTokenFailure(@NotNull Exception exception) {
271-
onConfigurationFailure(AnalyticsParamValue.START_CONFIGURATION_INTEGRITY_DEVICE_FAILURE,
272-
integrityTokenApiRequestHelper.getErrorForException(requireActivity(), exception));
270+
String errorCode = IntegrityTokenApiRequestHelper.Companion.getCodeForException(exception);
271+
FirebaseAnalyticsUtil.reportPersonalIdConfigurationIntegritySubmission(errorCode);
272+
273+
makeStartConfigurationCall(null, body, null);
273274
}
274275
});
275276
}
@@ -385,7 +386,12 @@ private void registerLauncher() {
385386

386387
private void makeStartConfigurationCall(String requestHash,
387388
HashMap<String, String> body,
388-
StandardIntegrityManager.@NotNull StandardIntegrityToken integrityTokenResponse) {
389+
StandardIntegrityManager.StandardIntegrityToken integrityTokenResponse) {
390+
String token = integrityTokenResponse != null ? integrityTokenResponse.token() : "";
391+
if(requestHash == null) {
392+
requestHash = "";
393+
}
394+
389395
new PersonalIdApiHandler<PersonalIdSessionData>() {
390396
@Override
391397
public void onSuccess(PersonalIdSessionData sessionData) {
@@ -425,7 +431,7 @@ public void onFailure(@androidx.annotation.NonNull PersonalIdOrConnectApiErrorCo
425431
break;
426432
}
427433
}
428-
}.makeStartConfigurationCall(requireActivity(), body, integrityTokenResponse.token(), requestHash);
434+
}.makeStartConfigurationCall(requireActivity(), body, token, requestHash);
429435
}
430436

431437
private void handleIntegritySubError(StandardIntegrityManager.StandardIntegrityToken tokenResponse,

app/src/org/commcare/google/services/analytics/CCAnalyticsParam.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class CCAnalyticsParam {
2828
static final String FORM_ID = "form_id";
2929
static final String REQUEST_ID = "request_id";
3030
static final String RESULT_CODE = "result_code";
31+
static final String TRIGGER = "trigger";
3132

3233
static final String USER_RETURNED = "user_returned";
3334
static final String NOTIFICATION_TYPE = "notification_type";

app/src/org/commcare/google/services/analytics/FirebaseAnalyticsUtil.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,19 @@ public static void reportFormUploadAttempt(FormUploadResult first, Integer secon
409409
new String[]{String.valueOf(first), String.valueOf(second)});
410410
}
411411

412-
public static void reportPersonalIdIntegritySubmission(String requestId, String responseCode) {
412+
public static void reportPersonalIdConfigurationIntegritySubmission(String responseCode) {
413+
String label = "PersonalID Configuration";
414+
reportPersonalIdIntegritySubmission(label, label, responseCode);
415+
}
416+
417+
public static void reportPersonalIdHeartbeatIntegritySubmission(String requestId, String responseCode) {
418+
reportPersonalIdIntegritySubmission("Heartbeat", requestId, responseCode);
419+
}
420+
421+
public static void reportPersonalIdIntegritySubmission(String trigger, String requestId, String responseCode) {
413422
Bundle b = new Bundle();
414423
b.putString(CCAnalyticsParam.REQUEST_ID, requestId);
424+
b.putString(CCAnalyticsParam.TRIGGER, trigger);
415425
b.putString(CCAnalyticsParam.RESULT_CODE, responseCode);
416426
reportEvent(CCAnalyticsEvent.PERSONAL_ID_INTEGRITY_REPORTED, b);
417427
}

0 commit comments

Comments
 (0)